1 /*	$FreeBSD$	*/
2 
3 /*
4  * Copyright (C) 2012 by Darren Reed.
5  *
6  * See the IPFILTER.LICENCE file for details on licencing.
7  */
8 #if defined(KERNEL) || defined(_KERNEL)
9 # undef KERNEL
10 # undef _KERNEL
11 # define        KERNEL	1
12 # define        _KERNEL	1
13 #endif
14 #include <sys/errno.h>
15 #include <sys/types.h>
16 #include <sys/param.h>
17 #include <sys/time.h>
18 #include <sys/file.h>
19 #if defined(_KERNEL) && \
20     (defined(__NetBSD_Version) && (__NetBSD_Version >= 399002000))
21 # include <sys/kauth.h>
22 #endif
23 #if !defined(_KERNEL)
24 # include <stdio.h>
25 # include <string.h>
26 # include <stdlib.h>
27 # define KERNEL
28 # ifdef _OpenBSD__
29 struct file;
30 # endif
31 # include <sys/uio.h>
32 # undef KERNEL
33 #endif
34 #if defined(_KERNEL) && defined(__FreeBSD__)
35 # include <sys/filio.h>
36 # include <sys/fcntl.h>
37 #else
38 # include <sys/ioctl.h>
39 #endif
40 # include <sys/fcntl.h>
41 # include <sys/protosw.h>
42 #include <sys/socket.h>
43 #if defined(_KERNEL)
44 # include <sys/systm.h>
45 # if !defined(__SVR4)
46 #  include <sys/mbuf.h>
47 # endif
48 #endif
49 #if defined(__SVR4)
50 # include <sys/filio.h>
51 # include <sys/byteorder.h>
52 # ifdef KERNEL
53 #  include <sys/dditypes.h>
54 # endif
55 # include <sys/stream.h>
56 # include <sys/kmem.h>
57 #endif
58 #if defined(__FreeBSD__)
59 # include <sys/queue.h>
60 #endif
61 #include <net/if.h>
62 #if defined(__FreeBSD__)
63 # include <net/if_var.h>
64 #endif
65 #ifdef sun
66 # include <net/af.h>
67 #endif
68 #include <netinet/in.h>
69 #include <netinet/in_systm.h>
70 #include <netinet/ip.h>
71 
72 #ifdef RFC1825
73 # include <vpn/md5.h>
74 # include <vpn/ipsec.h>
75 extern struct ifnet vpnif;
76 #endif
77 
78 # include <netinet/ip_var.h>
79 #include <netinet/tcp.h>
80 #include <netinet/udp.h>
81 #include <netinet/ip_icmp.h>
82 #include "netinet/ip_compat.h"
83 #include <netinet/tcpip.h>
84 #include "netinet/ipl.h"
85 #include "netinet/ip_fil.h"
86 #include "netinet/ip_nat.h"
87 #include "netinet/ip_frag.h"
88 #include "netinet/ip_state.h"
89 #include "netinet/ip_proxy.h"
90 #include "netinet/ip_lookup.h"
91 #include "netinet/ip_dstlist.h"
92 #include "netinet/ip_sync.h"
93 #if defined(__FreeBSD__)
94 # include <sys/malloc.h>
95 #endif
96 #ifdef HAS_SYS_MD5_H
97 # include <sys/md5.h>
98 #else
99 # include "md5.h"
100 #endif
101 /* END OF INCLUDES */
102 
103 #undef	SOCKADDR_IN
104 #define	SOCKADDR_IN	struct sockaddr_in
105 
106 #if !defined(lint)
107 static const char sccsid[] = "@(#)ip_nat.c	1.11 6/5/96 (C) 1995 Darren Reed";
108 static const char rcsid[] = "@(#)$FreeBSD$";
109 /* static const char rcsid[] = "@(#)$Id: ip_nat.c,v 2.195.2.102 2007/10/16 10:08:10 darrenr Exp $"; */
110 #endif
111 
112 
113 #define	NATFSUM(n,v,f)	((v) == 4 ? (n)->f.in4.s_addr : (n)->f.i6[0] + \
114 			 (n)->f.i6[1] + (n)->f.i6[2] + (n)->f.i6[3])
115 #define	NBUMP(x)	softn->(x)++
116 #define	NBUMPD(x, y)	do { \
117 				softn->x.y++; \
118 				DT(y); \
119 			} while (0)
120 #define	NBUMPSIDE(y,x)	softn->ipf_nat_stats.ns_side[y].x++
121 #define	NBUMPSIDED(y,x)	do { softn->ipf_nat_stats.ns_side[y].x++; \
122 			     DT(x); } while (0)
123 #define	NBUMPSIDEX(y,x,z) \
124 			do { softn->ipf_nat_stats.ns_side[y].x++; \
125 			     DT(z); } while (0)
126 #define	NBUMPSIDEDF(y,x)do { softn->ipf_nat_stats.ns_side[y].x++; \
127 			     DT1(x, fr_info_t *, fin); } while (0)
128 
129 static ipftuneable_t ipf_nat_tuneables[] = {
130 	/* nat */
131 	{ { (void *)offsetof(ipf_nat_softc_t, ipf_nat_lock) },
132 		"nat_lock",	0,	1,
133 		stsizeof(ipf_nat_softc_t, ipf_nat_lock),
134 		IPFT_RDONLY,		NULL,	NULL },
135 	{ { (void *)offsetof(ipf_nat_softc_t, ipf_nat_table_sz) },
136 		"nat_table_size", 1,	0x7fffffff,
137 		stsizeof(ipf_nat_softc_t, ipf_nat_table_sz),
138 		0,			NULL,	ipf_nat_rehash },
139 	{ { (void *)offsetof(ipf_nat_softc_t, ipf_nat_table_max) },
140 		"nat_table_max", 1,	0x7fffffff,
141 		stsizeof(ipf_nat_softc_t, ipf_nat_table_max),
142 		0,			NULL,	NULL },
143 	{ { (void *)offsetof(ipf_nat_softc_t, ipf_nat_maprules_sz) },
144 		"nat_rules_size", 1,	0x7fffffff,
145 		stsizeof(ipf_nat_softc_t, ipf_nat_maprules_sz),
146 		0,			NULL,	ipf_nat_rehash_rules },
147 	{ { (void *)offsetof(ipf_nat_softc_t, ipf_nat_rdrrules_sz) },
148 		"rdr_rules_size", 1,	0x7fffffff,
149 		stsizeof(ipf_nat_softc_t, ipf_nat_rdrrules_sz),
150 		0,			NULL,	ipf_nat_rehash_rules },
151 	{ { (void *)offsetof(ipf_nat_softc_t, ipf_nat_hostmap_sz) },
152 		"hostmap_size",	1,	0x7fffffff,
153 		stsizeof(ipf_nat_softc_t, ipf_nat_hostmap_sz),
154 		0,			NULL,	ipf_nat_hostmap_rehash },
155 	{ { (void *)offsetof(ipf_nat_softc_t, ipf_nat_maxbucket) },
156 		"nat_maxbucket",1,	0x7fffffff,
157 		stsizeof(ipf_nat_softc_t, ipf_nat_maxbucket),
158 		0,			NULL,	NULL },
159 	{ { (void *)offsetof(ipf_nat_softc_t, ipf_nat_logging) },
160 		"nat_logging",	0,	1,
161 		stsizeof(ipf_nat_softc_t, ipf_nat_logging),
162 		0,			NULL,	NULL },
163 	{ { (void *)offsetof(ipf_nat_softc_t, ipf_nat_doflush) },
164 		"nat_doflush",	0,	1,
165 		stsizeof(ipf_nat_softc_t, ipf_nat_doflush),
166 		0,			NULL,	NULL },
167 	{ { (void *)offsetof(ipf_nat_softc_t, ipf_nat_table_wm_low) },
168 		"nat_table_wm_low",	1,	99,
169 		stsizeof(ipf_nat_softc_t, ipf_nat_table_wm_low),
170 		0,			NULL,	NULL },
171 	{ { (void *)offsetof(ipf_nat_softc_t, ipf_nat_table_wm_high) },
172 		"nat_table_wm_high",	2,	100,
173 		stsizeof(ipf_nat_softc_t, ipf_nat_table_wm_high),
174 		0,			NULL,	NULL },
175 	{ { 0 },
176 		NULL,			0,	0,
177 		0,
178 		0,			NULL,	NULL }
179 };
180 
181 /* ======================================================================== */
182 /* How the NAT is organised and works.                                      */
183 /*                                                                          */
184 /* Inside (interface y) NAT       Outside (interface x)                     */
185 /* -------------------- -+- -------------------------------------           */
186 /* Packet going          |   out, processsed by ipf_nat_checkout() for x    */
187 /* ------------>         |   ------------>                                  */
188 /* src=10.1.1.1          |   src=192.1.1.1                                  */
189 /*                       |                                                  */
190 /*                       |   in, processed by ipf_nat_checkin() for x       */
191 /* <------------         |   <------------                                  */
192 /* dst=10.1.1.1          |   dst=192.1.1.1                                  */
193 /* -------------------- -+- -------------------------------------           */
194 /* ipf_nat_checkout() - changes ip_src and if required, sport               */
195 /*             - creates a new mapping, if required.                        */
196 /* ipf_nat_checkin()  - changes ip_dst and if required, dport               */
197 /*                                                                          */
198 /* In the NAT table, internal source is recorded as "in" and externally     */
199 /* seen as "out".                                                           */
200 /* ======================================================================== */
201 
202 
203 #if SOLARIS && !defined(INSTANCES)
204 extern	int		pfil_delayed_copy;
205 #endif
206 
207 static	int	ipf_nat_flush_entry(ipf_main_softc_t *, void *);
208 static	int	ipf_nat_getent(ipf_main_softc_t *, caddr_t, int);
209 static	int	ipf_nat_getsz(ipf_main_softc_t *, caddr_t, int);
210 static	int	ipf_nat_putent(ipf_main_softc_t *, caddr_t, int);
211 static	void	ipf_nat_addmap(ipf_nat_softc_t *, ipnat_t *);
212 static	void	ipf_nat_addrdr(ipf_nat_softc_t *, ipnat_t *);
213 static	int	ipf_nat_builddivertmp(ipf_nat_softc_t *, ipnat_t *);
214 static	int	ipf_nat_clearlist(ipf_main_softc_t *, ipf_nat_softc_t *);
215 static	int	ipf_nat_cmp_rules(ipnat_t *, ipnat_t *);
216 static	int	ipf_nat_decap(fr_info_t *, nat_t *);
217 static	void	ipf_nat_delrule(ipf_main_softc_t *, ipf_nat_softc_t *,
218 				     ipnat_t *, int);
219 static	int	ipf_nat_extraflush(ipf_main_softc_t *, ipf_nat_softc_t *, int);
220 static	int	ipf_nat_finalise(fr_info_t *, nat_t *);
221 static	int	ipf_nat_flushtable(ipf_main_softc_t *, ipf_nat_softc_t *);
222 static	int	ipf_nat_getnext(ipf_main_softc_t *, ipftoken_t *,
223 				     ipfgeniter_t *, ipfobj_t *);
224 static	int	ipf_nat_gettable(ipf_main_softc_t *, ipf_nat_softc_t *,
225 				      char *);
226 static	hostmap_t *ipf_nat_hostmap(ipf_nat_softc_t *, ipnat_t *,
227 					struct in_addr, struct in_addr,
228 					struct in_addr, u_32_t);
229 static	int	ipf_nat_icmpquerytype(int);
230 static	int	ipf_nat_iterator(ipf_main_softc_t *, ipftoken_t *,
231 				      ipfgeniter_t *, ipfobj_t *);
232 static	int	ipf_nat_match(fr_info_t *, ipnat_t *);
233 static	int	ipf_nat_matcharray(nat_t *, int *, u_long);
234 static	int	ipf_nat_matchflush(ipf_main_softc_t *, ipf_nat_softc_t *,
235 					caddr_t);
236 static	void	ipf_nat_mssclamp(tcphdr_t *, u_32_t, fr_info_t *,
237 				      u_short *);
238 static	int	ipf_nat_newmap(fr_info_t *, nat_t *, natinfo_t *);
239 static	int	ipf_nat_newdivert(fr_info_t *, nat_t *, natinfo_t *);
240 static	int	ipf_nat_newrdr(fr_info_t *, nat_t *, natinfo_t *);
241 static	int	ipf_nat_newrewrite(fr_info_t *, nat_t *, natinfo_t *);
242 static	int	ipf_nat_nextaddr(fr_info_t *, nat_addr_t *, u_32_t *,
243 				      u_32_t *);
244 static	int	ipf_nat_nextaddrinit(ipf_main_softc_t *, char *,
245 					  nat_addr_t *, int, void *);
246 static	int	ipf_nat_resolverule(ipf_main_softc_t *, ipnat_t *);
247 static	int	ipf_nat_ruleaddrinit(ipf_main_softc_t *,
248 					  ipf_nat_softc_t *, ipnat_t *);
249 static	void	ipf_nat_rule_fini(ipf_main_softc_t *, ipnat_t *);
250 static	int	ipf_nat_rule_init(ipf_main_softc_t *, ipf_nat_softc_t *,
251 				       ipnat_t *);
252 static	int	ipf_nat_siocaddnat(ipf_main_softc_t *, ipf_nat_softc_t *,
253 					ipnat_t *, int);
254 static	void	ipf_nat_siocdelnat(ipf_main_softc_t *, ipf_nat_softc_t *,
255 					ipnat_t *, int);
256 static	void	ipf_nat_tabmove(ipf_nat_softc_t *, nat_t *);
257 
258 /* ------------------------------------------------------------------------ */
259 /* Function:    ipf_nat_main_load                                           */
260 /* Returns:     int - 0 == success, -1 == failure                           */
261 /* Parameters:  Nil                                                         */
262 /*                                                                          */
263 /* The only global NAT structure that needs to be initialised is the filter */
264 /* rule that is used with blocking packets.                                 */
265 /* ------------------------------------------------------------------------ */
266 int
ipf_nat_main_load(void)267 ipf_nat_main_load(void)
268 {
269 
270 	return (0);
271 }
272 
273 
274 /* ------------------------------------------------------------------------ */
275 /* Function:    ipf_nat_main_unload                                         */
276 /* Returns:     int - 0 == success, -1 == failure                           */
277 /* Parameters:  Nil                                                         */
278 /*                                                                          */
279 /* A null-op function that exists as a placeholder so that the flow in      */
280 /* other functions is obvious.                                              */
281 /* ------------------------------------------------------------------------ */
282 int
ipf_nat_main_unload(void)283 ipf_nat_main_unload(void)
284 {
285 	return (0);
286 }
287 
288 
289 /* ------------------------------------------------------------------------ */
290 /* Function:    ipf_nat_soft_create                                         */
291 /* Returns:     void * - NULL = failure, else pointer to NAT context        */
292 /* Parameters:  softc(I) - pointer to soft context main structure           */
293 /*                                                                          */
294 /* Allocate the initial soft context structure for NAT and populate it with */
295 /* some default values. Creating the tables is left until we call _init so  */
296 /* that sizes can be changed before we get under way.                       */
297 /* ------------------------------------------------------------------------ */
298 void *
ipf_nat_soft_create(ipf_main_softc_t * softc)299 ipf_nat_soft_create(ipf_main_softc_t *softc)
300 {
301 	ipf_nat_softc_t *softn;
302 
303 	KMALLOC(softn, ipf_nat_softc_t *);
304 	if (softn == NULL)
305 		return (NULL);
306 
307 	bzero((char *)softn, sizeof(*softn));
308 
309 	softn->ipf_nat_tune = ipf_tune_array_copy(softn,
310 						  sizeof(ipf_nat_tuneables),
311 						  ipf_nat_tuneables);
312 	if (softn->ipf_nat_tune == NULL) {
313 		ipf_nat_soft_destroy(softc, softn);
314 		return (NULL);
315 	}
316 	if (ipf_tune_array_link(softc, softn->ipf_nat_tune) == -1) {
317 		ipf_nat_soft_destroy(softc, softn);
318 		return (NULL);
319 	}
320 
321 	softn->ipf_nat_list_tail = &softn->ipf_nat_list;
322 
323 	if (softc->ipf_large_nat) {
324 	softn->ipf_nat_table_max = NAT_TABLE_MAX_LARGE;
325 	softn->ipf_nat_table_sz = NAT_TABLE_SZ_LARGE;
326 	softn->ipf_nat_maprules_sz = NAT_SIZE_LARGE;
327 	softn->ipf_nat_rdrrules_sz = RDR_SIZE_LARGE;
328 	softn->ipf_nat_hostmap_sz = HOSTMAP_SIZE_LARGE;
329 	} else {
330 	softn->ipf_nat_table_max = NAT_TABLE_MAX_NORMAL;
331 	softn->ipf_nat_table_sz = NAT_TABLE_SZ_NORMAL;
332 	softn->ipf_nat_maprules_sz = NAT_SIZE_NORMAL;
333 	softn->ipf_nat_rdrrules_sz = RDR_SIZE_NORMAL;
334 	softn->ipf_nat_hostmap_sz = HOSTMAP_SIZE_NORMAL;
335 	}
336 	softn->ipf_nat_doflush = 0;
337 #ifdef  IPFILTER_LOG
338 	softn->ipf_nat_logging = 1;
339 #else
340 	softn->ipf_nat_logging = 0;
341 #endif
342 
343 	softn->ipf_nat_defage = DEF_NAT_AGE;
344 	softn->ipf_nat_defipage = IPF_TTLVAL(60);
345 	softn->ipf_nat_deficmpage = IPF_TTLVAL(3);
346 	softn->ipf_nat_table_wm_high = 99;
347 	softn->ipf_nat_table_wm_low = 90;
348 
349 	return (softn);
350 }
351 
352 /* ------------------------------------------------------------------------ */
353 /* Function:    ipf_nat_soft_destroy                                        */
354 /* Returns:     Nil                                                         */
355 /* Parameters:  softc(I) - pointer to soft context main structure           */
356 /*                                                                          */
357 /* ------------------------------------------------------------------------ */
358 void
ipf_nat_soft_destroy(ipf_main_softc_t * softc,void * arg)359 ipf_nat_soft_destroy(ipf_main_softc_t *softc, void *arg)
360 {
361 	ipf_nat_softc_t *softn = arg;
362 
363 	if (softn->ipf_nat_tune != NULL) {
364 		ipf_tune_array_unlink(softc, softn->ipf_nat_tune);
365 		KFREES(softn->ipf_nat_tune, sizeof(ipf_nat_tuneables));
366 		softn->ipf_nat_tune = NULL;
367 	}
368 
369 	KFREE(softn);
370 }
371 
372 
373 /* ------------------------------------------------------------------------ */
374 /* Function:    ipf_nat_init                                                */
375 /* Returns:     int - 0 == success, -1 == failure                           */
376 /* Parameters:  softc(I) - pointer to soft context main structure           */
377 /*                                                                          */
378 /* Initialise all of the NAT locks, tables and other structures.            */
379 /* ------------------------------------------------------------------------ */
380 int
ipf_nat_soft_init(ipf_main_softc_t * softc,void * arg)381 ipf_nat_soft_init(ipf_main_softc_t *softc, void *arg)
382 {
383 	ipf_nat_softc_t *softn = arg;
384 	ipftq_t *tq;
385 	int i;
386 
387 	KMALLOCS(softn->ipf_nat_table[0], nat_t **, \
388 		 sizeof(nat_t *) * softn->ipf_nat_table_sz);
389 
390 	if (softn->ipf_nat_table[0] != NULL) {
391 		bzero((char *)softn->ipf_nat_table[0],
392 		      softn->ipf_nat_table_sz * sizeof(nat_t *));
393 	} else {
394 		return (-1);
395 	}
396 
397 	KMALLOCS(softn->ipf_nat_table[1], nat_t **, \
398 		 sizeof(nat_t *) * softn->ipf_nat_table_sz);
399 
400 	if (softn->ipf_nat_table[1] != NULL) {
401 		bzero((char *)softn->ipf_nat_table[1],
402 		      softn->ipf_nat_table_sz * sizeof(nat_t *));
403 	} else {
404 		return (-2);
405 	}
406 
407 	KMALLOCS(softn->ipf_nat_map_rules, ipnat_t **, \
408 		 sizeof(ipnat_t *) * softn->ipf_nat_maprules_sz);
409 
410 	if (softn->ipf_nat_map_rules != NULL) {
411 		bzero((char *)softn->ipf_nat_map_rules,
412 		      softn->ipf_nat_maprules_sz * sizeof(ipnat_t *));
413 	} else {
414 		return (-3);
415 	}
416 
417 	KMALLOCS(softn->ipf_nat_rdr_rules, ipnat_t **, \
418 		 sizeof(ipnat_t *) * softn->ipf_nat_rdrrules_sz);
419 
420 	if (softn->ipf_nat_rdr_rules != NULL) {
421 		bzero((char *)softn->ipf_nat_rdr_rules,
422 		      softn->ipf_nat_rdrrules_sz * sizeof(ipnat_t *));
423 	} else {
424 		return (-4);
425 	}
426 
427 	KMALLOCS(softn->ipf_hm_maptable, hostmap_t **, \
428 		 sizeof(hostmap_t *) * softn->ipf_nat_hostmap_sz);
429 
430 	if (softn->ipf_hm_maptable != NULL) {
431 		bzero((char *)softn->ipf_hm_maptable,
432 		      sizeof(hostmap_t *) * softn->ipf_nat_hostmap_sz);
433 	} else {
434 		return (-5);
435 	}
436 	softn->ipf_hm_maplist = NULL;
437 
438 	KMALLOCS(softn->ipf_nat_stats.ns_side[0].ns_bucketlen, u_int *,
439 		 softn->ipf_nat_table_sz * sizeof(u_int));
440 
441 	if (softn->ipf_nat_stats.ns_side[0].ns_bucketlen == NULL) {
442 		return (-6);
443 	}
444 	bzero((char *)softn->ipf_nat_stats.ns_side[0].ns_bucketlen,
445 	      softn->ipf_nat_table_sz * sizeof(u_int));
446 
447 	KMALLOCS(softn->ipf_nat_stats.ns_side[1].ns_bucketlen, u_int *,
448 		 softn->ipf_nat_table_sz * sizeof(u_int));
449 
450 	if (softn->ipf_nat_stats.ns_side[1].ns_bucketlen == NULL) {
451 		return (-7);
452 	}
453 
454 	bzero((char *)softn->ipf_nat_stats.ns_side[1].ns_bucketlen,
455 	      softn->ipf_nat_table_sz * sizeof(u_int));
456 
457 	if (softn->ipf_nat_maxbucket == 0) {
458 		for (i = softn->ipf_nat_table_sz; i > 0; i >>= 1)
459 			softn->ipf_nat_maxbucket++;
460 		softn->ipf_nat_maxbucket *= 2;
461 	}
462 
463 	ipf_sttab_init(softc, softn->ipf_nat_tcptq);
464 	/*
465 	 * Increase this because we may have "keep state" following this too
466 	 * and packet storms can occur if this is removed too quickly.
467 	 */
468 	softn->ipf_nat_tcptq[IPF_TCPS_CLOSED].ifq_ttl = softc->ipf_tcplastack;
469 	softn->ipf_nat_tcptq[IPF_TCP_NSTATES - 1].ifq_next =
470 							&softn->ipf_nat_udptq;
471 
472 	IPFTQ_INIT(&softn->ipf_nat_udptq, softn->ipf_nat_defage,
473 		   "nat ipftq udp tab");
474 	softn->ipf_nat_udptq.ifq_next = &softn->ipf_nat_udpacktq;
475 
476 	IPFTQ_INIT(&softn->ipf_nat_udpacktq, softn->ipf_nat_defage,
477 		   "nat ipftq udpack tab");
478 	softn->ipf_nat_udpacktq.ifq_next = &softn->ipf_nat_icmptq;
479 
480 	IPFTQ_INIT(&softn->ipf_nat_icmptq, softn->ipf_nat_deficmpage,
481 		   "nat icmp ipftq tab");
482 	softn->ipf_nat_icmptq.ifq_next = &softn->ipf_nat_icmpacktq;
483 
484 	IPFTQ_INIT(&softn->ipf_nat_icmpacktq, softn->ipf_nat_defage,
485 		   "nat icmpack ipftq tab");
486 	softn->ipf_nat_icmpacktq.ifq_next = &softn->ipf_nat_iptq;
487 
488 	IPFTQ_INIT(&softn->ipf_nat_iptq, softn->ipf_nat_defipage,
489 		   "nat ip ipftq tab");
490 	softn->ipf_nat_iptq.ifq_next = &softn->ipf_nat_pending;
491 
492 	IPFTQ_INIT(&softn->ipf_nat_pending, 1, "nat pending ipftq tab");
493 	softn->ipf_nat_pending.ifq_next = NULL;
494 
495 	for (i = 0, tq = softn->ipf_nat_tcptq; i < IPF_TCP_NSTATES; i++, tq++) {
496 		if (tq->ifq_ttl < softn->ipf_nat_deficmpage)
497 			tq->ifq_ttl = softn->ipf_nat_deficmpage;
498 		else if (tq->ifq_ttl > softn->ipf_nat_defage && softc->ipf_large_nat)
499 			tq->ifq_ttl = softn->ipf_nat_defage;
500 	}
501 
502 	/*
503 	 * Increase this because we may have "keep state" following
504 	 * this too and packet storms can occur if this is removed
505 	 * too quickly.
506 	 */
507 	softn->ipf_nat_tcptq[IPF_TCPS_CLOSED].ifq_ttl = softc->ipf_tcplastack;
508 
509 	MUTEX_INIT(&softn->ipf_nat_new, "ipf nat new mutex");
510 	MUTEX_INIT(&softn->ipf_nat_io, "ipf nat io mutex");
511 
512 	softn->ipf_nat_inited = 1;
513 
514 	return (0);
515 }
516 
517 
518 /* ------------------------------------------------------------------------ */
519 /* Function:    ipf_nat_soft_fini                                           */
520 /* Returns:     Nil                                                         */
521 /* Parameters:  softc(I) - pointer to soft context main structure           */
522 /*                                                                          */
523 /* Free all memory used by NAT structures allocated at runtime.             */
524 /* ------------------------------------------------------------------------ */
525 int
ipf_nat_soft_fini(ipf_main_softc_t * softc,void * arg)526 ipf_nat_soft_fini(ipf_main_softc_t *softc, void *arg)
527 {
528 	ipf_nat_softc_t *softn = arg;
529 	ipftq_t *ifq, *ifqnext;
530 
531 	(void) ipf_nat_clearlist(softc, softn);
532 	(void) ipf_nat_flushtable(softc, softn);
533 
534 	/*
535 	 * Proxy timeout queues are not cleaned here because although they
536 	 * exist on the NAT list, ipf_proxy_unload is called after unload
537 	 * and the proxies actually are responsible for them being created.
538 	 * Should the proxy timeouts have their own list?  There's no real
539 	 * justification as this is the only complication.
540 	 */
541 	for (ifq = softn->ipf_nat_utqe; ifq != NULL; ifq = ifqnext) {
542 		ifqnext = ifq->ifq_next;
543 		if (ipf_deletetimeoutqueue(ifq) == 0)
544 			ipf_freetimeoutqueue(softc, ifq);
545 	}
546 
547 	if (softn->ipf_nat_table[0] != NULL) {
548 		KFREES(softn->ipf_nat_table[0],
549 		       sizeof(nat_t *) * softn->ipf_nat_table_sz);
550 		softn->ipf_nat_table[0] = NULL;
551 	}
552 	if (softn->ipf_nat_table[1] != NULL) {
553 		KFREES(softn->ipf_nat_table[1],
554 		       sizeof(nat_t *) * softn->ipf_nat_table_sz);
555 		softn->ipf_nat_table[1] = NULL;
556 	}
557 	if (softn->ipf_nat_map_rules != NULL) {
558 		KFREES(softn->ipf_nat_map_rules,
559 		       sizeof(ipnat_t *) * softn->ipf_nat_maprules_sz);
560 		softn->ipf_nat_map_rules = NULL;
561 	}
562 	if (softn->ipf_nat_rdr_rules != NULL) {
563 		KFREES(softn->ipf_nat_rdr_rules,
564 		       sizeof(ipnat_t *) * softn->ipf_nat_rdrrules_sz);
565 		softn->ipf_nat_rdr_rules = NULL;
566 	}
567 	if (softn->ipf_hm_maptable != NULL) {
568 		KFREES(softn->ipf_hm_maptable,
569 		       sizeof(hostmap_t *) * softn->ipf_nat_hostmap_sz);
570 		softn->ipf_hm_maptable = NULL;
571 	}
572 	if (softn->ipf_nat_stats.ns_side[0].ns_bucketlen != NULL) {
573 		KFREES(softn->ipf_nat_stats.ns_side[0].ns_bucketlen,
574 		       sizeof(u_int) * softn->ipf_nat_table_sz);
575 		softn->ipf_nat_stats.ns_side[0].ns_bucketlen = NULL;
576 	}
577 	if (softn->ipf_nat_stats.ns_side[1].ns_bucketlen != NULL) {
578 		KFREES(softn->ipf_nat_stats.ns_side[1].ns_bucketlen,
579 		       sizeof(u_int) * softn->ipf_nat_table_sz);
580 		softn->ipf_nat_stats.ns_side[1].ns_bucketlen = NULL;
581 	}
582 
583 	if (softn->ipf_nat_inited == 1) {
584 		softn->ipf_nat_inited = 0;
585 		ipf_sttab_destroy(softn->ipf_nat_tcptq);
586 
587 		MUTEX_DESTROY(&softn->ipf_nat_new);
588 		MUTEX_DESTROY(&softn->ipf_nat_io);
589 
590 		MUTEX_DESTROY(&softn->ipf_nat_udptq.ifq_lock);
591 		MUTEX_DESTROY(&softn->ipf_nat_udpacktq.ifq_lock);
592 		MUTEX_DESTROY(&softn->ipf_nat_icmptq.ifq_lock);
593 		MUTEX_DESTROY(&softn->ipf_nat_icmpacktq.ifq_lock);
594 		MUTEX_DESTROY(&softn->ipf_nat_iptq.ifq_lock);
595 		MUTEX_DESTROY(&softn->ipf_nat_pending.ifq_lock);
596 	}
597 
598 	return (0);
599 }
600 
601 
602 /* ------------------------------------------------------------------------ */
603 /* Function:    ipf_nat_setlock                                             */
604 /* Returns:     Nil                                                         */
605 /* Parameters:  arg(I) - pointer to soft state information                  */
606 /*              tmp(I) - new lock value                                     */
607 /*                                                                          */
608 /* Set the "lock status" of NAT to the value in tmp.                        */
609 /* ------------------------------------------------------------------------ */
610 void
ipf_nat_setlock(void * arg,int tmp)611 ipf_nat_setlock(void *arg, int tmp)
612 {
613 	ipf_nat_softc_t *softn = arg;
614 
615 	softn->ipf_nat_lock = tmp;
616 }
617 
618 
619 /* ------------------------------------------------------------------------ */
620 /* Function:    ipf_nat_addrdr                                              */
621 /* Returns:     Nil                                                         */
622 /* Parameters:  n(I) - pointer to NAT rule to add                           */
623 /*                                                                          */
624 /* Adds a redirect rule to the hash table of redirect rules and the list of */
625 /* loaded NAT rules.  Updates the bitmask indicating which netmasks are in  */
626 /* use by redirect rules.                                                   */
627 /* ------------------------------------------------------------------------ */
628 static void
ipf_nat_addrdr(ipf_nat_softc_t * softn,ipnat_t * n)629 ipf_nat_addrdr(ipf_nat_softc_t *softn, ipnat_t *n)
630 {
631 	ipnat_t **np;
632 	u_32_t j;
633 	u_int hv;
634 	u_int rhv;
635 	int k;
636 
637 	if (n->in_odstatype == FRI_NORMAL) {
638 		k = count4bits(n->in_odstmsk);
639 		ipf_inet_mask_add(k, &softn->ipf_nat_rdr_mask);
640 		j = (n->in_odstaddr & n->in_odstmsk);
641 		rhv = NAT_HASH_FN(j, 0, 0xffffffff);
642 	} else {
643 		ipf_inet_mask_add(0, &softn->ipf_nat_rdr_mask);
644 		j = 0;
645 		rhv = 0;
646 	}
647 	hv = rhv % softn->ipf_nat_rdrrules_sz;
648 	np = softn->ipf_nat_rdr_rules + hv;
649 	while (*np != NULL)
650 		np = &(*np)->in_rnext;
651 	n->in_rnext = NULL;
652 	n->in_prnext = np;
653 	n->in_hv[0] = hv;
654 	n->in_use++;
655 	*np = n;
656 }
657 
658 
659 /* ------------------------------------------------------------------------ */
660 /* Function:    ipf_nat_addmap                                              */
661 /* Returns:     Nil                                                         */
662 /* Parameters:  n(I) - pointer to NAT rule to add                           */
663 /*                                                                          */
664 /* Adds a NAT map rule to the hash table of rules and the list of  loaded   */
665 /* NAT rules.  Updates the bitmask indicating which netmasks are in use by  */
666 /* redirect rules.                                                          */
667 /* ------------------------------------------------------------------------ */
668 static void
ipf_nat_addmap(ipf_nat_softc_t * softn,ipnat_t * n)669 ipf_nat_addmap(ipf_nat_softc_t *softn, ipnat_t *n)
670 {
671 	ipnat_t **np;
672 	u_32_t j;
673 	u_int hv;
674 	u_int rhv;
675 	int k;
676 
677 	if (n->in_osrcatype == FRI_NORMAL) {
678 		k = count4bits(n->in_osrcmsk);
679 		ipf_inet_mask_add(k, &softn->ipf_nat_map_mask);
680 		j = (n->in_osrcaddr & n->in_osrcmsk);
681 		rhv = NAT_HASH_FN(j, 0, 0xffffffff);
682 	} else {
683 		ipf_inet_mask_add(0, &softn->ipf_nat_map_mask);
684 		j = 0;
685 		rhv = 0;
686 	}
687 	hv = rhv % softn->ipf_nat_maprules_sz;
688 	np = softn->ipf_nat_map_rules + hv;
689 	while (*np != NULL)
690 		np = &(*np)->in_mnext;
691 	n->in_mnext = NULL;
692 	n->in_pmnext = np;
693 	n->in_hv[1] = rhv;
694 	n->in_use++;
695 	*np = n;
696 }
697 
698 
699 /* ------------------------------------------------------------------------ */
700 /* Function:    ipf_nat_delrdr                                              */
701 /* Returns:     Nil                                                         */
702 /* Parameters:  n(I) - pointer to NAT rule to delete                        */
703 /*                                                                          */
704 /* Removes a redirect rule from the hash table of redirect rules.           */
705 /* ------------------------------------------------------------------------ */
706 void
ipf_nat_delrdr(ipf_nat_softc_t * softn,ipnat_t * n)707 ipf_nat_delrdr(ipf_nat_softc_t *softn, ipnat_t *n)
708 {
709 	if (n->in_odstatype == FRI_NORMAL) {
710 		int k = count4bits(n->in_odstmsk);
711 		ipf_inet_mask_del(k, &softn->ipf_nat_rdr_mask);
712 	} else {
713 		ipf_inet_mask_del(0, &softn->ipf_nat_rdr_mask);
714 	}
715 	if (n->in_rnext)
716 		n->in_rnext->in_prnext = n->in_prnext;
717 	*n->in_prnext = n->in_rnext;
718 	n->in_use--;
719 }
720 
721 
722 /* ------------------------------------------------------------------------ */
723 /* Function:    ipf_nat_delmap                                              */
724 /* Returns:     Nil                                                         */
725 /* Parameters:  n(I) - pointer to NAT rule to delete                        */
726 /*                                                                          */
727 /* Removes a NAT map rule from the hash table of NAT map rules.             */
728 /* ------------------------------------------------------------------------ */
729 void
ipf_nat_delmap(ipf_nat_softc_t * softn,ipnat_t * n)730 ipf_nat_delmap(ipf_nat_softc_t *softn, ipnat_t *n)
731 {
732 	if (n->in_osrcatype == FRI_NORMAL) {
733 		int k = count4bits(n->in_osrcmsk);
734 		ipf_inet_mask_del(k, &softn->ipf_nat_map_mask);
735 	} else {
736 		ipf_inet_mask_del(0, &softn->ipf_nat_map_mask);
737 	}
738 	if (n->in_mnext != NULL)
739 		n->in_mnext->in_pmnext = n->in_pmnext;
740 	*n->in_pmnext = n->in_mnext;
741 	n->in_use--;
742 }
743 
744 
745 /* ------------------------------------------------------------------------ */
746 /* Function:    ipf_nat_hostmap                                             */
747 /* Returns:     struct hostmap* - NULL if no hostmap could be created,      */
748 /*                                else a pointer to the hostmapping to use  */
749 /* Parameters:  np(I)   - pointer to NAT rule                               */
750 /*              real(I) - real IP address                                   */
751 /*              map(I)  - mapped IP address                                 */
752 /*              port(I) - destination port number                           */
753 /* Write Locks: ipf_nat                                                     */
754 /*                                                                          */
755 /* Check if an ip address has already been allocated for a given mapping    */
756 /* that is not doing port based translation.  If is not yet allocated, then */
757 /* create a new entry if a non-NULL NAT rule pointer has been supplied.     */
758 /* ------------------------------------------------------------------------ */
759 static struct hostmap *
ipf_nat_hostmap(ipf_nat_softc_t * softn,ipnat_t * np,struct in_addr src,struct in_addr dst,struct in_addr map,u_32_t port)760 ipf_nat_hostmap(ipf_nat_softc_t *softn, ipnat_t *np, struct in_addr src,
761 	struct in_addr dst, struct in_addr map, u_32_t port)
762 {
763 	hostmap_t *hm;
764 	u_int hv, rhv;
765 
766 	hv = (src.s_addr ^ dst.s_addr);
767 	hv += src.s_addr;
768 	hv += dst.s_addr;
769 	rhv = hv;
770 	hv %= softn->ipf_nat_hostmap_sz;
771 	for (hm = softn->ipf_hm_maptable[hv]; hm; hm = hm->hm_hnext)
772 		if ((hm->hm_osrcip.s_addr == src.s_addr) &&
773 		    (hm->hm_odstip.s_addr == dst.s_addr) &&
774 		    ((np == NULL) || (np == hm->hm_ipnat)) &&
775 		    ((port == 0) || (port == hm->hm_port))) {
776 			softn->ipf_nat_stats.ns_hm_addref++;
777 			hm->hm_ref++;
778 			return (hm);
779 		}
780 
781 	if (np == NULL) {
782 		softn->ipf_nat_stats.ns_hm_nullnp++;
783 		return (NULL);
784 	}
785 
786 	KMALLOC(hm, hostmap_t *);
787 	if (hm) {
788 		hm->hm_next = softn->ipf_hm_maplist;
789 		hm->hm_pnext = &softn->ipf_hm_maplist;
790 		if (softn->ipf_hm_maplist != NULL)
791 			softn->ipf_hm_maplist->hm_pnext = &hm->hm_next;
792 		softn->ipf_hm_maplist = hm;
793 		hm->hm_hnext = softn->ipf_hm_maptable[hv];
794 		hm->hm_phnext = softn->ipf_hm_maptable + hv;
795 		if (softn->ipf_hm_maptable[hv] != NULL)
796 			softn->ipf_hm_maptable[hv]->hm_phnext = &hm->hm_hnext;
797 		softn->ipf_hm_maptable[hv] = hm;
798 		hm->hm_ipnat = np;
799 		np->in_use++;
800 		hm->hm_osrcip = src;
801 		hm->hm_odstip = dst;
802 		hm->hm_nsrcip = map;
803 		hm->hm_ndstip.s_addr = 0;
804 		hm->hm_ref = 1;
805 		hm->hm_port = port;
806 		hm->hm_hv = rhv;
807 		hm->hm_v = 4;
808 		softn->ipf_nat_stats.ns_hm_new++;
809 	} else {
810 		softn->ipf_nat_stats.ns_hm_newfail++;
811 	}
812 	return (hm);
813 }
814 
815 
816 /* ------------------------------------------------------------------------ */
817 /* Function:    ipf_nat_hostmapdel                                          */
818 /* Returns:     Nil                                                         */
819 /* Parameters:  hmp(I) - pointer to hostmap structure pointer               */
820 /* Write Locks: ipf_nat                                                     */
821 /*                                                                          */
822 /* Decrement the references to this hostmap structure by one.  If this      */
823 /* reaches zero then remove it and free it.                                 */
824 /* ------------------------------------------------------------------------ */
825 void
ipf_nat_hostmapdel(ipf_main_softc_t * softc,struct hostmap ** hmp)826 ipf_nat_hostmapdel(ipf_main_softc_t *softc, struct hostmap **hmp)
827 {
828 	struct hostmap *hm;
829 
830 	hm = *hmp;
831 	*hmp = NULL;
832 
833 	hm->hm_ref--;
834 	if (hm->hm_ref == 0) {
835 		ipf_nat_rule_deref(softc, &hm->hm_ipnat);
836 		if (hm->hm_hnext)
837 			hm->hm_hnext->hm_phnext = hm->hm_phnext;
838 		*hm->hm_phnext = hm->hm_hnext;
839 		if (hm->hm_next)
840 			hm->hm_next->hm_pnext = hm->hm_pnext;
841 		*hm->hm_pnext = hm->hm_next;
842 		KFREE(hm);
843 	}
844 }
845 
846 
847 /* ------------------------------------------------------------------------ */
848 /* Function:    ipf_fix_outcksum                                            */
849 /* Returns:     Nil                                                         */
850 /* Parameters:  cksum(I) - ipf_cksum_t, value of fin_cksum                  */
851 /*              sp(I)  - location of 16bit checksum to update               */
852 /*              n(I)  - amount to adjust checksum by                        */
853 /*		partial(I) - partial checksum				    */
854 /*                                                                          */
855 /* Adjusts the 16bit checksum by "n" for packets going out.                 */
856 /* ------------------------------------------------------------------------ */
857 void
ipf_fix_outcksum(int cksum,u_short * sp,u_32_t n,u_32_t partial)858 ipf_fix_outcksum(int cksum, u_short *sp, u_32_t n, u_32_t partial)
859 {
860 	u_short sumshort;
861 	u_32_t sum1;
862 
863 	if (n == 0)
864 		return;
865 
866 	if (cksum == 4) {
867 		*sp = 0;
868 		return;
869 	}
870 	if (cksum == 2) {
871 		sum1 = partial;
872 		sum1 = (sum1 & 0xffff) + (sum1 >> 16);
873 		*sp = htons(sum1);
874 		return;
875 	}
876 	sum1 = (~ntohs(*sp)) & 0xffff;
877 	sum1 += (n);
878 	sum1 = (sum1 >> 16) + (sum1 & 0xffff);
879 	/* Again */
880 	sum1 = (sum1 >> 16) + (sum1 & 0xffff);
881 	sumshort = ~(u_short)sum1;
882 	*(sp) = htons(sumshort);
883 }
884 
885 
886 /* ------------------------------------------------------------------------ */
887 /* Function:    ipf_fix_incksum                                             */
888 /* Returns:     Nil                                                         */
889 /* Parameters:  cksum(I) - ipf_cksum_t, value of fin_cksum                  */
890 /*              sp(I)  - location of 16bit checksum to update               */
891 /*              n(I)  - amount to adjust checksum by                        */
892 /*		partial(I) - partial checksum				    */
893 /*                                                                          */
894 /* Adjusts the 16bit checksum by "n" for packets going in.                  */
895 /* ------------------------------------------------------------------------ */
896 void
ipf_fix_incksum(int cksum,u_short * sp,u_32_t n,u_32_t partial)897 ipf_fix_incksum(int cksum, u_short *sp, u_32_t n, u_32_t partial)
898 {
899 	u_short sumshort;
900 	u_32_t sum1;
901 
902 	if (n == 0)
903 		return;
904 
905 	if (cksum == 4) {
906 		*sp = 0;
907 		return;
908 	}
909 	if (cksum == 2) {
910 		sum1 = partial;
911 		sum1 = (sum1 & 0xffff) + (sum1 >> 16);
912 		*sp = htons(sum1);
913 		return;
914 	}
915 
916 	sum1 = (~ntohs(*sp)) & 0xffff;
917 	sum1 += ~(n) & 0xffff;
918 	sum1 = (sum1 >> 16) + (sum1 & 0xffff);
919 	/* Again */
920 	sum1 = (sum1 >> 16) + (sum1 & 0xffff);
921 	sumshort = ~(u_short)sum1;
922 	*(sp) = htons(sumshort);
923 }
924 
925 
926 /* ------------------------------------------------------------------------ */
927 /* Function:    ipf_fix_datacksum                                           */
928 /* Returns:     Nil                                                         */
929 /* Parameters:  sp(I)  - location of 16bit checksum to update               */
930 /*              n(I)  - amount to adjust checksum by                        */
931 /*                                                                          */
932 /* Fix_datacksum is used *only* for the adjustments of checksums in the     */
933 /* data section of an IP packet.                                            */
934 /*                                                                          */
935 /* The only situation in which you need to do this is when NAT'ing an       */
936 /* ICMP error message. Such a message, contains in its body the IP header   */
937 /* of the original IP packet, that causes the error.                        */
938 /*                                                                          */
939 /* You can't use fix_incksum or fix_outcksum in that case, because for the  */
940 /* kernel the data section of the ICMP error is just data, and no special   */
941 /* processing like hardware cksum or ntohs processing have been done by the */
942 /* kernel on the data section.                                              */
943 /* ------------------------------------------------------------------------ */
944 void
ipf_fix_datacksum(u_short * sp,u_32_t n)945 ipf_fix_datacksum(u_short *sp, u_32_t n)
946 {
947 	u_short sumshort;
948 	u_32_t sum1;
949 
950 	if (n == 0)
951 		return;
952 
953 	sum1 = (~ntohs(*sp)) & 0xffff;
954 	sum1 += (n);
955 	sum1 = (sum1 >> 16) + (sum1 & 0xffff);
956 	/* Again */
957 	sum1 = (sum1 >> 16) + (sum1 & 0xffff);
958 	sumshort = ~(u_short)sum1;
959 	*(sp) = htons(sumshort);
960 }
961 
962 
963 /* ------------------------------------------------------------------------ */
964 /* Function:    ipf_nat_ioctl                                               */
965 /* Returns:     int - 0 == success, != 0 == failure                         */
966 /* Parameters:  softc(I) - pointer to soft context main structure           */
967 /*              data(I)  - pointer to ioctl data                            */
968 /*              cmd(I)   - ioctl command integer                            */
969 /*              mode(I)  - file mode bits used with open                    */
970 /*              uid(I)   - uid of calling process                           */
971 /*              ctx(I)   - pointer used as key for finding context          */
972 /*                                                                          */
973 /* Processes an ioctl call made to operate on the IP Filter NAT device.     */
974 /* ------------------------------------------------------------------------ */
975 int
ipf_nat_ioctl(ipf_main_softc_t * softc,caddr_t data,ioctlcmd_t cmd,int mode,int uid,void * ctx)976 ipf_nat_ioctl(ipf_main_softc_t *softc, caddr_t data, ioctlcmd_t cmd,
977 	int mode, int uid, void *ctx)
978 {
979 	ipf_nat_softc_t *softn = softc->ipf_nat_soft;
980 	int error = 0, ret, arg, getlock;
981 	ipnat_t *nat, *nt, *n;
982 	ipnat_t natd;
983 	SPL_INT(s);
984 
985 #if !SOLARIS && defined(_KERNEL)
986 # if NETBSD_GE_REV(399002000)
987 	if ((mode & FWRITE) &&
988 	     kauth_authorize_network(curlwp->l_cred, KAUTH_NETWORK_FIREWALL,
989 				     KAUTH_REQ_NETWORK_FIREWALL_FW,
990 				     NULL, NULL, NULL))
991 # else
992 #  if defined(__FreeBSD__)
993 	if (securelevel_ge(curthread->td_ucred, 3) && (mode & FWRITE))
994 #  else
995 	if ((securelevel >= 3) && (mode & FWRITE))
996 #  endif
997 # endif
998 	{
999 		IPFERROR(60001);
1000 		return (EPERM);
1001 	}
1002 #endif
1003 
1004 	getlock = (mode & NAT_LOCKHELD) ? 0 : 1;
1005 
1006 	n = NULL;
1007 	nt = NULL;
1008 	nat = NULL;
1009 
1010 	if ((cmd == (ioctlcmd_t)SIOCADNAT) || (cmd == (ioctlcmd_t)SIOCRMNAT) ||
1011 	    (cmd == (ioctlcmd_t)SIOCPURGENAT)) {
1012 		if (mode & NAT_SYSSPACE) {
1013 			bcopy(data, (char *)&natd, sizeof(natd));
1014 			nat = &natd;
1015 			error = 0;
1016 		} else {
1017 			bzero(&natd, sizeof(natd));
1018 			error = ipf_inobj(softc, data, NULL, &natd,
1019 					  IPFOBJ_IPNAT);
1020 			if (error != 0)
1021 				goto done;
1022 
1023 			if (natd.in_size < sizeof(ipnat_t)) {
1024 				error = EINVAL;
1025 				goto done;
1026 			}
1027 			KMALLOCS(nt, ipnat_t *, natd.in_size);
1028 			if (nt == NULL) {
1029 				IPFERROR(60070);
1030 				error = ENOMEM;
1031 				goto done;
1032 			}
1033 			bzero(nt, natd.in_size);
1034 			error = ipf_inobjsz(softc, data, nt, IPFOBJ_IPNAT,
1035 					    natd.in_size);
1036 			if (error)
1037 				goto done;
1038 			nat = nt;
1039 		}
1040 
1041 		/*
1042 		 * For add/delete, look to see if the NAT entry is
1043 		 * already present
1044 		 */
1045 		nat->in_flags &= IPN_USERFLAGS;
1046 		if ((nat->in_redir & NAT_MAPBLK) == 0) {
1047 			if (nat->in_osrcatype == FRI_NORMAL ||
1048 			    nat->in_osrcatype == FRI_NONE)
1049 				nat->in_osrcaddr &= nat->in_osrcmsk;
1050 			if (nat->in_odstatype == FRI_NORMAL ||
1051 			    nat->in_odstatype == FRI_NONE)
1052 				nat->in_odstaddr &= nat->in_odstmsk;
1053 			if ((nat->in_flags & (IPN_SPLIT|IPN_SIPRANGE)) == 0) {
1054 				if (nat->in_nsrcatype == FRI_NORMAL)
1055 					nat->in_nsrcaddr &= nat->in_nsrcmsk;
1056 				if (nat->in_ndstatype == FRI_NORMAL)
1057 					nat->in_ndstaddr &= nat->in_ndstmsk;
1058 			}
1059 		}
1060 
1061 		error = ipf_nat_rule_init(softc, softn, nat);
1062 		if (error != 0)
1063 			goto done;
1064 
1065 		MUTEX_ENTER(&softn->ipf_nat_io);
1066 		for (n = softn->ipf_nat_list; n != NULL; n = n->in_next)
1067 			if (ipf_nat_cmp_rules(nat, n) == 0)
1068 				break;
1069 	}
1070 
1071 	switch (cmd)
1072 	{
1073 #ifdef  IPFILTER_LOG
1074 	case SIOCIPFFB :
1075 	{
1076 		int tmp;
1077 
1078 		if (!(mode & FWRITE)) {
1079 			IPFERROR(60002);
1080 			error = EPERM;
1081 		} else {
1082 			tmp = ipf_log_clear(softc, IPL_LOGNAT);
1083 			error = BCOPYOUT(&tmp, data, sizeof(tmp));
1084 			if (error != 0) {
1085 				IPFERROR(60057);
1086 				error = EFAULT;
1087 			}
1088 		}
1089 		break;
1090 	}
1091 
1092 	case SIOCSETLG :
1093 		if (!(mode & FWRITE)) {
1094 			IPFERROR(60003);
1095 			error = EPERM;
1096 		} else {
1097 			error = BCOPYIN(data, &softn->ipf_nat_logging,
1098 					sizeof(softn->ipf_nat_logging));
1099 			if (error != 0)
1100 				error = EFAULT;
1101 		}
1102 		break;
1103 
1104 	case SIOCGETLG :
1105 		error = BCOPYOUT(&softn->ipf_nat_logging, data,
1106 				 sizeof(softn->ipf_nat_logging));
1107 		if (error != 0) {
1108 			IPFERROR(60004);
1109 			error = EFAULT;
1110 		}
1111 		break;
1112 
1113 	case FIONREAD :
1114 		arg = ipf_log_bytesused(softc, IPL_LOGNAT);
1115 		error = BCOPYOUT(&arg, data, sizeof(arg));
1116 		if (error != 0) {
1117 			IPFERROR(60005);
1118 			error = EFAULT;
1119 		}
1120 		break;
1121 #endif
1122 	case SIOCADNAT :
1123 		if (!(mode & FWRITE)) {
1124 			IPFERROR(60006);
1125 			error = EPERM;
1126 		} else if (n != NULL) {
1127 			natd.in_flineno = n->in_flineno;
1128 			(void) ipf_outobj(softc, data, &natd, IPFOBJ_IPNAT);
1129 			IPFERROR(60007);
1130 			error = EEXIST;
1131 		} else if (nt == NULL) {
1132 			IPFERROR(60008);
1133 			error = ENOMEM;
1134 		}
1135 		if (error != 0) {
1136 			MUTEX_EXIT(&softn->ipf_nat_io);
1137 			break;
1138 		}
1139 		if (nat != nt)
1140 			bcopy((char *)nat, (char *)nt, sizeof(*n));
1141 		error = ipf_nat_siocaddnat(softc, softn, nt, getlock);
1142 		MUTEX_EXIT(&softn->ipf_nat_io);
1143 		if (error == 0) {
1144 			nat = NULL;
1145 			nt = NULL;
1146 		}
1147 		break;
1148 
1149 	case SIOCRMNAT :
1150 	case SIOCPURGENAT :
1151 		if (!(mode & FWRITE)) {
1152 			IPFERROR(60009);
1153 			error = EPERM;
1154 			n = NULL;
1155 		} else if (n == NULL) {
1156 			IPFERROR(60010);
1157 			error = ESRCH;
1158 		}
1159 
1160 		if (error != 0) {
1161 			MUTEX_EXIT(&softn->ipf_nat_io);
1162 			break;
1163 		}
1164 		if (cmd == (ioctlcmd_t)SIOCPURGENAT) {
1165 			error = ipf_outobjsz(softc, data, n, IPFOBJ_IPNAT,
1166 					     n->in_size);
1167 			if (error) {
1168 				MUTEX_EXIT(&softn->ipf_nat_io);
1169 				goto done;
1170 			}
1171 			n->in_flags |= IPN_PURGE;
1172 		}
1173 		ipf_nat_siocdelnat(softc, softn, n, getlock);
1174 
1175 		MUTEX_EXIT(&softn->ipf_nat_io);
1176 		n = NULL;
1177 		break;
1178 
1179 	case SIOCGNATS :
1180 	    {
1181 		natstat_t *nsp = &softn->ipf_nat_stats;
1182 
1183 		nsp->ns_side[0].ns_table = softn->ipf_nat_table[0];
1184 		nsp->ns_side[1].ns_table = softn->ipf_nat_table[1];
1185 		nsp->ns_list = softn->ipf_nat_list;
1186 		nsp->ns_maptable = softn->ipf_hm_maptable;
1187 		nsp->ns_maplist = softn->ipf_hm_maplist;
1188 		nsp->ns_nattab_sz = softn->ipf_nat_table_sz;
1189 		nsp->ns_nattab_max = softn->ipf_nat_table_max;
1190 		nsp->ns_rultab_sz = softn->ipf_nat_maprules_sz;
1191 		nsp->ns_rdrtab_sz = softn->ipf_nat_rdrrules_sz;
1192 		nsp->ns_hostmap_sz = softn->ipf_nat_hostmap_sz;
1193 		nsp->ns_instances = softn->ipf_nat_instances;
1194 		nsp->ns_ticks = softc->ipf_ticks;
1195 #ifdef IPFILTER_LOGGING
1196 		nsp->ns_log_ok = ipf_log_logok(softc, IPF_LOGNAT);
1197 		nsp->ns_log_fail = ipf_log_failures(softc, IPF_LOGNAT);
1198 #else
1199 		nsp->ns_log_ok = 0;
1200 		nsp->ns_log_fail = 0;
1201 #endif
1202 		error = ipf_outobj(softc, data, nsp, IPFOBJ_NATSTAT);
1203 		break;
1204 	    }
1205 
1206 	case SIOCGNATL :
1207 	    {
1208 		natlookup_t nl;
1209 
1210 		error = ipf_inobj(softc, data, NULL, &nl, IPFOBJ_NATLOOKUP);
1211 		if (error == 0) {
1212 			void *ptr;
1213 
1214 			if (getlock) {
1215 				READ_ENTER(&softc->ipf_nat);
1216 			}
1217 
1218 			switch (nl.nl_v)
1219 			{
1220 			case 4 :
1221 				ptr = ipf_nat_lookupredir(&nl);
1222 				break;
1223 #ifdef USE_INET6
1224 			case 6 :
1225 				ptr = ipf_nat6_lookupredir(&nl);
1226 				break;
1227 #endif
1228 			default:
1229 				ptr = NULL;
1230 				break;
1231 			}
1232 
1233 			if (getlock) {
1234 				RWLOCK_EXIT(&softc->ipf_nat);
1235 			}
1236 			if (ptr != NULL) {
1237 				error = ipf_outobj(softc, data, &nl,
1238 						   IPFOBJ_NATLOOKUP);
1239 			} else {
1240 				IPFERROR(60011);
1241 				error = ESRCH;
1242 			}
1243 		}
1244 		break;
1245 	    }
1246 
1247 	case SIOCIPFFL :	/* old SIOCFLNAT & SIOCCNATL */
1248 		if (!(mode & FWRITE)) {
1249 			IPFERROR(60012);
1250 			error = EPERM;
1251 			break;
1252 		}
1253 		if (getlock) {
1254 			WRITE_ENTER(&softc->ipf_nat);
1255 		}
1256 
1257 		error = BCOPYIN(data, &arg, sizeof(arg));
1258 		if (error != 0) {
1259 			IPFERROR(60013);
1260 			error = EFAULT;
1261 		} else {
1262 			if (arg == 0)
1263 				ret = ipf_nat_flushtable(softc, softn);
1264 			else if (arg == 1)
1265 				ret = ipf_nat_clearlist(softc, softn);
1266 			else
1267 				ret = ipf_nat_extraflush(softc, softn, arg);
1268 			ipf_proxy_flush(softc->ipf_proxy_soft, arg);
1269 		}
1270 
1271 		if (getlock) {
1272 			RWLOCK_EXIT(&softc->ipf_nat);
1273 		}
1274 		if (error == 0) {
1275 			error = BCOPYOUT(&ret, data, sizeof(ret));
1276 		}
1277 		break;
1278 
1279 	case SIOCMATCHFLUSH :
1280 		if (!(mode & FWRITE)) {
1281 			IPFERROR(60014);
1282 			error = EPERM;
1283 			break;
1284 		}
1285 		if (getlock) {
1286 			WRITE_ENTER(&softc->ipf_nat);
1287 		}
1288 
1289 		error = ipf_nat_matchflush(softc, softn, data);
1290 
1291 		if (getlock) {
1292 			RWLOCK_EXIT(&softc->ipf_nat);
1293 		}
1294 		break;
1295 
1296 	case SIOCPROXY :
1297 		error = ipf_proxy_ioctl(softc, data, cmd, mode, ctx);
1298 		break;
1299 
1300 	case SIOCSTLCK :
1301 		if (!(mode & FWRITE)) {
1302 			IPFERROR(60015);
1303 			error = EPERM;
1304 		} else {
1305 			error = ipf_lock(data, &softn->ipf_nat_lock);
1306 		}
1307 		break;
1308 
1309 	case SIOCSTPUT :
1310 		if ((mode & FWRITE) != 0) {
1311 			error = ipf_nat_putent(softc, data, getlock);
1312 		} else {
1313 			IPFERROR(60016);
1314 			error = EACCES;
1315 		}
1316 		break;
1317 
1318 	case SIOCSTGSZ :
1319 		if (softn->ipf_nat_lock) {
1320 			error = ipf_nat_getsz(softc, data, getlock);
1321 		} else {
1322 			IPFERROR(60017);
1323 			error = EACCES;
1324 		}
1325 		break;
1326 
1327 	case SIOCSTGET :
1328 		if (softn->ipf_nat_lock) {
1329 			error = ipf_nat_getent(softc, data, getlock);
1330 		} else {
1331 			IPFERROR(60018);
1332 			error = EACCES;
1333 		}
1334 		break;
1335 
1336 	case SIOCGENITER :
1337 	    {
1338 		ipfgeniter_t iter;
1339 		ipftoken_t *token;
1340 		ipfobj_t obj;
1341 
1342 		error = ipf_inobj(softc, data, &obj, &iter, IPFOBJ_GENITER);
1343 		if (error != 0)
1344 			break;
1345 
1346 		SPL_SCHED(s);
1347 		token = ipf_token_find(softc, iter.igi_type, uid, ctx);
1348 		if (token != NULL) {
1349 			error  = ipf_nat_iterator(softc, token, &iter, &obj);
1350 			WRITE_ENTER(&softc->ipf_tokens);
1351 			ipf_token_deref(softc, token);
1352 			RWLOCK_EXIT(&softc->ipf_tokens);
1353 		}
1354 		SPL_X(s);
1355 		break;
1356 	    }
1357 
1358 	case SIOCIPFDELTOK :
1359 		error = BCOPYIN(data, &arg, sizeof(arg));
1360 		if (error == 0) {
1361 			SPL_SCHED(s);
1362 			error = ipf_token_del(softc, arg, uid, ctx);
1363 			SPL_X(s);
1364 		} else {
1365 			IPFERROR(60019);
1366 			error = EFAULT;
1367 		}
1368 		break;
1369 
1370 	case SIOCGTQTAB :
1371 		error = ipf_outobj(softc, data, softn->ipf_nat_tcptq,
1372 				   IPFOBJ_STATETQTAB);
1373 		break;
1374 
1375 	case SIOCGTABL :
1376 		error = ipf_nat_gettable(softc, softn, data);
1377 		break;
1378 
1379 	default :
1380 		IPFERROR(60020);
1381 		error = EINVAL;
1382 		break;
1383 	}
1384 done:
1385 	if (nat != NULL)
1386 		ipf_nat_rule_fini(softc, nat);
1387 	if (nt != NULL)
1388 		KFREES(nt, nt->in_size);
1389 	return (error);
1390 }
1391 
1392 
1393 /* ------------------------------------------------------------------------ */
1394 /* Function:    ipf_nat_siocaddnat                                          */
1395 /* Returns:     int - 0 == success, != 0 == failure                         */
1396 /* Parameters:  softc(I) - pointer to soft context main structure           */
1397 /*              softn(I) - pointer to NAT context structure                 */
1398 /*              n(I)       - pointer to new NAT rule                        */
1399 /*              np(I)      - pointer to where to insert new NAT rule        */
1400 /*              getlock(I) - flag indicating if lock on  is held            */
1401 /* Mutex Locks: ipf_nat_io                                                  */
1402 /*                                                                          */
1403 /* Handle SIOCADNAT.  Resolve and calculate details inside the NAT rule     */
1404 /* from information passed to the kernel, then add it  to the appropriate   */
1405 /* NAT rule table(s).                                                       */
1406 /* ------------------------------------------------------------------------ */
1407 static int
ipf_nat_siocaddnat(ipf_main_softc_t * softc,ipf_nat_softc_t * softn,ipnat_t * n,int getlock)1408 ipf_nat_siocaddnat(ipf_main_softc_t *softc, ipf_nat_softc_t *softn, ipnat_t *n,
1409 	int getlock)
1410 {
1411 	int error = 0;
1412 
1413 	if (ipf_nat_resolverule(softc, n) != 0) {
1414 		IPFERROR(60022);
1415 		return (ENOENT);
1416 	}
1417 
1418 	if ((n->in_age[0] == 0) && (n->in_age[1] != 0)) {
1419 		IPFERROR(60023);
1420 		return (EINVAL);
1421 	}
1422 
1423 	if (n->in_redir == (NAT_DIVERTUDP|NAT_MAP)) {
1424 		/*
1425 		 * Prerecord whether or not the destination of the divert
1426 		 * is local or not to the interface the packet is going
1427 		 * to be sent out.
1428 		 */
1429 		n->in_dlocal = ipf_deliverlocal(softc, n->in_v[1],
1430 						n->in_ifps[1], &n->in_ndstip6);
1431 	}
1432 
1433 	if (getlock) {
1434 		WRITE_ENTER(&softc->ipf_nat);
1435 	}
1436 	n->in_next = NULL;
1437 	n->in_pnext = softn->ipf_nat_list_tail;
1438 	*n->in_pnext = n;
1439 	softn->ipf_nat_list_tail = &n->in_next;
1440 	n->in_use++;
1441 
1442 	if (n->in_redir & NAT_REDIRECT) {
1443 		n->in_flags &= ~IPN_NOTDST;
1444 		switch (n->in_v[0])
1445 		{
1446 		case 4 :
1447 			ipf_nat_addrdr(softn, n);
1448 			break;
1449 #ifdef USE_INET6
1450 		case 6 :
1451 			ipf_nat6_addrdr(softn, n);
1452 			break;
1453 #endif
1454 		default :
1455 			break;
1456 		}
1457 		ATOMIC_INC32(softn->ipf_nat_stats.ns_rules_rdr);
1458 	}
1459 
1460 	if (n->in_redir & (NAT_MAP|NAT_MAPBLK)) {
1461 		n->in_flags &= ~IPN_NOTSRC;
1462 		switch (n->in_v[0])
1463 		{
1464 		case 4 :
1465 			ipf_nat_addmap(softn, n);
1466 			break;
1467 #ifdef USE_INET6
1468 		case 6 :
1469 			ipf_nat6_addmap(softn, n);
1470 			break;
1471 #endif
1472 		default :
1473 			break;
1474 		}
1475 		ATOMIC_INC32(softn->ipf_nat_stats.ns_rules_map);
1476 	}
1477 
1478 	if (n->in_age[0] != 0)
1479 		n->in_tqehead[0] = ipf_addtimeoutqueue(softc,
1480 						       &softn->ipf_nat_utqe,
1481 						       n->in_age[0]);
1482 
1483 	if (n->in_age[1] != 0)
1484 		n->in_tqehead[1] = ipf_addtimeoutqueue(softc,
1485 						       &softn->ipf_nat_utqe,
1486 						       n->in_age[1]);
1487 
1488 	MUTEX_INIT(&n->in_lock, "ipnat rule lock");
1489 
1490 	n = NULL;
1491 	ATOMIC_INC32(softn->ipf_nat_stats.ns_rules);
1492 #if SOLARIS && !defined(INSTANCES)
1493 	pfil_delayed_copy = 0;
1494 #endif
1495 	if (getlock) {
1496 		RWLOCK_EXIT(&softc->ipf_nat);			/* WRITE */
1497 	}
1498 
1499 	return (error);
1500 }
1501 
1502 
1503 /* ------------------------------------------------------------------------ */
1504 /* Function:    ipf_nat_ruleaddrinit                                        */
1505 /* Parameters:  softc(I) - pointer to soft context main structure           */
1506 /*              softn(I) - pointer to NAT context structure                 */
1507 /*              n(I)     - pointer to NAT rule                              */
1508 /*                                                                          */
1509 /* Initialise all of the NAT address structures in a NAT rule.              */
1510 /* ------------------------------------------------------------------------ */
1511 static int
ipf_nat_ruleaddrinit(ipf_main_softc_t * softc,ipf_nat_softc_t * softn,ipnat_t * n)1512 ipf_nat_ruleaddrinit(ipf_main_softc_t *softc, ipf_nat_softc_t *softn,
1513 	ipnat_t *n)
1514 {
1515 	int idx, error;
1516 
1517 	if ((n->in_ndst.na_atype == FRI_LOOKUP) &&
1518 	    (n->in_ndst.na_type != IPLT_DSTLIST)) {
1519 		IPFERROR(60071);
1520 		return (EINVAL);
1521 	}
1522 	if ((n->in_nsrc.na_atype == FRI_LOOKUP) &&
1523 	    (n->in_nsrc.na_type != IPLT_DSTLIST)) {
1524 		IPFERROR(60069);
1525 		return (EINVAL);
1526 	}
1527 
1528 	if (n->in_redir == NAT_BIMAP) {
1529 		n->in_ndstaddr = n->in_osrcaddr;
1530 		n->in_ndstmsk = n->in_osrcmsk;
1531 		n->in_odstaddr = n->in_nsrcaddr;
1532 		n->in_odstmsk = n->in_nsrcmsk;
1533 
1534 	}
1535 
1536 	if (n->in_redir & NAT_REDIRECT)
1537 		idx = 1;
1538 	else
1539 		idx = 0;
1540 	/*
1541 	 * Initialise all of the address fields.
1542 	 */
1543 	error = ipf_nat_nextaddrinit(softc, n->in_names, &n->in_osrc, 1,
1544 				     n->in_ifps[idx]);
1545 	if (error != 0)
1546 		return (error);
1547 
1548 	error = ipf_nat_nextaddrinit(softc, n->in_names, &n->in_odst, 1,
1549 				     n->in_ifps[idx]);
1550 	if (error != 0)
1551 		return (error);
1552 
1553 	error = ipf_nat_nextaddrinit(softc, n->in_names, &n->in_nsrc, 1,
1554 				     n->in_ifps[idx]);
1555 	if (error != 0)
1556 		return (error);
1557 
1558 	error = ipf_nat_nextaddrinit(softc, n->in_names, &n->in_ndst, 1,
1559 				     n->in_ifps[idx]);
1560 	if (error != 0)
1561 		return (error);
1562 
1563 	if (n->in_redir & NAT_DIVERTUDP)
1564 		ipf_nat_builddivertmp(softn, n);
1565 
1566 	return (0);
1567 }
1568 
1569 
1570 /* ------------------------------------------------------------------------ */
1571 /* Function:    ipf_nat_resolvrule                                          */
1572 /* Returns:     Nil                                                         */
1573 /* Parameters:  softc(I) - pointer to soft context main structure           */
1574 /*              n(I)     - pointer to NAT rule                              */
1575 /*                                                                          */
1576 /* Handle SIOCADNAT.  Resolve and calculate details inside the NAT rule     */
1577 /* from information passed to the kernel, then add it  to the appropriate   */
1578 /* NAT rule table(s).                                                       */
1579 /* ------------------------------------------------------------------------ */
1580 static int
ipf_nat_resolverule(ipf_main_softc_t * softc,ipnat_t * n)1581 ipf_nat_resolverule(ipf_main_softc_t *softc, ipnat_t *n)
1582 {
1583 	char *base;
1584 
1585 	base = n->in_names;
1586 
1587 	n->in_ifps[0] = ipf_resolvenic(softc, base + n->in_ifnames[0],
1588 				       n->in_v[0]);
1589 
1590 	if (n->in_ifnames[1] == -1) {
1591 		n->in_ifnames[1] = n->in_ifnames[0];
1592 		n->in_ifps[1] = n->in_ifps[0];
1593 	} else {
1594 		n->in_ifps[1] = ipf_resolvenic(softc, base + n->in_ifnames[1],
1595 					       n->in_v[1]);
1596 	}
1597 
1598 	if (n->in_plabel != -1) {
1599 		if (n->in_redir & NAT_REDIRECT)
1600 			n->in_apr = ipf_proxy_lookup(softc->ipf_proxy_soft,
1601 						     n->in_pr[0],
1602 						     base + n->in_plabel);
1603 		else
1604 			n->in_apr = ipf_proxy_lookup(softc->ipf_proxy_soft,
1605 						     n->in_pr[1],
1606 						     base + n->in_plabel);
1607 		if (n->in_apr == NULL)
1608 			return (-1);
1609 	}
1610 	return (0);
1611 }
1612 
1613 
1614 /* ------------------------------------------------------------------------ */
1615 /* Function:    ipf_nat_siocdelnat                                          */
1616 /* Returns:     int - 0 == success, != 0 == failure                         */
1617 /* Parameters:  softc(I)   - pointer to soft context main structure         */
1618 /*              softn(I)   - pointer to NAT context structure               */
1619 /*              n(I)       - pointer to new NAT rule                        */
1620 /*              getlock(I) - flag indicating if lock on  is held            */
1621 /* Mutex Locks: ipf_nat_io                                                  */
1622 /*                                                                          */
1623 /* Handle SIOCADNAT.  Resolve and calculate details inside the NAT rule     */
1624 /* from information passed to the kernel, then add it  to the appropriate   */
1625 /* NAT rule table(s).                                                       */
1626 /* ------------------------------------------------------------------------ */
1627 static void
ipf_nat_siocdelnat(ipf_main_softc_t * softc,ipf_nat_softc_t * softn,ipnat_t * n,int getlock)1628 ipf_nat_siocdelnat(ipf_main_softc_t *softc, ipf_nat_softc_t *softn, ipnat_t *n,
1629 	int getlock)
1630 {
1631 	if (getlock) {
1632 		WRITE_ENTER(&softc->ipf_nat);
1633 	}
1634 
1635 	ipf_nat_delrule(softc, softn, n, 1);
1636 
1637 	if (getlock) {
1638 		RWLOCK_EXIT(&softc->ipf_nat);			/* READ/WRITE */
1639 	}
1640 }
1641 
1642 
1643 /* ------------------------------------------------------------------------ */
1644 /* Function:    ipf_nat_getsz                                               */
1645 /* Returns:     int - 0 == success, != 0 is the error value.                */
1646 /* Parameters:  softc(I)   - pointer to soft context main structure         */
1647 /*              data(I)    - pointer to natget structure with kernel        */
1648 /*                           pointer get the size of.                       */
1649 /*              getlock(I) - flag indicating whether or not the caller      */
1650 /*                           holds a lock on ipf_nat                        */
1651 /*                                                                          */
1652 /* Handle SIOCSTGSZ.                                                        */
1653 /* Return the size of the nat list entry to be copied back to user space.   */
1654 /* The size of the entry is stored in the ng_sz field and the enture natget */
1655 /* structure is copied back to the user.                                    */
1656 /* ------------------------------------------------------------------------ */
1657 static int
ipf_nat_getsz(ipf_main_softc_t * softc,caddr_t data,int getlock)1658 ipf_nat_getsz(ipf_main_softc_t *softc, caddr_t data, int getlock)
1659 {
1660 	ipf_nat_softc_t *softn = softc->ipf_nat_soft;
1661 	ap_session_t *aps;
1662 	nat_t *nat, *n;
1663 	natget_t ng;
1664 	int error;
1665 
1666 	error = BCOPYIN(data, &ng, sizeof(ng));
1667 	if (error != 0) {
1668 		IPFERROR(60024);
1669 		return (EFAULT);
1670 	}
1671 
1672 	if (getlock) {
1673 		READ_ENTER(&softc->ipf_nat);
1674 	}
1675 
1676 	nat = ng.ng_ptr;
1677 	if (!nat) {
1678 		nat = softn->ipf_nat_instances;
1679 		ng.ng_sz = 0;
1680 		/*
1681 		 * Empty list so the size returned is 0.  Simple.
1682 		 */
1683 		if (nat == NULL) {
1684 			if (getlock) {
1685 				RWLOCK_EXIT(&softc->ipf_nat);
1686 			}
1687 			error = BCOPYOUT(&ng, data, sizeof(ng));
1688 			if (error != 0) {
1689 				IPFERROR(60025);
1690 				return (EFAULT);
1691 			}
1692 			return (0);
1693 		}
1694 	} else {
1695 		/*
1696 		 * Make sure the pointer we're copying from exists in the
1697 		 * current list of entries.  Security precaution to prevent
1698 		 * copying of random kernel data.
1699 		 */
1700 		for (n = softn->ipf_nat_instances; n; n = n->nat_next)
1701 			if (n == nat)
1702 				break;
1703 		if (n == NULL) {
1704 			if (getlock) {
1705 				RWLOCK_EXIT(&softc->ipf_nat);
1706 			}
1707 			IPFERROR(60026);
1708 			return (ESRCH);
1709 		}
1710 	}
1711 
1712 	/*
1713 	 * Include any space required for proxy data structures.
1714 	 */
1715 	ng.ng_sz = sizeof(nat_save_t);
1716 	aps = nat->nat_aps;
1717 	if (aps != NULL) {
1718 		ng.ng_sz += sizeof(ap_session_t) - 4;
1719 		if (aps->aps_data != 0)
1720 			ng.ng_sz += aps->aps_psiz;
1721 	}
1722 	if (getlock) {
1723 		RWLOCK_EXIT(&softc->ipf_nat);
1724 	}
1725 
1726 	error = BCOPYOUT(&ng, data, sizeof(ng));
1727 	if (error != 0) {
1728 		IPFERROR(60027);
1729 		return (EFAULT);
1730 	}
1731 	return (0);
1732 }
1733 
1734 
1735 /* ------------------------------------------------------------------------ */
1736 /* Function:    ipf_nat_getent                                              */
1737 /* Returns:     int - 0 == success, != 0 is the error value.                */
1738 /* Parameters:  softc(I)   - pointer to soft context main structure         */
1739 /*              data(I)    - pointer to natget structure with kernel pointer*/
1740 /*                           to NAT structure to copy out.                  */
1741 /*              getlock(I) - flag indicating whether or not the caller      */
1742 /*                           holds a lock on ipf_nat                        */
1743 /*                                                                          */
1744 /* Handle SIOCSTGET.                                                        */
1745 /* Copies out NAT entry to user space.  Any additional data held for a      */
1746 /* proxy is also copied, as to is the NAT rule which was responsible for it */
1747 /* ------------------------------------------------------------------------ */
1748 static int
ipf_nat_getent(ipf_main_softc_t * softc,caddr_t data,int getlock)1749 ipf_nat_getent(ipf_main_softc_t *softc, caddr_t data, int getlock)
1750 {
1751 	ipf_nat_softc_t *softn = softc->ipf_nat_soft;
1752 	int error, outsize;
1753 	ap_session_t *aps;
1754 	nat_save_t *ipn, ipns;
1755 	nat_t *n, *nat;
1756 
1757 	error = ipf_inobj(softc, data, NULL, &ipns, IPFOBJ_NATSAVE);
1758 	if (error != 0)
1759 		return (error);
1760 
1761 	if ((ipns.ipn_dsize < sizeof(ipns)) || (ipns.ipn_dsize > 81920)) {
1762 		IPFERROR(60028);
1763 		return (EINVAL);
1764 	}
1765 
1766 	KMALLOCS(ipn, nat_save_t *, ipns.ipn_dsize);
1767 	if (ipn == NULL) {
1768 		IPFERROR(60029);
1769 		return (ENOMEM);
1770 	}
1771 
1772 	if (getlock) {
1773 		READ_ENTER(&softc->ipf_nat);
1774 	}
1775 
1776 	ipn->ipn_dsize = ipns.ipn_dsize;
1777 	nat = ipns.ipn_next;
1778 	if (nat == NULL) {
1779 		nat = softn->ipf_nat_instances;
1780 		if (nat == NULL) {
1781 			if (softn->ipf_nat_instances == NULL) {
1782 				IPFERROR(60030);
1783 				error = ENOENT;
1784 			}
1785 			goto finished;
1786 		}
1787 	} else {
1788 		/*
1789 		 * Make sure the pointer we're copying from exists in the
1790 		 * current list of entries.  Security precaution to prevent
1791 		 * copying of random kernel data.
1792 		 */
1793 		for (n = softn->ipf_nat_instances; n; n = n->nat_next)
1794 			if (n == nat)
1795 				break;
1796 		if (n == NULL) {
1797 			IPFERROR(60031);
1798 			error = ESRCH;
1799 			goto finished;
1800 		}
1801 	}
1802 	ipn->ipn_next = nat->nat_next;
1803 
1804 	/*
1805 	 * Copy the NAT structure.
1806 	 */
1807 	bcopy((char *)nat, &ipn->ipn_nat, sizeof(*nat));
1808 
1809 	/*
1810 	 * If we have a pointer to the NAT rule it belongs to, save that too.
1811 	 */
1812 	if (nat->nat_ptr != NULL)
1813 		bcopy((char *)nat->nat_ptr, (char *)&ipn->ipn_ipnat,
1814 		      sizeof(nat->nat_ptr));
1815 
1816 	/*
1817 	 * If we also know the NAT entry has an associated filter rule,
1818 	 * save that too.
1819 	 */
1820 	if (nat->nat_fr != NULL)
1821 		bcopy((char *)nat->nat_fr, (char *)&ipn->ipn_fr,
1822 		      sizeof(ipn->ipn_fr));
1823 
1824 	/*
1825 	 * Last but not least, if there is an application proxy session set
1826 	 * up for this NAT entry, then copy that out too, including any
1827 	 * private data saved along side it by the proxy.
1828 	 */
1829 	aps = nat->nat_aps;
1830 	outsize = ipn->ipn_dsize - sizeof(*ipn) + sizeof(ipn->ipn_data);
1831 	if (aps != NULL) {
1832 		char *s;
1833 
1834 		if (outsize < sizeof(*aps)) {
1835 			IPFERROR(60032);
1836 			error = ENOBUFS;
1837 			goto finished;
1838 		}
1839 
1840 		s = ipn->ipn_data;
1841 		bcopy((char *)aps, s, sizeof(*aps));
1842 		s += sizeof(*aps);
1843 		outsize -= sizeof(*aps);
1844 		if ((aps->aps_data != NULL) && (outsize >= aps->aps_psiz))
1845 			bcopy(aps->aps_data, s, aps->aps_psiz);
1846 		else {
1847 			IPFERROR(60033);
1848 			error = ENOBUFS;
1849 		}
1850 	}
1851 	if (error == 0) {
1852 		error = ipf_outobjsz(softc, data, ipn, IPFOBJ_NATSAVE,
1853 				     ipns.ipn_dsize);
1854 	}
1855 
1856 finished:
1857 	if (ipn != NULL) {
1858 		KFREES(ipn, ipns.ipn_dsize);
1859 	}
1860 	if (getlock) {
1861 		RWLOCK_EXIT(&softc->ipf_nat);
1862 	}
1863 	return (error);
1864 }
1865 
1866 
1867 /* ------------------------------------------------------------------------ */
1868 /* Function:    ipf_nat_putent                                              */
1869 /* Returns:     int - 0 == success, != 0 is the error value.                */
1870 /* Parameters:  softc(I)   - pointer to soft context main structure         */
1871 /*              data(I)    - pointer to natget structure with NAT           */
1872 /*                           structure information to load into the kernel  */
1873 /*              getlock(I) - flag indicating whether or not a write lock    */
1874 /*                           on is already held.                            */
1875 /*                                                                          */
1876 /* Handle SIOCSTPUT.                                                        */
1877 /* Loads a NAT table entry from user space, including a NAT rule, proxy and */
1878 /* firewall rule data structures, if pointers to them indicate so.          */
1879 /* ------------------------------------------------------------------------ */
1880 static int
ipf_nat_putent(ipf_main_softc_t * softc,caddr_t data,int getlock)1881 ipf_nat_putent(ipf_main_softc_t *softc, caddr_t data, int getlock)
1882 {
1883 	ipf_nat_softc_t *softn = softc->ipf_nat_soft;
1884 	nat_save_t ipn, *ipnn;
1885 	ap_session_t *aps;
1886 	nat_t *n, *nat;
1887 	frentry_t *fr;
1888 	fr_info_t fin;
1889 	ipnat_t *in;
1890 	int error;
1891 
1892 	error = ipf_inobj(softc, data, NULL, &ipn, IPFOBJ_NATSAVE);
1893 	if (error != 0)
1894 		return (error);
1895 
1896 	/*
1897 	 * Initialise early because of code at junkput label.
1898 	 */
1899 	n = NULL;
1900 	in = NULL;
1901 	aps = NULL;
1902 	nat = NULL;
1903 	ipnn = NULL;
1904 	fr = NULL;
1905 
1906 	/*
1907 	 * New entry, copy in the rest of the NAT entry if it's size is more
1908 	 * than just the nat_t structure.
1909 	 */
1910 	if (ipn.ipn_dsize > sizeof(ipn)) {
1911 		if (ipn.ipn_dsize > 81920) {
1912 			IPFERROR(60034);
1913 			error = ENOMEM;
1914 			goto junkput;
1915 		}
1916 
1917 		KMALLOCS(ipnn, nat_save_t *, ipn.ipn_dsize);
1918 		if (ipnn == NULL) {
1919 			IPFERROR(60035);
1920 			return (ENOMEM);
1921 		}
1922 
1923 		bzero(ipnn, ipn.ipn_dsize);
1924 		error = ipf_inobjsz(softc, data, ipnn, IPFOBJ_NATSAVE,
1925 				    ipn.ipn_dsize);
1926 		if (error != 0) {
1927 			goto junkput;
1928 		}
1929 	} else
1930 		ipnn = &ipn;
1931 
1932 	KMALLOC(nat, nat_t *);
1933 	if (nat == NULL) {
1934 		IPFERROR(60037);
1935 		error = ENOMEM;
1936 		goto junkput;
1937 	}
1938 
1939 	bcopy((char *)&ipnn->ipn_nat, (char *)nat, sizeof(*nat));
1940 
1941 	switch (nat->nat_v[0])
1942 	{
1943 	case 4:
1944 #ifdef USE_INET6
1945 	case 6 :
1946 #endif
1947 		break;
1948 	default :
1949 		IPFERROR(60061);
1950 		error = EPROTONOSUPPORT;
1951 		goto junkput;
1952 		/*NOTREACHED*/
1953 	}
1954 
1955 	/*
1956 	 * Initialize all these so that ipf_nat_delete() doesn't cause a crash.
1957 	 */
1958 	bzero((char *)nat, offsetof(struct nat, nat_tqe));
1959 	nat->nat_tqe.tqe_pnext = NULL;
1960 	nat->nat_tqe.tqe_next = NULL;
1961 	nat->nat_tqe.tqe_ifq = NULL;
1962 	nat->nat_tqe.tqe_parent = nat;
1963 
1964 	/*
1965 	 * Restore the rule associated with this nat session
1966 	 */
1967 	in = ipnn->ipn_nat.nat_ptr;
1968 	if (in != NULL) {
1969 		KMALLOCS(in, ipnat_t *, ipnn->ipn_ipnat.in_size);
1970 		nat->nat_ptr = in;
1971 		if (in == NULL) {
1972 			IPFERROR(60038);
1973 			error = ENOMEM;
1974 			goto junkput;
1975 		}
1976 		bcopy((char *)&ipnn->ipn_ipnat, (char *)in,
1977 		      ipnn->ipn_ipnat.in_size);
1978 		in->in_use = 1;
1979 		in->in_flags |= IPN_DELETE;
1980 
1981 		ATOMIC_INC32(softn->ipf_nat_stats.ns_rules);
1982 
1983 		if (ipf_nat_resolverule(softc, in) != 0) {
1984 			IPFERROR(60039);
1985 			error = ESRCH;
1986 			goto junkput;
1987 		}
1988 	}
1989 
1990 	/*
1991 	 * Check that the NAT entry doesn't already exist in the kernel.
1992 	 *
1993 	 * For NAT_OUTBOUND, we're lookup for a duplicate MAP entry.  To do
1994 	 * this, we check to see if the inbound combination of addresses and
1995 	 * ports is already known.  Similar logic is applied for NAT_INBOUND.
1996 	 *
1997 	 */
1998 	bzero((char *)&fin, sizeof(fin));
1999 	fin.fin_v = nat->nat_v[0];
2000 	fin.fin_p = nat->nat_pr[0];
2001 	fin.fin_rev = nat->nat_rev;
2002 	fin.fin_ifp = nat->nat_ifps[0];
2003 	fin.fin_data[0] = ntohs(nat->nat_ndport);
2004 	fin.fin_data[1] = ntohs(nat->nat_nsport);
2005 
2006 	switch (nat->nat_dir)
2007 	{
2008 	case NAT_OUTBOUND :
2009 	case NAT_DIVERTOUT :
2010 		if (getlock) {
2011 			READ_ENTER(&softc->ipf_nat);
2012 		}
2013 
2014 		fin.fin_v = nat->nat_v[1];
2015 		if (nat->nat_v[1] == 4) {
2016 			n = ipf_nat_inlookup(&fin, nat->nat_flags, fin.fin_p,
2017 					     nat->nat_ndstip, nat->nat_nsrcip);
2018 #ifdef USE_INET6
2019 		} else if (nat->nat_v[1] == 6) {
2020 			n = ipf_nat6_inlookup(&fin, nat->nat_flags, fin.fin_p,
2021 					      &nat->nat_ndst6.in6,
2022 					      &nat->nat_nsrc6.in6);
2023 #endif
2024 		}
2025 
2026 		if (getlock) {
2027 			RWLOCK_EXIT(&softc->ipf_nat);
2028 		}
2029 		if (n != NULL) {
2030 			IPFERROR(60040);
2031 			error = EEXIST;
2032 			goto junkput;
2033 		}
2034 		break;
2035 
2036 	case NAT_INBOUND :
2037 	case NAT_DIVERTIN :
2038 		if (getlock) {
2039 			READ_ENTER(&softc->ipf_nat);
2040 		}
2041 
2042 		if (fin.fin_v == 4) {
2043 			n = ipf_nat_outlookup(&fin, nat->nat_flags, fin.fin_p,
2044 					      nat->nat_ndstip,
2045 					      nat->nat_nsrcip);
2046 #ifdef USE_INET6
2047 		} else if (fin.fin_v == 6) {
2048 			n = ipf_nat6_outlookup(&fin, nat->nat_flags, fin.fin_p,
2049 					       &nat->nat_ndst6.in6,
2050 					       &nat->nat_nsrc6.in6);
2051 #endif
2052 		}
2053 
2054 		if (getlock) {
2055 			RWLOCK_EXIT(&softc->ipf_nat);
2056 		}
2057 		if (n != NULL) {
2058 			IPFERROR(60041);
2059 			error = EEXIST;
2060 			goto junkput;
2061 		}
2062 		break;
2063 
2064 	default :
2065 		IPFERROR(60042);
2066 		error = EINVAL;
2067 		goto junkput;
2068 	}
2069 
2070 	/*
2071 	 * Restore ap_session_t structure.  Include the private data allocated
2072 	 * if it was there.
2073 	 */
2074 	aps = nat->nat_aps;
2075 	if (aps != NULL) {
2076 		KMALLOC(aps, ap_session_t *);
2077 		nat->nat_aps = aps;
2078 		if (aps == NULL) {
2079 			IPFERROR(60043);
2080 			error = ENOMEM;
2081 			goto junkput;
2082 		}
2083 		bcopy(ipnn->ipn_data, (char *)aps, sizeof(*aps));
2084 		if (in != NULL)
2085 			aps->aps_apr = in->in_apr;
2086 		else
2087 			aps->aps_apr = NULL;
2088 		if (aps->aps_psiz != 0) {
2089 			if (aps->aps_psiz > 81920) {
2090 				IPFERROR(60044);
2091 				error = ENOMEM;
2092 				goto junkput;
2093 			}
2094 			KMALLOCS(aps->aps_data, void *, aps->aps_psiz);
2095 			if (aps->aps_data == NULL) {
2096 				IPFERROR(60045);
2097 				error = ENOMEM;
2098 				goto junkput;
2099 			}
2100 			bcopy(ipnn->ipn_data + sizeof(*aps), aps->aps_data,
2101 			      aps->aps_psiz);
2102 		} else {
2103 			aps->aps_psiz = 0;
2104 			aps->aps_data = NULL;
2105 		}
2106 	}
2107 
2108 	/*
2109 	 * If there was a filtering rule associated with this entry then
2110 	 * build up a new one.
2111 	 */
2112 	fr = nat->nat_fr;
2113 	if (fr != NULL) {
2114 		if ((nat->nat_flags & SI_NEWFR) != 0) {
2115 			KMALLOC(fr, frentry_t *);
2116 			nat->nat_fr = fr;
2117 			if (fr == NULL) {
2118 				IPFERROR(60046);
2119 				error = ENOMEM;
2120 				goto junkput;
2121 			}
2122 			ipnn->ipn_nat.nat_fr = fr;
2123 			fr->fr_ref = 1;
2124 			(void) ipf_outobj(softc, data, ipnn, IPFOBJ_NATSAVE);
2125 			bcopy((char *)&ipnn->ipn_fr, (char *)fr, sizeof(*fr));
2126 
2127 			fr->fr_ref = 1;
2128 			fr->fr_dsize = 0;
2129 			fr->fr_data = NULL;
2130 			fr->fr_type = FR_T_NONE;
2131 
2132 			MUTEX_NUKE(&fr->fr_lock);
2133 			MUTEX_INIT(&fr->fr_lock, "nat-filter rule lock");
2134 		} else {
2135 			if (getlock) {
2136 				READ_ENTER(&softc->ipf_nat);
2137 			}
2138 			for (n = softn->ipf_nat_instances; n; n = n->nat_next)
2139 				if (n->nat_fr == fr)
2140 					break;
2141 
2142 			if (n != NULL) {
2143 				MUTEX_ENTER(&fr->fr_lock);
2144 				fr->fr_ref++;
2145 				MUTEX_EXIT(&fr->fr_lock);
2146 			}
2147 			if (getlock) {
2148 				RWLOCK_EXIT(&softc->ipf_nat);
2149 			}
2150 
2151 			if (n == NULL) {
2152 				IPFERROR(60047);
2153 				error = ESRCH;
2154 				goto junkput;
2155 			}
2156 		}
2157 	}
2158 
2159 	if (ipnn != &ipn) {
2160 		KFREES(ipnn, ipn.ipn_dsize);
2161 		ipnn = NULL;
2162 	}
2163 
2164 	if (getlock) {
2165 		WRITE_ENTER(&softc->ipf_nat);
2166 	}
2167 
2168 	if (fin.fin_v == 4)
2169 		error = ipf_nat_finalise(&fin, nat);
2170 #ifdef USE_INET6
2171 	else
2172 		error = ipf_nat6_finalise(&fin, nat);
2173 #endif
2174 
2175 	if (getlock) {
2176 		RWLOCK_EXIT(&softc->ipf_nat);
2177 	}
2178 
2179 	if (error == 0)
2180 		return (0);
2181 
2182 	IPFERROR(60048);
2183 	error = ENOMEM;
2184 
2185 junkput:
2186 	if (fr != NULL) {
2187 		(void) ipf_derefrule(softc, &fr);
2188 	}
2189 
2190 	if ((ipnn != NULL) && (ipnn != &ipn)) {
2191 		KFREES(ipnn, ipn.ipn_dsize);
2192 	}
2193 	if (nat != NULL) {
2194 		if (aps != NULL) {
2195 			if (aps->aps_data != NULL) {
2196 				KFREES(aps->aps_data, aps->aps_psiz);
2197 			}
2198 			KFREE(aps);
2199 		}
2200 		if (in != NULL) {
2201 			if (in->in_apr)
2202 				ipf_proxy_deref(in->in_apr);
2203 			KFREES(in, in->in_size);
2204 		}
2205 		KFREE(nat);
2206 	}
2207 	return (error);
2208 }
2209 
2210 
2211 /* ------------------------------------------------------------------------ */
2212 /* Function:    ipf_nat_delete                                              */
2213 /* Returns:     Nil                                                         */
2214 /* Parameters:  softc(I)   - pointer to soft context main structure         */
2215 /*              nat(I)     - pointer to NAT structure to delete             */
2216 /*              logtype(I) - type of LOG record to create before deleting   */
2217 /* Write Lock:  ipf_nat                                                     */
2218 /*                                                                          */
2219 /* Delete a nat entry from the various lists and table.  If NAT logging is  */
2220 /* enabled then generate a NAT log record for this event.                   */
2221 /* ------------------------------------------------------------------------ */
2222 void
ipf_nat_delete(ipf_main_softc_t * softc,struct nat * nat,int logtype)2223 ipf_nat_delete(ipf_main_softc_t *softc, struct nat *nat, int logtype)
2224 {
2225 	ipf_nat_softc_t *softn = softc->ipf_nat_soft;
2226 	int madeorphan = 0, bkt, removed = 0;
2227 	nat_stat_side_t *nss;
2228 	struct ipnat *ipn;
2229 
2230 	if (logtype != 0 && softn->ipf_nat_logging != 0)
2231 		ipf_nat_log(softc, softn, nat, logtype);
2232 
2233 	/*
2234 	 * Take it as a general indication that all the pointers are set if
2235 	 * nat_pnext is set.
2236 	 */
2237 	if (nat->nat_pnext != NULL) {
2238 		removed = 1;
2239 
2240 		bkt = nat->nat_hv[0] % softn->ipf_nat_table_sz;
2241 		nss = &softn->ipf_nat_stats.ns_side[0];
2242 		if (nss->ns_bucketlen[bkt] > 0)
2243 			nss->ns_bucketlen[bkt]--;
2244 		if (nss->ns_bucketlen[bkt] == 0) {
2245 			nss->ns_inuse--;
2246 		}
2247 
2248 		bkt = nat->nat_hv[1] % softn->ipf_nat_table_sz;
2249 		nss = &softn->ipf_nat_stats.ns_side[1];
2250 		if (nss->ns_bucketlen[bkt] > 0)
2251 			nss->ns_bucketlen[bkt]--;
2252 		if (nss->ns_bucketlen[bkt] == 0) {
2253 			nss->ns_inuse--;
2254 		}
2255 
2256 		*nat->nat_pnext = nat->nat_next;
2257 		if (nat->nat_next != NULL) {
2258 			nat->nat_next->nat_pnext = nat->nat_pnext;
2259 			nat->nat_next = NULL;
2260 		}
2261 		nat->nat_pnext = NULL;
2262 
2263 		*nat->nat_phnext[0] = nat->nat_hnext[0];
2264 		if (nat->nat_hnext[0] != NULL) {
2265 			nat->nat_hnext[0]->nat_phnext[0] = nat->nat_phnext[0];
2266 			nat->nat_hnext[0] = NULL;
2267 		}
2268 		nat->nat_phnext[0] = NULL;
2269 
2270 		*nat->nat_phnext[1] = nat->nat_hnext[1];
2271 		if (nat->nat_hnext[1] != NULL) {
2272 			nat->nat_hnext[1]->nat_phnext[1] = nat->nat_phnext[1];
2273 			nat->nat_hnext[1] = NULL;
2274 		}
2275 		nat->nat_phnext[1] = NULL;
2276 
2277 		if ((nat->nat_flags & SI_WILDP) != 0) {
2278 			ATOMIC_DEC32(softn->ipf_nat_stats.ns_wilds);
2279 		}
2280 		madeorphan = 1;
2281 	}
2282 
2283 	if (nat->nat_me != NULL) {
2284 		*nat->nat_me = NULL;
2285 		nat->nat_me = NULL;
2286 		nat->nat_ref--;
2287 		ASSERT(nat->nat_ref >= 0);
2288 	}
2289 
2290 	if (nat->nat_tqe.tqe_ifq != NULL) {
2291 		/*
2292 		 * No call to ipf_freetimeoutqueue() is made here, they are
2293 		 * garbage collected in ipf_nat_expire().
2294 		 */
2295 		(void) ipf_deletequeueentry(&nat->nat_tqe);
2296 	}
2297 
2298 	if (nat->nat_sync) {
2299 		ipf_sync_del_nat(softc->ipf_sync_soft, nat->nat_sync);
2300 		nat->nat_sync = NULL;
2301 	}
2302 
2303 	if (logtype == NL_EXPIRE)
2304 		softn->ipf_nat_stats.ns_expire++;
2305 
2306 	MUTEX_ENTER(&nat->nat_lock);
2307 	/*
2308 	 * NL_DESTROY should only be passed in when we've got nat_ref >= 2.
2309 	 * This happens when a nat'd packet is blocked and we want to throw
2310 	 * away the NAT session.
2311 	 */
2312 	if (logtype == NL_DESTROY) {
2313 		if (nat->nat_ref > 2) {
2314 			nat->nat_ref -= 2;
2315 			MUTEX_EXIT(&nat->nat_lock);
2316 			if (removed)
2317 				softn->ipf_nat_stats.ns_orphans++;
2318 			return;
2319 		}
2320 	} else if (nat->nat_ref > 1) {
2321 		nat->nat_ref--;
2322 		MUTEX_EXIT(&nat->nat_lock);
2323 		if (madeorphan == 1)
2324 			softn->ipf_nat_stats.ns_orphans++;
2325 		return;
2326 	}
2327 	ASSERT(nat->nat_ref >= 0);
2328 	MUTEX_EXIT(&nat->nat_lock);
2329 
2330 	nat->nat_ref = 0;
2331 
2332 	if (madeorphan == 0)
2333 		softn->ipf_nat_stats.ns_orphans--;
2334 
2335 	/*
2336 	 * At this point, nat_ref can be either 0 or -1
2337 	 */
2338 	softn->ipf_nat_stats.ns_proto[nat->nat_pr[0]]--;
2339 
2340 	if (nat->nat_fr != NULL) {
2341 		(void) ipf_derefrule(softc, &nat->nat_fr);
2342 	}
2343 
2344 	if (nat->nat_hm != NULL) {
2345 		ipf_nat_hostmapdel(softc, &nat->nat_hm);
2346 	}
2347 
2348 	/*
2349 	 * If there is an active reference from the nat entry to its parent
2350 	 * rule, decrement the rule's reference count and free it too if no
2351 	 * longer being used.
2352 	 */
2353 	ipn = nat->nat_ptr;
2354 	nat->nat_ptr = NULL;
2355 
2356 	if (ipn != NULL) {
2357 		ipn->in_space++;
2358 		ipf_nat_rule_deref(softc, &ipn);
2359 	}
2360 
2361 	if (nat->nat_aps != NULL) {
2362 		ipf_proxy_free(softc, nat->nat_aps);
2363 		nat->nat_aps = NULL;
2364 	}
2365 
2366 	MUTEX_DESTROY(&nat->nat_lock);
2367 
2368 	softn->ipf_nat_stats.ns_active--;
2369 
2370 	/*
2371 	 * If there's a fragment table entry too for this nat entry, then
2372 	 * dereference that as well.  This is after nat_lock is released
2373 	 * because of Tru64.
2374 	 */
2375 	ipf_frag_natforget(softc, (void *)nat);
2376 
2377 	KFREE(nat);
2378 }
2379 
2380 
2381 /* ------------------------------------------------------------------------ */
2382 /* Function:    ipf_nat_flushtable                                          */
2383 /* Returns:     int - number of NAT rules deleted                           */
2384 /* Parameters:  softc(I) - pointer to soft context main structure           */
2385 /*              softn(I) - pointer to NAT context structure                 */
2386 /* Write Lock:  ipf_nat                                                     */
2387 /*                                                                          */
2388 /* Deletes all currently active NAT sessions.  In deleting each NAT entry a */
2389 /* log record should be emitted in ipf_nat_delete() if NAT logging is       */
2390 /* enabled.                                                                 */
2391 /* ------------------------------------------------------------------------ */
2392 /*
2393  * nat_flushtable - clear the NAT table of all mapping entries.
2394  */
2395 static int
ipf_nat_flushtable(ipf_main_softc_t * softc,ipf_nat_softc_t * softn)2396 ipf_nat_flushtable(ipf_main_softc_t *softc, ipf_nat_softc_t *softn)
2397 {
2398 	nat_t *nat;
2399 	int j = 0;
2400 
2401 	/*
2402 	 * ALL NAT mappings deleted, so lets just make the deletions
2403 	 * quicker.
2404 	 */
2405 	if (softn->ipf_nat_table[0] != NULL)
2406 		bzero((char *)softn->ipf_nat_table[0],
2407 		      sizeof(softn->ipf_nat_table[0]) *
2408 		      softn->ipf_nat_table_sz);
2409 	if (softn->ipf_nat_table[1] != NULL)
2410 		bzero((char *)softn->ipf_nat_table[1],
2411 		      sizeof(softn->ipf_nat_table[1]) *
2412 		      softn->ipf_nat_table_sz);
2413 
2414 	while ((nat = softn->ipf_nat_instances) != NULL) {
2415 		ipf_nat_delete(softc, nat, NL_FLUSH);
2416 		j++;
2417 	}
2418 
2419 	return (j);
2420 }
2421 
2422 
2423 /* ------------------------------------------------------------------------ */
2424 /* Function:    ipf_nat_clearlist                                           */
2425 /* Returns:     int - number of NAT/RDR rules deleted                       */
2426 /* Parameters:  softc(I) - pointer to soft context main structure           */
2427 /*              softn(I) - pointer to NAT context structure                 */
2428 /*                                                                          */
2429 /* Delete all rules in the current list of rules.  There is nothing elegant */
2430 /* about this cleanup: simply free all entries on the list of rules and     */
2431 /* clear out the tables used for hashed NAT rule lookups.                   */
2432 /* ------------------------------------------------------------------------ */
2433 static int
ipf_nat_clearlist(ipf_main_softc_t * softc,ipf_nat_softc_t * softn)2434 ipf_nat_clearlist(ipf_main_softc_t *softc, ipf_nat_softc_t *softn)
2435 {
2436 	ipnat_t *n;
2437 	int i = 0;
2438 
2439 	if (softn->ipf_nat_map_rules != NULL) {
2440 		bzero((char *)softn->ipf_nat_map_rules,
2441 		      sizeof(*softn->ipf_nat_map_rules) *
2442 		      softn->ipf_nat_maprules_sz);
2443 	}
2444 	if (softn->ipf_nat_rdr_rules != NULL) {
2445 		bzero((char *)softn->ipf_nat_rdr_rules,
2446 		      sizeof(*softn->ipf_nat_rdr_rules) *
2447 		      softn->ipf_nat_rdrrules_sz);
2448 	}
2449 
2450 	while ((n = softn->ipf_nat_list) != NULL) {
2451 		ipf_nat_delrule(softc, softn, n, 0);
2452 		i++;
2453 	}
2454 #if SOLARIS && !defined(INSTANCES)
2455 	pfil_delayed_copy = 1;
2456 #endif
2457 	return (i);
2458 }
2459 
2460 
2461 /* ------------------------------------------------------------------------ */
2462 /* Function:    ipf_nat_delrule                                             */
2463 /* Returns:     Nil                                                         */
2464 /* Parameters:  softc(I) - pointer to soft context main structure           */
2465 /*              softn(I) - pointer to NAT context structure                 */
2466 /*              np(I)    - pointer to NAT rule to delete                    */
2467 /*              purge(I) - 1 == allow purge, 0 == prevent purge             */
2468 /* Locks:       WRITE(ipf_nat)                                              */
2469 /*                                                                          */
2470 /* Preventing "purge" from occuring is allowed because when all of the NAT  */
2471 /* rules are being removed, allowing the "purge" to walk through the list   */
2472 /* of NAT sessions, possibly multiple times, would be a large performance   */
2473 /* hit, on the order of O(N^2).                                             */
2474 /* ------------------------------------------------------------------------ */
2475 static void
ipf_nat_delrule(ipf_main_softc_t * softc,ipf_nat_softc_t * softn,ipnat_t * np,int purge)2476 ipf_nat_delrule(ipf_main_softc_t *softc, ipf_nat_softc_t *softn, ipnat_t *np,
2477 	int purge)
2478 {
2479 
2480 	if (np->in_pnext != NULL) {
2481 		*np->in_pnext = np->in_next;
2482 		if (np->in_next != NULL)
2483 			np->in_next->in_pnext = np->in_pnext;
2484 		if (softn->ipf_nat_list_tail == &np->in_next)
2485 			softn->ipf_nat_list_tail = np->in_pnext;
2486 	}
2487 
2488 	if ((purge == 1) && ((np->in_flags & IPN_PURGE) != 0)) {
2489 		nat_t *next;
2490 		nat_t *nat;
2491 
2492 		for (next = softn->ipf_nat_instances; (nat = next) != NULL;) {
2493 			next = nat->nat_next;
2494 			if (nat->nat_ptr == np)
2495 				ipf_nat_delete(softc, nat, NL_PURGE);
2496 		}
2497 	}
2498 
2499 	if ((np->in_flags & IPN_DELETE) == 0) {
2500 		if (np->in_redir & NAT_REDIRECT) {
2501 			switch (np->in_v[0])
2502 			{
2503 			case 4 :
2504 				ipf_nat_delrdr(softn, np);
2505 				break;
2506 #ifdef USE_INET6
2507 			case 6 :
2508 				ipf_nat6_delrdr(softn, np);
2509 				break;
2510 #endif
2511 			}
2512 		}
2513 		if (np->in_redir & (NAT_MAPBLK|NAT_MAP)) {
2514 			switch (np->in_v[0])
2515 			{
2516 			case 4 :
2517 				ipf_nat_delmap(softn, np);
2518 				break;
2519 #ifdef USE_INET6
2520 			case 6 :
2521 				ipf_nat6_delmap(softn, np);
2522 				break;
2523 #endif
2524 			}
2525 		}
2526 	}
2527 
2528 	np->in_flags |= IPN_DELETE;
2529 	ipf_nat_rule_deref(softc, &np);
2530 }
2531 
2532 
2533 /* ------------------------------------------------------------------------ */
2534 /* Function:    ipf_nat_newmap                                              */
2535 /* Returns:     int - -1 == error, 0 == success                             */
2536 /* Parameters:  fin(I) - pointer to packet information                      */
2537 /*              nat(I) - pointer to NAT entry                               */
2538 /*              ni(I)  - pointer to structure with misc. information needed */
2539 /*                       to create new NAT entry.                           */
2540 /*                                                                          */
2541 /* Given an empty NAT structure, populate it with new information about a   */
2542 /* new NAT session, as defined by the matching NAT rule.                    */
2543 /* ni.nai_ip is passed in uninitialised and must be set, in host byte order,*/
2544 /* to the new IP address for the translation.                               */
2545 /* ------------------------------------------------------------------------ */
2546 static int
ipf_nat_newmap(fr_info_t * fin,nat_t * nat,natinfo_t * ni)2547 ipf_nat_newmap(fr_info_t *fin, nat_t *nat, natinfo_t *ni)
2548 {
2549 	ipf_main_softc_t *softc = fin->fin_main_soft;
2550 	ipf_nat_softc_t *softn = softc->ipf_nat_soft;
2551 	u_short st_port, dport, sport, port, sp, dp;
2552 	struct in_addr in, inb;
2553 	hostmap_t *hm;
2554 	u_32_t flags;
2555 	u_32_t st_ip;
2556 	ipnat_t *np;
2557 	nat_t *natl;
2558 	int l;
2559 
2560 	/*
2561 	 * If it's an outbound packet which doesn't match any existing
2562 	 * record, then create a new port
2563 	 */
2564 	l = 0;
2565 	hm = NULL;
2566 	np = ni->nai_np;
2567 	st_ip = np->in_snip;
2568 	st_port = np->in_spnext;
2569 	flags = nat->nat_flags;
2570 
2571 	if (flags & IPN_ICMPQUERY) {
2572 		sport = fin->fin_data[1];
2573 		dport = 0;
2574 	} else {
2575 		sport = htons(fin->fin_data[0]);
2576 		dport = htons(fin->fin_data[1]);
2577 	}
2578 
2579 	/*
2580 	 * Do a loop until we either run out of entries to try or we find
2581 	 * a NAT mapping that isn't currently being used.  This is done
2582 	 * because the change to the source is not (usually) being fixed.
2583 	 */
2584 	do {
2585 		port = 0;
2586 		in.s_addr = htonl(np->in_snip);
2587 		if (l == 0) {
2588 			/*
2589 			 * Check to see if there is an existing NAT
2590 			 * setup for this IP address pair.
2591 			 */
2592 			hm = ipf_nat_hostmap(softn, np, fin->fin_src,
2593 					     fin->fin_dst, in, 0);
2594 			if (hm != NULL)
2595 				in.s_addr = hm->hm_nsrcip.s_addr;
2596 		} else if ((l == 1) && (hm != NULL)) {
2597 			ipf_nat_hostmapdel(softc, &hm);
2598 		}
2599 		in.s_addr = ntohl(in.s_addr);
2600 
2601 		nat->nat_hm = hm;
2602 
2603 		if ((np->in_nsrcmsk == 0xffffffff) && (np->in_spnext == 0)) {
2604 			if (l > 0) {
2605 				NBUMPSIDEX(1, ns_exhausted, ns_exhausted_1);
2606 				DT4(ns_exhausted_1, fr_info_t *, fin, nat_t *, nat, natinfo_t *, ni, ipnat_t *, np);
2607 				return (-1);
2608 			}
2609 		}
2610 
2611 		if (np->in_redir == NAT_BIMAP &&
2612 		    np->in_osrcmsk == np->in_nsrcmsk) {
2613 			/*
2614 			 * map the address block in a 1:1 fashion
2615 			 */
2616 			in.s_addr = np->in_nsrcaddr;
2617 			in.s_addr |= fin->fin_saddr & ~np->in_osrcmsk;
2618 			in.s_addr = ntohl(in.s_addr);
2619 
2620 		} else if (np->in_redir & NAT_MAPBLK) {
2621 			if ((l >= np->in_ppip) || ((l > 0) &&
2622 			     !(flags & IPN_TCPUDP))) {
2623 				NBUMPSIDEX(1, ns_exhausted, ns_exhausted_2);
2624 				DT4(ns_exhausted_2, fr_info_t *, fin, nat_t *, nat, natinfo_t *, ni, ipnat_t *, np);
2625 				return (-1);
2626 			}
2627 			/*
2628 			 * map-block - Calculate destination address.
2629 			 */
2630 			in.s_addr = ntohl(fin->fin_saddr);
2631 			in.s_addr &= ntohl(~np->in_osrcmsk);
2632 			inb.s_addr = in.s_addr;
2633 			in.s_addr /= np->in_ippip;
2634 			in.s_addr &= ntohl(~np->in_nsrcmsk);
2635 			in.s_addr += ntohl(np->in_nsrcaddr);
2636 			/*
2637 			 * Calculate destination port.
2638 			 */
2639 			if ((flags & IPN_TCPUDP) &&
2640 			    (np->in_ppip != 0)) {
2641 				port = ntohs(sport) + l;
2642 				port %= np->in_ppip;
2643 				port += np->in_ppip *
2644 					(inb.s_addr % np->in_ippip);
2645 				port += MAPBLK_MINPORT;
2646 				port = htons(port);
2647 			}
2648 
2649 		} else if ((np->in_nsrcaddr == 0) &&
2650 			   (np->in_nsrcmsk == 0xffffffff)) {
2651 			i6addr_t in6;
2652 
2653 			/*
2654 			 * 0/32 - use the interface's IP address.
2655 			 */
2656 			if ((l > 0) ||
2657 			    ipf_ifpaddr(softc, 4, FRI_NORMAL, fin->fin_ifp,
2658 				       &in6, NULL) == -1) {
2659 				NBUMPSIDEX(1, ns_new_ifpaddr, ns_new_ifpaddr_1);
2660 				DT4(ns_new_ifpaddr_1, fr_info_t *, fin, nat_t *, nat, natinfo_t *, ni, ipnat_t *, np);
2661 				return (-1);
2662 			}
2663 			in.s_addr = ntohl(in6.in4.s_addr);
2664 
2665 		} else if ((np->in_nsrcaddr == 0) && (np->in_nsrcmsk == 0)) {
2666 			/*
2667 			 * 0/0 - use the original source address/port.
2668 			 */
2669 			if (l > 0) {
2670 				NBUMPSIDEX(1, ns_exhausted, ns_exhausted_3);
2671 				DT4(ns_exhausted_3, fr_info_t *, fin, nat_t *, nat, natinfo_t *, ni, ipnat_t *, np);
2672 				return (-1);
2673 			}
2674 			in.s_addr = ntohl(fin->fin_saddr);
2675 
2676 		} else if ((np->in_nsrcmsk != 0xffffffff) &&
2677 			   (np->in_spnext == 0) && ((l > 0) || (hm == NULL)))
2678 			np->in_snip++;
2679 
2680 		natl = NULL;
2681 
2682 		if ((flags & IPN_TCPUDP) &&
2683 		    ((np->in_redir & NAT_MAPBLK) == 0) &&
2684 		    (np->in_flags & IPN_AUTOPORTMAP)) {
2685 			/*
2686 			 * "ports auto" (without map-block)
2687 			 */
2688 			if ((l > 0) && (l % np->in_ppip == 0)) {
2689 				if ((l > np->in_ppip) &&
2690 				    np->in_nsrcmsk != 0xffffffff)
2691 					np->in_snip++;
2692 			}
2693 			if (np->in_ppip != 0) {
2694 				port = ntohs(sport);
2695 				port += (l % np->in_ppip);
2696 				port %= np->in_ppip;
2697 				port += np->in_ppip *
2698 					(ntohl(fin->fin_saddr) %
2699 					 np->in_ippip);
2700 				port += MAPBLK_MINPORT;
2701 				port = htons(port);
2702 			}
2703 
2704 		} else if (((np->in_redir & NAT_MAPBLK) == 0) &&
2705 			   (flags & IPN_TCPUDPICMP) && (np->in_spnext != 0)) {
2706 			/*
2707 			 * Standard port translation.  Select next port.
2708 			 */
2709 			if (np->in_flags & IPN_SEQUENTIAL) {
2710 				port = np->in_spnext;
2711 			} else {
2712 				port = ipf_random() % (np->in_spmax -
2713 						       np->in_spmin + 1);
2714 				port += np->in_spmin;
2715 			}
2716 			port = htons(port);
2717 			np->in_spnext++;
2718 
2719 			if (np->in_spnext > np->in_spmax) {
2720 				np->in_spnext = np->in_spmin;
2721 				if (np->in_nsrcmsk != 0xffffffff)
2722 					np->in_snip++;
2723 			}
2724 		}
2725 
2726 		if (np->in_flags & IPN_SIPRANGE) {
2727 			if (np->in_snip > ntohl(np->in_nsrcmsk))
2728 				np->in_snip = ntohl(np->in_nsrcaddr);
2729 		} else {
2730 			if ((np->in_nsrcmsk != 0xffffffff) &&
2731 			    ((np->in_snip + 1) & ntohl(np->in_nsrcmsk)) >
2732 			    ntohl(np->in_nsrcaddr))
2733 				np->in_snip = ntohl(np->in_nsrcaddr) + 1;
2734 		}
2735 
2736 		if ((port == 0) && (flags & (IPN_TCPUDPICMP|IPN_ICMPQUERY)))
2737 			port = sport;
2738 
2739 		/*
2740 		 * Here we do a lookup of the connection as seen from
2741 		 * the outside.  If an IP# pair already exists, try
2742 		 * again.  So if you have A->B becomes C->B, you can
2743 		 * also have D->E become C->E but not D->B causing
2744 		 * another C->B.  Also take protocol and ports into
2745 		 * account when determining whether a pre-existing
2746 		 * NAT setup will cause an external conflict where
2747 		 * this is appropriate.
2748 		 */
2749 		inb.s_addr = htonl(in.s_addr);
2750 		sp = fin->fin_data[0];
2751 		dp = fin->fin_data[1];
2752 		fin->fin_data[0] = fin->fin_data[1];
2753 		fin->fin_data[1] = ntohs(port);
2754 		natl = ipf_nat_inlookup(fin, flags & ~(SI_WILDP|NAT_SEARCH),
2755 					(u_int)fin->fin_p, fin->fin_dst, inb);
2756 		fin->fin_data[0] = sp;
2757 		fin->fin_data[1] = dp;
2758 
2759 		/*
2760 		 * Has the search wrapped around and come back to the
2761 		 * start ?
2762 		 */
2763 		if ((natl != NULL) &&
2764 		    (np->in_spnext != 0) && (st_port == np->in_spnext) &&
2765 		    (np->in_snip != 0) && (st_ip == np->in_snip)) {
2766 			NBUMPSIDED(1, ns_wrap);
2767 			DT4(ns_wrap, fr_info_t *, fin, nat_t *, nat, natinfo_t *, ni, ipnat_t *, np);
2768 			return (-1);
2769 		}
2770 		l++;
2771 	} while (natl != NULL);
2772 
2773 	/* Setup the NAT table */
2774 	nat->nat_osrcip = fin->fin_src;
2775 	nat->nat_nsrcaddr = htonl(in.s_addr);
2776 	nat->nat_odstip = fin->fin_dst;
2777 	nat->nat_ndstip = fin->fin_dst;
2778 	if (nat->nat_hm == NULL)
2779 		nat->nat_hm = ipf_nat_hostmap(softn, np, fin->fin_src,
2780 					      fin->fin_dst, nat->nat_nsrcip,
2781 					      0);
2782 
2783 	if (flags & IPN_TCPUDP) {
2784 		nat->nat_osport = sport;
2785 		nat->nat_nsport = port;	/* sport */
2786 		nat->nat_odport = dport;
2787 		nat->nat_ndport = dport;
2788 		((tcphdr_t *)fin->fin_dp)->th_sport = port;
2789 	} else if (flags & IPN_ICMPQUERY) {
2790 		nat->nat_oicmpid = fin->fin_data[1];
2791 		((icmphdr_t *)fin->fin_dp)->icmp_id = port;
2792 		nat->nat_nicmpid = port;
2793 	}
2794 	return (0);
2795 }
2796 
2797 
2798 /* ------------------------------------------------------------------------ */
2799 /* Function:    ipf_nat_newrdr                                              */
2800 /* Returns:     int - -1 == error, 0 == success (no move), 1 == success and */
2801 /*                    allow rule to be moved if IPN_ROUNDR is set.          */
2802 /* Parameters:  fin(I) - pointer to packet information                      */
2803 /*              nat(I) - pointer to NAT entry                               */
2804 /*              ni(I)  - pointer to structure with misc. information needed */
2805 /*                       to create new NAT entry.                           */
2806 /*                                                                          */
2807 /* ni.nai_ip is passed in uninitialised and must be set, in host byte order,*/
2808 /* to the new IP address for the translation.                               */
2809 /* ------------------------------------------------------------------------ */
2810 static int
ipf_nat_newrdr(fr_info_t * fin,nat_t * nat,natinfo_t * ni)2811 ipf_nat_newrdr(fr_info_t *fin, nat_t *nat, natinfo_t *ni)
2812 {
2813 	ipf_main_softc_t *softc = fin->fin_main_soft;
2814 	ipf_nat_softc_t *softn = softc->ipf_nat_soft;
2815 	u_short nport, dport, sport;
2816 	struct in_addr in, inb;
2817 	u_short sp, dp;
2818 	hostmap_t *hm;
2819 	u_32_t flags;
2820 	ipnat_t *np;
2821 	nat_t *natl;
2822 	int move;
2823 
2824 	move = 1;
2825 	hm = NULL;
2826 	in.s_addr = 0;
2827 	np = ni->nai_np;
2828 	flags = nat->nat_flags;
2829 
2830 	if (flags & IPN_ICMPQUERY) {
2831 		dport = fin->fin_data[1];
2832 		sport = 0;
2833 	} else {
2834 		sport = htons(fin->fin_data[0]);
2835 		dport = htons(fin->fin_data[1]);
2836 	}
2837 
2838 	/* TRACE sport, dport */
2839 
2840 
2841 	/*
2842 	 * If the matching rule has IPN_STICKY set, then we want to have the
2843 	 * same rule kick in as before.  Why would this happen?  If you have
2844 	 * a collection of rdr rules with "round-robin sticky", the current
2845 	 * packet might match a different one to the previous connection but
2846 	 * we want the same destination to be used.
2847 	 */
2848 	if (((np->in_flags & (IPN_ROUNDR|IPN_SPLIT)) != 0) &&
2849 	    ((np->in_flags & IPN_STICKY) != 0)) {
2850 		hm = ipf_nat_hostmap(softn, NULL, fin->fin_src, fin->fin_dst,
2851 				     in, (u_32_t)dport);
2852 		if (hm != NULL) {
2853 			in.s_addr = ntohl(hm->hm_ndstip.s_addr);
2854 			np = hm->hm_ipnat;
2855 			ni->nai_np = np;
2856 			move = 0;
2857 			ipf_nat_hostmapdel(softc, &hm);
2858 		}
2859 	}
2860 
2861 	/*
2862 	 * Otherwise, it's an inbound packet. Most likely, we don't
2863 	 * want to rewrite source ports and source addresses. Instead,
2864 	 * we want to rewrite to a fixed internal address and fixed
2865 	 * internal port.
2866 	 */
2867 	if (np->in_flags & IPN_SPLIT) {
2868 		in.s_addr = np->in_dnip;
2869 		inb.s_addr = htonl(in.s_addr);
2870 
2871 		if ((np->in_flags & (IPN_ROUNDR|IPN_STICKY)) == IPN_STICKY) {
2872 			hm = ipf_nat_hostmap(softn, NULL, fin->fin_src,
2873 					     fin->fin_dst, inb, (u_32_t)dport);
2874 			if (hm != NULL) {
2875 				in.s_addr = hm->hm_ndstip.s_addr;
2876 				move = 0;
2877 			}
2878 		}
2879 
2880 		if (hm == NULL || hm->hm_ref == 1) {
2881 			if (np->in_ndstaddr == htonl(in.s_addr)) {
2882 				np->in_dnip = ntohl(np->in_ndstmsk);
2883 				move = 0;
2884 			} else {
2885 				np->in_dnip = ntohl(np->in_ndstaddr);
2886 			}
2887 		}
2888 		if (hm != NULL)
2889 			ipf_nat_hostmapdel(softc, &hm);
2890 
2891 	} else if ((np->in_ndstaddr == 0) && (np->in_ndstmsk == 0xffffffff)) {
2892 		i6addr_t in6;
2893 
2894 		/*
2895 		 * 0/32 - use the interface's IP address.
2896 		 */
2897 		if (ipf_ifpaddr(softc, 4, FRI_NORMAL, fin->fin_ifp,
2898 			       &in6, NULL) == -1) {
2899 			NBUMPSIDEX(0, ns_new_ifpaddr, ns_new_ifpaddr_2);
2900 			DT3(ns_new_ifpaddr_2, fr_info_t *, fin, nat_t *, nat, natinfo_t, ni);
2901 			return (-1);
2902 		}
2903 		in.s_addr = ntohl(in6.in4.s_addr);
2904 
2905 	} else if ((np->in_ndstaddr == 0) && (np->in_ndstmsk== 0)) {
2906 		/*
2907 		 * 0/0 - use the original destination address/port.
2908 		 */
2909 		in.s_addr = ntohl(fin->fin_daddr);
2910 
2911 	} else if (np->in_redir == NAT_BIMAP &&
2912 		   np->in_ndstmsk == np->in_odstmsk) {
2913 		/*
2914 		 * map the address block in a 1:1 fashion
2915 		 */
2916 		in.s_addr = np->in_ndstaddr;
2917 		in.s_addr |= fin->fin_daddr & ~np->in_ndstmsk;
2918 		in.s_addr = ntohl(in.s_addr);
2919 	} else {
2920 		in.s_addr = ntohl(np->in_ndstaddr);
2921 	}
2922 
2923 	if ((np->in_dpnext == 0) || ((flags & NAT_NOTRULEPORT) != 0))
2924 		nport = dport;
2925 	else {
2926 		/*
2927 		 * Whilst not optimized for the case where
2928 		 * pmin == pmax, the gain is not significant.
2929 		 */
2930 		if (((np->in_flags & IPN_FIXEDDPORT) == 0) &&
2931 		    (np->in_odport != np->in_dtop)) {
2932 			nport = ntohs(dport) - np->in_odport + np->in_dpmax;
2933 			nport = htons(nport);
2934 		} else {
2935 			nport = htons(np->in_dpnext);
2936 			np->in_dpnext++;
2937 			if (np->in_dpnext > np->in_dpmax)
2938 				np->in_dpnext = np->in_dpmin;
2939 		}
2940 	}
2941 
2942 	/*
2943 	 * When the redirect-to address is set to 0.0.0.0, just
2944 	 * assume a blank `forwarding' of the packet.  We don't
2945 	 * setup any translation for this either.
2946 	 */
2947 	if (in.s_addr == 0) {
2948 		if (nport == dport) {
2949 			NBUMPSIDED(0, ns_xlate_null);
2950 			return (-1);
2951 		}
2952 		in.s_addr = ntohl(fin->fin_daddr);
2953 	}
2954 
2955 	/*
2956 	 * Check to see if this redirect mapping already exists and if
2957 	* it does, return "failure" (allowing it to be created will just
2958 	 * cause one or both of these "connections" to stop working.)
2959 	 */
2960 	inb.s_addr = htonl(in.s_addr);
2961 	sp = fin->fin_data[0];
2962 	dp = fin->fin_data[1];
2963 	fin->fin_data[1] = fin->fin_data[0];
2964 	fin->fin_data[0] = ntohs(nport);
2965 	natl = ipf_nat_outlookup(fin, flags & ~(SI_WILDP|NAT_SEARCH),
2966 			     (u_int)fin->fin_p, inb, fin->fin_src);
2967 	fin->fin_data[0] = sp;
2968 	fin->fin_data[1] = dp;
2969 	if (natl != NULL) {
2970 		DT2(ns_new_xlate_exists, fr_info_t *, fin, nat_t *, natl);
2971 		NBUMPSIDE(0, ns_xlate_exists);
2972 		return (-1);
2973 	}
2974 
2975 	inb.s_addr = htonl(in.s_addr);
2976 	nat->nat_ndstaddr = htonl(in.s_addr);
2977 	nat->nat_odstip = fin->fin_dst;
2978 	nat->nat_nsrcip = fin->fin_src;
2979 	nat->nat_osrcip = fin->fin_src;
2980 	if ((nat->nat_hm == NULL) && ((np->in_flags & IPN_STICKY) != 0))
2981 		nat->nat_hm = ipf_nat_hostmap(softn, np, fin->fin_src,
2982 					      fin->fin_dst, inb, (u_32_t)dport);
2983 
2984 	if (flags & IPN_TCPUDP) {
2985 		nat->nat_odport = dport;
2986 		nat->nat_ndport = nport;
2987 		nat->nat_osport = sport;
2988 		nat->nat_nsport = sport;
2989 		((tcphdr_t *)fin->fin_dp)->th_dport = nport;
2990 	} else if (flags & IPN_ICMPQUERY) {
2991 		nat->nat_oicmpid = fin->fin_data[1];
2992 		((icmphdr_t *)fin->fin_dp)->icmp_id = nport;
2993 		nat->nat_nicmpid = nport;
2994 	}
2995 
2996 	return (move);
2997 }
2998 
2999 /* ------------------------------------------------------------------------ */
3000 /* Function:    ipf_nat_add                                                 */
3001 /* Returns:     nat_t* - NULL == failure to create new NAT structure,       */
3002 /*                       else pointer to new NAT structure                  */
3003 /* Parameters:  fin(I)       - pointer to packet information                */
3004 /*              np(I)        - pointer to NAT rule                          */
3005 /*              natsave(I)   - pointer to where to store NAT struct pointer */
3006 /*              flags(I)     - flags describing the current packet          */
3007 /*              direction(I) - direction of packet (in/out)                 */
3008 /* Write Lock:  ipf_nat                                                     */
3009 /*                                                                          */
3010 /* Attempts to create a new NAT entry.  Does not actually change the packet */
3011 /* in any way.                                                              */
3012 /*                                                                          */
3013 /* This function is in three main parts: (1) deal with creating a new NAT   */
3014 /* structure for a "MAP" rule (outgoing NAT translation); (2) deal with     */
3015 /* creating a new NAT structure for a "RDR" rule (incoming NAT translation) */
3016 /* and (3) building that structure and putting it into the NAT table(s).    */
3017 /*                                                                          */
3018 /* NOTE: natsave should NOT be used to point back to an ipstate_t struct    */
3019 /*       as it can result in memory being corrupted.                        */
3020 /* ------------------------------------------------------------------------ */
3021 nat_t *
ipf_nat_add(fr_info_t * fin,ipnat_t * np,nat_t ** natsave,u_int flags,int direction)3022 ipf_nat_add(fr_info_t *fin, ipnat_t *np, nat_t **natsave, u_int flags,
3023 	int direction)
3024 {
3025 	ipf_main_softc_t *softc = fin->fin_main_soft;
3026 	ipf_nat_softc_t *softn = softc->ipf_nat_soft;
3027 	hostmap_t *hm = NULL;
3028 	nat_t *nat, *natl;
3029 	natstat_t *nsp;
3030 	u_int nflags;
3031 	natinfo_t ni;
3032 	int move;
3033 
3034 	nsp = &softn->ipf_nat_stats;
3035 
3036 	if ((nsp->ns_active * 100 / softn->ipf_nat_table_max) >
3037 	    softn->ipf_nat_table_wm_high) {
3038 		softn->ipf_nat_doflush = 1;
3039 	}
3040 
3041 	if (nsp->ns_active >= softn->ipf_nat_table_max) {
3042 		NBUMPSIDED(fin->fin_out, ns_table_max);
3043 		DT2(ns_table_max, nat_stat_t *, nsp, ipf_nat_softc_t *, softn);
3044 		return (NULL);
3045 	}
3046 
3047 	move = 1;
3048 	nflags = np->in_flags & flags;
3049 	nflags &= NAT_FROMRULE;
3050 
3051 	ni.nai_np = np;
3052 	ni.nai_dport = 0;
3053 	ni.nai_sport = 0;
3054 
3055 	/* Give me a new nat */
3056 	KMALLOC(nat, nat_t *);
3057 	if (nat == NULL) {
3058 		DT(ns_memfail);
3059 		NBUMPSIDED(fin->fin_out, ns_memfail);
3060 		/*
3061 		 * Try to automatically tune the max # of entries in the
3062 		 * table allowed to be less than what will cause kmem_alloc()
3063 		 * to fail and try to eliminate panics due to out of memory
3064 		 * conditions arising.
3065 		 */
3066 		if ((softn->ipf_nat_table_max > softn->ipf_nat_table_sz) &&
3067 		    (nsp->ns_active > 100)) {
3068 			softn->ipf_nat_table_max = nsp->ns_active - 100;
3069 			printf("table_max reduced to %d\n",
3070 				softn->ipf_nat_table_max);
3071 		}
3072 		return (NULL);
3073 	}
3074 
3075 	if (flags & IPN_ICMPQUERY) {
3076 		/*
3077 		 * In the ICMP query NAT code, we translate the ICMP id fields
3078 		 * to make them unique. This is indepedent of the ICMP type
3079 		 * (e.g. in the unlikely event that a host sends an echo and
3080 		 * an tstamp request with the same id, both packets will have
3081 		 * their ip address/id field changed in the same way).
3082 		 */
3083 		/* The icmp_id field is used by the sender to identify the
3084 		 * process making the icmp request. (the receiver justs
3085 		 * copies it back in its response). So, it closely matches
3086 		 * the concept of source port. We overlay sport, so we can
3087 		 * maximally reuse the existing code.
3088 		 */
3089 		ni.nai_sport = fin->fin_data[1];
3090 		ni.nai_dport = 0;
3091 	}
3092 
3093 	bzero((char *)nat, sizeof(*nat));
3094 	nat->nat_flags = flags;
3095 	nat->nat_redir = np->in_redir;
3096 	nat->nat_dir = direction;
3097 	nat->nat_pr[0] = fin->fin_p;
3098 	nat->nat_pr[1] = fin->fin_p;
3099 
3100 	/*
3101 	 * Search the current table for a match and create a new mapping
3102 	 * if there is none found.
3103 	 */
3104 	if (np->in_redir & NAT_DIVERTUDP) {
3105 		move = ipf_nat_newdivert(fin, nat, &ni);
3106 
3107 	} else if (np->in_redir & NAT_REWRITE) {
3108 		move = ipf_nat_newrewrite(fin, nat, &ni);
3109 
3110 	} else if (direction == NAT_OUTBOUND) {
3111 		/*
3112 		 * We can now arrange to call this for the same connection
3113 		 * because ipf_nat_new doesn't protect the code path into
3114 		 * this function.
3115 		 */
3116 		natl = ipf_nat_outlookup(fin, nflags, (u_int)fin->fin_p,
3117 				     fin->fin_src, fin->fin_dst);
3118 		if (natl != NULL) {
3119 			KFREE(nat);
3120 			nat = natl;
3121 			goto done;
3122 		}
3123 
3124 		move = ipf_nat_newmap(fin, nat, &ni);
3125 	} else {
3126 		/*
3127 		 * NAT_INBOUND is used for redirects rules
3128 		 */
3129 		natl = ipf_nat_inlookup(fin, nflags, (u_int)fin->fin_p,
3130 					fin->fin_src, fin->fin_dst);
3131 		if (natl != NULL) {
3132 			KFREE(nat);
3133 			nat = natl;
3134 			goto done;
3135 		}
3136 
3137 		move = ipf_nat_newrdr(fin, nat, &ni);
3138 	}
3139 	if (move == -1)
3140 		goto badnat;
3141 
3142 	np = ni.nai_np;
3143 
3144 	nat->nat_mssclamp = np->in_mssclamp;
3145 	nat->nat_me = natsave;
3146 	nat->nat_fr = fin->fin_fr;
3147 	nat->nat_rev = fin->fin_rev;
3148 	nat->nat_ptr = np;
3149 	nat->nat_dlocal = np->in_dlocal;
3150 
3151 	if ((np->in_apr != NULL) && ((nat->nat_flags & NAT_SLAVE) == 0)) {
3152 		if (ipf_proxy_new(fin, nat) == -1) {
3153 			NBUMPSIDED(fin->fin_out, ns_appr_fail);
3154 			DT3(ns_appr_fail, fr_info_t *, fin, nat_t *, nat, ipnat_t *, np);
3155 			goto badnat;
3156 		}
3157 	}
3158 
3159 	nat->nat_ifps[0] = np->in_ifps[0];
3160 	if (np->in_ifps[0] != NULL) {
3161 		COPYIFNAME(np->in_v[0], np->in_ifps[0], nat->nat_ifnames[0]);
3162 	}
3163 
3164 	nat->nat_ifps[1] = np->in_ifps[1];
3165 	if (np->in_ifps[1] != NULL) {
3166 		COPYIFNAME(np->in_v[1], np->in_ifps[1], nat->nat_ifnames[1]);
3167 	}
3168 
3169 	if (ipf_nat_finalise(fin, nat) == -1) {
3170 		goto badnat;
3171 	}
3172 
3173 	np->in_use++;
3174 
3175 	if ((move == 1) && (np->in_flags & IPN_ROUNDR)) {
3176 		if ((np->in_redir & (NAT_REDIRECT|NAT_MAP)) == NAT_REDIRECT) {
3177 			ipf_nat_delrdr(softn, np);
3178 			ipf_nat_addrdr(softn, np);
3179 		} else if ((np->in_redir & (NAT_REDIRECT|NAT_MAP)) == NAT_MAP) {
3180 			ipf_nat_delmap(softn, np);
3181 			ipf_nat_addmap(softn, np);
3182 		}
3183 	}
3184 
3185 	if (flags & SI_WILDP)
3186 		nsp->ns_wilds++;
3187 	nsp->ns_proto[nat->nat_pr[0]]++;
3188 
3189 	goto done;
3190 badnat:
3191 	DT3(ns_badnatnew, fr_info_t *, fin, nat_t *, nat, ipnat_t *, np);
3192 	NBUMPSIDE(fin->fin_out, ns_badnatnew);
3193 	if ((hm = nat->nat_hm) != NULL)
3194 		ipf_nat_hostmapdel(softc, &hm);
3195 	KFREE(nat);
3196 	nat = NULL;
3197 done:
3198 	if (nat != NULL && np != NULL)
3199 		np->in_hits++;
3200 	if (natsave != NULL)
3201 		*natsave = nat;
3202 	return (nat);
3203 }
3204 
3205 
3206 /* ------------------------------------------------------------------------ */
3207 /* Function:    ipf_nat_finalise                                            */
3208 /* Returns:     int - 0 == sucess, -1 == failure                            */
3209 /* Parameters:  fin(I) - pointer to packet information                      */
3210 /*              nat(I) - pointer to NAT entry                               */
3211 /* Write Lock:  ipf_nat                                                     */
3212 /*                                                                          */
3213 /* This is the tail end of constructing a new NAT entry and is the same     */
3214 /* for both IPv4 and IPv6.                                                  */
3215 /* ------------------------------------------------------------------------ */
3216 /*ARGSUSED*/
3217 static int
ipf_nat_finalise(fr_info_t * fin,nat_t * nat)3218 ipf_nat_finalise(fr_info_t *fin, nat_t *nat)
3219 {
3220 	ipf_main_softc_t *softc = fin->fin_main_soft;
3221 	ipf_nat_softc_t *softn = softc->ipf_nat_soft;
3222 	u_32_t sum1, sum2, sumd;
3223 	frentry_t *fr;
3224 	u_32_t flags;
3225 #if SOLARIS && defined(_KERNEL) && defined(ICK_M_CTL_MAGIC)
3226 	qpktinfo_t *qpi = fin->fin_qpi;
3227 #endif
3228 
3229 	flags = nat->nat_flags;
3230 
3231 	switch (nat->nat_pr[0])
3232 	{
3233 	case IPPROTO_ICMP :
3234 		sum1 = LONG_SUM(ntohs(nat->nat_oicmpid));
3235 		sum2 = LONG_SUM(ntohs(nat->nat_nicmpid));
3236 		CALC_SUMD(sum1, sum2, sumd);
3237 		nat->nat_sumd[0] = (sumd & 0xffff) + (sumd >> 16);
3238 
3239 		break;
3240 
3241 	default :
3242 		sum1 = LONG_SUM(ntohl(nat->nat_osrcaddr) + \
3243 				ntohs(nat->nat_osport));
3244 		sum2 = LONG_SUM(ntohl(nat->nat_nsrcaddr) + \
3245 				ntohs(nat->nat_nsport));
3246 		CALC_SUMD(sum1, sum2, sumd);
3247 		nat->nat_sumd[0] = (sumd & 0xffff) + (sumd >> 16);
3248 
3249 		sum1 = LONG_SUM(ntohl(nat->nat_odstaddr) + \
3250 				ntohs(nat->nat_odport));
3251 		sum2 = LONG_SUM(ntohl(nat->nat_ndstaddr) + \
3252 				ntohs(nat->nat_ndport));
3253 		CALC_SUMD(sum1, sum2, sumd);
3254 		nat->nat_sumd[0] += (sumd & 0xffff) + (sumd >> 16);
3255 		break;
3256 	}
3257 
3258 	/*
3259 	 * Compute the partial checksum, just in case.
3260 	 * This is only ever placed into outbound packets so care needs
3261 	 * to be taken over which pair of addresses are used.
3262 	 */
3263 	if (nat->nat_dir == NAT_OUTBOUND) {
3264 		sum1 = LONG_SUM(ntohl(nat->nat_nsrcaddr));
3265 		sum1 += LONG_SUM(ntohl(nat->nat_ndstaddr));
3266 	} else {
3267 		sum1 = LONG_SUM(ntohl(nat->nat_osrcaddr));
3268 		sum1 += LONG_SUM(ntohl(nat->nat_odstaddr));
3269 	}
3270 	sum1 += nat->nat_pr[1];
3271 	nat->nat_sumd[1] = (sum1 & 0xffff) + (sum1 >> 16);
3272 
3273 	sum1 = LONG_SUM(ntohl(nat->nat_osrcaddr));
3274 	sum2 = LONG_SUM(ntohl(nat->nat_nsrcaddr));
3275 	CALC_SUMD(sum1, sum2, sumd);
3276 	nat->nat_ipsumd = (sumd & 0xffff) + (sumd >> 16);
3277 
3278 	sum1 = LONG_SUM(ntohl(nat->nat_odstaddr));
3279 	sum2 = LONG_SUM(ntohl(nat->nat_ndstaddr));
3280 	CALC_SUMD(sum1, sum2, sumd);
3281 	nat->nat_ipsumd += (sumd & 0xffff) + (sumd >> 16);
3282 
3283 	nat->nat_v[0] = 4;
3284 	nat->nat_v[1] = 4;
3285 
3286 	if ((nat->nat_ifps[0] != NULL) && (nat->nat_ifps[0] != (void *)-1)) {
3287 		nat->nat_mtu[0] = GETIFMTU_4(nat->nat_ifps[0]);
3288 	}
3289 
3290 	if ((nat->nat_ifps[1] != NULL) && (nat->nat_ifps[1] != (void *)-1)) {
3291 		nat->nat_mtu[1] = GETIFMTU_4(nat->nat_ifps[1]);
3292 	}
3293 
3294 	if ((nat->nat_flags & SI_CLONE) == 0)
3295 		nat->nat_sync = ipf_sync_new(softc, SMC_NAT, fin, nat);
3296 
3297 	if (ipf_nat_insert(softc, softn, nat) == 0) {
3298 		if (softn->ipf_nat_logging)
3299 			ipf_nat_log(softc, softn, nat, NL_NEW);
3300 		fr = nat->nat_fr;
3301 		if (fr != NULL) {
3302 			MUTEX_ENTER(&fr->fr_lock);
3303 			fr->fr_ref++;
3304 			MUTEX_EXIT(&fr->fr_lock);
3305 		}
3306 		return (0);
3307 	}
3308 
3309 	NBUMPSIDED(fin->fin_out, ns_unfinalised);
3310 	DT2(ns_unfinalised, fr_info_t *, fin, nat_t *, nat);
3311 	/*
3312 	 * nat_insert failed, so cleanup time...
3313 	 */
3314 	if (nat->nat_sync != NULL)
3315 		ipf_sync_del_nat(softc->ipf_sync_soft, nat->nat_sync);
3316 	return (-1);
3317 }
3318 
3319 
3320 /* ------------------------------------------------------------------------ */
3321 /* Function:    ipf_nat_insert                                              */
3322 /* Returns:     int - 0 == sucess, -1 == failure                            */
3323 /* Parameters:  softc(I) - pointer to soft context main structure           */
3324 /*              softn(I) - pointer to NAT context structure                 */
3325 /*              nat(I) - pointer to NAT structure                           */
3326 /* Write Lock:  ipf_nat                                                     */
3327 /*                                                                          */
3328 /* Insert a NAT entry into the hash tables for searching and add it to the  */
3329 /* list of active NAT entries.  Adjust global counters when complete.       */
3330 /* ------------------------------------------------------------------------ */
3331 int
ipf_nat_insert(ipf_main_softc_t * softc,ipf_nat_softc_t * softn,nat_t * nat)3332 ipf_nat_insert(ipf_main_softc_t *softc, ipf_nat_softc_t *softn, nat_t *nat)
3333 {
3334 	u_int hv0, hv1;
3335 	u_int sp, dp;
3336 	ipnat_t *in;
3337 	int ret;
3338 
3339 	/*
3340 	* Try and return an error as early as possible, so calculate the hash
3341 	 * entry numbers first and then proceed.
3342 	 */
3343 	if ((nat->nat_flags & (SI_W_SPORT|SI_W_DPORT)) == 0) {
3344 		if ((nat->nat_flags & IPN_TCPUDP) != 0) {
3345 			sp = nat->nat_osport;
3346 			dp = nat->nat_odport;
3347 		} else if ((nat->nat_flags & IPN_ICMPQUERY) != 0) {
3348 			sp = 0;
3349 			dp = nat->nat_oicmpid;
3350 		} else {
3351 			sp = 0;
3352 			dp = 0;
3353 		}
3354 		hv0 = NAT_HASH_FN(nat->nat_osrcaddr, sp, 0xffffffff);
3355 		hv0 = NAT_HASH_FN(nat->nat_odstaddr, hv0 + dp, 0xffffffff);
3356 		/*
3357 		 * TRACE nat_osrcaddr, nat_osport, nat_odstaddr,
3358 		 * nat_odport, hv0
3359 		 */
3360 
3361 		if ((nat->nat_flags & IPN_TCPUDP) != 0) {
3362 			sp = nat->nat_nsport;
3363 			dp = nat->nat_ndport;
3364 		} else if ((nat->nat_flags & IPN_ICMPQUERY) != 0) {
3365 			sp = 0;
3366 			dp = nat->nat_nicmpid;
3367 		} else {
3368 			sp = 0;
3369 			dp = 0;
3370 		}
3371 		hv1 = NAT_HASH_FN(nat->nat_nsrcaddr, sp, 0xffffffff);
3372 		hv1 = NAT_HASH_FN(nat->nat_ndstaddr, hv1 + dp, 0xffffffff);
3373 		/*
3374 		 * TRACE nat_nsrcaddr, nat_nsport, nat_ndstaddr,
3375 		 * nat_ndport, hv1
3376 		 */
3377 	} else {
3378 		hv0 = NAT_HASH_FN(nat->nat_osrcaddr, 0, 0xffffffff);
3379 		hv0 = NAT_HASH_FN(nat->nat_odstaddr, hv0, 0xffffffff);
3380 		/* TRACE nat_osrcaddr, nat_odstaddr, hv0 */
3381 
3382 		hv1 = NAT_HASH_FN(nat->nat_nsrcaddr, 0, 0xffffffff);
3383 		hv1 = NAT_HASH_FN(nat->nat_ndstaddr, hv1, 0xffffffff);
3384 		/* TRACE nat_nsrcaddr, nat_ndstaddr, hv1 */
3385 	}
3386 
3387 	nat->nat_hv[0] = hv0;
3388 	nat->nat_hv[1] = hv1;
3389 
3390 	MUTEX_INIT(&nat->nat_lock, "nat entry lock");
3391 
3392 	in = nat->nat_ptr;
3393 	nat->nat_ref = nat->nat_me ? 2 : 1;
3394 
3395 	nat->nat_ifnames[0][LIFNAMSIZ - 1] = '\0';
3396 	nat->nat_ifps[0] = ipf_resolvenic(softc, nat->nat_ifnames[0], 4);
3397 
3398 	if (nat->nat_ifnames[1][0] != '\0') {
3399 		nat->nat_ifnames[1][LIFNAMSIZ - 1] = '\0';
3400 		nat->nat_ifps[1] = ipf_resolvenic(softc,
3401 						  nat->nat_ifnames[1], 4);
3402 	} else if (in->in_ifnames[1] != -1) {
3403 		char *name;
3404 
3405 		name = in->in_names + in->in_ifnames[1];
3406 		if (name[1] != '\0' && name[0] != '-' && name[0] != '*') {
3407 			(void) strncpy(nat->nat_ifnames[1],
3408 				       nat->nat_ifnames[0], LIFNAMSIZ);
3409 			nat->nat_ifnames[1][LIFNAMSIZ - 1] = '\0';
3410 			nat->nat_ifps[1] = nat->nat_ifps[0];
3411 		}
3412 	}
3413 	if ((nat->nat_ifps[0] != NULL) && (nat->nat_ifps[0] != (void *)-1)) {
3414 		nat->nat_mtu[0] = GETIFMTU_4(nat->nat_ifps[0]);
3415 	}
3416 	if ((nat->nat_ifps[1] != NULL) && (nat->nat_ifps[1] != (void *)-1)) {
3417 		nat->nat_mtu[1] = GETIFMTU_4(nat->nat_ifps[1]);
3418 	}
3419 
3420 	ret = ipf_nat_hashtab_add(softc, softn, nat);
3421 	if (ret == -1)
3422 		MUTEX_DESTROY(&nat->nat_lock);
3423 	return (ret);
3424 }
3425 
3426 
3427 /* ------------------------------------------------------------------------ */
3428 /* Function:    ipf_nat_hashtab_add                                         */
3429 /* Returns:     int - 0 == sucess, -1 == failure                            */
3430 /* Parameters:  softc(I) - pointer to soft context main structure           */
3431 /*              softn(I) - pointer to NAT context structure                 */
3432 /*              nat(I) - pointer to NAT structure                           */
3433 /*                                                                          */
3434 /* Handle the insertion of a NAT entry into the table/list.                 */
3435 /* ------------------------------------------------------------------------ */
3436 int
ipf_nat_hashtab_add(ipf_main_softc_t * softc,ipf_nat_softc_t * softn,nat_t * nat)3437 ipf_nat_hashtab_add(ipf_main_softc_t *softc, ipf_nat_softc_t *softn,
3438 	nat_t *nat)
3439 {
3440 	nat_t **natp;
3441 	u_int hv0;
3442 	u_int hv1;
3443 
3444 	hv0 = nat->nat_hv[0] % softn->ipf_nat_table_sz;
3445 	hv1 = nat->nat_hv[1] % softn->ipf_nat_table_sz;
3446 
3447 	if (nat->nat_dir == NAT_INBOUND || nat->nat_dir == NAT_DIVERTIN) {
3448 		u_int swap;
3449 
3450 		swap = hv0;
3451 		hv0 = hv1;
3452 		hv1 = swap;
3453 	}
3454 
3455 	if (softn->ipf_nat_stats.ns_side[0].ns_bucketlen[hv0] >=
3456 	    softn->ipf_nat_maxbucket) {
3457 		DT1(ns_bucket_max_0, int,
3458 		    softn->ipf_nat_stats.ns_side[0].ns_bucketlen[hv0]);
3459 		NBUMPSIDE(0, ns_bucket_max);
3460 		return (-1);
3461 	}
3462 
3463 	if (softn->ipf_nat_stats.ns_side[1].ns_bucketlen[hv1] >=
3464 	    softn->ipf_nat_maxbucket) {
3465 		DT1(ns_bucket_max_1, int,
3466 		    softn->ipf_nat_stats.ns_side[1].ns_bucketlen[hv1]);
3467 		NBUMPSIDE(1, ns_bucket_max);
3468 		return (-1);
3469 	}
3470 
3471 	/*
3472 	 * The ordering of operations in the list and hash table insertion
3473 	 * is very important.  The last operation for each task should be
3474 	 * to update the top of the list, after all the "nexts" have been
3475 	 * done so that walking the list while it is being done does not
3476 	 * find strange pointers.
3477 	 *
3478 	 * Global list of NAT instances
3479 	 */
3480 	nat->nat_next = softn->ipf_nat_instances;
3481 	nat->nat_pnext = &softn->ipf_nat_instances;
3482 	if (softn->ipf_nat_instances)
3483 		softn->ipf_nat_instances->nat_pnext = &nat->nat_next;
3484 	softn->ipf_nat_instances = nat;
3485 
3486 	/*
3487 	 * Inbound hash table.
3488 	 */
3489 	natp = &softn->ipf_nat_table[0][hv0];
3490 	nat->nat_phnext[0] = natp;
3491 	nat->nat_hnext[0] = *natp;
3492 	if (*natp) {
3493 		(*natp)->nat_phnext[0] = &nat->nat_hnext[0];
3494 	} else {
3495 		NBUMPSIDE(0, ns_inuse);
3496 	}
3497 	*natp = nat;
3498 	NBUMPSIDE(0, ns_bucketlen[hv0]);
3499 
3500 	/*
3501 	 * Outbound hash table.
3502 	 */
3503 	natp = &softn->ipf_nat_table[1][hv1];
3504 	nat->nat_phnext[1] = natp;
3505 	nat->nat_hnext[1] = *natp;
3506 	if (*natp)
3507 		(*natp)->nat_phnext[1] = &nat->nat_hnext[1];
3508 	else {
3509 		NBUMPSIDE(1, ns_inuse);
3510 	}
3511 	*natp = nat;
3512 	NBUMPSIDE(1, ns_bucketlen[hv1]);
3513 
3514 	ipf_nat_setqueue(softc, softn, nat);
3515 
3516 	if (nat->nat_dir & NAT_OUTBOUND) {
3517 		NBUMPSIDE(1, ns_added);
3518 	} else {
3519 		NBUMPSIDE(0, ns_added);
3520 	}
3521 	softn->ipf_nat_stats.ns_active++;
3522 	return (0);
3523 }
3524 
3525 
3526 /* ------------------------------------------------------------------------ */
3527 /* Function:    ipf_nat_icmperrorlookup                                     */
3528 /* Returns:     nat_t* - point to matching NAT structure                    */
3529 /* Parameters:  fin(I) - pointer to packet information                      */
3530 /*              dir(I) - direction of packet (in/out)                       */
3531 /*                                                                          */
3532 /* Check if the ICMP error message is related to an existing TCP, UDP or    */
3533 /* ICMP query nat entry.  It is assumed that the packet is already of the   */
3534 /* the required length.                                                     */
3535 /* ------------------------------------------------------------------------ */
3536 nat_t *
ipf_nat_icmperrorlookup(fr_info_t * fin,int dir)3537 ipf_nat_icmperrorlookup(fr_info_t *fin, int dir)
3538 {
3539 	ipf_main_softc_t *softc = fin->fin_main_soft;
3540 	ipf_nat_softc_t *softn = softc->ipf_nat_soft;
3541 	int flags = 0, type, minlen;
3542 	icmphdr_t *icmp, *orgicmp;
3543 	nat_stat_side_t *nside;
3544 	tcphdr_t *tcp = NULL;
3545 	u_short data[2];
3546 	nat_t *nat;
3547 	ip_t *oip;
3548 	u_int p;
3549 
3550 	icmp = fin->fin_dp;
3551 	type = icmp->icmp_type;
3552 	nside = &softn->ipf_nat_stats.ns_side[fin->fin_out];
3553 	/*
3554 	 * Does it at least have the return (basic) IP header ?
3555 	 * Only a basic IP header (no options) should be with an ICMP error
3556 	 * header.  Also, if it's not an error type, then return.
3557 	 */
3558 	if ((fin->fin_hlen != sizeof(ip_t)) || !(fin->fin_flx & FI_ICMPERR)) {
3559 		ATOMIC_INCL(nside->ns_icmp_basic);
3560 		return (NULL);
3561 	}
3562 
3563 	/*
3564 	 * Check packet size
3565 	 */
3566 	oip = (ip_t *)((char *)fin->fin_dp + 8);
3567 	minlen = IP_HL(oip) << 2;
3568 	if ((minlen < sizeof(ip_t)) ||
3569 	    (fin->fin_plen < ICMPERR_IPICMPHLEN + minlen)) {
3570 		ATOMIC_INCL(nside->ns_icmp_size);
3571 		return (NULL);
3572 	}
3573 
3574 	/*
3575 	 * Is the buffer big enough for all of it ?  It's the size of the IP
3576 	 * header claimed in the encapsulated part which is of concern.  It
3577 	 * may be too big to be in this buffer but not so big that it's
3578 	 * outside the ICMP packet, leading to TCP deref's causing problems.
3579 	 * This is possible because we don't know how big oip_hl is when we
3580 	 * do the pullup early in ipf_check() and thus can't gaurantee it is
3581 	 * all here now.
3582 	 */
3583 #ifdef  ipf_nat_KERNEL
3584 	{
3585 	mb_t *m;
3586 
3587 	m = fin->fin_m;
3588 # if SOLARIS
3589 	if ((char *)oip + fin->fin_dlen - ICMPERR_ICMPHLEN >
3590 	    (char *)m->b_wptr) {
3591 		ATOMIC_INCL(nside->ns_icmp_mbuf);
3592 		return (NULL);
3593 	}
3594 # else
3595 	if ((char *)oip + fin->fin_dlen - ICMPERR_ICMPHLEN >
3596 	    (char *)fin->fin_ip + M_LEN(m)) {
3597 		ATOMIC_INCL(nside->ns_icmp_mbuf);
3598 		return (NULL);
3599 	}
3600 # endif
3601 	}
3602 #endif
3603 
3604 	if (fin->fin_daddr != oip->ip_src.s_addr) {
3605 		ATOMIC_INCL(nside->ns_icmp_address);
3606 		return (NULL);
3607 	}
3608 
3609 	p = oip->ip_p;
3610 	if (p == IPPROTO_TCP)
3611 		flags = IPN_TCP;
3612 	else if (p == IPPROTO_UDP)
3613 		flags = IPN_UDP;
3614 	else if (p == IPPROTO_ICMP) {
3615 		orgicmp = (icmphdr_t *)((char *)oip + (IP_HL(oip) << 2));
3616 
3617 		/* see if this is related to an ICMP query */
3618 		if (ipf_nat_icmpquerytype(orgicmp->icmp_type)) {
3619 			data[0] = fin->fin_data[0];
3620 			data[1] = fin->fin_data[1];
3621 			fin->fin_data[0] = 0;
3622 			fin->fin_data[1] = orgicmp->icmp_id;
3623 
3624 			flags = IPN_ICMPERR|IPN_ICMPQUERY;
3625 			/*
3626 			 * NOTE : dir refers to the direction of the original
3627 			 *        ip packet. By definition the icmp error
3628 			 *        message flows in the opposite direction.
3629 			 */
3630 			if (dir == NAT_INBOUND)
3631 				nat = ipf_nat_inlookup(fin, flags, p,
3632 						       oip->ip_dst,
3633 						       oip->ip_src);
3634 			else
3635 				nat = ipf_nat_outlookup(fin, flags, p,
3636 							oip->ip_dst,
3637 							oip->ip_src);
3638 			fin->fin_data[0] = data[0];
3639 			fin->fin_data[1] = data[1];
3640 			return (nat);
3641 		}
3642 	}
3643 
3644 	if (flags & IPN_TCPUDP) {
3645 		minlen += 8;		/* + 64bits of data to get ports */
3646 		/* TRACE (fin,minlen) */
3647 		if (fin->fin_plen < ICMPERR_IPICMPHLEN + minlen) {
3648 			ATOMIC_INCL(nside->ns_icmp_short);
3649 			return (NULL);
3650 		}
3651 
3652 		data[0] = fin->fin_data[0];
3653 		data[1] = fin->fin_data[1];
3654 		tcp = (tcphdr_t *)((char *)oip + (IP_HL(oip) << 2));
3655 		fin->fin_data[0] = ntohs(tcp->th_dport);
3656 		fin->fin_data[1] = ntohs(tcp->th_sport);
3657 
3658 		if (dir == NAT_INBOUND) {
3659 			nat = ipf_nat_inlookup(fin, flags, p, oip->ip_dst,
3660 					       oip->ip_src);
3661 		} else {
3662 			nat = ipf_nat_outlookup(fin, flags, p, oip->ip_dst,
3663 					    oip->ip_src);
3664 		}
3665 		fin->fin_data[0] = data[0];
3666 		fin->fin_data[1] = data[1];
3667 		return (nat);
3668 	}
3669 	if (dir == NAT_INBOUND)
3670 		nat = ipf_nat_inlookup(fin, 0, p, oip->ip_dst, oip->ip_src);
3671 	else
3672 		nat = ipf_nat_outlookup(fin, 0, p, oip->ip_dst, oip->ip_src);
3673 
3674 	return (nat);
3675 }
3676 
3677 
3678 /* ------------------------------------------------------------------------ */
3679 /* Function:    ipf_nat_icmperror                                           */
3680 /* Returns:     nat_t* - point to matching NAT structure                    */
3681 /* Parameters:  fin(I)    - pointer to packet information                   */
3682 /*              nflags(I) - NAT flags for this packet                       */
3683 /*              dir(I)    - direction of packet (in/out)                    */
3684 /*                                                                          */
3685 /* Fix up an ICMP packet which is an error message for an existing NAT      */
3686 /* session.  This will correct both packet header data and checksums.       */
3687 /*                                                                          */
3688 /* This should *ONLY* be used for incoming ICMP error packets to make sure  */
3689 /* a NAT'd ICMP packet gets correctly recognised.                           */
3690 /* ------------------------------------------------------------------------ */
3691 nat_t *
ipf_nat_icmperror(fr_info_t * fin,u_int * nflags,int dir)3692 ipf_nat_icmperror(fr_info_t *fin, u_int *nflags, int dir)
3693 {
3694 	ipf_main_softc_t *softc = fin->fin_main_soft;
3695 	ipf_nat_softc_t *softn = softc->ipf_nat_soft;
3696 	u_32_t sum1, sum2, sumd, sumd2;
3697 	struct in_addr a1, a2, a3, a4;
3698 	int flags, dlen, odst;
3699 	icmphdr_t *icmp;
3700 	u_short *csump;
3701 	tcphdr_t *tcp;
3702 	nat_t *nat;
3703 	ip_t *oip;
3704 	void *dp;
3705 
3706 	if ((fin->fin_flx & (FI_SHORT|FI_FRAGBODY))) {
3707 		NBUMPSIDED(fin->fin_out, ns_icmp_short);
3708 		return (NULL);
3709 	}
3710 
3711 	/*
3712 	* ipf_nat_icmperrorlookup() will return NULL for `defective' packets.
3713 	 */
3714 	if ((fin->fin_v != 4) || !(nat = ipf_nat_icmperrorlookup(fin, dir))) {
3715 		NBUMPSIDED(fin->fin_out, ns_icmp_notfound);
3716 		return (NULL);
3717 	}
3718 
3719 	tcp = NULL;
3720 	csump = NULL;
3721 	flags = 0;
3722 	sumd2 = 0;
3723 	*nflags = IPN_ICMPERR;
3724 	icmp = fin->fin_dp;
3725 	oip = (ip_t *)&icmp->icmp_ip;
3726 	dp = (((char *)oip) + (IP_HL(oip) << 2));
3727 	if (oip->ip_p == IPPROTO_TCP) {
3728 		tcp = (tcphdr_t *)dp;
3729 		csump = (u_short *)&tcp->th_sum;
3730 		flags = IPN_TCP;
3731 	} else if (oip->ip_p == IPPROTO_UDP) {
3732 		udphdr_t *udp;
3733 
3734 		udp = (udphdr_t *)dp;
3735 		tcp = (tcphdr_t *)dp;
3736 		csump = (u_short *)&udp->uh_sum;
3737 		flags = IPN_UDP;
3738 	} else if (oip->ip_p == IPPROTO_ICMP)
3739 		flags = IPN_ICMPQUERY;
3740 	dlen = fin->fin_plen - ((char *)dp - (char *)fin->fin_ip);
3741 
3742 	/*
3743 	 * Need to adjust ICMP header to include the real IP#'s and
3744 	 * port #'s.  Only apply a checksum change relative to the
3745 	 * IP address change as it will be modified again in ipf_nat_checkout
3746 	 * for both address and port.  Two checksum changes are
3747 	 * necessary for the two header address changes.  Be careful
3748 	 * to only modify the checksum once for the port # and twice
3749 	 * for the IP#.
3750 	 */
3751 
3752 	/*
3753 	 * Step 1
3754 	 * Fix the IP addresses in the offending IP packet. You also need
3755 	 * to adjust the IP header checksum of that offending IP packet.
3756 	 *
3757 	 * Normally, you would expect that the ICMP checksum of the
3758 	 * ICMP error message needs to be adjusted as well for the
3759 	 * IP address change in oip.
3760 	 * However, this is a NOP, because the ICMP checksum is
3761 	 * calculated over the complete ICMP packet, which includes the
3762 	 * changed oip IP addresses and oip->ip_sum. However, these
3763 	 * two changes cancel each other out (if the delta for
3764 	 * the IP address is x, then the delta for ip_sum is minus x),
3765 	 * so no change in the icmp_cksum is necessary.
3766 	 *
3767 	 * Inbound ICMP
3768 	 * ------------
3769 	 * MAP rule, SRC=a,DST=b -> SRC=c,DST=b
3770 	 * - response to outgoing packet (a,b)=>(c,b) (OIP_SRC=c,OIP_DST=b)
3771 	 * - OIP_SRC(c)=nat_newsrcip,          OIP_DST(b)=nat_newdstip
3772 	 *=> OIP_SRC(c)=nat_oldsrcip,          OIP_DST(b)=nat_olddstip
3773 	 *
3774 	 * RDR rule, SRC=a,DST=b -> SRC=a,DST=c
3775 	 * - response to outgoing packet (c,a)=>(b,a) (OIP_SRC=b,OIP_DST=a)
3776 	 * - OIP_SRC(b)=nat_olddstip,          OIP_DST(a)=nat_oldsrcip
3777 	 *=> OIP_SRC(b)=nat_newdstip,          OIP_DST(a)=nat_newsrcip
3778 	 *
3779 	 * REWRITE out rule, SRC=a,DST=b -> SRC=c,DST=d
3780 	 * - response to outgoing packet (a,b)=>(c,d) (OIP_SRC=c,OIP_DST=d)
3781 	 * - OIP_SRC(c)=nat_newsrcip,          OIP_DST(d)=nat_newdstip
3782 	 *=> OIP_SRC(c)=nat_oldsrcip,          OIP_DST(d)=nat_olddstip
3783 	 *
3784 	 * REWRITE in rule, SRC=a,DST=b -> SRC=c,DST=d
3785 	 * - response to outgoing packet (d,c)=>(b,a) (OIP_SRC=b,OIP_DST=a)
3786 	 * - OIP_SRC(b)=nat_olddstip,          OIP_DST(a)=nat_oldsrcip
3787 	 *=> OIP_SRC(b)=nat_newdstip,          OIP_DST(a)=nat_newsrcip
3788 	 *
3789 	 * Outbound ICMP
3790 	 * -------------
3791 	 * MAP rule, SRC=a,DST=b -> SRC=c,DST=b
3792 	 * - response to incoming packet (b,c)=>(b,a) (OIP_SRC=b,OIP_DST=a)
3793 	 * - OIP_SRC(b)=nat_olddstip,          OIP_DST(a)=nat_oldsrcip
3794 	 *=> OIP_SRC(b)=nat_newdstip,          OIP_DST(a)=nat_newsrcip
3795 	 *
3796 	 * RDR rule, SRC=a,DST=b -> SRC=a,DST=c
3797 	 * - response to incoming packet (a,b)=>(a,c) (OIP_SRC=a,OIP_DST=c)
3798 	 * - OIP_SRC(a)=nat_newsrcip,          OIP_DST(c)=nat_newdstip
3799 	 *=> OIP_SRC(a)=nat_oldsrcip,          OIP_DST(c)=nat_olddstip
3800 	 *
3801 	 * REWRITE out rule, SRC=a,DST=b -> SRC=c,DST=d
3802 	 * - response to incoming packet (d,c)=>(b,a) (OIP_SRC=c,OIP_DST=d)
3803 	 * - OIP_SRC(c)=nat_olddstip,          OIP_DST(d)=nat_oldsrcip
3804 	 *=> OIP_SRC(b)=nat_newdstip,          OIP_DST(a)=nat_newsrcip
3805 	 *
3806 	 * REWRITE in rule, SRC=a,DST=b -> SRC=c,DST=d
3807 	 * - response to incoming packet (a,b)=>(c,d) (OIP_SRC=b,OIP_DST=a)
3808 	 * - OIP_SRC(b)=nat_newsrcip,          OIP_DST(a)=nat_newdstip
3809 	 *=> OIP_SRC(a)=nat_oldsrcip,          OIP_DST(c)=nat_olddstip
3810 	 */
3811 
3812 	if (((fin->fin_out == 0) && ((nat->nat_redir & NAT_MAP) != 0)) ||
3813 	    ((fin->fin_out == 1) && ((nat->nat_redir & NAT_REDIRECT) != 0))) {
3814 		a1.s_addr = ntohl(nat->nat_osrcaddr);
3815 		a4.s_addr = ntohl(oip->ip_src.s_addr);
3816 		a3.s_addr = ntohl(nat->nat_odstaddr);
3817 		a2.s_addr = ntohl(oip->ip_dst.s_addr);
3818 		oip->ip_src.s_addr = htonl(a1.s_addr);
3819 		oip->ip_dst.s_addr = htonl(a3.s_addr);
3820 		odst = 1;
3821 	} else {
3822 		a1.s_addr = ntohl(nat->nat_ndstaddr);
3823 		a2.s_addr = ntohl(oip->ip_dst.s_addr);
3824 		a3.s_addr = ntohl(nat->nat_nsrcaddr);
3825 		a4.s_addr = ntohl(oip->ip_src.s_addr);
3826 		oip->ip_dst.s_addr = htonl(a3.s_addr);
3827 		oip->ip_src.s_addr = htonl(a1.s_addr);
3828 		odst = 0;
3829 	}
3830 	sum1 = 0;
3831 	sum2 = 0;
3832 	sumd = 0;
3833 	CALC_SUMD(a2.s_addr, a3.s_addr, sum1);
3834 	CALC_SUMD(a4.s_addr, a1.s_addr, sum2);
3835 	sumd = sum2 + sum1;
3836 	if (sumd != 0)
3837 		ipf_fix_datacksum(&oip->ip_sum, sumd);
3838 
3839 	sumd2 = sumd;
3840 	sum1 = 0;
3841 	sum2 = 0;
3842 
3843 	/*
3844 	 * Fix UDP pseudo header checksum to compensate for the
3845 	 * IP address change.
3846 	 */
3847 	if (((flags & IPN_TCPUDP) != 0) && (dlen >= 4)) {
3848 		u_32_t sum3, sum4, sumt;
3849 
3850 		/*
3851 		 * Step 2 :
3852 		 * For offending TCP/UDP IP packets, translate the ports as
3853 		 * well, based on the NAT specification. Of course such
3854 		 * a change may be reflected in the ICMP checksum as well.
3855 		 *
3856 		 * Since the port fields are part of the TCP/UDP checksum
3857 		 * of the offending IP packet, you need to adjust that checksum
3858 		 * as well... except that the change in the port numbers should
3859 		 * be offset by the checksum change.  However, the TCP/UDP
3860 		 * checksum will also need to change if there has been an
3861 		 * IP address change.
3862 		 */
3863 		if (odst == 1) {
3864 			sum1 = ntohs(nat->nat_osport);
3865 			sum4 = ntohs(tcp->th_sport);
3866 			sum3 = ntohs(nat->nat_odport);
3867 			sum2 = ntohs(tcp->th_dport);
3868 
3869 			tcp->th_sport = htons(sum1);
3870 			tcp->th_dport = htons(sum3);
3871 		} else {
3872 			sum1 = ntohs(nat->nat_ndport);
3873 			sum2 = ntohs(tcp->th_dport);
3874 			sum3 = ntohs(nat->nat_nsport);
3875 			sum4 = ntohs(tcp->th_sport);
3876 
3877 			tcp->th_dport = htons(sum3);
3878 			tcp->th_sport = htons(sum1);
3879 		}
3880 		CALC_SUMD(sum4, sum1, sumt);
3881 		sumd += sumt;
3882 		CALC_SUMD(sum2, sum3, sumt);
3883 		sumd += sumt;
3884 
3885 		if (sumd != 0 || sumd2 != 0) {
3886 			/*
3887 			 * At this point, sumd is the delta to apply to the
3888 			 * TCP/UDP header, given the changes in both the IP
3889 			 * address and the ports and sumd2 is the delta to
3890 			 * apply to the ICMP header, given the IP address
3891 			 * change delta that may need to be applied to the
3892 			 * TCP/UDP checksum instead.
3893 			 *
3894 			 * If we will both the IP and TCP/UDP checksums
3895 			 * then the ICMP checksum changes by the address
3896 			 * delta applied to the TCP/UDP checksum.  If we
3897 			 * do not change the TCP/UDP checksum them we
3898 			 * apply the delta in ports to the ICMP checksum.
3899 			 */
3900 			if (oip->ip_p == IPPROTO_UDP) {
3901 				if ((dlen >= 8) && (*csump != 0)) {
3902 					ipf_fix_datacksum(csump, sumd);
3903 				} else {
3904 					CALC_SUMD(sum1, sum4, sumd2);
3905 					CALC_SUMD(sum3, sum2, sumt);
3906 					sumd2 += sumt;
3907 				}
3908 			} else if (oip->ip_p == IPPROTO_TCP) {
3909 				if (dlen >= 18) {
3910 					ipf_fix_datacksum(csump, sumd);
3911 				} else {
3912 					CALC_SUMD(sum1, sum4, sumd2);
3913 					CALC_SUMD(sum3, sum2, sumt);
3914 					sumd2 += sumt;
3915 				}
3916 			}
3917 			if (sumd2 != 0) {
3918 				sumd2 = (sumd2 & 0xffff) + (sumd2 >> 16);
3919 				sumd2 = (sumd2 & 0xffff) + (sumd2 >> 16);
3920 				sumd2 = (sumd2 & 0xffff) + (sumd2 >> 16);
3921 				ipf_fix_incksum(0, &icmp->icmp_cksum, sumd2, 0);
3922 			}
3923 		}
3924 	} else if (((flags & IPN_ICMPQUERY) != 0) && (dlen >= 8)) {
3925 		icmphdr_t *orgicmp;
3926 
3927 		/*
3928 		 * XXX - what if this is bogus hl and we go off the end ?
3929 		 * In this case, ipf_nat_icmperrorlookup() will have
3930 		 * returned NULL.
3931 		 */
3932 		orgicmp = (icmphdr_t *)dp;
3933 
3934 		if (odst == 1) {
3935 			if (orgicmp->icmp_id != nat->nat_osport) {
3936 
3937 				/*
3938 				 * Fix ICMP checksum (of the offening ICMP
3939 				 * query packet) to compensate the change
3940 				 * in the ICMP id of the offending ICMP
3941 				 * packet.
3942 				 *
3943 				 * Since you modify orgicmp->icmp_id with
3944 				 * a delta (say x) and you compensate that
3945 				 * in origicmp->icmp_cksum with a delta
3946 				 * minus x, you don't have to adjust the
3947 				 * overall icmp->icmp_cksum
3948 				 */
3949 				sum1 = ntohs(orgicmp->icmp_id);
3950 				sum2 = ntohs(nat->nat_oicmpid);
3951 				CALC_SUMD(sum1, sum2, sumd);
3952 				orgicmp->icmp_id = nat->nat_oicmpid;
3953 				ipf_fix_datacksum(&orgicmp->icmp_cksum, sumd);
3954 			}
3955 		} /* nat_dir == NAT_INBOUND is impossible for icmp queries */
3956 	}
3957 	return (nat);
3958 }
3959 
3960 
3961 /*
3962  *       MAP-IN    MAP-OUT   RDR-IN   RDR-OUT
3963  * osrc    X       == src    == src      X
3964  * odst    X       == dst    == dst      X
3965  * nsrc  == dst      X         X      == dst
3966  * ndst  == src      X         X      == src
3967  * MAP = NAT_OUTBOUND, RDR = NAT_INBOUND
3968  */
3969 /*
3970  * NB: these lookups don't lock access to the list, it assumed that it has
3971  * already been done!
3972  */
3973 /* ------------------------------------------------------------------------ */
3974 /* Function:    ipf_nat_inlookup                                            */
3975 /* Returns:     nat_t* - NULL == no match,                                  */
3976 /*                       else pointer to matching NAT entry                 */
3977 /* Parameters:  fin(I)    - pointer to packet information                   */
3978 /*              flags(I)  - NAT flags for this packet                       */
3979 /*              p(I)      - protocol for this packet                        */
3980 /*              src(I)    - source IP address                               */
3981 /*              mapdst(I) - destination IP address                          */
3982 /*                                                                          */
3983 /* Lookup a nat entry based on the mapped destination ip address/port and   */
3984 /* real source address/port.  We use this lookup when receiving a packet,   */
3985 /* we're looking for a table entry, based on the destination address.       */
3986 /*                                                                          */
3987 /* NOTE: THE PACKET BEING CHECKED (IF FOUND) HAS A MAPPING ALREADY.         */
3988 /*                                                                          */
3989 /* NOTE: IT IS ASSUMED THAT  IS ONLY HELD WITH A READ LOCK WHEN             */
3990 /*       THIS FUNCTION IS CALLED WITH NAT_SEARCH SET IN nflags.             */
3991 /*                                                                          */
3992 /* flags   -> relevant are IPN_UDP/IPN_TCP/IPN_ICMPQUERY that indicate if   */
3993 /*            the packet is of said protocol                                */
3994 /* ------------------------------------------------------------------------ */
3995 nat_t *
ipf_nat_inlookup(fr_info_t * fin,u_int flags,u_int p,struct in_addr src,struct in_addr mapdst)3996 ipf_nat_inlookup(fr_info_t *fin, u_int flags, u_int p,
3997 	struct in_addr src , struct in_addr mapdst)
3998 {
3999 	ipf_main_softc_t *softc = fin->fin_main_soft;
4000 	ipf_nat_softc_t *softn = softc->ipf_nat_soft;
4001 	u_short sport, dport;
4002 	grehdr_t *gre;
4003 	ipnat_t *ipn;
4004 	u_int sflags;
4005 	nat_t *nat;
4006 	int nflags;
4007 	u_32_t dst;
4008 	void *ifp;
4009 	u_int hv, rhv;
4010 
4011 	ifp = fin->fin_ifp;
4012 	gre = NULL;
4013 	dst = mapdst.s_addr;
4014 	sflags = flags & NAT_TCPUDPICMP;
4015 
4016 	switch (p)
4017 	{
4018 	case IPPROTO_TCP :
4019 	case IPPROTO_UDP :
4020 		sport = htons(fin->fin_data[0]);
4021 		dport = htons(fin->fin_data[1]);
4022 		break;
4023 	case IPPROTO_ICMP :
4024 		sport = 0;
4025 		dport = fin->fin_data[1];
4026 		break;
4027 	default :
4028 		sport = 0;
4029 		dport = 0;
4030 		break;
4031 	}
4032 
4033 
4034 	if ((flags & SI_WILDP) != 0)
4035 		goto find_in_wild_ports;
4036 
4037 	rhv = NAT_HASH_FN(dst, dport, 0xffffffff);
4038 	rhv = NAT_HASH_FN(src.s_addr, rhv + sport, 0xffffffff);
4039 	hv = rhv % softn->ipf_nat_table_sz;
4040 	nat = softn->ipf_nat_table[1][hv];
4041 	/* TRACE dst, dport, src, sport, hv, nat */
4042 
4043 	for (; nat; nat = nat->nat_hnext[1]) {
4044 		if (nat->nat_ifps[0] != NULL) {
4045 			if ((ifp != NULL) && (ifp != nat->nat_ifps[0]))
4046 				continue;
4047 		}
4048 
4049 		if (nat->nat_pr[0] != p)
4050 			continue;
4051 
4052 		switch (nat->nat_dir)
4053 		{
4054 		case NAT_INBOUND :
4055 		case NAT_DIVERTIN :
4056 			if (nat->nat_v[0] != 4)
4057 				continue;
4058 			if (nat->nat_osrcaddr != src.s_addr ||
4059 			    nat->nat_odstaddr != dst)
4060 				continue;
4061 			if ((nat->nat_flags & IPN_TCPUDP) != 0) {
4062 				if (nat->nat_osport != sport)
4063 					continue;
4064 				if (nat->nat_odport != dport)
4065 					continue;
4066 
4067 			} else if (p == IPPROTO_ICMP) {
4068 				if (nat->nat_osport != dport) {
4069 					continue;
4070 				}
4071 			}
4072 			break;
4073 		case NAT_DIVERTOUT :
4074 			if (nat->nat_dlocal)
4075 				continue;
4076 		case NAT_OUTBOUND :
4077 			if (nat->nat_v[1] != 4)
4078 				continue;
4079 			if (nat->nat_dlocal)
4080 				continue;
4081 			if (nat->nat_dlocal)
4082 				continue;
4083 			if (nat->nat_ndstaddr != src.s_addr ||
4084 			    nat->nat_nsrcaddr != dst)
4085 				continue;
4086 			if ((nat->nat_flags & IPN_TCPUDP) != 0) {
4087 				if (nat->nat_ndport != sport)
4088 					continue;
4089 				if (nat->nat_nsport != dport)
4090 					continue;
4091 
4092 			} else if (p == IPPROTO_ICMP) {
4093 				if (nat->nat_osport != dport) {
4094 					continue;
4095 				}
4096 			}
4097 			break;
4098 		}
4099 
4100 
4101 		if ((nat->nat_flags & IPN_TCPUDP) != 0) {
4102 			ipn = nat->nat_ptr;
4103 			if ((ipn != NULL) && (nat->nat_aps != NULL))
4104 				if (ipf_proxy_match(fin, nat) != 0)
4105 					continue;
4106 		}
4107 		if ((nat->nat_ifps[0] == NULL) && (ifp != NULL)) {
4108 			nat->nat_ifps[0] = ifp;
4109 			nat->nat_mtu[0] = GETIFMTU_4(ifp);
4110 		}
4111 		return (nat);
4112 	}
4113 
4114 	/*
4115 	 * So if we didn't find it but there are wildcard members in the hash
4116 	 * table, go back and look for them.  We do this search and update here
4117 	 * because it is modifying the NAT table and we want to do this only
4118 	 * for the first packet that matches.  The exception, of course, is
4119 	 * for "dummy" (FI_IGNORE) lookups.
4120 	 */
4121 find_in_wild_ports:
4122 	if (!(flags & NAT_TCPUDP) || !(flags & NAT_SEARCH)) {
4123 		NBUMPSIDEX(0, ns_lookup_miss, ns_lookup_miss_0);
4124 		return (NULL);
4125 	}
4126 	if (softn->ipf_nat_stats.ns_wilds == 0 || (fin->fin_flx & FI_NOWILD)) {
4127 		NBUMPSIDEX(0, ns_lookup_nowild, ns_lookup_nowild_0);
4128 		return (NULL);
4129 	}
4130 
4131 	RWLOCK_EXIT(&softc->ipf_nat);
4132 
4133 	hv = NAT_HASH_FN(dst, 0, 0xffffffff);
4134 	hv = NAT_HASH_FN(src.s_addr, hv, softn->ipf_nat_table_sz);
4135 	WRITE_ENTER(&softc->ipf_nat);
4136 
4137 	nat = softn->ipf_nat_table[1][hv];
4138 	/* TRACE dst, src, hv, nat */
4139 	for (; nat; nat = nat->nat_hnext[1]) {
4140 		if (nat->nat_ifps[0] != NULL) {
4141 			if ((ifp != NULL) && (ifp != nat->nat_ifps[0]))
4142 				continue;
4143 		}
4144 
4145 		if (nat->nat_pr[0] != fin->fin_p)
4146 			continue;
4147 
4148 		switch (nat->nat_dir & (NAT_INBOUND|NAT_OUTBOUND))
4149 		{
4150 		case NAT_INBOUND :
4151 			if (nat->nat_v[0] != 4)
4152 				continue;
4153 			if (nat->nat_osrcaddr != src.s_addr ||
4154 			    nat->nat_odstaddr != dst)
4155 				continue;
4156 			break;
4157 		case NAT_OUTBOUND :
4158 			if (nat->nat_v[1] != 4)
4159 				continue;
4160 			if (nat->nat_ndstaddr != src.s_addr ||
4161 			    nat->nat_nsrcaddr != dst)
4162 				continue;
4163 			break;
4164 		}
4165 
4166 		nflags = nat->nat_flags;
4167 		if (!(nflags & (NAT_TCPUDP|SI_WILDP)))
4168 			continue;
4169 
4170 		if (ipf_nat_wildok(nat, (int)sport, (int)dport, nflags,
4171 				   NAT_INBOUND) == 1) {
4172 			if ((fin->fin_flx & FI_IGNORE) != 0)
4173 				break;
4174 			if ((nflags & SI_CLONE) != 0) {
4175 				nat = ipf_nat_clone(fin, nat);
4176 				if (nat == NULL)
4177 					break;
4178 			} else {
4179 				MUTEX_ENTER(&softn->ipf_nat_new);
4180 				softn->ipf_nat_stats.ns_wilds--;
4181 				MUTEX_EXIT(&softn->ipf_nat_new);
4182 			}
4183 
4184 			if (nat->nat_dir == NAT_INBOUND) {
4185 				if (nat->nat_osport == 0) {
4186 					nat->nat_osport = sport;
4187 					nat->nat_nsport = sport;
4188 				}
4189 				if (nat->nat_odport == 0) {
4190 					nat->nat_odport = dport;
4191 					nat->nat_ndport = dport;
4192 				}
4193 			} else if (nat->nat_dir == NAT_OUTBOUND) {
4194 				if (nat->nat_osport == 0) {
4195 					nat->nat_osport = dport;
4196 					nat->nat_nsport = dport;
4197 				}
4198 				if (nat->nat_odport == 0) {
4199 					nat->nat_odport = sport;
4200 					nat->nat_ndport = sport;
4201 				}
4202 			}
4203 			if ((nat->nat_ifps[0] == NULL) && (ifp != NULL)) {
4204 				nat->nat_ifps[0] = ifp;
4205 				nat->nat_mtu[0] = GETIFMTU_4(ifp);
4206 			}
4207 			nat->nat_flags &= ~(SI_W_DPORT|SI_W_SPORT);
4208 			ipf_nat_tabmove(softn, nat);
4209 			break;
4210 		}
4211 	}
4212 
4213 	MUTEX_DOWNGRADE(&softc->ipf_nat);
4214 
4215 	if (nat == NULL) {
4216 		NBUMPSIDE(0, ns_lookup_miss);
4217 	}
4218 	return (nat);
4219 }
4220 
4221 
4222 /* ------------------------------------------------------------------------ */
4223 /* Function:    ipf_nat_tabmove                                             */
4224 /* Returns:     Nil                                                         */
4225 /* Parameters:  softn(I) - pointer to NAT context structure                 */
4226 /*              nat(I)   - pointer to NAT structure                         */
4227 /* Write Lock:  ipf_nat                                                     */
4228 /*                                                                          */
4229 /* This function is only called for TCP/UDP NAT table entries where the     */
4230 /* original was placed in the table without hashing on the ports and we now */
4231 /* want to include hashing on port numbers.                                 */
4232 /* ------------------------------------------------------------------------ */
4233 static void
ipf_nat_tabmove(ipf_nat_softc_t * softn,nat_t * nat)4234 ipf_nat_tabmove(ipf_nat_softc_t *softn, nat_t *nat)
4235 {
4236 	u_int hv0, hv1, rhv0, rhv1;
4237 	natstat_t *nsp;
4238 	nat_t **natp;
4239 
4240 	if (nat->nat_flags & SI_CLONE)
4241 		return;
4242 
4243 	nsp = &softn->ipf_nat_stats;
4244 	/*
4245 	 * Remove the NAT entry from the old location
4246 	 */
4247 	if (nat->nat_hnext[0])
4248 		nat->nat_hnext[0]->nat_phnext[0] = nat->nat_phnext[0];
4249 	*nat->nat_phnext[0] = nat->nat_hnext[0];
4250 	nsp->ns_side[0].ns_bucketlen[nat->nat_hv[0] %
4251 				     softn->ipf_nat_table_sz]--;
4252 
4253 	if (nat->nat_hnext[1])
4254 		nat->nat_hnext[1]->nat_phnext[1] = nat->nat_phnext[1];
4255 	*nat->nat_phnext[1] = nat->nat_hnext[1];
4256 	nsp->ns_side[1].ns_bucketlen[nat->nat_hv[1] %
4257 				     softn->ipf_nat_table_sz]--;
4258 
4259 	/*
4260 	 * Add into the NAT table in the new position
4261 	 */
4262 	rhv0 = NAT_HASH_FN(nat->nat_osrcaddr, nat->nat_osport, 0xffffffff);
4263 	rhv0 = NAT_HASH_FN(nat->nat_odstaddr, rhv0 + nat->nat_odport,
4264 			   0xffffffff);
4265 	rhv1 = NAT_HASH_FN(nat->nat_nsrcaddr, nat->nat_nsport, 0xffffffff);
4266 	rhv1 = NAT_HASH_FN(nat->nat_ndstaddr, rhv1 + nat->nat_ndport,
4267 			   0xffffffff);
4268 
4269 	hv0 = rhv0 % softn->ipf_nat_table_sz;
4270 	hv1 = rhv1 % softn->ipf_nat_table_sz;
4271 
4272 	if (nat->nat_dir == NAT_INBOUND || nat->nat_dir == NAT_DIVERTIN) {
4273 		u_int swap;
4274 
4275 		swap = hv0;
4276 		hv0 = hv1;
4277 		hv1 = swap;
4278 	}
4279 
4280 	/* TRACE nat_osrcaddr, nat_osport, nat_odstaddr, nat_odport, hv0 */
4281 	/* TRACE nat_nsrcaddr, nat_nsport, nat_ndstaddr, nat_ndport, hv1 */
4282 
4283 	nat->nat_hv[0] = rhv0;
4284 	natp = &softn->ipf_nat_table[0][hv0];
4285 	if (*natp)
4286 		(*natp)->nat_phnext[0] = &nat->nat_hnext[0];
4287 	nat->nat_phnext[0] = natp;
4288 	nat->nat_hnext[0] = *natp;
4289 	*natp = nat;
4290 	nsp->ns_side[0].ns_bucketlen[hv0]++;
4291 
4292 	nat->nat_hv[1] = rhv1;
4293 	natp = &softn->ipf_nat_table[1][hv1];
4294 	if (*natp)
4295 		(*natp)->nat_phnext[1] = &nat->nat_hnext[1];
4296 	nat->nat_phnext[1] = natp;
4297 	nat->nat_hnext[1] = *natp;
4298 	*natp = nat;
4299 	nsp->ns_side[1].ns_bucketlen[hv1]++;
4300 }
4301 
4302 
4303 /* ------------------------------------------------------------------------ */
4304 /* Function:    ipf_nat_outlookup                                           */
4305 /* Returns:     nat_t* - NULL == no match,                                  */
4306 /*                       else pointer to matching NAT entry                 */
4307 /* Parameters:  fin(I)   - pointer to packet information                    */
4308 /*              flags(I) - NAT flags for this packet                        */
4309 /*              p(I)     - protocol for this packet                         */
4310 /*              src(I)   - source IP address                                */
4311 /*              dst(I)   - destination IP address                           */
4312 /*              rw(I)    - 1 == write lock on  held, 0 == read lock.        */
4313 /*                                                                          */
4314 /* Lookup a nat entry based on the source 'real' ip address/port and        */
4315 /* destination address/port.  We use this lookup when sending a packet out, */
4316 /* we're looking for a table entry, based on the source address.            */
4317 /*                                                                          */
4318 /* NOTE: THE PACKET BEING CHECKED (IF FOUND) HAS A MAPPING ALREADY.         */
4319 /*                                                                          */
4320 /* NOTE: IT IS ASSUMED THAT  IS ONLY HELD WITH A READ LOCK WHEN             */
4321 /*       THIS FUNCTION IS CALLED WITH NAT_SEARCH SET IN nflags.             */
4322 /*                                                                          */
4323 /* flags   -> relevant are IPN_UDP/IPN_TCP/IPN_ICMPQUERY that indicate if   */
4324 /*            the packet is of said protocol                                */
4325 /* ------------------------------------------------------------------------ */
4326 nat_t *
ipf_nat_outlookup(fr_info_t * fin,u_int flags,u_int p,struct in_addr src,struct in_addr dst)4327 ipf_nat_outlookup(fr_info_t *fin, u_int flags, u_int p,
4328 	struct in_addr src , struct in_addr dst)
4329 {
4330 	ipf_main_softc_t *softc = fin->fin_main_soft;
4331 	ipf_nat_softc_t *softn = softc->ipf_nat_soft;
4332 	u_short sport, dport;
4333 	u_int sflags;
4334 	ipnat_t *ipn;
4335 	nat_t *nat;
4336 	void *ifp;
4337 	u_int hv;
4338 
4339 	ifp = fin->fin_ifp;
4340 	sflags = flags & IPN_TCPUDPICMP;
4341 
4342 	switch (p)
4343 	{
4344 	case IPPROTO_TCP :
4345 	case IPPROTO_UDP :
4346 		sport = htons(fin->fin_data[0]);
4347 		dport = htons(fin->fin_data[1]);
4348 		break;
4349 	case IPPROTO_ICMP :
4350 		sport = 0;
4351 		dport = fin->fin_data[1];
4352 		break;
4353 	default :
4354 		sport = 0;
4355 		dport = 0;
4356 		break;
4357 	}
4358 
4359 	if ((flags & SI_WILDP) != 0)
4360 		goto find_out_wild_ports;
4361 
4362 	hv = NAT_HASH_FN(src.s_addr, sport, 0xffffffff);
4363 	hv = NAT_HASH_FN(dst.s_addr, hv + dport, softn->ipf_nat_table_sz);
4364 	nat = softn->ipf_nat_table[0][hv];
4365 
4366 	/* TRACE src, sport, dst, dport, hv, nat */
4367 
4368 	for (; nat; nat = nat->nat_hnext[0]) {
4369 		if (nat->nat_ifps[1] != NULL) {
4370 			if ((ifp != NULL) && (ifp != nat->nat_ifps[1]))
4371 				continue;
4372 		}
4373 
4374 		if (nat->nat_pr[1] != p)
4375 			continue;
4376 
4377 		switch (nat->nat_dir)
4378 		{
4379 		case NAT_INBOUND :
4380 		case NAT_DIVERTIN :
4381 			if (nat->nat_v[1] != 4)
4382 				continue;
4383 			if (nat->nat_ndstaddr != src.s_addr ||
4384 			    nat->nat_nsrcaddr != dst.s_addr)
4385 				continue;
4386 
4387 			if ((nat->nat_flags & IPN_TCPUDP) != 0) {
4388 				if (nat->nat_ndport != sport)
4389 					continue;
4390 				if (nat->nat_nsport != dport)
4391 					continue;
4392 
4393 			} else if (p == IPPROTO_ICMP) {
4394 				if (nat->nat_osport != dport) {
4395 					continue;
4396 				}
4397 			}
4398 			break;
4399 		case NAT_OUTBOUND :
4400 		case NAT_DIVERTOUT :
4401 			if (nat->nat_v[0] != 4)
4402 				continue;
4403 			if (nat->nat_osrcaddr != src.s_addr ||
4404 			    nat->nat_odstaddr != dst.s_addr)
4405 				continue;
4406 
4407 			if ((nat->nat_flags & IPN_TCPUDP) != 0) {
4408 				if (nat->nat_odport != dport)
4409 					continue;
4410 				if (nat->nat_osport != sport)
4411 					continue;
4412 
4413 			} else if (p == IPPROTO_ICMP) {
4414 				if (nat->nat_osport != dport) {
4415 					continue;
4416 				}
4417 			}
4418 			break;
4419 		}
4420 
4421 		ipn = nat->nat_ptr;
4422 		if ((ipn != NULL) && (nat->nat_aps != NULL))
4423 			if (ipf_proxy_match(fin, nat) != 0)
4424 				continue;
4425 
4426 		if ((nat->nat_ifps[1] == NULL) && (ifp != NULL)) {
4427 			nat->nat_ifps[1] = ifp;
4428 			nat->nat_mtu[1] = GETIFMTU_4(ifp);
4429 		}
4430 		return (nat);
4431 	}
4432 
4433 	/*
4434 	 * So if we didn't find it but there are wildcard members in the hash
4435 	 * table, go back and look for them.  We do this search and update here
4436 	 * because it is modifying the NAT table and we want to do this only
4437 	 * for the first packet that matches.  The exception, of course, is
4438 	 * for "dummy" (FI_IGNORE) lookups.
4439 	 */
4440 find_out_wild_ports:
4441 	if (!(flags & NAT_TCPUDP) || !(flags & NAT_SEARCH)) {
4442 		NBUMPSIDEX(1, ns_lookup_miss, ns_lookup_miss_1);
4443 		return (NULL);
4444 	}
4445 	if (softn->ipf_nat_stats.ns_wilds == 0 || (fin->fin_flx & FI_NOWILD)) {
4446 		NBUMPSIDEX(1, ns_lookup_nowild, ns_lookup_nowild_1);
4447 		return (NULL);
4448 	}
4449 
4450 	RWLOCK_EXIT(&softc->ipf_nat);
4451 
4452 	hv = NAT_HASH_FN(src.s_addr, 0, 0xffffffff);
4453 	hv = NAT_HASH_FN(dst.s_addr, hv, softn->ipf_nat_table_sz);
4454 
4455 	WRITE_ENTER(&softc->ipf_nat);
4456 
4457 	nat = softn->ipf_nat_table[0][hv];
4458 	for (; nat; nat = nat->nat_hnext[0]) {
4459 		if (nat->nat_ifps[1] != NULL) {
4460 			if ((ifp != NULL) && (ifp != nat->nat_ifps[1]))
4461 				continue;
4462 		}
4463 
4464 		if (nat->nat_pr[1] != fin->fin_p)
4465 			continue;
4466 
4467 		switch (nat->nat_dir & (NAT_INBOUND|NAT_OUTBOUND))
4468 		{
4469 		case NAT_INBOUND :
4470 			if (nat->nat_v[1] != 4)
4471 				continue;
4472 			if (nat->nat_ndstaddr != src.s_addr ||
4473 			    nat->nat_nsrcaddr != dst.s_addr)
4474 				continue;
4475 			break;
4476 		case NAT_OUTBOUND :
4477 			if (nat->nat_v[0] != 4)
4478 				continue;
4479 			if (nat->nat_osrcaddr != src.s_addr ||
4480 			    nat->nat_odstaddr != dst.s_addr)
4481 				continue;
4482 			break;
4483 		}
4484 
4485 		if (!(nat->nat_flags & (NAT_TCPUDP|SI_WILDP)))
4486 			continue;
4487 
4488 		if (ipf_nat_wildok(nat, (int)sport, (int)dport, nat->nat_flags,
4489 				   NAT_OUTBOUND) == 1) {
4490 			if ((fin->fin_flx & FI_IGNORE) != 0)
4491 				break;
4492 			if ((nat->nat_flags & SI_CLONE) != 0) {
4493 				nat = ipf_nat_clone(fin, nat);
4494 				if (nat == NULL)
4495 					break;
4496 			} else {
4497 				MUTEX_ENTER(&softn->ipf_nat_new);
4498 				softn->ipf_nat_stats.ns_wilds--;
4499 				MUTEX_EXIT(&softn->ipf_nat_new);
4500 			}
4501 
4502 			if (nat->nat_dir == NAT_OUTBOUND) {
4503 				if (nat->nat_osport == 0) {
4504 					nat->nat_osport = sport;
4505 					nat->nat_nsport = sport;
4506 				}
4507 				if (nat->nat_odport == 0) {
4508 					nat->nat_odport = dport;
4509 					nat->nat_ndport = dport;
4510 				}
4511 			} else if (nat->nat_dir == NAT_INBOUND) {
4512 				if (nat->nat_osport == 0) {
4513 					nat->nat_osport = dport;
4514 					nat->nat_nsport = dport;
4515 				}
4516 				if (nat->nat_odport == 0) {
4517 					nat->nat_odport = sport;
4518 					nat->nat_ndport = sport;
4519 				}
4520 			}
4521 			if ((nat->nat_ifps[1] == NULL) && (ifp != NULL)) {
4522 				nat->nat_ifps[1] = ifp;
4523 				nat->nat_mtu[1] = GETIFMTU_4(ifp);
4524 			}
4525 			nat->nat_flags &= ~(SI_W_DPORT|SI_W_SPORT);
4526 			ipf_nat_tabmove(softn, nat);
4527 			break;
4528 		}
4529 	}
4530 
4531 	MUTEX_DOWNGRADE(&softc->ipf_nat);
4532 
4533 	if (nat == NULL) {
4534 		NBUMPSIDE(1, ns_lookup_miss);
4535 	}
4536 	return (nat);
4537 }
4538 
4539 
4540 /* ------------------------------------------------------------------------ */
4541 /* Function:    ipf_nat_lookupredir                                         */
4542 /* Returns:     nat_t* - NULL == no match,                                  */
4543 /*                       else pointer to matching NAT entry                 */
4544 /* Parameters:  np(I) - pointer to description of packet to find NAT table  */
4545 /*                      entry for.                                          */
4546 /*                                                                          */
4547 /* Lookup the NAT tables to search for a matching redirect                  */
4548 /* The contents of natlookup_t should imitate those found in a packet that  */
4549 /* would be translated - ie a packet coming in for RDR or going out for MAP.*/
4550 /* We can do the lookup in one of two ways, imitating an inbound or         */
4551 /* outbound  packet.  By default we assume outbound, unless IPN_IN is set.  */
4552 /* For IN, the fields are set as follows:                                   */
4553 /*     nl_real* = source information                                        */
4554 /*     nl_out* = destination information (translated)                       */
4555 /* For an out packet, the fields are set like this:                         */
4556 /*     nl_in* = source information (untranslated)                           */
4557 /*     nl_out* = destination information (translated)                       */
4558 /* ------------------------------------------------------------------------ */
4559 nat_t *
ipf_nat_lookupredir(natlookup_t * np)4560 ipf_nat_lookupredir(natlookup_t *np)
4561 {
4562 	fr_info_t fi;
4563 	nat_t *nat;
4564 
4565 	bzero((char *)&fi, sizeof(fi));
4566 	if (np->nl_flags & IPN_IN) {
4567 		fi.fin_data[0] = ntohs(np->nl_realport);
4568 		fi.fin_data[1] = ntohs(np->nl_outport);
4569 	} else {
4570 		fi.fin_data[0] = ntohs(np->nl_inport);
4571 		fi.fin_data[1] = ntohs(np->nl_outport);
4572 	}
4573 	if (np->nl_flags & IPN_TCP)
4574 		fi.fin_p = IPPROTO_TCP;
4575 	else if (np->nl_flags & IPN_UDP)
4576 		fi.fin_p = IPPROTO_UDP;
4577 	else if (np->nl_flags & (IPN_ICMPERR|IPN_ICMPQUERY))
4578 		fi.fin_p = IPPROTO_ICMP;
4579 
4580 	/*
4581 	 * We can do two sorts of lookups:
4582 	 * - IPN_IN: we have the `real' and `out' address, look for `in'.
4583 	 * - default: we have the `in' and `out' address, look for `real'.
4584 	 */
4585 	if (np->nl_flags & IPN_IN) {
4586 		if ((nat = ipf_nat_inlookup(&fi, np->nl_flags, fi.fin_p,
4587 					    np->nl_realip, np->nl_outip))) {
4588 			np->nl_inip = nat->nat_odstip;
4589 			np->nl_inport = nat->nat_odport;
4590 		}
4591 	} else {
4592 		/*
4593 		 * If nl_inip is non null, this is a lookup based on the real
4594 		 * ip address. Else, we use the fake.
4595 		 */
4596 		if ((nat = ipf_nat_outlookup(&fi, np->nl_flags, fi.fin_p,
4597 					 np->nl_inip, np->nl_outip))) {
4598 
4599 			if ((np->nl_flags & IPN_FINDFORWARD) != 0) {
4600 				fr_info_t fin;
4601 				bzero((char *)&fin, sizeof(fin));
4602 				fin.fin_p = nat->nat_pr[0];
4603 				fin.fin_data[0] = ntohs(nat->nat_ndport);
4604 				fin.fin_data[1] = ntohs(nat->nat_nsport);
4605 				if (ipf_nat_inlookup(&fin, np->nl_flags,
4606 						     fin.fin_p, nat->nat_ndstip,
4607 						     nat->nat_nsrcip) != NULL) {
4608 					np->nl_flags &= ~IPN_FINDFORWARD;
4609 				}
4610 			}
4611 
4612 			np->nl_realip = nat->nat_odstip;
4613 			np->nl_realport = nat->nat_odport;
4614 		}
4615  	}
4616 
4617 	return (nat);
4618 }
4619 
4620 
4621 /* ------------------------------------------------------------------------ */
4622 /* Function:    ipf_nat_match                                               */
4623 /* Returns:     int - 0 == no match, 1 == match                             */
4624 /* Parameters:  fin(I)   - pointer to packet information                    */
4625 /*              np(I)    - pointer to NAT rule                              */
4626 /*                                                                          */
4627 /* Pull the matching of a packet against a NAT rule out of that complex     */
4628 /* loop inside ipf_nat_checkin() and lay it out properly in its own function. */
4629 /* ------------------------------------------------------------------------ */
4630 static int
ipf_nat_match(fr_info_t * fin,ipnat_t * np)4631 ipf_nat_match(fr_info_t *fin, ipnat_t *np)
4632 {
4633 	ipf_main_softc_t *softc = fin->fin_main_soft;
4634 	frtuc_t *ft;
4635 	int match;
4636 
4637 	match = 0;
4638 	switch (np->in_osrcatype)
4639 	{
4640 	case FRI_NORMAL :
4641 		match = ((fin->fin_saddr & np->in_osrcmsk) != np->in_osrcaddr);
4642 		break;
4643 	case FRI_LOOKUP :
4644 		match = (*np->in_osrcfunc)(softc, np->in_osrcptr,
4645 					   4, &fin->fin_saddr, fin->fin_plen);
4646 		break;
4647 	}
4648 	match ^= ((np->in_flags & IPN_NOTSRC) != 0);
4649 	if (match)
4650 		return (0);
4651 
4652 	match = 0;
4653 	switch (np->in_odstatype)
4654 	{
4655 	case FRI_NORMAL :
4656 		match = ((fin->fin_daddr & np->in_odstmsk) != np->in_odstaddr);
4657 		break;
4658 	case FRI_LOOKUP :
4659 		match = (*np->in_odstfunc)(softc, np->in_odstptr,
4660 					   4, &fin->fin_daddr, fin->fin_plen);
4661 		break;
4662 	}
4663 
4664 	match ^= ((np->in_flags & IPN_NOTDST) != 0);
4665 	if (match)
4666 		return (0);
4667 
4668 	ft = &np->in_tuc;
4669 	if (!(fin->fin_flx & FI_TCPUDP) ||
4670 	    (fin->fin_flx & (FI_SHORT|FI_FRAGBODY))) {
4671 		if (ft->ftu_scmp || ft->ftu_dcmp)
4672 			return (0);
4673 		return (1);
4674 	}
4675 
4676 	return (ipf_tcpudpchk(&fin->fin_fi, ft));
4677 }
4678 
4679 
4680 /* ------------------------------------------------------------------------ */
4681 /* Function:    ipf_nat_update                                              */
4682 /* Returns:     Nil                                                         */
4683 /* Parameters:  fin(I) - pointer to packet information                      */
4684 /*              nat(I) - pointer to NAT structure                           */
4685 /*                                                                          */
4686 /* Updates the lifetime of a NAT table entry for non-TCP packets.  Must be  */
4687 /* called with fin_rev updated - i.e. after calling ipf_nat_proto().        */
4688 /*                                                                          */
4689 /* This *MUST* be called after ipf_nat_proto() as it expects fin_rev to     */
4690 /* already be set.                                                          */
4691 /* ------------------------------------------------------------------------ */
4692 void
ipf_nat_update(fr_info_t * fin,nat_t * nat)4693 ipf_nat_update(fr_info_t *fin, nat_t *nat)
4694 {
4695 	ipf_main_softc_t *softc = fin->fin_main_soft;
4696 	ipf_nat_softc_t *softn = softc->ipf_nat_soft;
4697 	ipftq_t *ifq, *ifq2;
4698 	ipftqent_t *tqe;
4699 	ipnat_t *np = nat->nat_ptr;
4700 
4701 	tqe = &nat->nat_tqe;
4702 	ifq = tqe->tqe_ifq;
4703 
4704 	/*
4705 	 * We allow over-riding of NAT timeouts from NAT rules, even for
4706 	 * TCP, however, if it is TCP and there is no rule timeout set,
4707 	 * then do not update the timeout here.
4708 	 */
4709 	if (np != NULL) {
4710 		np->in_bytes[fin->fin_rev] += fin->fin_plen;
4711 		ifq2 = np->in_tqehead[fin->fin_rev];
4712 	} else {
4713 		ifq2 = NULL;
4714 	}
4715 
4716 	if (nat->nat_pr[0] == IPPROTO_TCP && ifq2 == NULL) {
4717 		(void) ipf_tcp_age(&nat->nat_tqe, fin, softn->ipf_nat_tcptq,
4718 				   0, 2);
4719 	} else {
4720 		if (ifq2 == NULL) {
4721 			if (nat->nat_pr[0] == IPPROTO_UDP)
4722 				ifq2 = fin->fin_rev ? &softn->ipf_nat_udpacktq :
4723 						      &softn->ipf_nat_udptq;
4724 			else if (nat->nat_pr[0] == IPPROTO_ICMP ||
4725 				 nat->nat_pr[0] == IPPROTO_ICMPV6)
4726 				ifq2 = fin->fin_rev ? &softn->ipf_nat_icmpacktq:
4727 						      &softn->ipf_nat_icmptq;
4728 			else
4729 				ifq2 = &softn->ipf_nat_iptq;
4730 		}
4731 
4732 		ipf_movequeue(softc->ipf_ticks, tqe, ifq, ifq2);
4733 	}
4734 }
4735 
4736 
4737 /* ------------------------------------------------------------------------ */
4738 /* Function:    ipf_nat_checkout                                            */
4739 /* Returns:     int - -1 == packet failed NAT checks so block it,           */
4740 /*                     0 == no packet translation occurred,                 */
4741 /*                     1 == packet was successfully translated.             */
4742 /* Parameters:  fin(I)   - pointer to packet information                    */
4743 /*              passp(I) - pointer to filtering result flags                */
4744 /*                                                                          */
4745 /* Check to see if an outcoming packet should be changed.  ICMP packets are */
4746 /* first checked to see if they match an existing entry (if an error),      */
4747 /* otherwise a search of the current NAT table is made.  If neither results */
4748 /* in a match then a search for a matching NAT rule is made.  Create a new  */
4749 /* NAT entry if a we matched a NAT rule.  Lastly, actually change the       */
4750 /* packet header(s) as required.                                            */
4751 /* ------------------------------------------------------------------------ */
4752 int
ipf_nat_checkout(fr_info_t * fin,u_32_t * passp)4753 ipf_nat_checkout(fr_info_t *fin, u_32_t *passp)
4754 {
4755 	ipnat_t *np = NULL, *npnext;
4756 	struct ifnet *ifp, *sifp;
4757 	ipf_main_softc_t *softc;
4758 	ipf_nat_softc_t *softn;
4759 	icmphdr_t *icmp = NULL;
4760 	tcphdr_t *tcp = NULL;
4761 	int rval, natfailed;
4762 	u_int nflags = 0;
4763 	u_32_t ipa, iph;
4764 	int natadd = 1;
4765 	frentry_t *fr;
4766 	nat_t *nat;
4767 
4768 	if (fin->fin_v == 6) {
4769 #ifdef USE_INET6
4770 		return (ipf_nat6_checkout(fin, passp));
4771 #else
4772 		return (0);
4773 #endif
4774 	}
4775 
4776 	softc = fin->fin_main_soft;
4777 	softn = softc->ipf_nat_soft;
4778 
4779 	if (softn->ipf_nat_lock != 0)
4780 		return (0);
4781 	if (softn->ipf_nat_stats.ns_rules == 0 &&
4782 	    softn->ipf_nat_instances == NULL)
4783 		return (0);
4784 
4785 	natfailed = 0;
4786 	fr = fin->fin_fr;
4787 	sifp = fin->fin_ifp;
4788 	if (fr != NULL) {
4789 		ifp = fr->fr_tifs[fin->fin_rev].fd_ptr;
4790 		if ((ifp != NULL) && (ifp != (void *)-1))
4791 			fin->fin_ifp = ifp;
4792 	}
4793 	ifp = fin->fin_ifp;
4794 
4795 	if (!(fin->fin_flx & FI_SHORT) && (fin->fin_off == 0)) {
4796 		switch (fin->fin_p)
4797 		{
4798 		case IPPROTO_TCP :
4799 			nflags = IPN_TCP;
4800 			break;
4801 		case IPPROTO_UDP :
4802 			nflags = IPN_UDP;
4803 			break;
4804 		case IPPROTO_ICMP :
4805 			icmp = fin->fin_dp;
4806 
4807 			/*
4808 			 * This is an incoming packet, so the destination is
4809 			 * the icmp_id and the source port equals 0
4810 			 */
4811 			if ((fin->fin_flx & FI_ICMPQUERY) != 0)
4812 				nflags = IPN_ICMPQUERY;
4813 			break;
4814 		default :
4815 			break;
4816 		}
4817 
4818 		if ((nflags & IPN_TCPUDP))
4819 			tcp = fin->fin_dp;
4820 	}
4821 
4822 	ipa = fin->fin_saddr;
4823 
4824 	READ_ENTER(&softc->ipf_nat);
4825 
4826 	if ((fin->fin_p == IPPROTO_ICMP) && !(nflags & IPN_ICMPQUERY) &&
4827 	    (nat = ipf_nat_icmperror(fin, &nflags, NAT_OUTBOUND)))
4828 		/*EMPTY*/;
4829 	else if ((fin->fin_flx & FI_FRAG) && (nat = ipf_frag_natknown(fin)))
4830 		natadd = 0;
4831 	else if ((nat = ipf_nat_outlookup(fin, nflags|NAT_SEARCH,
4832 				      (u_int)fin->fin_p, fin->fin_src,
4833 				      fin->fin_dst))) {
4834 		nflags = nat->nat_flags;
4835 	} else if (fin->fin_off == 0) {
4836 		u_32_t hv, msk, nmsk = 0;
4837 
4838 		/*
4839 		 * If there is no current entry in the nat table for this IP#,
4840 		 * create one for it (if there is a matching rule).
4841 		 */
4842 maskloop:
4843 		msk = softn->ipf_nat_map_active_masks[nmsk];
4844 		iph = ipa & msk;
4845 		hv = NAT_HASH_FN(iph, 0, softn->ipf_nat_maprules_sz);
4846 retry_roundrobin:
4847 		for (np = softn->ipf_nat_map_rules[hv]; np; np = npnext) {
4848 			npnext = np->in_mnext;
4849 			if ((np->in_ifps[1] && (np->in_ifps[1] != ifp)))
4850 				continue;
4851 			if (np->in_v[0] != 4)
4852 				continue;
4853 			if (np->in_pr[1] && (np->in_pr[1] != fin->fin_p))
4854 				continue;
4855 			if ((np->in_flags & IPN_RF) &&
4856 			    !(np->in_flags & nflags))
4857 				continue;
4858 			if (np->in_flags & IPN_FILTER) {
4859 				switch (ipf_nat_match(fin, np))
4860 				{
4861 				case 0 :
4862 					continue;
4863 				case -1 :
4864 					rval = -3;
4865 					goto outmatchfail;
4866 				case 1 :
4867 				default :
4868 					break;
4869 				}
4870 			} else if ((ipa & np->in_osrcmsk) != np->in_osrcaddr)
4871 				continue;
4872 
4873 			if ((fr != NULL) &&
4874 			    !ipf_matchtag(&np->in_tag, &fr->fr_nattag))
4875 				continue;
4876 
4877 			if (np->in_plabel != -1) {
4878 				if (((np->in_flags & IPN_FILTER) == 0) &&
4879 				    (np->in_odport != fin->fin_data[1]))
4880 					continue;
4881 				if (ipf_proxy_ok(fin, tcp, np) == 0)
4882 					continue;
4883 			}
4884 
4885 			if (np->in_flags & IPN_NO) {
4886 				np->in_hits++;
4887 				break;
4888 			}
4889 			MUTEX_ENTER(&softn->ipf_nat_new);
4890 			/*
4891 			 * If we've matched a round-robin rule but it has
4892 			 * moved in the list since we got it, start over as
4893 			 * this is now no longer correct.
4894 			 */
4895 			if (npnext != np->in_mnext) {
4896 				if ((np->in_flags & IPN_ROUNDR) != 0) {
4897 					MUTEX_EXIT(&softn->ipf_nat_new);
4898 					goto retry_roundrobin;
4899 				}
4900 				npnext = np->in_mnext;
4901 			}
4902 
4903 			nat = ipf_nat_add(fin, np, NULL, nflags, NAT_OUTBOUND);
4904 			MUTEX_EXIT(&softn->ipf_nat_new);
4905 			if (nat != NULL) {
4906 				natfailed = 0;
4907 				break;
4908 			}
4909 			natfailed = -2;
4910 		}
4911 		if ((np == NULL) && (nmsk < softn->ipf_nat_map_max)) {
4912 			nmsk++;
4913 			goto maskloop;
4914 		}
4915 	}
4916 
4917 	if (nat != NULL) {
4918 		rval = ipf_nat_out(fin, nat, natadd, nflags);
4919 		if (rval == 1) {
4920 			MUTEX_ENTER(&nat->nat_lock);
4921 			ipf_nat_update(fin, nat);
4922 			nat->nat_bytes[1] += fin->fin_plen;
4923 			nat->nat_pkts[1]++;
4924 			fin->fin_pktnum = nat->nat_pkts[1];
4925 			MUTEX_EXIT(&nat->nat_lock);
4926 		}
4927 	} else
4928 		rval = natfailed;
4929 outmatchfail:
4930 	RWLOCK_EXIT(&softc->ipf_nat);
4931 
4932 	switch (rval)
4933 	{
4934 	case -3 :
4935 		/* ipf_nat_match() failure */
4936 		/* FALLTHROUGH */
4937 	case -2 :
4938 		/* retry_roundrobin loop failure */
4939 		/* FALLTHROUGH */
4940 	case -1 :
4941 		/* proxy failure detected by ipf_nat_out() */
4942 		if (passp != NULL) {
4943 			DT2(frb_natv4out, fr_info_t *, fin, int, rval);
4944 			NBUMPSIDED(1, ns_drop);
4945 			*passp = FR_BLOCK;
4946 			fin->fin_reason = FRB_NATV4;
4947 		}
4948 		fin->fin_flx |= FI_BADNAT;
4949 		NBUMPSIDED(1, ns_badnat);
4950 		rval = -1;	/* We only return -1 on error. */
4951 		break;
4952 	case 0 :
4953 		NBUMPSIDE(1, ns_ignored);
4954 		break;
4955 	case 1 :
4956 		NBUMPSIDE(1, ns_translated);
4957 		break;
4958 	}
4959 	fin->fin_ifp = sifp;
4960 	return (rval);
4961 }
4962 
4963 /* ------------------------------------------------------------------------ */
4964 /* Function:    ipf_nat_out                                                 */
4965 /* Returns:     int - -1 == packet failed NAT checks so block it,           */
4966 /*                     1 == packet was successfully translated.             */
4967 /* Parameters:  fin(I)    - pointer to packet information                   */
4968 /*              nat(I)    - pointer to NAT structure                        */
4969 /*              natadd(I) - flag indicating if it is safe to add frag cache */
4970 /*              nflags(I) - NAT flags set for this packet                   */
4971 /*                                                                          */
4972 /* Translate a packet coming "out" on an interface.                         */
4973 /* ------------------------------------------------------------------------ */
4974 int
ipf_nat_out(fr_info_t * fin,nat_t * nat,int natadd,u_32_t nflags)4975 ipf_nat_out(fr_info_t *fin, nat_t *nat, int natadd, u_32_t nflags)
4976 {
4977 	ipf_main_softc_t *softc = fin->fin_main_soft;
4978 	ipf_nat_softc_t *softn = softc->ipf_nat_soft;
4979 	icmphdr_t *icmp;
4980 	tcphdr_t *tcp;
4981 	ipnat_t *np;
4982 	int skip;
4983 	int i;
4984 
4985 	tcp = NULL;
4986 	icmp = NULL;
4987 	np = nat->nat_ptr;
4988 
4989 	if ((natadd != 0) && (fin->fin_flx & FI_FRAG) && (np != NULL))
4990 		(void) ipf_frag_natnew(softc, fin, 0, nat);
4991 
4992 	/*
4993 	 * Fix up checksums, not by recalculating them, but
4994 	 * simply computing adjustments.
4995 	 * This is only done for STREAMS based IP implementations where the
4996 	 * checksum has already been calculated by IP.  In all other cases,
4997 	 * IPFilter is called before the checksum needs calculating so there
4998 	 * is no call to modify whatever is in the header now.
4999 	 */
5000 	if (nflags == IPN_ICMPERR) {
5001 		u_32_t s1, s2, sumd, msumd;
5002 
5003 		s1 = LONG_SUM(ntohl(fin->fin_saddr));
5004 		if (nat->nat_dir == NAT_OUTBOUND) {
5005 			s2 = LONG_SUM(ntohl(nat->nat_nsrcaddr));
5006 		} else {
5007 			s2 = LONG_SUM(ntohl(nat->nat_odstaddr));
5008 		}
5009 		CALC_SUMD(s1, s2, sumd);
5010 		msumd = sumd;
5011 
5012 		s1 = LONG_SUM(ntohl(fin->fin_daddr));
5013 		if (nat->nat_dir == NAT_OUTBOUND) {
5014 			s2 = LONG_SUM(ntohl(nat->nat_ndstaddr));
5015 		} else {
5016 			s2 = LONG_SUM(ntohl(nat->nat_osrcaddr));
5017 		}
5018 		CALC_SUMD(s1, s2, sumd);
5019 		msumd += sumd;
5020 
5021 		ipf_fix_outcksum(0, &fin->fin_ip->ip_sum, msumd, 0);
5022 	}
5023 #if !defined(_KERNEL) || SOLARIS || \
5024     defined(BRIDGE_IPF) || defined(__FreeBSD__)
5025 	else {
5026 		/*
5027 		 * We always do this on FreeBSD because this code doesn't
5028 		 * exist in fastforward.
5029 		 */
5030 		switch (nat->nat_dir)
5031 		{
5032 		case NAT_OUTBOUND :
5033 			ipf_fix_outcksum(fin->fin_cksum & FI_CK_L4PART,
5034 					 &fin->fin_ip->ip_sum,
5035 					 nat->nat_ipsumd, 0);
5036 			break;
5037 
5038 		case NAT_INBOUND :
5039 			ipf_fix_incksum(fin->fin_cksum & FI_CK_L4PART,
5040 					&fin->fin_ip->ip_sum,
5041 					nat->nat_ipsumd, 0);
5042 			break;
5043 
5044 		default :
5045 			break;
5046 		}
5047 	}
5048 #endif
5049 
5050 	/*
5051 	 * Address assignment is after the checksum modification because
5052 	 * we are using the address in the packet for determining the
5053 	 * correct checksum offset (the ICMP error could be coming from
5054 	 * anyone...)
5055 	 */
5056 	switch (nat->nat_dir)
5057 	{
5058 	case NAT_OUTBOUND :
5059 		fin->fin_ip->ip_src = nat->nat_nsrcip;
5060 		fin->fin_saddr = nat->nat_nsrcaddr;
5061 		fin->fin_ip->ip_dst = nat->nat_ndstip;
5062 		fin->fin_daddr = nat->nat_ndstaddr;
5063 		break;
5064 
5065 	case NAT_INBOUND :
5066 		fin->fin_ip->ip_src = nat->nat_odstip;
5067 		fin->fin_saddr = nat->nat_ndstaddr;
5068 		fin->fin_ip->ip_dst = nat->nat_osrcip;
5069 		fin->fin_daddr = nat->nat_nsrcaddr;
5070 		break;
5071 
5072 	case NAT_DIVERTIN :
5073 	    {
5074 		mb_t *m;
5075 
5076 		skip = ipf_nat_decap(fin, nat);
5077 		if (skip <= 0) {
5078 			NBUMPSIDED(1, ns_decap_fail);
5079 			return (-1);
5080 		}
5081 
5082 		m = fin->fin_m;
5083 
5084 #if SOLARIS && defined(_KERNEL)
5085 		m->b_rptr += skip;
5086 #else
5087 		m->m_data += skip;
5088 		m->m_len -= skip;
5089 
5090 # ifdef M_PKTHDR
5091 		if (m->m_flags & M_PKTHDR)
5092 			m->m_pkthdr.len -= skip;
5093 # endif
5094 #endif
5095 
5096 		MUTEX_ENTER(&nat->nat_lock);
5097 		ipf_nat_update(fin, nat);
5098 		MUTEX_EXIT(&nat->nat_lock);
5099 		fin->fin_flx |= FI_NATED;
5100 		if (np != NULL && np->in_tag.ipt_num[0] != 0)
5101 			fin->fin_nattag = &np->in_tag;
5102 		return (1);
5103 		/* NOTREACHED */
5104 	    }
5105 
5106 	case NAT_DIVERTOUT :
5107 	    {
5108 		u_32_t s1, s2, sumd;
5109 		udphdr_t *uh;
5110 		ip_t *ip;
5111 		mb_t *m;
5112 
5113 		m = M_DUP(np->in_divmp);
5114 		if (m == NULL) {
5115 			NBUMPSIDED(1, ns_divert_dup);
5116 			return (-1);
5117 		}
5118 
5119 		ip = MTOD(m, ip_t *);
5120 		ip_fillid(ip);
5121 		s2 = ntohs(ip->ip_id);
5122 
5123 		s1 = ip->ip_len;
5124 		ip->ip_len = ntohs(ip->ip_len);
5125 		ip->ip_len += fin->fin_plen;
5126 		ip->ip_len = htons(ip->ip_len);
5127 		s2 += ntohs(ip->ip_len);
5128 		CALC_SUMD(s1, s2, sumd);
5129 
5130 		uh = (udphdr_t *)(ip + 1);
5131 		uh->uh_ulen += fin->fin_plen;
5132 		uh->uh_ulen = htons(uh->uh_ulen);
5133 #if !defined(_KERNEL) || SOLARIS || \
5134     defined(BRIDGE_IPF) || defined(__FreeBSD__)
5135 		ipf_fix_outcksum(0, &ip->ip_sum, sumd, 0);
5136 #endif
5137 
5138 		PREP_MB_T(fin, m);
5139 
5140 		fin->fin_src = ip->ip_src;
5141 		fin->fin_dst = ip->ip_dst;
5142 		fin->fin_ip = ip;
5143 		fin->fin_plen += sizeof(ip_t) + 8;	/* UDP + IPv4 hdr */
5144 		fin->fin_dlen += sizeof(ip_t) + 8;	/* UDP + IPv4 hdr */
5145 
5146 		nflags &= ~IPN_TCPUDPICMP;
5147 
5148 		break;
5149 	    }
5150 
5151 	default :
5152 		break;
5153 	}
5154 
5155 	if (!(fin->fin_flx & FI_SHORT) && (fin->fin_off == 0)) {
5156 		u_short *csump;
5157 
5158 		if ((nat->nat_nsport != 0) && (nflags & IPN_TCPUDP)) {
5159 			tcp = fin->fin_dp;
5160 
5161 			switch (nat->nat_dir)
5162 			{
5163 			case NAT_OUTBOUND :
5164 				tcp->th_sport = nat->nat_nsport;
5165 				fin->fin_data[0] = ntohs(nat->nat_nsport);
5166 				tcp->th_dport = nat->nat_ndport;
5167 				fin->fin_data[1] = ntohs(nat->nat_ndport);
5168 				break;
5169 
5170 			case NAT_INBOUND :
5171 				tcp->th_sport = nat->nat_odport;
5172 				fin->fin_data[0] = ntohs(nat->nat_odport);
5173 				tcp->th_dport = nat->nat_osport;
5174 				fin->fin_data[1] = ntohs(nat->nat_osport);
5175 				break;
5176 			}
5177 		}
5178 
5179 		if ((nat->nat_nsport != 0) && (nflags & IPN_ICMPQUERY)) {
5180 			icmp = fin->fin_dp;
5181 			icmp->icmp_id = nat->nat_nicmpid;
5182 		}
5183 
5184 		csump = ipf_nat_proto(fin, nat, nflags);
5185 
5186 		/*
5187 		 * The above comments do not hold for layer 4 (or higher)
5188 		 * checksums...
5189 		 */
5190 		if (csump != NULL) {
5191 			if (nat->nat_dir == NAT_OUTBOUND)
5192 				ipf_fix_outcksum(fin->fin_cksum, csump,
5193 						 nat->nat_sumd[0],
5194 						 nat->nat_sumd[1] +
5195 						 fin->fin_dlen);
5196 			else
5197 				ipf_fix_incksum(fin->fin_cksum, csump,
5198 						nat->nat_sumd[0],
5199 						nat->nat_sumd[1] +
5200 						fin->fin_dlen);
5201 		}
5202 	}
5203 
5204 	ipf_sync_update(softc, SMC_NAT, fin, nat->nat_sync);
5205 	/* ------------------------------------------------------------- */
5206 	/* A few quick notes:                                            */
5207 	/*      Following are test conditions prior to calling the       */
5208 	/*      ipf_proxy_check routine.                                 */
5209 	/*                                                               */
5210 	/*      A NULL tcp indicates a non TCP/UDP packet.  When dealing */
5211 	/*      with a redirect rule, we attempt to match the packet's   */
5212 	/*      source port against in_dport, otherwise we'd compare the */
5213 	/*      packet's destination.                                    */
5214 	/* ------------------------------------------------------------- */
5215 	if ((np != NULL) && (np->in_apr != NULL)) {
5216 		i = ipf_proxy_check(fin, nat);
5217 		if (i == -1) {
5218 			NBUMPSIDED(1, ns_ipf_proxy_fail);
5219 		}
5220 	} else {
5221 		i = 1;
5222 	}
5223 	fin->fin_flx |= FI_NATED;
5224 	return (i);
5225 }
5226 
5227 
5228 /* ------------------------------------------------------------------------ */
5229 /* Function:    ipf_nat_checkin                                             */
5230 /* Returns:     int - -1 == packet failed NAT checks so block it,           */
5231 /*                     0 == no packet translation occurred,                 */
5232 /*                     1 == packet was successfully translated.             */
5233 /* Parameters:  fin(I)   - pointer to packet information                    */
5234 /*              passp(I) - pointer to filtering result flags                */
5235 /*                                                                          */
5236 /* Check to see if an incoming packet should be changed.  ICMP packets are  */
5237 /* first checked to see if they match an existing entry (if an error),      */
5238 /* otherwise a search of the current NAT table is made.  If neither results */
5239 /* in a match then a search for a matching NAT rule is made.  Create a new  */
5240 /* NAT entry if a we matched a NAT rule.  Lastly, actually change the       */
5241 /* packet header(s) as required.                                            */
5242 /* ------------------------------------------------------------------------ */
5243 int
ipf_nat_checkin(fr_info_t * fin,u_32_t * passp)5244 ipf_nat_checkin(fr_info_t *fin, u_32_t *passp)
5245 {
5246 	ipf_main_softc_t *softc;
5247 	ipf_nat_softc_t *softn;
5248 	u_int nflags, natadd;
5249 	ipnat_t *np, *npnext;
5250 	int rval, natfailed;
5251 	struct ifnet *ifp;
5252 	struct in_addr in;
5253 	icmphdr_t *icmp;
5254 	tcphdr_t *tcp;
5255 	u_short dport;
5256 	nat_t *nat;
5257 	u_32_t iph;
5258 
5259 	softc = fin->fin_main_soft;
5260 	softn = softc->ipf_nat_soft;
5261 
5262 	if (softn->ipf_nat_lock != 0)
5263 		return (0);
5264 	if (softn->ipf_nat_stats.ns_rules == 0 &&
5265 	    softn->ipf_nat_instances == NULL)
5266 		return (0);
5267 
5268 	tcp = NULL;
5269 	icmp = NULL;
5270 	dport = 0;
5271 	natadd = 1;
5272 	nflags = 0;
5273 	natfailed = 0;
5274 	ifp = fin->fin_ifp;
5275 
5276 	if (!(fin->fin_flx & FI_SHORT) && (fin->fin_off == 0)) {
5277 		switch (fin->fin_p)
5278 		{
5279 		case IPPROTO_TCP :
5280 			nflags = IPN_TCP;
5281 			break;
5282 		case IPPROTO_UDP :
5283 			nflags = IPN_UDP;
5284 			break;
5285 		case IPPROTO_ICMP :
5286 			icmp = fin->fin_dp;
5287 
5288 			/*
5289 			 * This is an incoming packet, so the destination is
5290 			 * the icmp_id and the source port equals 0
5291 			 */
5292 			if ((fin->fin_flx & FI_ICMPQUERY) != 0) {
5293 				nflags = IPN_ICMPQUERY;
5294 				dport = icmp->icmp_id;
5295 			} break;
5296 		default :
5297 			break;
5298 		}
5299 
5300 		if ((nflags & IPN_TCPUDP)) {
5301 			tcp = fin->fin_dp;
5302 			dport = fin->fin_data[1];
5303 		}
5304 	}
5305 
5306 	in = fin->fin_dst;
5307 
5308 	READ_ENTER(&softc->ipf_nat);
5309 
5310 	if ((fin->fin_p == IPPROTO_ICMP) && !(nflags & IPN_ICMPQUERY) &&
5311 	    (nat = ipf_nat_icmperror(fin, &nflags, NAT_INBOUND)))
5312 		/*EMPTY*/;
5313 	else if ((fin->fin_flx & FI_FRAG) && (nat = ipf_frag_natknown(fin)))
5314 		natadd = 0;
5315 	else if ((nat = ipf_nat_inlookup(fin, nflags|NAT_SEARCH,
5316 					 (u_int)fin->fin_p,
5317 					 fin->fin_src, in))) {
5318 		nflags = nat->nat_flags;
5319 	} else if (fin->fin_off == 0) {
5320 		u_32_t hv, msk, rmsk = 0;
5321 
5322 		/*
5323 		 * If there is no current entry in the nat table for this IP#,
5324 		 * create one for it (if there is a matching rule).
5325 		 */
5326 maskloop:
5327 		msk = softn->ipf_nat_rdr_active_masks[rmsk];
5328 		iph = in.s_addr & msk;
5329 		hv = NAT_HASH_FN(iph, 0, softn->ipf_nat_rdrrules_sz);
5330 retry_roundrobin:
5331 		/* TRACE (iph,msk,rmsk,hv,softn->ipf_nat_rdrrules_sz) */
5332 		for (np = softn->ipf_nat_rdr_rules[hv]; np; np = npnext) {
5333 			npnext = np->in_rnext;
5334 			if (np->in_ifps[0] && (np->in_ifps[0] != ifp))
5335 				continue;
5336 			if (np->in_v[0] != 4)
5337 				continue;
5338 			if (np->in_pr[0] && (np->in_pr[0] != fin->fin_p))
5339 				continue;
5340 			if ((np->in_flags & IPN_RF) && !(np->in_flags & nflags))
5341 				continue;
5342 			if (np->in_flags & IPN_FILTER) {
5343 				switch (ipf_nat_match(fin, np))
5344 				{
5345 				case 0 :
5346 					continue;
5347 				case -1 :
5348 					rval = -3;
5349 					goto inmatchfail;
5350 				case 1 :
5351 				default :
5352 					break;
5353 				}
5354 			} else {
5355 				if ((in.s_addr & np->in_odstmsk) !=
5356 				    np->in_odstaddr)
5357 					continue;
5358 				if (np->in_odport &&
5359 				    ((np->in_dtop < dport) ||
5360 				     (dport < np->in_odport)))
5361 					continue;
5362 			}
5363 
5364 			if (np->in_plabel != -1) {
5365 				if (!ipf_proxy_ok(fin, tcp, np)) {
5366 					continue;
5367 				}
5368 			}
5369 
5370 			if (np->in_flags & IPN_NO) {
5371 				np->in_hits++;
5372 				break;
5373 			}
5374 
5375 			MUTEX_ENTER(&softn->ipf_nat_new);
5376 			/*
5377 			 * If we've matched a round-robin rule but it has
5378 			 * moved in the list since we got it, start over as
5379 			 * this is now no longer correct.
5380 			 */
5381 			if (npnext != np->in_rnext) {
5382 				if ((np->in_flags & IPN_ROUNDR) != 0) {
5383 					MUTEX_EXIT(&softn->ipf_nat_new);
5384 					goto retry_roundrobin;
5385 				}
5386 				npnext = np->in_rnext;
5387 			}
5388 
5389 			nat = ipf_nat_add(fin, np, NULL, nflags, NAT_INBOUND);
5390 			MUTEX_EXIT(&softn->ipf_nat_new);
5391 			if (nat != NULL) {
5392 				natfailed = 0;
5393 				break;
5394 			}
5395 			natfailed = -2;
5396 		}
5397 		if ((np == NULL) && (rmsk < softn->ipf_nat_rdr_max)) {
5398 			rmsk++;
5399 			goto maskloop;
5400 		}
5401 	}
5402 
5403 	if (nat != NULL) {
5404 		rval = ipf_nat_in(fin, nat, natadd, nflags);
5405 		if (rval == 1) {
5406 			MUTEX_ENTER(&nat->nat_lock);
5407 			ipf_nat_update(fin, nat);
5408 			nat->nat_bytes[0] += fin->fin_plen;
5409 			nat->nat_pkts[0]++;
5410 			fin->fin_pktnum = nat->nat_pkts[0];
5411 			MUTEX_EXIT(&nat->nat_lock);
5412 		}
5413 	} else
5414 		rval = natfailed;
5415 inmatchfail:
5416 	RWLOCK_EXIT(&softc->ipf_nat);
5417 
5418 	DT2(frb_natv4in, fr_info_t *, fin, int, rval);
5419 	switch (rval)
5420 	{
5421 	case -3 :
5422 		/* ipf_nat_match() failure */
5423 		/* FALLTHROUGH */
5424 	case -2 :
5425 		/* retry_roundrobin loop failure */
5426 		/* FALLTHROUGH */
5427 	case -1 :
5428 		/* proxy failure detected by ipf_nat_in() */
5429 		if (passp != NULL) {
5430 			NBUMPSIDED(0, ns_drop);
5431 			*passp = FR_BLOCK;
5432 			fin->fin_reason = FRB_NATV4;
5433 		}
5434 		fin->fin_flx |= FI_BADNAT;
5435 		NBUMPSIDED(0, ns_badnat);
5436 		rval = -1;	/* We only return -1 on error. */
5437 		break;
5438 	case 0 :
5439 		NBUMPSIDE(0, ns_ignored);
5440 		break;
5441 	case 1 :
5442 		NBUMPSIDE(0, ns_translated);
5443 		break;
5444 	}
5445 	return (rval);
5446 }
5447 
5448 
5449 /* ------------------------------------------------------------------------ */
5450 /* Function:    ipf_nat_in                                                  */
5451 /* Returns:     int - -1 == packet failed NAT checks so block it,           */
5452 /*                     1 == packet was successfully translated.             */
5453 /* Parameters:  fin(I)    - pointer to packet information                   */
5454 /*              nat(I)    - pointer to NAT structure                        */
5455 /*              natadd(I) - flag indicating if it is safe to add frag cache */
5456 /*              nflags(I) - NAT flags set for this packet                   */
5457 /* Locks Held:  ipf_nat(READ)                                               */
5458 /*                                                                          */
5459 /* Translate a packet coming "in" on an interface.                          */
5460 /* ------------------------------------------------------------------------ */
5461 int
ipf_nat_in(fr_info_t * fin,nat_t * nat,int natadd,u_32_t nflags)5462 ipf_nat_in(fr_info_t *fin, nat_t *nat, int natadd, u_32_t nflags)
5463 {
5464 	ipf_main_softc_t *softc = fin->fin_main_soft;
5465 	ipf_nat_softc_t *softn = softc->ipf_nat_soft;
5466 	u_32_t sumd, ipsumd, sum1, sum2;
5467 	icmphdr_t *icmp;
5468 	tcphdr_t *tcp;
5469 	ipnat_t *np;
5470 	int skip;
5471 	int i;
5472 
5473 	tcp = NULL;
5474 	np = nat->nat_ptr;
5475 	fin->fin_fr = nat->nat_fr;
5476 
5477 	if (np != NULL) {
5478 		if ((natadd != 0) && (fin->fin_flx & FI_FRAG))
5479 			(void) ipf_frag_natnew(softc, fin, 0, nat);
5480 
5481 	/* ------------------------------------------------------------- */
5482 	/* A few quick notes:                                            */
5483 	/*      Following are test conditions prior to calling the       */
5484 	/*      ipf_proxy_check routine.                                 */
5485 	/*                                                               */
5486 	/*      A NULL tcp indicates a non TCP/UDP packet.  When dealing */
5487 	/*      with a map rule, we attempt to match the packet's        */
5488 	/*      source port against in_dport, otherwise we'd compare the */
5489 	/*      packet's destination.                                    */
5490 	/* ------------------------------------------------------------- */
5491 		if (np->in_apr != NULL) {
5492 			i = ipf_proxy_check(fin, nat);
5493 			if (i == -1) {
5494 				NBUMPSIDED(0, ns_ipf_proxy_fail);
5495 				return (-1);
5496 			}
5497 		}
5498 	}
5499 
5500 	ipf_sync_update(softc, SMC_NAT, fin, nat->nat_sync);
5501 
5502 	ipsumd = nat->nat_ipsumd;
5503 	/*
5504 	 * Fix up checksums, not by recalculating them, but
5505 	 * simply computing adjustments.
5506 	 * Why only do this for some platforms on inbound packets ?
5507 	 * Because for those that it is done, IP processing is yet to happen
5508 	 * and so the IPv4 header checksum has not yet been evaluated.
5509 	 * Perhaps it should always be done for the benefit of things like
5510 	 * fast forwarding (so that it doesn't need to be recomputed) but with
5511 	 * header checksum offloading, perhaps it is a moot point.
5512 	 */
5513 
5514 	switch (nat->nat_dir)
5515 	{
5516 	case NAT_INBOUND :
5517 		if ((fin->fin_flx & FI_ICMPERR) == 0) {
5518 			fin->fin_ip->ip_src = nat->nat_nsrcip;
5519 			fin->fin_saddr = nat->nat_nsrcaddr;
5520 		} else {
5521 			sum1 = nat->nat_osrcaddr;
5522 			sum2 = nat->nat_nsrcaddr;
5523 			CALC_SUMD(sum1, sum2, sumd);
5524 			ipsumd -= sumd;
5525 		}
5526 		fin->fin_ip->ip_dst = nat->nat_ndstip;
5527 		fin->fin_daddr = nat->nat_ndstaddr;
5528 #if !defined(_KERNEL) || SOLARIS
5529 		ipf_fix_outcksum(0, &fin->fin_ip->ip_sum, ipsumd, 0);
5530 #endif
5531 		break;
5532 
5533 	case NAT_OUTBOUND :
5534 		if ((fin->fin_flx & FI_ICMPERR) == 0) {
5535 			fin->fin_ip->ip_src = nat->nat_odstip;
5536 			fin->fin_saddr = nat->nat_odstaddr;
5537 		} else {
5538 			sum1 = nat->nat_odstaddr;
5539 			sum2 = nat->nat_ndstaddr;
5540 			CALC_SUMD(sum1, sum2, sumd);
5541 			ipsumd -= sumd;
5542 		}
5543 		fin->fin_ip->ip_dst = nat->nat_osrcip;
5544 		fin->fin_daddr = nat->nat_osrcaddr;
5545 #if !defined(_KERNEL) || SOLARIS
5546 		ipf_fix_incksum(0, &fin->fin_ip->ip_sum, ipsumd, 0);
5547 #endif
5548 		break;
5549 
5550 	case NAT_DIVERTIN :
5551 	    {
5552 		udphdr_t *uh;
5553 		ip_t *ip;
5554 		mb_t *m;
5555 
5556 		m = M_DUP(np->in_divmp);
5557 		if (m == NULL) {
5558 			NBUMPSIDED(0, ns_divert_dup);
5559 			return (-1);
5560 		}
5561 
5562 		ip = MTOD(m, ip_t *);
5563 		ip_fillid(ip);
5564 		sum1 = ntohs(ip->ip_len);
5565 		ip->ip_len = ntohs(ip->ip_len);
5566 		ip->ip_len += fin->fin_plen;
5567 		ip->ip_len = htons(ip->ip_len);
5568 
5569 		uh = (udphdr_t *)(ip + 1);
5570 		uh->uh_ulen += fin->fin_plen;
5571 		uh->uh_ulen = htons(uh->uh_ulen);
5572 
5573 		sum2 = ntohs(ip->ip_id) + ntohs(ip->ip_len);
5574 		sum2 += ntohs(ip->ip_off) & IP_DF;
5575 		CALC_SUMD(sum1, sum2, sumd);
5576 
5577 #if !defined(_KERNEL) || SOLARIS
5578 		ipf_fix_outcksum(0, &ip->ip_sum, sumd, 0);
5579 #endif
5580 		PREP_MB_T(fin, m);
5581 
5582 		fin->fin_ip = ip;
5583 		fin->fin_plen += sizeof(ip_t) + 8;	/* UDP + new IPv4 hdr */
5584 		fin->fin_dlen += sizeof(ip_t) + 8;	/* UDP + old IPv4 hdr */
5585 
5586 		nflags &= ~IPN_TCPUDPICMP;
5587 
5588 		break;
5589 	    }
5590 
5591 	case NAT_DIVERTOUT :
5592 	    {
5593 		mb_t *m;
5594 
5595 		skip = ipf_nat_decap(fin, nat);
5596 		if (skip <= 0) {
5597 			NBUMPSIDED(0, ns_decap_fail);
5598 			return (-1);
5599 		}
5600 
5601 		m = fin->fin_m;
5602 
5603 #if SOLARIS && defined(_KERNEL)
5604 		m->b_rptr += skip;
5605 #else
5606 		m->m_data += skip;
5607 		m->m_len -= skip;
5608 
5609 # ifdef M_PKTHDR
5610 		if (m->m_flags & M_PKTHDR)
5611 			m->m_pkthdr.len -= skip;
5612 # endif
5613 #endif
5614 
5615 		ipf_nat_update(fin, nat);
5616 		nflags &= ~IPN_TCPUDPICMP;
5617 		fin->fin_flx |= FI_NATED;
5618 		if (np != NULL && np->in_tag.ipt_num[0] != 0)
5619 			fin->fin_nattag = &np->in_tag;
5620 		return (1);
5621 		/* NOTREACHED */
5622 	    }
5623 	}
5624 	if (nflags & IPN_TCPUDP)
5625 		tcp = fin->fin_dp;
5626 
5627 	if (!(fin->fin_flx & FI_SHORT) && (fin->fin_off == 0)) {
5628 		u_short *csump;
5629 
5630 		if ((nat->nat_odport != 0) && (nflags & IPN_TCPUDP)) {
5631 			switch (nat->nat_dir)
5632 			{
5633 			case NAT_INBOUND :
5634 				tcp->th_sport = nat->nat_nsport;
5635 				fin->fin_data[0] = ntohs(nat->nat_nsport);
5636 				tcp->th_dport = nat->nat_ndport;
5637 				fin->fin_data[1] = ntohs(nat->nat_ndport);
5638 				break;
5639 
5640 			case NAT_OUTBOUND :
5641 				tcp->th_sport = nat->nat_odport;
5642 				fin->fin_data[0] = ntohs(nat->nat_odport);
5643 				tcp->th_dport = nat->nat_osport;
5644 				fin->fin_data[1] = ntohs(nat->nat_osport);
5645 				break;
5646 			}
5647 		}
5648 
5649 
5650 		if ((nat->nat_odport != 0) && (nflags & IPN_ICMPQUERY)) {
5651 			icmp = fin->fin_dp;
5652 
5653 			icmp->icmp_id = nat->nat_nicmpid;
5654 		}
5655 
5656 		csump = ipf_nat_proto(fin, nat, nflags);
5657 
5658 		/*
5659 		 * The above comments do not hold for layer 4 (or higher)
5660 		 * checksums...
5661 		 */
5662 		if (csump != NULL) {
5663 			if (nat->nat_dir == NAT_OUTBOUND)
5664 				ipf_fix_incksum(0, csump, nat->nat_sumd[0], 0);
5665 			else
5666 				ipf_fix_outcksum(0, csump, nat->nat_sumd[0], 0);
5667 		}
5668 	}
5669 
5670 	fin->fin_flx |= FI_NATED;
5671 	if (np != NULL && np->in_tag.ipt_num[0] != 0)
5672 		fin->fin_nattag = &np->in_tag;
5673 	return (1);
5674 }
5675 
5676 
5677 /* ------------------------------------------------------------------------ */
5678 /* Function:    ipf_nat_proto                                               */
5679 /* Returns:     u_short* - pointer to transport header checksum to update,  */
5680 /*                         NULL if the transport protocol is not recognised */
5681 /*                         as needing a checksum update.                    */
5682 /* Parameters:  fin(I)    - pointer to packet information                   */
5683 /*              nat(I)    - pointer to NAT structure                        */
5684 /*              nflags(I) - NAT flags set for this packet                   */
5685 /*                                                                          */
5686 /* Return the pointer to the checksum field for each protocol so understood.*/
5687 /* If support for making other changes to a protocol header is required,    */
5688 /* that is not strictly 'address' translation, such as clamping the MSS in  */
5689 /* TCP down to a specific value, then do it from here.                      */
5690 /* ------------------------------------------------------------------------ */
5691 u_short *
ipf_nat_proto(fr_info_t * fin,nat_t * nat,u_int nflags)5692 ipf_nat_proto(fr_info_t *fin, nat_t *nat, u_int nflags)
5693 {
5694 	icmphdr_t *icmp;
5695 	u_short *csump;
5696 	tcphdr_t *tcp;
5697 	udphdr_t *udp;
5698 
5699 	csump = NULL;
5700 	if (fin->fin_out == 0) {
5701 		fin->fin_rev = (nat->nat_dir & NAT_OUTBOUND);
5702 	} else {
5703 		fin->fin_rev = ((nat->nat_dir & NAT_OUTBOUND) == 0);
5704 	}
5705 
5706 	switch (fin->fin_p)
5707 	{
5708 	case IPPROTO_TCP :
5709 		tcp = fin->fin_dp;
5710 
5711 		if ((nflags & IPN_TCP) != 0)
5712 			csump = &tcp->th_sum;
5713 
5714 		/*
5715 		 * Do a MSS CLAMPING on a SYN packet,
5716 		 * only deal IPv4 for now.
5717 		 */
5718 		if ((nat->nat_mssclamp != 0) && (tcp->th_flags & TH_SYN) != 0)
5719 			ipf_nat_mssclamp(tcp, nat->nat_mssclamp, fin, csump);
5720 
5721 		break;
5722 
5723 	case IPPROTO_UDP :
5724 		udp = fin->fin_dp;
5725 
5726 		if ((nflags & IPN_UDP) != 0) {
5727 			if (udp->uh_sum != 0)
5728 				csump = &udp->uh_sum;
5729 		}
5730 		break;
5731 
5732 	case IPPROTO_ICMP :
5733 		icmp = fin->fin_dp;
5734 
5735 		if ((nflags & IPN_ICMPQUERY) != 0) {
5736 			if (icmp->icmp_cksum != 0)
5737 				csump = &icmp->icmp_cksum;
5738 		}
5739 		break;
5740 
5741 #ifdef USE_INET6
5742 	case IPPROTO_ICMPV6 :
5743 	    {
5744 		struct icmp6_hdr *icmp6 = (struct icmp6_hdr *)fin->fin_dp;
5745 
5746 		icmp6 = fin->fin_dp;
5747 
5748 		if ((nflags & IPN_ICMPQUERY) != 0) {
5749 			if (icmp6->icmp6_cksum != 0)
5750 				csump = &icmp6->icmp6_cksum;
5751 		}
5752 		break;
5753 	    }
5754 #endif
5755 	}
5756 	return (csump);
5757 }
5758 
5759 
5760 /* ------------------------------------------------------------------------ */
5761 /* Function:    ipf_nat_expire                                              */
5762 /* Returns:     Nil                                                         */
5763 /* Parameters:  softc(I) - pointer to soft context main structure           */
5764 /*                                                                          */
5765 /* Check all of the timeout queues for entries at the top which need to be  */
5766 /* expired.                                                                 */
5767 /* ------------------------------------------------------------------------ */
5768 void
ipf_nat_expire(ipf_main_softc_t * softc)5769 ipf_nat_expire(ipf_main_softc_t *softc)
5770 {
5771 	ipf_nat_softc_t *softn = softc->ipf_nat_soft;
5772 	ipftq_t *ifq, *ifqnext;
5773 	ipftqent_t *tqe, *tqn;
5774 	int i;
5775 	SPL_INT(s);
5776 
5777 	SPL_NET(s);
5778 	WRITE_ENTER(&softc->ipf_nat);
5779 	for (ifq = softn->ipf_nat_tcptq, i = 0; ifq != NULL;
5780 	     ifq = ifq->ifq_next) {
5781 		for (tqn = ifq->ifq_head; ((tqe = tqn) != NULL); i++) {
5782 			if (tqe->tqe_die > softc->ipf_ticks)
5783 				break;
5784 			tqn = tqe->tqe_next;
5785 			ipf_nat_delete(softc, tqe->tqe_parent, NL_EXPIRE);
5786 		}
5787 	}
5788 
5789 	for (ifq = softn->ipf_nat_utqe; ifq != NULL; ifq = ifq->ifq_next) {
5790 		for (tqn = ifq->ifq_head; ((tqe = tqn) != NULL); i++) {
5791 			if (tqe->tqe_die > softc->ipf_ticks)
5792 				break;
5793 			tqn = tqe->tqe_next;
5794 			ipf_nat_delete(softc, tqe->tqe_parent, NL_EXPIRE);
5795 		}
5796 	}
5797 
5798 	for (ifq = softn->ipf_nat_utqe; ifq != NULL; ifq = ifqnext) {
5799 		ifqnext = ifq->ifq_next;
5800 
5801 		if (((ifq->ifq_flags & IFQF_DELETE) != 0) &&
5802 		    (ifq->ifq_ref == 0)) {
5803 			ipf_freetimeoutqueue(softc, ifq);
5804 		}
5805 	}
5806 
5807 	if (softn->ipf_nat_doflush != 0) {
5808 		ipf_nat_extraflush(softc, softn, 2);
5809 		softn->ipf_nat_doflush = 0;
5810 	}
5811 
5812 	RWLOCK_EXIT(&softc->ipf_nat);
5813 	SPL_X(s);
5814 }
5815 
5816 
5817 /* ------------------------------------------------------------------------ */
5818 /* Function:    ipf_nat_sync                                                */
5819 /* Returns:     Nil                                                         */
5820 /* Parameters:  softc(I) - pointer to soft context main structure           */
5821 /*              ifp(I) - pointer to network interface                       */
5822 /*                                                                          */
5823 /* Walk through all of the currently active NAT sessions, looking for those */
5824 /* which need to have their translated address updated.                     */
5825 /* ------------------------------------------------------------------------ */
5826 void
ipf_nat_sync(ipf_main_softc_t * softc,void * ifp)5827 ipf_nat_sync(ipf_main_softc_t *softc, void *ifp)
5828 {
5829 	ipf_nat_softc_t *softn = softc->ipf_nat_soft;
5830 	u_32_t sum1, sum2, sumd;
5831 	i6addr_t in;
5832 	ipnat_t *n;
5833 	nat_t *nat;
5834 	void *ifp2;
5835 	int idx;
5836 	SPL_INT(s);
5837 
5838 	if (softc->ipf_running <= 0)
5839 		return;
5840 
5841 	/*
5842 	 * Change IP addresses for NAT sessions for any protocol except TCP
5843 	 * since it will break the TCP connection anyway.  The only rules
5844 	 * which will get changed are those which are "map ... -> 0/32",
5845 	 * where the rule specifies the address is taken from the interface.
5846 	 */
5847 	SPL_NET(s);
5848 	WRITE_ENTER(&softc->ipf_nat);
5849 
5850 	if (softc->ipf_running <= 0) {
5851 		RWLOCK_EXIT(&softc->ipf_nat);
5852 		return;
5853 	}
5854 
5855 	for (nat = softn->ipf_nat_instances; nat; nat = nat->nat_next) {
5856 		if ((nat->nat_flags & IPN_TCP) != 0)
5857 			continue;
5858 
5859 		n = nat->nat_ptr;
5860 		if (n != NULL) {
5861 			if (n->in_v[1] == 4) {
5862 				if (n->in_redir & NAT_MAP) {
5863 					if ((n->in_nsrcaddr != 0) ||
5864 					    (n->in_nsrcmsk != 0xffffffff))
5865 						continue;
5866 				} else if (n->in_redir & NAT_REDIRECT) {
5867 					if ((n->in_ndstaddr != 0) ||
5868 					    (n->in_ndstmsk != 0xffffffff))
5869 						continue;
5870 				}
5871 			}
5872 #ifdef USE_INET6
5873 			if (n->in_v[1] == 4) {
5874 				if (n->in_redir & NAT_MAP) {
5875 					if (!IP6_ISZERO(&n->in_nsrcaddr) ||
5876 					    !IP6_ISONES(&n->in_nsrcmsk))
5877 						continue;
5878 				} else if (n->in_redir & NAT_REDIRECT) {
5879 					if (!IP6_ISZERO(&n->in_ndstaddr) ||
5880 					    !IP6_ISONES(&n->in_ndstmsk))
5881 						continue;
5882 				}
5883 			}
5884 #endif
5885 		}
5886 
5887 		if (((ifp == NULL) || (ifp == nat->nat_ifps[0]) ||
5888 		     (ifp == nat->nat_ifps[1]))) {
5889 			nat->nat_ifps[0] = GETIFP(nat->nat_ifnames[0],
5890 						  nat->nat_v[0]);
5891 			if ((nat->nat_ifps[0] != NULL) &&
5892 			    (nat->nat_ifps[0] != (void *)-1)) {
5893 				nat->nat_mtu[0] = GETIFMTU_4(nat->nat_ifps[0]);
5894 			}
5895 			if (nat->nat_ifnames[1][0] != '\0') {
5896 				nat->nat_ifps[1] = GETIFP(nat->nat_ifnames[1],
5897 							  nat->nat_v[1]);
5898 			} else {
5899 				nat->nat_ifps[1] = nat->nat_ifps[0];
5900 			}
5901 			if ((nat->nat_ifps[1] != NULL) &&
5902 			    (nat->nat_ifps[1] != (void *)-1)) {
5903 				nat->nat_mtu[1] = GETIFMTU_4(nat->nat_ifps[1]);
5904 			}
5905 			ifp2 = nat->nat_ifps[0];
5906 			if (ifp2 == NULL)
5907 				continue;
5908 
5909 			/*
5910 			 * Change the map-to address to be the same as the
5911 			 * new one.
5912 			 */
5913 			sum1 = NATFSUM(nat, nat->nat_v[1], nat_nsrc6);
5914 			if (ipf_ifpaddr(softc, nat->nat_v[0], FRI_NORMAL, ifp2,
5915 				       &in, NULL) != -1) {
5916 				if (nat->nat_v[0] == 4)
5917 					nat->nat_nsrcip = in.in4;
5918 			}
5919 			sum2 = NATFSUM(nat, nat->nat_v[1], nat_nsrc6);
5920 
5921 			if (sum1 == sum2)
5922 				continue;
5923 			/*
5924 			 * Readjust the checksum adjustment to take into
5925 			 * account the new IP#.
5926 			 */
5927 			CALC_SUMD(sum1, sum2, sumd);
5928 			/* XXX - dont change for TCP when solaris does
5929 			 * hardware checksumming.
5930 			 */
5931 			sumd += nat->nat_sumd[0];
5932 			nat->nat_sumd[0] = (sumd & 0xffff) + (sumd >> 16);
5933 			nat->nat_sumd[1] = nat->nat_sumd[0];
5934 		}
5935 	}
5936 
5937 	for (n = softn->ipf_nat_list; (n != NULL); n = n->in_next) {
5938 		char *base = n->in_names;
5939 
5940 		if ((ifp == NULL) || (n->in_ifps[0] == ifp))
5941 			n->in_ifps[0] = ipf_resolvenic(softc,
5942 						       base + n->in_ifnames[0],
5943 						       n->in_v[0]);
5944 		if ((ifp == NULL) || (n->in_ifps[1] == ifp))
5945 			n->in_ifps[1] = ipf_resolvenic(softc,
5946 						       base + n->in_ifnames[1],
5947 						       n->in_v[1]);
5948 
5949 		if (n->in_redir & NAT_REDIRECT)
5950 			idx = 1;
5951 		else
5952 			idx = 0;
5953 
5954 		if (((ifp == NULL) || (n->in_ifps[idx] == ifp)) &&
5955 		    (n->in_ifps[idx] != NULL &&
5956 		     n->in_ifps[idx] != (void *)-1)) {
5957 
5958 			ipf_nat_nextaddrinit(softc, n->in_names, &n->in_osrc,
5959 					     0, n->in_ifps[idx]);
5960 			ipf_nat_nextaddrinit(softc, n->in_names, &n->in_odst,
5961 					     0, n->in_ifps[idx]);
5962 			ipf_nat_nextaddrinit(softc, n->in_names, &n->in_nsrc,
5963 					     0, n->in_ifps[idx]);
5964 			ipf_nat_nextaddrinit(softc, n->in_names, &n->in_ndst,
5965 					     0, n->in_ifps[idx]);
5966 		}
5967 	}
5968 	RWLOCK_EXIT(&softc->ipf_nat);
5969 	SPL_X(s);
5970 }
5971 
5972 
5973 /* ------------------------------------------------------------------------ */
5974 /* Function:    ipf_nat_icmpquerytype                                       */
5975 /* Returns:     int - 1 == success, 0 == failure                            */
5976 /* Parameters:  icmptype(I) - ICMP type number                              */
5977 /*                                                                          */
5978 /* Tests to see if the ICMP type number passed is a query/response type or  */
5979 /* not.                                                                     */
5980 /* ------------------------------------------------------------------------ */
5981 static int
ipf_nat_icmpquerytype(int icmptype)5982 ipf_nat_icmpquerytype(int icmptype)
5983 {
5984 
5985 	/*
5986 	 * For the ICMP query NAT code, it is essential that both the query
5987 	 * and the reply match on the NAT rule. Because the NAT structure
5988 	 * does not keep track of the icmptype, and a single NAT structure
5989 	 * is used for all icmp types with the same src, dest and id, we
5990 	 * simply define the replies as queries as well. The funny thing is,
5991 	 * altough it seems silly to call a reply a query, this is exactly
5992 	 * as it is defined in the IPv4 specification
5993 	 */
5994 	switch (icmptype)
5995 	{
5996 	case ICMP_ECHOREPLY:
5997 	case ICMP_ECHO:
5998 	/* route advertisement/solicitation is currently unsupported: */
5999 	/* it would require rewriting the ICMP data section          */
6000 	case ICMP_TSTAMP:
6001 	case ICMP_TSTAMPREPLY:
6002 	case ICMP_IREQ:
6003 	case ICMP_IREQREPLY:
6004 	case ICMP_MASKREQ:
6005 	case ICMP_MASKREPLY:
6006 		return (1);
6007 	default:
6008 		return (0);
6009 	}
6010 }
6011 
6012 
6013 /* ------------------------------------------------------------------------ */
6014 /* Function:    nat_log                                                     */
6015 /* Returns:     Nil                                                         */
6016 /* Parameters:  softc(I) - pointer to soft context main structure           */
6017 /*              softn(I) - pointer to NAT context structure                 */
6018 /*              nat(I)    - pointer to NAT structure                        */
6019 /*              action(I) - action related to NAT structure being performed */
6020 /*                                                                          */
6021 /* Creates a NAT log entry.                                                 */
6022 /* ------------------------------------------------------------------------ */
6023 void
ipf_nat_log(ipf_main_softc_t * softc,ipf_nat_softc_t * softn,struct nat * nat,u_int action)6024 ipf_nat_log(ipf_main_softc_t *softc, ipf_nat_softc_t *softn, struct nat *nat,
6025 	u_int action)
6026 {
6027 #ifdef	IPFILTER_LOG
6028 	struct ipnat *np;
6029 	int rulen;
6030 	struct natlog natl;
6031 	void *items[1];
6032 	size_t sizes[1];
6033 	int types[1];
6034 
6035 	bcopy((char *)&nat->nat_osrc6, (char *)&natl.nl_osrcip,
6036 	      sizeof(natl.nl_osrcip));
6037 	bcopy((char *)&nat->nat_nsrc6, (char *)&natl.nl_nsrcip,
6038 	      sizeof(natl.nl_nsrcip));
6039 	bcopy((char *)&nat->nat_odst6, (char *)&natl.nl_odstip,
6040 	      sizeof(natl.nl_odstip));
6041 	bcopy((char *)&nat->nat_ndst6, (char *)&natl.nl_ndstip,
6042 	      sizeof(natl.nl_ndstip));
6043 
6044 	natl.nl_bytes[0] = nat->nat_bytes[0];
6045 	natl.nl_bytes[1] = nat->nat_bytes[1];
6046 	natl.nl_pkts[0] = nat->nat_pkts[0];
6047 	natl.nl_pkts[1] = nat->nat_pkts[1];
6048 	natl.nl_odstport = nat->nat_odport;
6049 	natl.nl_osrcport = nat->nat_osport;
6050 	natl.nl_nsrcport = nat->nat_nsport;
6051 	natl.nl_ndstport = nat->nat_ndport;
6052 	natl.nl_p[0] = nat->nat_pr[0];
6053 	natl.nl_p[1] = nat->nat_pr[1];
6054 	natl.nl_v[0] = nat->nat_v[0];
6055 	natl.nl_v[1] = nat->nat_v[1];
6056 	natl.nl_type = nat->nat_redir;
6057 	natl.nl_action = action;
6058 	natl.nl_rule = -1;
6059 
6060 	bcopy(nat->nat_ifnames[0], natl.nl_ifnames[0],
6061 	      sizeof(nat->nat_ifnames[0]));
6062 	bcopy(nat->nat_ifnames[1], natl.nl_ifnames[1],
6063 	      sizeof(nat->nat_ifnames[1]));
6064 
6065 	if (softc->ipf_large_nat && nat->nat_ptr != NULL) {
6066 		for (rulen = 0, np = softn->ipf_nat_list; np != NULL;
6067 		     np = np->in_next, rulen++)
6068 			if (np == nat->nat_ptr) {
6069 				natl.nl_rule = rulen;
6070 				break;
6071 			}
6072 	}
6073 	items[0] = &natl;
6074 	sizes[0] = sizeof(natl);
6075 	types[0] = 0;
6076 
6077 	(void) ipf_log_items(softc, IPL_LOGNAT, NULL, items, sizes, types, 1);
6078 #endif
6079 }
6080 
6081 
6082 
6083 
6084 /* ------------------------------------------------------------------------ */
6085 /* Function:    ipf_nat_rule_deref                                          */
6086 /* Returns:     Nil                                                         */
6087 /* Parameters:  softc(I) - pointer to soft context main structure           */
6088 /*              inp(I)   - pointer to pointer to NAT rule                   */
6089 /* Write Locks: ipf_nat                                                     */
6090 /*                                                                          */
6091 /* Dropping the refernce count for a rule means that whatever held the      */
6092 /* pointer to this rule (*inp) is no longer interested in it and when the   */
6093 /* reference count drops to zero, any resources allocated for the rule can  */
6094 /* be released and the rule itself free'd.                                  */
6095 /* ------------------------------------------------------------------------ */
6096 void
ipf_nat_rule_deref(ipf_main_softc_t * softc,ipnat_t ** inp)6097 ipf_nat_rule_deref(ipf_main_softc_t *softc, ipnat_t **inp)
6098 {
6099 	ipf_nat_softc_t *softn = softc->ipf_nat_soft;
6100 	ipnat_t *n;
6101 
6102 	n = *inp;
6103 	*inp = NULL;
6104 	n->in_use--;
6105 	if (n->in_use > 0)
6106 		return;
6107 
6108 	if (n->in_apr != NULL)
6109 		ipf_proxy_deref(n->in_apr);
6110 
6111 	ipf_nat_rule_fini(softc, n);
6112 
6113 	if (n->in_redir & NAT_REDIRECT) {
6114 		if ((n->in_flags & IPN_PROXYRULE) == 0) {
6115 			ATOMIC_DEC32(softn->ipf_nat_stats.ns_rules_rdr);
6116 		}
6117 	}
6118 	if (n->in_redir & (NAT_MAP|NAT_MAPBLK)) {
6119 		if ((n->in_flags & IPN_PROXYRULE) == 0) {
6120 			ATOMIC_DEC32(softn->ipf_nat_stats.ns_rules_map);
6121 		}
6122 	}
6123 
6124 	if (n->in_tqehead[0] != NULL) {
6125 		if (ipf_deletetimeoutqueue(n->in_tqehead[0]) == 0) {
6126 			ipf_freetimeoutqueue(softc, n->in_tqehead[0]);
6127 		}
6128 	}
6129 
6130 	if (n->in_tqehead[1] != NULL) {
6131 		if (ipf_deletetimeoutqueue(n->in_tqehead[1]) == 0) {
6132 			ipf_freetimeoutqueue(softc, n->in_tqehead[1]);
6133 		}
6134 	}
6135 
6136 	if ((n->in_flags & IPN_PROXYRULE) == 0) {
6137 		ATOMIC_DEC32(softn->ipf_nat_stats.ns_rules);
6138 	}
6139 
6140 	MUTEX_DESTROY(&n->in_lock);
6141 
6142 	KFREES(n, n->in_size);
6143 
6144 #if SOLARIS && !defined(INSTANCES)
6145 	if (softn->ipf_nat_stats.ns_rules == 0)
6146 		pfil_delayed_copy = 1;
6147 #endif
6148 }
6149 
6150 
6151 /* ------------------------------------------------------------------------ */
6152 /* Function:    ipf_nat_deref                                               */
6153 /* Returns:     Nil                                                         */
6154 /* Parameters:  softc(I) - pointer to soft context main structure           */
6155 /*              natp(I)  - pointer to pointer to NAT table entry            */
6156 /*                                                                          */
6157 /* Decrement the reference counter for this NAT table entry and free it if  */
6158 /* there are no more things using it.                                       */
6159 /*                                                                          */
6160 /* IF nat_ref == 1 when this function is called, then we have an orphan nat */
6161 /* structure *because* it only gets called on paths _after_ nat_ref has been*/
6162 /* incremented.  If nat_ref == 1 then we shouldn't decrement it here        */
6163 /* because nat_delete() will do that and send nat_ref to -1.                */
6164 /*                                                                          */
6165 /* Holding the lock on nat_lock is required to serialise nat_delete() being */
6166 /* called from a NAT flush ioctl with a deref happening because of a packet.*/
6167 /* ------------------------------------------------------------------------ */
6168 void
ipf_nat_deref(ipf_main_softc_t * softc,nat_t ** natp)6169 ipf_nat_deref(ipf_main_softc_t *softc, nat_t **natp)
6170 {
6171 	nat_t *nat;
6172 
6173 	nat = *natp;
6174 	*natp = NULL;
6175 
6176 	MUTEX_ENTER(&nat->nat_lock);
6177 	if (nat->nat_ref > 1) {
6178 		nat->nat_ref--;
6179 		ASSERT(nat->nat_ref >= 0);
6180 		MUTEX_EXIT(&nat->nat_lock);
6181 		return;
6182 	}
6183 	MUTEX_EXIT(&nat->nat_lock);
6184 
6185 	WRITE_ENTER(&softc->ipf_nat);
6186 	ipf_nat_delete(softc, nat, NL_EXPIRE);
6187 	RWLOCK_EXIT(&softc->ipf_nat);
6188 }
6189 
6190 
6191 /* ------------------------------------------------------------------------ */
6192 /* Function:    ipf_nat_clone                                               */
6193 /* Returns:     ipstate_t* - NULL == cloning failed,                        */
6194 /*                           else pointer to new state structure            */
6195 /* Parameters:  fin(I) - pointer to packet information                      */
6196 /*              is(I)  - pointer to master state structure                  */
6197 /* Write Lock:  ipf_nat                                                     */
6198 /*                                                                          */
6199 /* Create a "duplcate" state table entry from the master.                   */
6200 /* ------------------------------------------------------------------------ */
6201 nat_t *
ipf_nat_clone(fr_info_t * fin,nat_t * nat)6202 ipf_nat_clone(fr_info_t *fin, nat_t *nat)
6203 {
6204 	ipf_main_softc_t *softc = fin->fin_main_soft;
6205 	ipf_nat_softc_t *softn = softc->ipf_nat_soft;
6206 	frentry_t *fr;
6207 	nat_t *clone;
6208 	ipnat_t *np;
6209 
6210 	KMALLOC(clone, nat_t *);
6211 	if (clone == NULL) {
6212 		NBUMPSIDED(fin->fin_out, ns_clone_nomem);
6213 		return (NULL);
6214 	}
6215 	bcopy((char *)nat, (char *)clone, sizeof(*clone));
6216 
6217 	MUTEX_NUKE(&clone->nat_lock);
6218 
6219 	clone->nat_rev = fin->fin_rev;
6220 	clone->nat_aps = NULL;
6221 	/*
6222 	 * Initialize all these so that ipf_nat_delete() doesn't cause a crash.
6223 	 */
6224 	clone->nat_tqe.tqe_pnext = NULL;
6225 	clone->nat_tqe.tqe_next = NULL;
6226 	clone->nat_tqe.tqe_ifq = NULL;
6227 	clone->nat_tqe.tqe_parent = clone;
6228 
6229 	clone->nat_flags &= ~SI_CLONE;
6230 	clone->nat_flags |= SI_CLONED;
6231 
6232 	if (clone->nat_hm)
6233 		clone->nat_hm->hm_ref++;
6234 
6235 	if (ipf_nat_insert(softc, softn, clone) == -1) {
6236 		KFREE(clone);
6237 		NBUMPSIDED(fin->fin_out, ns_insert_fail);
6238 		return (NULL);
6239 	}
6240 
6241 	np = clone->nat_ptr;
6242 	if (np != NULL) {
6243 		if (softn->ipf_nat_logging)
6244 			ipf_nat_log(softc, softn, clone, NL_CLONE);
6245 		np->in_use++;
6246 	}
6247 	fr = clone->nat_fr;
6248 	if (fr != NULL) {
6249 		MUTEX_ENTER(&fr->fr_lock);
6250 		fr->fr_ref++;
6251 		MUTEX_EXIT(&fr->fr_lock);
6252 	}
6253 
6254 
6255 	/*
6256 	 * Because the clone is created outside the normal loop of things and
6257 	 * TCP has special needs in terms of state, initialise the timeout
6258 	 * state of the new NAT from here.
6259 	 */
6260 	if (clone->nat_pr[0] == IPPROTO_TCP) {
6261 		(void) ipf_tcp_age(&clone->nat_tqe, fin, softn->ipf_nat_tcptq,
6262 				   clone->nat_flags, 2);
6263 	}
6264 	clone->nat_sync = ipf_sync_new(softc, SMC_NAT, fin, clone);
6265 	if (softn->ipf_nat_logging)
6266 		ipf_nat_log(softc, softn, clone, NL_CLONE);
6267 	return (clone);
6268 }
6269 
6270 
6271 /* ------------------------------------------------------------------------ */
6272 /* Function:   ipf_nat_wildok                                               */
6273 /* Returns:    int - 1 == packet's ports match wildcards                    */
6274 /*                   0 == packet's ports don't match wildcards              */
6275 /* Parameters: nat(I)   - NAT entry                                         */
6276 /*             sport(I) - source port                                       */
6277 /*             dport(I) - destination port                                  */
6278 /*             flags(I) - wildcard flags                                    */
6279 /*             dir(I)   - packet direction                                  */
6280 /*                                                                          */
6281 /* Use NAT entry and packet direction to determine which combination of     */
6282 /* wildcard flags should be used.                                           */
6283 /* ------------------------------------------------------------------------ */
6284 int
ipf_nat_wildok(nat_t * nat,int sport,int dport,int flags,int dir)6285 ipf_nat_wildok(nat_t *nat, int sport, int dport, int flags, int dir)
6286 {
6287 	/*
6288 	 * When called by       dir is set to
6289 	 * nat_inlookup         NAT_INBOUND (0)
6290 	 * nat_outlookup        NAT_OUTBOUND (1)
6291 	 *
6292 	 * We simply combine the packet's direction in dir with the original
6293 	 * "intended" direction of that NAT entry in nat->nat_dir to decide
6294 	 * which combination of wildcard flags to allow.
6295 	 */
6296 	switch ((dir << 1) | (nat->nat_dir & (NAT_INBOUND|NAT_OUTBOUND)))
6297 	{
6298 	case 3: /* outbound packet / outbound entry */
6299 		if (((nat->nat_osport == sport) ||
6300 		    (flags & SI_W_SPORT)) &&
6301 		    ((nat->nat_odport == dport) ||
6302 		    (flags & SI_W_DPORT)))
6303 			return (1);
6304 		break;
6305 	case 2: /* outbound packet / inbound entry */
6306 		if (((nat->nat_osport == dport) ||
6307 		    (flags & SI_W_SPORT)) &&
6308 		    ((nat->nat_odport == sport) ||
6309 		    (flags & SI_W_DPORT)))
6310 			return (1);
6311 		break;
6312 	case 1: /* inbound packet / outbound entry */
6313 		if (((nat->nat_osport == dport) ||
6314 		    (flags & SI_W_SPORT)) &&
6315 		    ((nat->nat_odport == sport) ||
6316 		    (flags & SI_W_DPORT)))
6317 			return (1);
6318 		break;
6319 	case 0: /* inbound packet / inbound entry */
6320 		if (((nat->nat_osport == sport) ||
6321 		    (flags & SI_W_SPORT)) &&
6322 		    ((nat->nat_odport == dport) ||
6323 		    (flags & SI_W_DPORT)))
6324 			return (1);
6325 		break;
6326 	default:
6327 		break;
6328 	}
6329 
6330 	return (0);
6331 }
6332 
6333 
6334 /* ------------------------------------------------------------------------ */
6335 /* Function:    nat_mssclamp                                                */
6336 /* Returns:     Nil                                                         */
6337 /* Parameters:  tcp(I)    - pointer to TCP header                           */
6338 /*              maxmss(I) - value to clamp the TCP MSS to                   */
6339 /*              fin(I)    - pointer to packet information                   */
6340 /*              csump(I)  - pointer to TCP checksum                         */
6341 /*                                                                          */
6342 /* Check for MSS option and clamp it if necessary.  If found and changed,   */
6343 /* then the TCP header checksum will be updated to reflect the change in    */
6344 /* the MSS.                                                                 */
6345 /* ------------------------------------------------------------------------ */
6346 static void
ipf_nat_mssclamp(tcphdr_t * tcp,u_32_t maxmss,fr_info_t * fin,u_short * csump)6347 ipf_nat_mssclamp(tcphdr_t *tcp, u_32_t maxmss, fr_info_t *fin, u_short *csump)
6348 {
6349 	u_char *cp, *ep, opt;
6350 	int hlen, advance;
6351 	u_32_t mss, sumd;
6352 
6353 	hlen = TCP_OFF(tcp) << 2;
6354 	if (hlen > sizeof(*tcp)) {
6355 		cp = (u_char *)tcp + sizeof(*tcp);
6356 		ep = (u_char *)tcp + hlen;
6357 
6358 		while (cp < ep) {
6359 			opt = cp[0];
6360 			if (opt == TCPOPT_EOL)
6361 				break;
6362 			else if (opt == TCPOPT_NOP) {
6363 				cp++;
6364 				continue;
6365 			}
6366 
6367 			if (cp + 1 >= ep)
6368 				break;
6369 			advance = cp[1];
6370 			if ((cp + advance > ep) || (advance <= 0))
6371 				break;
6372 			switch (opt)
6373 			{
6374 			case TCPOPT_MAXSEG:
6375 				if (advance != 4)
6376 					break;
6377 				mss = cp[2] * 256 + cp[3];
6378 				if (mss > maxmss) {
6379 					cp[2] = maxmss / 256;
6380 					cp[3] = maxmss & 0xff;
6381 					CALC_SUMD(mss, maxmss, sumd);
6382 					ipf_fix_outcksum(0, csump, sumd, 0);
6383 				}
6384 				break;
6385 			default:
6386 				/* ignore unknown options */
6387 				break;
6388 			}
6389 
6390 			cp += advance;
6391 		}
6392 	}
6393 }
6394 
6395 
6396 /* ------------------------------------------------------------------------ */
6397 /* Function:    ipf_nat_setqueue                                            */
6398 /* Returns:     Nil                                                         */
6399 /* Parameters:  softc(I) - pointer to soft context main structure           */
6400 /*              softn(I) - pointer to NAT context structure                 */
6401 /*              nat(I)- pointer to NAT structure                            */
6402 /* Locks:       ipf_nat (read or write)                                     */
6403 /*                                                                          */
6404 /* Put the NAT entry on its default queue entry, using rev as a helped in   */
6405 /* determining which queue it should be placed on.                          */
6406 /* ------------------------------------------------------------------------ */
6407 void
ipf_nat_setqueue(ipf_main_softc_t * softc,ipf_nat_softc_t * softn,nat_t * nat)6408 ipf_nat_setqueue(ipf_main_softc_t *softc, ipf_nat_softc_t *softn, nat_t *nat)
6409 {
6410 	ipftq_t *oifq, *nifq;
6411 	int rev = nat->nat_rev;
6412 
6413 	if (nat->nat_ptr != NULL)
6414 		nifq = nat->nat_ptr->in_tqehead[rev];
6415 	else
6416 		nifq = NULL;
6417 
6418 	if (nifq == NULL) {
6419 		switch (nat->nat_pr[0])
6420 		{
6421 		case IPPROTO_UDP :
6422 			nifq = &softn->ipf_nat_udptq;
6423 			break;
6424 		case IPPROTO_ICMP :
6425 			nifq = &softn->ipf_nat_icmptq;
6426 			break;
6427 		case IPPROTO_TCP :
6428 			nifq = softn->ipf_nat_tcptq +
6429 			       nat->nat_tqe.tqe_state[rev];
6430 			break;
6431 		default :
6432 			nifq = &softn->ipf_nat_iptq;
6433 			break;
6434 		}
6435 	}
6436 
6437 	oifq = nat->nat_tqe.tqe_ifq;
6438 	/*
6439 	 * If it's currently on a timeout queue, move it from one queue to
6440 	 * another, else put it on the end of the newly determined queue.
6441 	 */
6442 	if (oifq != NULL)
6443 		ipf_movequeue(softc->ipf_ticks, &nat->nat_tqe, oifq, nifq);
6444 	else
6445 		ipf_queueappend(softc->ipf_ticks, &nat->nat_tqe, nifq, nat);
6446 	return;
6447 }
6448 
6449 
6450 /* ------------------------------------------------------------------------ */
6451 /* Function:    nat_getnext                                                 */
6452 /* Returns:     int - 0 == ok, else error                                   */
6453 /* Parameters:  softc(I) - pointer to soft context main structure           */
6454 /*              t(I)   - pointer to ipftoken structure                      */
6455 /*              itp(I) - pointer to ipfgeniter_t structure                  */
6456 /*                                                                          */
6457 /* Fetch the next nat/ipnat structure pointer from the linked list and      */
6458 /* copy it out to the storage space pointed to by itp_data.  The next item  */
6459 /* in the list to look at is put back in the ipftoken struture.             */
6460 /* ------------------------------------------------------------------------ */
6461 static int
ipf_nat_getnext(ipf_main_softc_t * softc,ipftoken_t * t,ipfgeniter_t * itp,ipfobj_t * objp)6462 ipf_nat_getnext(ipf_main_softc_t *softc, ipftoken_t *t, ipfgeniter_t *itp,
6463 	ipfobj_t *objp)
6464 {
6465 	ipf_nat_softc_t *softn = softc->ipf_nat_soft;
6466 	hostmap_t *hm, *nexthm = NULL, zerohm;
6467 	ipnat_t *ipn, *nextipnat = NULL, zeroipn;
6468 	nat_t *nat, *nextnat = NULL, zeronat;
6469 	int error = 0;
6470 	void *nnext;
6471 
6472 	if (itp->igi_nitems != 1) {
6473 		IPFERROR(60075);
6474 		return (ENOSPC);
6475 	}
6476 
6477 	READ_ENTER(&softc->ipf_nat);
6478 
6479 	switch (itp->igi_type)
6480 	{
6481 	case IPFGENITER_HOSTMAP :
6482 		hm = t->ipt_data;
6483 		if (hm == NULL) {
6484 			nexthm = softn->ipf_hm_maplist;
6485 		} else {
6486 			nexthm = hm->hm_next;
6487 		}
6488 		if (nexthm != NULL) {
6489 			ATOMIC_INC32(nexthm->hm_ref);
6490 			t->ipt_data = nexthm;
6491 		} else {
6492 			bzero(&zerohm, sizeof(zerohm));
6493 			nexthm = &zerohm;
6494 			t->ipt_data = NULL;
6495 		}
6496 		nnext = nexthm->hm_next;
6497 		break;
6498 
6499 	case IPFGENITER_IPNAT :
6500 		ipn = t->ipt_data;
6501 		if (ipn == NULL) {
6502 			nextipnat = softn->ipf_nat_list;
6503 		} else {
6504 			nextipnat = ipn->in_next;
6505 		}
6506 		if (nextipnat != NULL) {
6507 			ATOMIC_INC32(nextipnat->in_use);
6508 			t->ipt_data = nextipnat;
6509 		} else {
6510 			bzero(&zeroipn, sizeof(zeroipn));
6511 			nextipnat = &zeroipn;
6512 			t->ipt_data = NULL;
6513 		}
6514 		nnext = nextipnat->in_next;
6515 		break;
6516 
6517 	case IPFGENITER_NAT :
6518 		nat = t->ipt_data;
6519 		if (nat == NULL) {
6520 			nextnat = softn->ipf_nat_instances;
6521 		} else {
6522 			nextnat = nat->nat_next;
6523 		}
6524 		if (nextnat != NULL) {
6525 			MUTEX_ENTER(&nextnat->nat_lock);
6526 			nextnat->nat_ref++;
6527 			MUTEX_EXIT(&nextnat->nat_lock);
6528 			t->ipt_data = nextnat;
6529 		} else {
6530 			bzero(&zeronat, sizeof(zeronat));
6531 			nextnat = &zeronat;
6532 			t->ipt_data = NULL;
6533 		}
6534 		nnext = nextnat->nat_next;
6535 		break;
6536 
6537 	default :
6538 		RWLOCK_EXIT(&softc->ipf_nat);
6539 		IPFERROR(60055);
6540 		return (EINVAL);
6541 	}
6542 
6543 	RWLOCK_EXIT(&softc->ipf_nat);
6544 
6545 	objp->ipfo_ptr = itp->igi_data;
6546 
6547 	switch (itp->igi_type)
6548 	{
6549 	case IPFGENITER_HOSTMAP :
6550 		error = COPYOUT(nexthm, objp->ipfo_ptr, sizeof(*nexthm));
6551 		if (error != 0) {
6552 			IPFERROR(60049);
6553 			error = EFAULT;
6554 		}
6555 		if (hm != NULL) {
6556 			WRITE_ENTER(&softc->ipf_nat);
6557 			ipf_nat_hostmapdel(softc, &hm);
6558 			RWLOCK_EXIT(&softc->ipf_nat);
6559 		}
6560 		break;
6561 
6562 	case IPFGENITER_IPNAT :
6563 		objp->ipfo_size = nextipnat->in_size;
6564 		objp->ipfo_type = IPFOBJ_IPNAT;
6565 		error = ipf_outobjk(softc, objp, nextipnat);
6566 		if (ipn != NULL) {
6567 			WRITE_ENTER(&softc->ipf_nat);
6568 			ipf_nat_rule_deref(softc, &ipn);
6569 			RWLOCK_EXIT(&softc->ipf_nat);
6570 		}
6571 		break;
6572 
6573 	case IPFGENITER_NAT :
6574 		objp->ipfo_size = sizeof(nat_t);
6575 		objp->ipfo_type = IPFOBJ_NAT;
6576 		error = ipf_outobjk(softc, objp, nextnat);
6577 		if (nat != NULL)
6578 			ipf_nat_deref(softc, &nat);
6579 
6580 		break;
6581 	}
6582 
6583 	if (nnext == NULL)
6584 		ipf_token_mark_complete(t);
6585 
6586 	return (error);
6587 }
6588 
6589 
6590 /* ------------------------------------------------------------------------ */
6591 /* Function:    nat_extraflush                                              */
6592 /* Returns:     int - 0 == success, -1 == failure                           */
6593 /* Parameters:  softc(I) - pointer to soft context main structure           */
6594 /*              softn(I) - pointer to NAT context structure                 */
6595 /*              which(I) - how to flush the active NAT table                */
6596 /* Write Locks: ipf_nat                                                     */
6597 /*                                                                          */
6598 /* Flush nat tables.  Three actions currently defined:                      */
6599 /* which == 0 : flush all nat table entries                                 */
6600 /* which == 1 : flush TCP connections which have started to close but are   */
6601 /*	      stuck for some reason.                                        */
6602 /* which == 2 : flush TCP connections which have been idle for a long time, */
6603 /*	      starting at > 4 days idle and working back in successive half-*/
6604 /*	      days to at most 12 hours old.  If this fails to free enough   */
6605 /*            slots then work backwards in half hour slots to 30 minutes.   */
6606 /*            If that too fails, then work backwards in 30 second intervals */
6607 /*            for the last 30 minutes to at worst 30 seconds idle.          */
6608 /* ------------------------------------------------------------------------ */
6609 static int
ipf_nat_extraflush(ipf_main_softc_t * softc,ipf_nat_softc_t * softn,int which)6610 ipf_nat_extraflush(ipf_main_softc_t *softc, ipf_nat_softc_t *softn, int which)
6611 {
6612 	nat_t *nat, **natp;
6613 	ipftqent_t *tqn;
6614 	ipftq_t *ifq;
6615 	int removed;
6616 	SPL_INT(s);
6617 
6618 	removed = 0;
6619 
6620 	SPL_NET(s);
6621 	switch (which)
6622 	{
6623 	case 0 :
6624 		softn->ipf_nat_stats.ns_flush_all++;
6625 		/*
6626 		 * Style 0 flush removes everything...
6627 		 */
6628 		for (natp = &softn->ipf_nat_instances;
6629 		     ((nat = *natp) != NULL); ) {
6630 			ipf_nat_delete(softc, nat, NL_FLUSH);
6631 			removed++;
6632 		}
6633 		break;
6634 
6635 	case 1 :
6636 		softn->ipf_nat_stats.ns_flush_closing++;
6637 		/*
6638 		 * Since we're only interested in things that are closing,
6639 		 * we can start with the appropriate timeout queue.
6640 		 */
6641 		for (ifq = softn->ipf_nat_tcptq + IPF_TCPS_CLOSE_WAIT;
6642 		     ifq != NULL; ifq = ifq->ifq_next) {
6643 
6644 			for (tqn = ifq->ifq_head; tqn != NULL; ) {
6645 				nat = tqn->tqe_parent;
6646 				tqn = tqn->tqe_next;
6647 				if (nat->nat_pr[0] != IPPROTO_TCP ||
6648 				    nat->nat_pr[1] != IPPROTO_TCP)
6649 					break;
6650 				ipf_nat_delete(softc, nat, NL_EXPIRE);
6651 				removed++;
6652 			}
6653 		}
6654 
6655 		/*
6656 		 * Also need to look through the user defined queues.
6657 		 */
6658 		for (ifq = softn->ipf_nat_utqe; ifq != NULL;
6659 		     ifq = ifq->ifq_next) {
6660 			for (tqn = ifq->ifq_head; tqn != NULL; ) {
6661 				nat = tqn->tqe_parent;
6662 				tqn = tqn->tqe_next;
6663 				if (nat->nat_pr[0] != IPPROTO_TCP ||
6664 				    nat->nat_pr[1] != IPPROTO_TCP)
6665 					continue;
6666 
6667 				if ((nat->nat_tcpstate[0] >
6668 				     IPF_TCPS_ESTABLISHED) &&
6669 				    (nat->nat_tcpstate[1] >
6670 				     IPF_TCPS_ESTABLISHED)) {
6671 					ipf_nat_delete(softc, nat, NL_EXPIRE);
6672 					removed++;
6673 				}
6674 			}
6675 		}
6676 		break;
6677 
6678 		/*
6679 		 * Args 5-11 correspond to flushing those particular states
6680 		 * for TCP connections.
6681 		 */
6682 	case IPF_TCPS_CLOSE_WAIT :
6683 	case IPF_TCPS_FIN_WAIT_1 :
6684 	case IPF_TCPS_CLOSING :
6685 	case IPF_TCPS_LAST_ACK :
6686 	case IPF_TCPS_FIN_WAIT_2 :
6687 	case IPF_TCPS_TIME_WAIT :
6688 	case IPF_TCPS_CLOSED :
6689 		softn->ipf_nat_stats.ns_flush_state++;
6690 		tqn = softn->ipf_nat_tcptq[which].ifq_head;
6691 		while (tqn != NULL) {
6692 			nat = tqn->tqe_parent;
6693 			tqn = tqn->tqe_next;
6694 			ipf_nat_delete(softc, nat, NL_FLUSH);
6695 			removed++;
6696 		}
6697 		break;
6698 
6699 	default :
6700 		if (which < 30)
6701 			break;
6702 
6703 		softn->ipf_nat_stats.ns_flush_timeout++;
6704 		/*
6705 		 * Take a large arbitrary number to mean the number of seconds
6706 		 * for which which consider to be the maximum value we'll allow
6707 		 * the expiration to be.
6708 		 */
6709 		which = IPF_TTLVAL(which);
6710 		for (natp = &softn->ipf_nat_instances;
6711 		     ((nat = *natp) != NULL); ) {
6712 			if (softc->ipf_ticks - nat->nat_touched > which) {
6713 				ipf_nat_delete(softc, nat, NL_FLUSH);
6714 				removed++;
6715 			} else
6716 				natp = &nat->nat_next;
6717 		}
6718 		break;
6719 	}
6720 
6721 	if (which != 2) {
6722 		SPL_X(s);
6723 		return (removed);
6724 	}
6725 
6726 	softn->ipf_nat_stats.ns_flush_queue++;
6727 
6728 	/*
6729 	 * Asked to remove inactive entries because the table is full, try
6730 	 * again, 3 times, if first attempt failed with a different criteria
6731 	 * each time.  The order tried in must be in decreasing age.
6732 	 * Another alternative is to implement random drop and drop N entries
6733 	 * at random until N have been freed up.
6734 	 */
6735 	if (softc->ipf_ticks - softn->ipf_nat_last_force_flush >
6736 	    IPF_TTLVAL(5)) {
6737 		softn->ipf_nat_last_force_flush = softc->ipf_ticks;
6738 
6739 		removed = ipf_queueflush(softc, ipf_nat_flush_entry,
6740 					 softn->ipf_nat_tcptq,
6741 					 softn->ipf_nat_utqe,
6742 					 &softn->ipf_nat_stats.ns_active,
6743 					 softn->ipf_nat_table_sz,
6744 					 softn->ipf_nat_table_wm_low);
6745 	}
6746 
6747 	SPL_X(s);
6748 	return (removed);
6749 }
6750 
6751 
6752 /* ------------------------------------------------------------------------ */
6753 /* Function:    ipf_nat_flush_entry                                         */
6754 /* Returns:     0 - always succeeds                                         */
6755 /* Parameters:  softc(I) - pointer to soft context main structure           */
6756 /*              entry(I) - pointer to NAT entry                             */
6757 /* Write Locks: ipf_nat                                                     */
6758 /*                                                                          */
6759 /* This function is a stepping stone between ipf_queueflush() and           */
6760 /* nat_dlete().  It is used so we can provide a uniform interface via the   */
6761 /* ipf_queueflush() function.  Since the nat_delete() function returns void */
6762 /* we translate that to mean it always succeeds in deleting something.      */
6763 /* ------------------------------------------------------------------------ */
6764 static int
ipf_nat_flush_entry(ipf_main_softc_t * softc,void * entry)6765 ipf_nat_flush_entry(ipf_main_softc_t *softc, void *entry)
6766 {
6767 	ipf_nat_delete(softc, entry, NL_FLUSH);
6768 	return (0);
6769 }
6770 
6771 
6772 /* ------------------------------------------------------------------------ */
6773 /* Function:    ipf_nat_iterator                                            */
6774 /* Returns:     int - 0 == ok, else error                                   */
6775 /* Parameters:  softc(I) - pointer to soft context main structure           */
6776 /*              token(I) - pointer to ipftoken structure                    */
6777 /*              itp(I)   - pointer to ipfgeniter_t structure                */
6778 /*              obj(I)   - pointer to data description structure            */
6779 /*                                                                          */
6780 /* This function acts as a handler for the SIOCGENITER ioctls that use a    */
6781 /* generic structure to iterate through a list.  There are three different  */
6782 /* linked lists of NAT related information to go through: NAT rules, active */
6783 /* NAT mappings and the NAT fragment cache.                                 */
6784 /* ------------------------------------------------------------------------ */
6785 static int
ipf_nat_iterator(ipf_main_softc_t * softc,ipftoken_t * token,ipfgeniter_t * itp,ipfobj_t * obj)6786 ipf_nat_iterator(ipf_main_softc_t *softc, ipftoken_t *token, ipfgeniter_t *itp,
6787 	ipfobj_t *obj)
6788 {
6789 	int error;
6790 
6791 	if (itp->igi_data == NULL) {
6792 		IPFERROR(60052);
6793 		return (EFAULT);
6794 	}
6795 
6796 	switch (itp->igi_type)
6797 	{
6798 	case IPFGENITER_HOSTMAP :
6799 	case IPFGENITER_IPNAT :
6800 	case IPFGENITER_NAT :
6801 		error = ipf_nat_getnext(softc, token, itp, obj);
6802 		break;
6803 
6804 	case IPFGENITER_NATFRAG :
6805 		error = ipf_frag_nat_next(softc, token, itp);
6806 		break;
6807 	default :
6808 		IPFERROR(60053);
6809 		error = EINVAL;
6810 		break;
6811 	}
6812 
6813 	return (error);
6814 }
6815 
6816 
6817 /* ------------------------------------------------------------------------ */
6818 /* Function:    ipf_nat_setpending                                          */
6819 /* Returns:     Nil                                                         */
6820 /* Parameters:  softc(I) - pointer to soft context main structure           */
6821 /*              nat(I)   - pointer to NAT structure                         */
6822 /* Locks:       ipf_nat (read or write)                                     */
6823 /*                                                                          */
6824 /* Put the NAT entry on to the pending queue - this queue has a very short  */
6825 /* lifetime where items are put that can't be deleted straight away because */
6826 /* of locking issues but we want to delete them ASAP, anyway.  In calling   */
6827 /* this function, it is assumed that the owner (if there is one, as shown   */
6828 /* by nat_me) is no longer interested in it.                                */
6829 /* ------------------------------------------------------------------------ */
6830 void
ipf_nat_setpending(ipf_main_softc_t * softc,nat_t * nat)6831 ipf_nat_setpending(ipf_main_softc_t *softc, nat_t *nat)
6832 {
6833 	ipf_nat_softc_t *softn = softc->ipf_nat_soft;
6834 	ipftq_t *oifq;
6835 
6836 	oifq = nat->nat_tqe.tqe_ifq;
6837 	if (oifq != NULL)
6838 		ipf_movequeue(softc->ipf_ticks, &nat->nat_tqe, oifq,
6839 			      &softn->ipf_nat_pending);
6840 	else
6841 		ipf_queueappend(softc->ipf_ticks, &nat->nat_tqe,
6842 				&softn->ipf_nat_pending, nat);
6843 
6844 	if (nat->nat_me != NULL) {
6845 		*nat->nat_me = NULL;
6846 		nat->nat_me = NULL;
6847 		nat->nat_ref--;
6848 		ASSERT(nat->nat_ref >= 0);
6849 	}
6850 }
6851 
6852 
6853 /* ------------------------------------------------------------------------ */
6854 /* Function:    nat_newrewrite                                              */
6855 /* Returns:     int - -1 == error, 0 == success (no move), 1 == success and */
6856 /*                    allow rule to be moved if IPN_ROUNDR is set.          */
6857 /* Parameters:  fin(I) - pointer to packet information                      */
6858 /*              nat(I) - pointer to NAT entry                               */
6859 /*              ni(I)  - pointer to structure with misc. information needed */
6860 /*                       to create new NAT entry.                           */
6861 /* Write Lock:  ipf_nat                                                     */
6862 /*                                                                          */
6863 /* This function is responsible for setting up an active NAT session where  */
6864 /* we are changing both the source and destination parameters at the same   */
6865 /* time.  The loop in here works differently to elsewhere - each iteration  */
6866 /* is responsible for changing a single parameter that can be incremented.  */
6867 /* So one pass may increase the source IP#, next source port, next dest. IP#*/
6868 /* and the last destination port for a total of 4 iterations to try each.   */
6869 /* This is done to try and exhaustively use the translation space available.*/
6870 /* ------------------------------------------------------------------------ */
6871 static int
ipf_nat_newrewrite(fr_info_t * fin,nat_t * nat,natinfo_t * nai)6872 ipf_nat_newrewrite(fr_info_t *fin, nat_t *nat, natinfo_t *nai)
6873 {
6874 	int src_search = 1;
6875 	int dst_search = 1;
6876 	fr_info_t frnat;
6877 	u_32_t flags;
6878 	u_short swap;
6879 	ipnat_t *np;
6880 	nat_t *natl;
6881 	int l = 0;
6882 	int changed;
6883 
6884 	natl = NULL;
6885 	changed = -1;
6886 	np = nai->nai_np;
6887 	flags = nat->nat_flags;
6888 	bcopy((char *)fin, (char *)&frnat, sizeof(*fin));
6889 
6890 	nat->nat_hm = NULL;
6891 
6892 	do {
6893 		changed = -1;
6894 		/* TRACE (l, src_search, dst_search, np) */
6895 		DT4(ipf_nat_rewrite_1, int, l, int, src_search, int, dst_search, ipnat_t *, np);
6896 
6897 		if ((src_search == 0) && (np->in_spnext == 0) &&
6898 		    (dst_search == 0) && (np->in_dpnext == 0)) {
6899 			if (l > 0)
6900 				return (-1);
6901 		}
6902 
6903 		/*
6904 		 * Find a new source address
6905 		 */
6906 		if (ipf_nat_nextaddr(fin, &np->in_nsrc, &frnat.fin_saddr,
6907 				     &frnat.fin_saddr) == -1) {
6908 			return (-1);
6909 		}
6910 
6911 		if ((np->in_nsrcaddr == 0) && (np->in_nsrcmsk == 0xffffffff)) {
6912 			src_search = 0;
6913 			if (np->in_stepnext == 0)
6914 				np->in_stepnext = 1;
6915 
6916 		} else if ((np->in_nsrcaddr == 0) && (np->in_nsrcmsk == 0)) {
6917 			src_search = 0;
6918 			if (np->in_stepnext == 0)
6919 				np->in_stepnext = 1;
6920 
6921 		} else if (np->in_nsrcmsk == 0xffffffff) {
6922 			src_search = 0;
6923 			if (np->in_stepnext == 0)
6924 				np->in_stepnext = 1;
6925 
6926 		} else if (np->in_nsrcmsk != 0xffffffff) {
6927 			if (np->in_stepnext == 0 && changed == -1) {
6928 				np->in_snip++;
6929 				np->in_stepnext++;
6930 				changed = 0;
6931 			}
6932 		}
6933 
6934 		if ((flags & IPN_TCPUDPICMP) != 0) {
6935 			if (np->in_spnext != 0)
6936 				frnat.fin_data[0] = np->in_spnext;
6937 
6938 			/*
6939 			 * Standard port translation.  Select next port.
6940 			 */
6941 			if ((flags & IPN_FIXEDSPORT) != 0) {
6942 				np->in_stepnext = 2;
6943 			} else if ((np->in_stepnext == 1) &&
6944 				   (changed == -1) && (natl != NULL)) {
6945 				np->in_spnext++;
6946 				np->in_stepnext++;
6947 				changed = 1;
6948 				if (np->in_spnext > np->in_spmax)
6949 					np->in_spnext = np->in_spmin;
6950 			}
6951 		} else {
6952 			np->in_stepnext = 2;
6953 		}
6954 		np->in_stepnext &= 0x3;
6955 
6956 		/*
6957 		 * Find a new destination address
6958 		 */
6959 		/* TRACE (fin, np, l, frnat) */
6960 		DT4(ipf_nat_rewrite_2, frinfo_t *, fin, ipnat_t *, np, int, l, frinfo_t *, &frnat);
6961 
6962 		if (ipf_nat_nextaddr(fin, &np->in_ndst, &frnat.fin_daddr,
6963 				     &frnat.fin_daddr) == -1)
6964 			return (-1);
6965 		if ((np->in_ndstaddr == 0) && (np->in_ndstmsk == 0xffffffff)) {
6966 			dst_search = 0;
6967 			if (np->in_stepnext == 2)
6968 				np->in_stepnext = 3;
6969 
6970 		} else if ((np->in_ndstaddr == 0) && (np->in_ndstmsk == 0)) {
6971 			dst_search = 0;
6972 			if (np->in_stepnext == 2)
6973 				np->in_stepnext = 3;
6974 
6975 		} else if (np->in_ndstmsk == 0xffffffff) {
6976 			dst_search = 0;
6977 			if (np->in_stepnext == 2)
6978 				np->in_stepnext = 3;
6979 
6980 		} else if (np->in_ndstmsk != 0xffffffff) {
6981 			if ((np->in_stepnext == 2) && (changed == -1) &&
6982 			    (natl != NULL)) {
6983 				changed = 2;
6984 				np->in_stepnext++;
6985 				np->in_dnip++;
6986 			}
6987 		}
6988 
6989 		if ((flags & IPN_TCPUDPICMP) != 0) {
6990 			if (np->in_dpnext != 0)
6991 				frnat.fin_data[1] = np->in_dpnext;
6992 
6993 			/*
6994 			 * Standard port translation.  Select next port.
6995 			 */
6996 			if ((flags & IPN_FIXEDDPORT) != 0) {
6997 				np->in_stepnext = 0;
6998 			} else if (np->in_stepnext == 3 && changed == -1) {
6999 				np->in_dpnext++;
7000 				np->in_stepnext++;
7001 				changed = 3;
7002 				if (np->in_dpnext > np->in_dpmax)
7003 					np->in_dpnext = np->in_dpmin;
7004 			}
7005 		} else {
7006 			if (np->in_stepnext == 3)
7007 				np->in_stepnext = 0;
7008 		}
7009 
7010 		/* TRACE (frnat) */
7011 		DT1(ipf_nat_rewrite_3, frinfo_t *, &frnat);
7012 
7013 		/*
7014 		 * Here we do a lookup of the connection as seen from
7015 		 * the outside.  If an IP# pair already exists, try
7016 		 * again.  So if you have A->B becomes C->B, you can
7017 		 * also have D->E become C->E but not D->B causing
7018 		 * another C->B.  Also take protocol and ports into
7019 		 * account when determining whether a pre-existing
7020 		 * NAT setup will cause an external conflict where
7021 		 * this is appropriate.
7022 		 *
7023 		 * fin_data[] is swapped around because we are doing a
7024 		 * lookup of the packet is if it were moving in the opposite
7025 		 * direction of the one we are working with now.
7026 		 */
7027 		if (flags & IPN_TCPUDP) {
7028 			swap = frnat.fin_data[0];
7029 			frnat.fin_data[0] = frnat.fin_data[1];
7030 			frnat.fin_data[1] = swap;
7031 		}
7032 		if (fin->fin_out == 1) {
7033 			natl = ipf_nat_inlookup(&frnat,
7034 						flags & ~(SI_WILDP|NAT_SEARCH),
7035 						(u_int)frnat.fin_p,
7036 						frnat.fin_dst, frnat.fin_src);
7037 
7038 		} else {
7039 			natl = ipf_nat_outlookup(&frnat,
7040 						 flags & ~(SI_WILDP|NAT_SEARCH),
7041 						 (u_int)frnat.fin_p,
7042 						 frnat.fin_dst, frnat.fin_src);
7043 		}
7044 		if (flags & IPN_TCPUDP) {
7045 			swap = frnat.fin_data[0];
7046 			frnat.fin_data[0] = frnat.fin_data[1];
7047 			frnat.fin_data[1] = swap;
7048 		}
7049 
7050 		/* TRACE natl, in_stepnext, l */
7051 		DT3(ipf_nat_rewrite_2, nat_t *, natl, ipnat_t *, np , int, l);
7052 
7053 		if ((natl != NULL) && (l > 8))	/* XXX 8 is arbitrary */
7054 			return (-1);
7055 
7056 		np->in_stepnext &= 0x3;
7057 
7058 		l++;
7059 		changed = -1;
7060 	} while (natl != NULL);
7061 
7062 	nat->nat_osrcip = fin->fin_src;
7063 	nat->nat_odstip = fin->fin_dst;
7064 	nat->nat_nsrcip = frnat.fin_src;
7065 	nat->nat_ndstip = frnat.fin_dst;
7066 
7067 	if ((flags & IPN_TCPUDP) != 0) {
7068 		nat->nat_osport = htons(fin->fin_data[0]);
7069 		nat->nat_odport = htons(fin->fin_data[1]);
7070 		nat->nat_nsport = htons(frnat.fin_data[0]);
7071 		nat->nat_ndport = htons(frnat.fin_data[1]);
7072 	} else if ((flags & IPN_ICMPQUERY) != 0) {
7073 		nat->nat_oicmpid = fin->fin_data[1];
7074 		nat->nat_nicmpid = frnat.fin_data[1];
7075 	}
7076 
7077 	return (0);
7078 }
7079 
7080 
7081 /* ------------------------------------------------------------------------ */
7082 /* Function:    nat_newdivert                                               */
7083 /* Returns:     int - -1 == error, 0 == success                             */
7084 /* Parameters:  fin(I) - pointer to packet information                      */
7085 /*              nat(I) - pointer to NAT entry                               */
7086 /*              ni(I)  - pointer to structure with misc. information needed */
7087 /*                       to create new NAT entry.                           */
7088 /* Write Lock:  ipf_nat                                                     */
7089 /*                                                                          */
7090 /* Create a new NAT  divert session as defined by the NAT rule.  This is    */
7091 /* somewhat different to other NAT session creation routines because we     */
7092 /* do not iterate through either port numbers or IP addresses, searching    */
7093 /* for a unique mapping, however, a complimentary duplicate check is made.  */
7094 /* ------------------------------------------------------------------------ */
7095 static int
ipf_nat_newdivert(fr_info_t * fin,nat_t * nat,natinfo_t * nai)7096 ipf_nat_newdivert(fr_info_t *fin, nat_t *nat, natinfo_t *nai)
7097 {
7098 	ipf_main_softc_t *softc = fin->fin_main_soft;
7099 	ipf_nat_softc_t *softn = softc->ipf_nat_soft;
7100 	fr_info_t frnat;
7101 	ipnat_t *np;
7102 	nat_t *natl;
7103 	int p;
7104 
7105 	np = nai->nai_np;
7106 	bcopy((char *)fin, (char *)&frnat, sizeof(*fin));
7107 
7108 	nat->nat_pr[0] = 0;
7109 	nat->nat_osrcaddr = fin->fin_saddr;
7110 	nat->nat_odstaddr = fin->fin_daddr;
7111 	frnat.fin_saddr = htonl(np->in_snip);
7112 	frnat.fin_daddr = htonl(np->in_dnip);
7113 	if ((nat->nat_flags & IPN_TCPUDP) != 0) {
7114 		nat->nat_osport = htons(fin->fin_data[0]);
7115 		nat->nat_odport = htons(fin->fin_data[1]);
7116 	} else if ((nat->nat_flags & IPN_ICMPQUERY) != 0) {
7117 		nat->nat_oicmpid = fin->fin_data[1];
7118 	}
7119 
7120 	if (np->in_redir & NAT_DIVERTUDP) {
7121 		frnat.fin_data[0] = np->in_spnext;
7122 		frnat.fin_data[1] = np->in_dpnext;
7123 		frnat.fin_flx |= FI_TCPUDP;
7124 		p = IPPROTO_UDP;
7125 	} else {
7126 		frnat.fin_flx &= ~FI_TCPUDP;
7127 		p = IPPROTO_IPIP;
7128 	}
7129 
7130 	if (fin->fin_out == 1) {
7131 		natl = ipf_nat_inlookup(&frnat, 0, p,
7132 					frnat.fin_dst, frnat.fin_src);
7133 
7134 	} else {
7135 		natl = ipf_nat_outlookup(&frnat, 0, p,
7136 					 frnat.fin_dst, frnat.fin_src);
7137 	}
7138 
7139 	if (natl != NULL) {
7140 		NBUMPSIDED(fin->fin_out, ns_divert_exist);
7141 		DT3(ns_divert_exist, fr_info_t *, fin, nat_t *, nat, natinfo_t, nai);
7142 		return (-1);
7143 	}
7144 
7145 	nat->nat_nsrcaddr = frnat.fin_saddr;
7146 	nat->nat_ndstaddr = frnat.fin_daddr;
7147 	if ((nat->nat_flags & IPN_TCPUDP) != 0) {
7148 		nat->nat_nsport = htons(frnat.fin_data[0]);
7149 		nat->nat_ndport = htons(frnat.fin_data[1]);
7150 	} else if ((nat->nat_flags & IPN_ICMPQUERY) != 0) {
7151 		nat->nat_nicmpid = frnat.fin_data[1];
7152 	}
7153 
7154 	nat->nat_pr[fin->fin_out] = fin->fin_p;
7155 	nat->nat_pr[1 - fin->fin_out] = p;
7156 
7157 	if (np->in_redir & NAT_REDIRECT)
7158 		nat->nat_dir = NAT_DIVERTIN;
7159 	else
7160 		nat->nat_dir = NAT_DIVERTOUT;
7161 
7162 	return (0);
7163 }
7164 
7165 
7166 /* ------------------------------------------------------------------------ */
7167 /* Function:    nat_builddivertmp                                           */
7168 /* Returns:     int - -1 == error, 0 == success                             */
7169 /* Parameters:  softn(I) - pointer to NAT context structure                 */
7170 /*              np(I)    - pointer to a NAT rule                            */
7171 /*                                                                          */
7172 /* For divert rules, a skeleton packet representing what will be prepended  */
7173 /* to the real packet is created.  Even though we don't have the full       */
7174 /* packet here, a checksum is calculated that we update later when we       */
7175 /* fill in the final details.  At present a 0 checksum for UDP is being set */
7176 /* here because it is expected that divert will be used for localhost.      */
7177 /* ------------------------------------------------------------------------ */
7178 static int
ipf_nat_builddivertmp(ipf_nat_softc_t * softn,ipnat_t * np)7179 ipf_nat_builddivertmp(ipf_nat_softc_t *softn, ipnat_t *np)
7180 {
7181 	udphdr_t *uh;
7182 	size_t len;
7183 	ip_t *ip;
7184 
7185 	if ((np->in_redir & NAT_DIVERTUDP) != 0)
7186 		len = sizeof(ip_t) + sizeof(udphdr_t);
7187 	else
7188 		len = sizeof(ip_t);
7189 
7190 	ALLOC_MB_T(np->in_divmp, len);
7191 	if (np->in_divmp == NULL) {
7192 		NBUMPD(ipf_nat_stats, ns_divert_build);
7193 		return (-1);
7194 	}
7195 
7196 	/*
7197 	 * First, the header to get the packet diverted to the new destination
7198 	 */
7199 	ip = MTOD(np->in_divmp, ip_t *);
7200 	IP_V_A(ip, 4);
7201 	IP_HL_A(ip, 5);
7202 	ip->ip_tos = 0;
7203 	if ((np->in_redir & NAT_DIVERTUDP) != 0)
7204 		ip->ip_p = IPPROTO_UDP;
7205 	else
7206 		ip->ip_p = IPPROTO_IPIP;
7207 	ip->ip_ttl = 255;
7208 	ip->ip_off = 0;
7209 	ip->ip_sum = 0;
7210 	ip->ip_len = htons(len);
7211 	ip->ip_id = 0;
7212 	ip->ip_src.s_addr = htonl(np->in_snip);
7213 	ip->ip_dst.s_addr = htonl(np->in_dnip);
7214 	ip->ip_sum = ipf_cksum((u_short *)ip, sizeof(*ip));
7215 
7216 	if (np->in_redir & NAT_DIVERTUDP) {
7217 		uh = (udphdr_t *)(ip + 1);
7218 		uh->uh_sum = 0;
7219 		uh->uh_ulen = 8;
7220 		uh->uh_sport = htons(np->in_spnext);
7221 		uh->uh_dport = htons(np->in_dpnext);
7222 	}
7223 
7224 	return (0);
7225 }
7226 
7227 
7228 #define	MINDECAP	(sizeof(ip_t) + sizeof(udphdr_t) + sizeof(ip_t))
7229 
7230 /* ------------------------------------------------------------------------ */
7231 /* Function:    nat_decap                                                   */
7232 /* Returns:     int - -1 == error, 0 == success                             */
7233 /* Parameters:  fin(I) - pointer to packet information                      */
7234 /*              nat(I) - pointer to current NAT session                     */
7235 /*                                                                          */
7236 /* This function is responsible for undoing a packet's encapsulation in the */
7237 /* reverse of an encap/divert rule.  After removing the outer encapsulation */
7238 /* it is necessary to call ipf_makefrip() again so that the contents of 'fin'*/
7239 /* match the "new" packet as it may still be used by IPFilter elsewhere.    */
7240 /* We use "dir" here as the basis for some of the expectations about the    */
7241 /* outer header.  If we return an error, the goal is to leave the original  */
7242 /* packet information undisturbed - this falls short at the end where we'd  */
7243 /* need to back a backup copy of "fin" - expensive.                         */
7244 /* ------------------------------------------------------------------------ */
7245 static int
ipf_nat_decap(fr_info_t * fin,nat_t * nat)7246 ipf_nat_decap(fr_info_t *fin, nat_t *nat)
7247 {
7248 	ipf_main_softc_t *softc = fin->fin_main_soft;
7249 	ipf_nat_softc_t *softn = softc->ipf_nat_soft;
7250 	char *hdr;
7251 	int hlen;
7252 	int skip;
7253 	mb_t *m;
7254 
7255 	if ((fin->fin_flx & FI_ICMPERR) != 0) {
7256 		/*
7257 		 * ICMP packets don't get decapsulated, instead what we need
7258 		 * to do is change the ICMP reply from including (in the data
7259 		 * portion for errors) the encapsulated packet that we sent
7260 		 * out to something that resembles the original packet prior
7261 		 * to encapsulation.  This isn't done here - all we're doing
7262 		 * here is changing the outer address to ensure that it gets
7263 		 * targetted back to the correct system.
7264 		 */
7265 
7266 		if (nat->nat_dir & NAT_OUTBOUND) {
7267 			u_32_t sum1, sum2, sumd;
7268 
7269 			sum1 = ntohl(fin->fin_daddr);
7270 			sum2 = ntohl(nat->nat_osrcaddr);
7271 			CALC_SUMD(sum1, sum2, sumd);
7272 			fin->fin_ip->ip_dst = nat->nat_osrcip;
7273 			fin->fin_daddr = nat->nat_osrcaddr;
7274 #if !defined(_KERNEL) || SOLARIS
7275 			ipf_fix_outcksum(0, &fin->fin_ip->ip_sum, sumd, 0);
7276 #endif
7277 		}
7278 		return (0);
7279 	}
7280 
7281 	m = fin->fin_m;
7282 	skip = fin->fin_hlen;
7283 
7284 	switch (nat->nat_dir)
7285 	{
7286 	case NAT_DIVERTIN :
7287 	case NAT_DIVERTOUT :
7288 		if (fin->fin_plen < MINDECAP)
7289 			return (-1);
7290 		skip += sizeof(udphdr_t);
7291 		break;
7292 
7293 	case NAT_ENCAPIN :
7294 	case NAT_ENCAPOUT :
7295 		if (fin->fin_plen < (skip + sizeof(ip_t)))
7296 			return (-1);
7297 		break;
7298 	default :
7299 		return (-1);
7300 		/* NOTREACHED */
7301 	}
7302 
7303 	/*
7304 	 * The aim here is to keep the original packet details in "fin" for
7305 	 * as long as possible so that returning with an error is for the
7306 	 * original packet and there is little undoing work to do.
7307 	 */
7308 	if (M_LEN(m) < skip + sizeof(ip_t)) {
7309 		if (ipf_pr_pullup(fin, skip + sizeof(ip_t)) == -1)
7310 			return (-1);
7311 	}
7312 
7313 	hdr = MTOD(fin->fin_m, char *);
7314 	fin->fin_ip = (ip_t *)(hdr + skip);
7315 	hlen = IP_HL(fin->fin_ip) << 2;
7316 
7317 	if (ipf_pr_pullup(fin, skip + hlen) == -1) {
7318 		NBUMPSIDED(fin->fin_out, ns_decap_pullup);
7319 		return (-1);
7320 	}
7321 
7322 	fin->fin_hlen = hlen;
7323 	fin->fin_dlen -= skip;
7324 	fin->fin_plen -= skip;
7325 	fin->fin_ipoff += skip;
7326 
7327 	if (ipf_makefrip(hlen, (ip_t *)hdr, fin) == -1) {
7328 		NBUMPSIDED(fin->fin_out, ns_decap_bad);
7329 		return (-1);
7330 	}
7331 
7332 	return (skip);
7333 }
7334 
7335 
7336 /* ------------------------------------------------------------------------ */
7337 /* Function:    nat_nextaddr                                                */
7338 /* Returns:     int - -1 == bad input (no new address),                     */
7339 /*                     0 == success and dst has new address                 */
7340 /* Parameters:  fin(I) - pointer to packet information                      */
7341 /*              na(I)  - how to generate new address                        */
7342 /*              old(I) - original address being replaced                    */
7343 /*              dst(O) - where to put the new address                       */
7344 /* Write Lock:  ipf_nat                                                     */
7345 /*                                                                          */
7346 /* This function uses the contents of the "na" structure, in combination    */
7347 /* with "old" to produce a new address to store in "dst".  Not all of the   */
7348 /* possible uses of "na" will result in a new address.                      */
7349 /* ------------------------------------------------------------------------ */
7350 static int
ipf_nat_nextaddr(fr_info_t * fin,nat_addr_t * na,u_32_t * old,u_32_t * dst)7351 ipf_nat_nextaddr(fr_info_t *fin, nat_addr_t *na, u_32_t *old, u_32_t *dst)
7352 {
7353 	ipf_main_softc_t *softc = fin->fin_main_soft;
7354 	ipf_nat_softc_t *softn = softc->ipf_nat_soft;
7355 	u_32_t amin, amax, new;
7356 	i6addr_t newip;
7357 	int error;
7358 
7359 	new = 0;
7360 	amin = na->na_addr[0].in4.s_addr;
7361 
7362 	switch (na->na_atype)
7363 	{
7364 	case FRI_RANGE :
7365 		amax = na->na_addr[1].in4.s_addr;
7366 		break;
7367 
7368 	case FRI_NETMASKED :
7369 	case FRI_DYNAMIC :
7370 	case FRI_NORMAL :
7371 		/*
7372 		 * Compute the maximum address by adding the inverse of the
7373 		 * netmask to the minimum address.
7374 		 */
7375 		amax = ~na->na_addr[1].in4.s_addr;
7376 		amax |= amin;
7377 		break;
7378 
7379 	case FRI_LOOKUP :
7380 		break;
7381 
7382 	case FRI_BROADCAST :
7383 	case FRI_PEERADDR :
7384 	case FRI_NETWORK :
7385 	default :
7386 		DT4(ns_na_atype, fr_info_t *, fin, nat_addr_t *, na, u_32_t *, old, u_32_t *, new);
7387 		return (-1);
7388 	}
7389 
7390 	error = -1;
7391 
7392 	if (na->na_atype == FRI_LOOKUP) {
7393 		if (na->na_type == IPLT_DSTLIST) {
7394 			error = ipf_dstlist_select_node(fin, na->na_ptr, dst,
7395 							NULL);
7396 		} else {
7397 			NBUMPSIDE(fin->fin_out, ns_badnextaddr);
7398 			DT4(ns_badnextaddr_1, fr_info_t *, fin, nat_addr_t *, na, u_32_t *, old, u_32_t *, new);
7399 		}
7400 
7401 	} else if (na->na_atype == IPLT_NONE) {
7402 		/*
7403 		 * 0/0 as the new address means leave it alone.
7404 		 */
7405 		if (na->na_addr[0].in4.s_addr == 0 &&
7406 		    na->na_addr[1].in4.s_addr == 0) {
7407 			new = *old;
7408 
7409 		/*
7410 		 * 0/32 means get the interface's address
7411 		 */
7412 		} else if (na->na_addr[0].in4.s_addr == 0 &&
7413 			   na->na_addr[1].in4.s_addr == 0xffffffff) {
7414 			if (ipf_ifpaddr(softc, 4, na->na_atype,
7415 					fin->fin_ifp, &newip, NULL) == -1) {
7416 				NBUMPSIDED(fin->fin_out, ns_ifpaddrfail);
7417 				DT4(ns_ifpaddrfail, fr_info_t *, fin, nat_addr_t *, na, u_32_t *, old, u_32_t *, new);
7418 				return (-1);
7419 			}
7420 			new = newip.in4.s_addr;
7421 		} else {
7422 			new = htonl(na->na_nextip);
7423 		}
7424 		*dst = new;
7425 		error = 0;
7426 
7427 	} else {
7428 		NBUMPSIDE(fin->fin_out, ns_badnextaddr);
7429 		DT4(ns_badnextaddr_2, fr_info_t *, fin, nat_addr_t *, na, u_32_t *, old, u_32_t *, new);
7430 	}
7431 
7432 	return (error);
7433 }
7434 
7435 
7436 /* ------------------------------------------------------------------------ */
7437 /* Function:    nat_nextaddrinit                                            */
7438 /* Returns:     int - 0 == success, else error number                       */
7439 /* Parameters:  softc(I) - pointer to soft context main structure           */
7440 /*              na(I)      - NAT address information for generating new addr*/
7441 /*              initial(I) - flag indicating if it is the first call for    */
7442 /*                           this "na" structure.                           */
7443 /*              ifp(I)     - network interface to derive address            */
7444 /*                           information from.                              */
7445 /*                                                                          */
7446 /* This function is expected to be called in two scenarious: when a new NAT */
7447 /* rule is loaded into the kernel and when the list of NAT rules is sync'd  */
7448 /* up with the valid network interfaces (possibly due to them changing.)    */
7449 /* To distinguish between these, the "initial" parameter is used.  If it is */
7450 /* 1 then this indicates the rule has just been reloaded and 0 for when we  */
7451 /* are updating information.  This difference is important because in       */
7452 /* instances where we are not updating address information associated with  */
7453 /* a network interface, we don't want to disturb what the "next" address to */
7454 /* come out of ipf_nat_nextaddr() will be.                                  */
7455 /* ------------------------------------------------------------------------ */
7456 static int
ipf_nat_nextaddrinit(ipf_main_softc_t * softc,char * base,nat_addr_t * na,int initial,void * ifp)7457 ipf_nat_nextaddrinit(ipf_main_softc_t *softc, char *base, nat_addr_t *na,
7458 	int initial, void *ifp)
7459 {
7460 
7461 	switch (na->na_atype)
7462 	{
7463 	case FRI_LOOKUP :
7464 		if (na->na_subtype == 0) {
7465 			na->na_ptr = ipf_lookup_res_num(softc, IPL_LOGNAT,
7466 							na->na_type,
7467 							na->na_num,
7468 							&na->na_func);
7469 		} else if (na->na_subtype == 1) {
7470 			na->na_ptr = ipf_lookup_res_name(softc, IPL_LOGNAT,
7471 							 na->na_type,
7472 							 base + na->na_num,
7473 							 &na->na_func);
7474 		}
7475 		if (na->na_func == NULL) {
7476 			IPFERROR(60060);
7477 			return (ESRCH);
7478 		}
7479 		if (na->na_ptr == NULL) {
7480 			IPFERROR(60056);
7481 			return (ESRCH);
7482 		}
7483 		break;
7484 
7485 	case FRI_DYNAMIC :
7486 	case FRI_BROADCAST :
7487 	case FRI_NETWORK :
7488 	case FRI_NETMASKED :
7489 	case FRI_PEERADDR :
7490 		if (ifp != NULL)
7491 			(void )ipf_ifpaddr(softc, 4, na->na_atype, ifp,
7492 					   &na->na_addr[0], &na->na_addr[1]);
7493 		break;
7494 
7495 	case FRI_SPLIT :
7496 	case FRI_RANGE :
7497 		if (initial)
7498 			na->na_nextip = ntohl(na->na_addr[0].in4.s_addr);
7499 		break;
7500 
7501 	case FRI_NONE :
7502 		na->na_addr[0].in4.s_addr &= na->na_addr[1].in4.s_addr;
7503 		return (0);
7504 
7505 	case FRI_NORMAL :
7506 		na->na_addr[0].in4.s_addr &= na->na_addr[1].in4.s_addr;
7507 		break;
7508 
7509 	default :
7510 		IPFERROR(60054);
7511 		return (EINVAL);
7512 	}
7513 
7514 	if (initial && (na->na_atype == FRI_NORMAL)) {
7515 		if (na->na_addr[0].in4.s_addr == 0) {
7516 			if ((na->na_addr[1].in4.s_addr == 0xffffffff) ||
7517 			    (na->na_addr[1].in4.s_addr == 0)) {
7518 				return (0);
7519 			}
7520 		}
7521 
7522 		if (na->na_addr[1].in4.s_addr == 0xffffffff) {
7523 			na->na_nextip = ntohl(na->na_addr[0].in4.s_addr);
7524 		} else {
7525 			na->na_nextip = ntohl(na->na_addr[0].in4.s_addr) + 1;
7526 		}
7527 	}
7528 
7529 	return (0);
7530 }
7531 
7532 
7533 /* ------------------------------------------------------------------------ */
7534 /* Function:    ipf_nat_matchflush                                          */
7535 /* Returns:     int - -1 == error, 0 == success                             */
7536 /* Parameters:  softc(I) - pointer to soft context main structure           */
7537 /*              softn(I) - pointer to NAT context structure                 */
7538 /*              nat(I)   - pointer to current NAT session                   */
7539 /*                                                                          */
7540 /* ------------------------------------------------------------------------ */
7541 static int
ipf_nat_matchflush(ipf_main_softc_t * softc,ipf_nat_softc_t * softn,caddr_t data)7542 ipf_nat_matchflush(ipf_main_softc_t *softc, ipf_nat_softc_t *softn,
7543 	caddr_t data)
7544 {
7545 	int *array, flushed, error;
7546 	nat_t *nat, *natnext;
7547 	ipfobj_t obj;
7548 
7549 	error = ipf_matcharray_load(softc, data, &obj, &array);
7550 	if (error != 0)
7551 		return (error);
7552 
7553 	flushed = 0;
7554 
7555 	for (nat = softn->ipf_nat_instances; nat != NULL; nat = natnext) {
7556 		natnext = nat->nat_next;
7557 		if (ipf_nat_matcharray(nat, array, softc->ipf_ticks) == 0) {
7558 			ipf_nat_delete(softc, nat, NL_FLUSH);
7559 			flushed++;
7560 		}
7561 	}
7562 
7563 	obj.ipfo_retval = flushed;
7564 	error = BCOPYOUT(&obj, data, sizeof(obj));
7565 
7566 	KFREES(array, array[0] * sizeof(*array));
7567 
7568 	return (error);
7569 }
7570 
7571 
7572 /* ------------------------------------------------------------------------ */
7573 /* Function:    ipf_nat_matcharray                                          */
7574 /* Returns:     int - -1 == error, 0 == success                             */
7575 /* Parameters:  fin(I) - pointer to packet information                      */
7576 /*              nat(I) - pointer to current NAT session                     */
7577 /*                                                                          */
7578 /* ------------------------------------------------------------------------ */
7579 static int
ipf_nat_matcharray(nat_t * nat,int * array,u_long ticks)7580 ipf_nat_matcharray(nat_t *nat, int *array, u_long ticks)
7581 {
7582 	int i, n, *x, e, p;
7583 
7584 	e = 0;
7585 	n = array[0];
7586 	x = array + 1;
7587 
7588 	for (; n > 0; x += 3 + x[2]) {
7589 		if (x[0] == IPF_EXP_END)
7590 			break;
7591 		e = 0;
7592 
7593 		n -= x[2] + 3;
7594 		if (n < 0)
7595 			break;
7596 
7597 		p = x[0] >> 16;
7598 		if (p != 0 && p != nat->nat_pr[1])
7599 			break;
7600 
7601 		switch (x[0])
7602 		{
7603 		case IPF_EXP_IP_PR :
7604 			for (i = 0; !e && i < x[2]; i++) {
7605 				e |= (nat->nat_pr[1] == x[i + 3]);
7606 			}
7607 			break;
7608 
7609 		case IPF_EXP_IP_SRCADDR :
7610 			if (nat->nat_v[0] == 4) {
7611 				for (i = 0; !e && i < x[2]; i++) {
7612 					e |= ((nat->nat_osrcaddr & x[i + 4]) ==
7613 					      x[i + 3]);
7614 				}
7615 			}
7616 			if (nat->nat_v[1] == 4) {
7617 				for (i = 0; !e && i < x[2]; i++) {
7618 					e |= ((nat->nat_nsrcaddr & x[i + 4]) ==
7619 					      x[i + 3]);
7620 				}
7621 			}
7622 			break;
7623 
7624 		case IPF_EXP_IP_DSTADDR :
7625 			if (nat->nat_v[0] == 4) {
7626 				for (i = 0; !e && i < x[2]; i++) {
7627 					e |= ((nat->nat_odstaddr & x[i + 4]) ==
7628 					      x[i + 3]);
7629 				}
7630 			}
7631 			if (nat->nat_v[1] == 4) {
7632 				for (i = 0; !e && i < x[2]; i++) {
7633 					e |= ((nat->nat_ndstaddr & x[i + 4]) ==
7634 					      x[i + 3]);
7635 				}
7636 			}
7637 			break;
7638 
7639 		case IPF_EXP_IP_ADDR :
7640 			for (i = 0; !e && i < x[2]; i++) {
7641 				if (nat->nat_v[0] == 4) {
7642 					e |= ((nat->nat_osrcaddr & x[i + 4]) ==
7643 					      x[i + 3]);
7644 				}
7645 				if (nat->nat_v[1] == 4) {
7646 					e |= ((nat->nat_nsrcaddr & x[i + 4]) ==
7647 					      x[i + 3]);
7648 				}
7649 				if (nat->nat_v[0] == 4) {
7650 					e |= ((nat->nat_odstaddr & x[i + 4]) ==
7651 					      x[i + 3]);
7652 				}
7653 				if (nat->nat_v[1] == 4) {
7654 					e |= ((nat->nat_ndstaddr & x[i + 4]) ==
7655 					      x[i + 3]);
7656 				}
7657 			}
7658 			break;
7659 
7660 #ifdef USE_INET6
7661 		case IPF_EXP_IP6_SRCADDR :
7662 			if (nat->nat_v[0] == 6) {
7663 				for (i = 0; !e && i < x[3]; i++) {
7664 					e |= IP6_MASKEQ(&nat->nat_osrc6,
7665 							x + i + 7, x + i + 3);
7666 				}
7667 			}
7668 			if (nat->nat_v[1] == 6) {
7669 				for (i = 0; !e && i < x[3]; i++) {
7670 					e |= IP6_MASKEQ(&nat->nat_nsrc6,
7671 							x + i + 7, x + i + 3);
7672 				}
7673 			}
7674 			break;
7675 
7676 		case IPF_EXP_IP6_DSTADDR :
7677 			if (nat->nat_v[0] == 6) {
7678 				for (i = 0; !e && i < x[3]; i++) {
7679 					e |= IP6_MASKEQ(&nat->nat_odst6,
7680 							x + i + 7,
7681 							x + i + 3);
7682 				}
7683 			}
7684 			if (nat->nat_v[1] == 6) {
7685 				for (i = 0; !e && i < x[3]; i++) {
7686 					e |= IP6_MASKEQ(&nat->nat_ndst6,
7687 							x + i + 7,
7688 							x + i + 3);
7689 				}
7690 			}
7691 			break;
7692 
7693 		case IPF_EXP_IP6_ADDR :
7694 			for (i = 0; !e && i < x[3]; i++) {
7695 				if (nat->nat_v[0] == 6) {
7696 					e |= IP6_MASKEQ(&nat->nat_osrc6,
7697 							x + i + 7,
7698 							x + i + 3);
7699 				}
7700 				if (nat->nat_v[0] == 6) {
7701 					e |= IP6_MASKEQ(&nat->nat_odst6,
7702 							x + i + 7,
7703 							x + i + 3);
7704 				}
7705 				if (nat->nat_v[1] == 6) {
7706 					e |= IP6_MASKEQ(&nat->nat_nsrc6,
7707 							x + i + 7,
7708 							x + i + 3);
7709 				}
7710 				if (nat->nat_v[1] == 6) {
7711 					e |= IP6_MASKEQ(&nat->nat_ndst6,
7712 							x + i + 7,
7713 							x + i + 3);
7714 				}
7715 			}
7716 			break;
7717 #endif
7718 
7719 		case IPF_EXP_UDP_PORT :
7720 		case IPF_EXP_TCP_PORT :
7721 			for (i = 0; !e && i < x[2]; i++) {
7722 				e |= (nat->nat_nsport == x[i + 3]) ||
7723 				     (nat->nat_ndport == x[i + 3]);
7724 			}
7725 			break;
7726 
7727 		case IPF_EXP_UDP_SPORT :
7728 		case IPF_EXP_TCP_SPORT :
7729 			for (i = 0; !e && i < x[2]; i++) {
7730 				e |= (nat->nat_nsport == x[i + 3]);
7731 			}
7732 			break;
7733 
7734 		case IPF_EXP_UDP_DPORT :
7735 		case IPF_EXP_TCP_DPORT :
7736 			for (i = 0; !e && i < x[2]; i++) {
7737 				e |= (nat->nat_ndport == x[i + 3]);
7738 			}
7739 			break;
7740 
7741 		case IPF_EXP_TCP_STATE :
7742 			for (i = 0; !e && i < x[2]; i++) {
7743 				e |= (nat->nat_tcpstate[0] == x[i + 3]) ||
7744 				     (nat->nat_tcpstate[1] == x[i + 3]);
7745 			}
7746 			break;
7747 
7748 		case IPF_EXP_IDLE_GT :
7749 			e |= (ticks - nat->nat_touched > x[3]);
7750 			break;
7751 		}
7752 		e ^= x[1];
7753 
7754 		if (!e)
7755 			break;
7756 	}
7757 
7758 	return (e);
7759 }
7760 
7761 
7762 /* ------------------------------------------------------------------------ */
7763 /* Function:    ipf_nat_gettable                                            */
7764 /* Returns:     int     - 0 = success, else error                           */
7765 /* Parameters:  softc(I) - pointer to soft context main structure           */
7766 /*              softn(I) - pointer to NAT context structure                 */
7767 /*              data(I)  - pointer to ioctl data                            */
7768 /*                                                                          */
7769 /* This function handles ioctl requests for tables of nat information.      */
7770 /* At present the only table it deals with is the hash bucket statistics.   */
7771 /* ------------------------------------------------------------------------ */
7772 static int
ipf_nat_gettable(ipf_main_softc_t * softc,ipf_nat_softc_t * softn,char * data)7773 ipf_nat_gettable(ipf_main_softc_t *softc, ipf_nat_softc_t *softn, char *data)
7774 {
7775 	ipftable_t table;
7776 	int error;
7777 
7778 	error = ipf_inobj(softc, data, NULL, &table, IPFOBJ_GTABLE);
7779 	if (error != 0)
7780 		return (error);
7781 
7782 	switch (table.ita_type)
7783 	{
7784 	case IPFTABLE_BUCKETS_NATIN :
7785 		error = COPYOUT(softn->ipf_nat_stats.ns_side[0].ns_bucketlen,
7786 				table.ita_table,
7787 				softn->ipf_nat_table_sz * sizeof(u_int));
7788 		break;
7789 
7790 	case IPFTABLE_BUCKETS_NATOUT :
7791 		error = COPYOUT(softn->ipf_nat_stats.ns_side[1].ns_bucketlen,
7792 				table.ita_table,
7793 				softn->ipf_nat_table_sz * sizeof(u_int));
7794 		break;
7795 
7796 	default :
7797 		IPFERROR(60058);
7798 		return (EINVAL);
7799 	}
7800 
7801 	if (error != 0) {
7802 		IPFERROR(60059);
7803 		error = EFAULT;
7804 	}
7805 	return (error);
7806 }
7807 
7808 
7809 /* ------------------------------------------------------------------------ */
7810 /* Function:    ipf_nat_settimeout                                          */
7811 /* Returns:     int  - 0 = success, else failure			    */
7812 /* Parameters:  softc(I) - pointer to soft context main structure           */
7813 /*              t(I) - pointer to tunable                                   */
7814 /*              p(I) - pointer to new tuning data                           */
7815 /*                                                                          */
7816 /* Apply the timeout change to the NAT timeout queues.                      */
7817 /* ------------------------------------------------------------------------ */
7818 int
ipf_nat_settimeout(struct ipf_main_softc_s * softc,ipftuneable_t * t,ipftuneval_t * p)7819 ipf_nat_settimeout(struct ipf_main_softc_s *softc, ipftuneable_t *t,
7820 	ipftuneval_t *p)
7821 {
7822 	ipf_nat_softc_t *softn = softc->ipf_nat_soft;
7823 
7824 	if (!strncmp(t->ipft_name, "tcp_", 4))
7825 		return (ipf_settimeout_tcp(t, p, softn->ipf_nat_tcptq));
7826 
7827 	if (!strcmp(t->ipft_name, "udp_timeout")) {
7828 		ipf_apply_timeout(&softn->ipf_nat_udptq, p->ipftu_int);
7829 	} else if (!strcmp(t->ipft_name, "udp_ack_timeout")) {
7830 		ipf_apply_timeout(&softn->ipf_nat_udpacktq, p->ipftu_int);
7831 	} else if (!strcmp(t->ipft_name, "icmp_timeout")) {
7832 		ipf_apply_timeout(&softn->ipf_nat_icmptq, p->ipftu_int);
7833 	} else if (!strcmp(t->ipft_name, "icmp_ack_timeout")) {
7834 		ipf_apply_timeout(&softn->ipf_nat_icmpacktq, p->ipftu_int);
7835 	} else if (!strcmp(t->ipft_name, "ip_timeout")) {
7836 		ipf_apply_timeout(&softn->ipf_nat_iptq, p->ipftu_int);
7837 	} else {
7838 		IPFERROR(60062);
7839 		return (ESRCH);
7840 	}
7841 	return (0);
7842 }
7843 
7844 
7845 /* ------------------------------------------------------------------------ */
7846 /* Function:    ipf_nat_rehash                                              */
7847 /* Returns:     int  - 0 = success, else failure			    */
7848 /* Parameters:  softc(I) - pointer to soft context main structure           */
7849 /*              t(I) - pointer to tunable                                   */
7850 /*              p(I) - pointer to new tuning data                           */
7851 /*                                                                          */
7852 /* To change the size of the basic NAT table, we need to first allocate the */
7853 /* new tables (lest it fails and we've got nowhere to store all of the NAT  */
7854 /* sessions currently active) and then walk through the entire list and     */
7855 /* insert them into the table.  There are two tables here: an inbound one   */
7856 /* and an outbound one.  Each NAT entry goes into each table once.          */
7857 /* ------------------------------------------------------------------------ */
7858 int
ipf_nat_rehash(ipf_main_softc_t * softc,ipftuneable_t * t,ipftuneval_t * p)7859 ipf_nat_rehash(ipf_main_softc_t *softc, ipftuneable_t *t, ipftuneval_t *p)
7860 {
7861 	ipf_nat_softc_t *softn = softc->ipf_nat_soft;
7862 	nat_t **newtab[2], *nat, **natp;
7863 	u_int *bucketlens[2];
7864 	u_int maxbucket;
7865 	u_int newsize;
7866 	int error;
7867 	u_int hv;
7868 	int i;
7869 
7870 	newsize = p->ipftu_int;
7871 	/*
7872 	 * In case there is nothing to do...
7873 	 */
7874 	if (newsize == softn->ipf_nat_table_sz)
7875 		return (0);
7876 
7877 	newtab[0] = NULL;
7878 	newtab[1] = NULL;
7879 	bucketlens[0] = NULL;
7880 	bucketlens[1] = NULL;
7881 	/*
7882 	 * 4 tables depend on the NAT table size: the inbound looking table,
7883 	 * the outbound lookup table and the hash chain length for each.
7884 	 */
7885 	KMALLOCS(newtab[0], nat_t **, newsize * sizeof(nat_t *));
7886 	if (newtab[0] == NULL) {
7887 		error = 60063;
7888 		goto badrehash;
7889 	}
7890 
7891 	KMALLOCS(newtab[1], nat_t **, newsize * sizeof(nat_t *));
7892 	if (newtab[1] == NULL) {
7893 		error = 60064;
7894 		goto badrehash;
7895 	}
7896 
7897 	KMALLOCS(bucketlens[0], u_int *, newsize * sizeof(u_int));
7898 	if (bucketlens[0] == NULL) {
7899 		error = 60065;
7900 		goto badrehash;
7901 	}
7902 
7903 	KMALLOCS(bucketlens[1], u_int *, newsize * sizeof(u_int));
7904 	if (bucketlens[1] == NULL) {
7905 		error = 60066;
7906 		goto badrehash;
7907 	}
7908 
7909 	/*
7910 	 * Recalculate the maximum length based on the new size.
7911 	 */
7912 	for (maxbucket = 0, i = newsize; i > 0; i >>= 1)
7913 		maxbucket++;
7914 	maxbucket *= 2;
7915 
7916 	bzero((char *)newtab[0], newsize * sizeof(nat_t *));
7917 	bzero((char *)newtab[1], newsize * sizeof(nat_t *));
7918 	bzero((char *)bucketlens[0], newsize * sizeof(u_int));
7919 	bzero((char *)bucketlens[1], newsize * sizeof(u_int));
7920 
7921 	WRITE_ENTER(&softc->ipf_nat);
7922 
7923 	if (softn->ipf_nat_table[0] != NULL) {
7924 		KFREES(softn->ipf_nat_table[0],
7925 		       softn->ipf_nat_table_sz *
7926 		       sizeof(*softn->ipf_nat_table[0]));
7927 	}
7928 	softn->ipf_nat_table[0] = newtab[0];
7929 
7930 	if (softn->ipf_nat_table[1] != NULL) {
7931 		KFREES(softn->ipf_nat_table[1],
7932 		       softn->ipf_nat_table_sz *
7933 		       sizeof(*softn->ipf_nat_table[1]));
7934 	}
7935 	softn->ipf_nat_table[1] = newtab[1];
7936 
7937 	if (softn->ipf_nat_stats.ns_side[0].ns_bucketlen != NULL) {
7938 		KFREES(softn->ipf_nat_stats.ns_side[0].ns_bucketlen,
7939 		       softn->ipf_nat_table_sz * sizeof(u_int));
7940 	}
7941 	softn->ipf_nat_stats.ns_side[0].ns_bucketlen = bucketlens[0];
7942 
7943 	if (softn->ipf_nat_stats.ns_side[1].ns_bucketlen != NULL) {
7944 		KFREES(softn->ipf_nat_stats.ns_side[1].ns_bucketlen,
7945 		       softn->ipf_nat_table_sz * sizeof(u_int));
7946 	}
7947 	softn->ipf_nat_stats.ns_side[1].ns_bucketlen = bucketlens[1];
7948 
7949 #ifdef USE_INET6
7950 	if (softn->ipf_nat_stats.ns_side6[0].ns_bucketlen != NULL) {
7951 		KFREES(softn->ipf_nat_stats.ns_side6[0].ns_bucketlen,
7952 		       softn->ipf_nat_table_sz * sizeof(u_int));
7953 	}
7954 	softn->ipf_nat_stats.ns_side6[0].ns_bucketlen = bucketlens[0];
7955 
7956 	if (softn->ipf_nat_stats.ns_side6[1].ns_bucketlen != NULL) {
7957 		KFREES(softn->ipf_nat_stats.ns_side6[1].ns_bucketlen,
7958 		       softn->ipf_nat_table_sz * sizeof(u_int));
7959 	}
7960 	softn->ipf_nat_stats.ns_side6[1].ns_bucketlen = bucketlens[1];
7961 #endif
7962 
7963 	softn->ipf_nat_maxbucket = maxbucket;
7964 	softn->ipf_nat_table_sz = newsize;
7965 	/*
7966 	 * Walk through the entire list of NAT table entries and put them
7967 	 * in the new NAT table, somewhere.  Because we have a new table,
7968 	 * we need to restart the counter of how many chains are in use.
7969 	 */
7970 	softn->ipf_nat_stats.ns_side[0].ns_inuse = 0;
7971 	softn->ipf_nat_stats.ns_side[1].ns_inuse = 0;
7972 #ifdef USE_INET6
7973 	softn->ipf_nat_stats.ns_side6[0].ns_inuse = 0;
7974 	softn->ipf_nat_stats.ns_side6[1].ns_inuse = 0;
7975 #endif
7976 
7977 	for (nat = softn->ipf_nat_instances; nat != NULL; nat = nat->nat_next) {
7978 		nat->nat_hnext[0] = NULL;
7979 		nat->nat_phnext[0] = NULL;
7980 		hv = nat->nat_hv[0] % softn->ipf_nat_table_sz;
7981 
7982 		natp = &softn->ipf_nat_table[0][hv];
7983 		if (*natp) {
7984 			(*natp)->nat_phnext[0] = &nat->nat_hnext[0];
7985 		} else {
7986 			NBUMPSIDE(0, ns_inuse);
7987 		}
7988 		nat->nat_phnext[0] = natp;
7989 		nat->nat_hnext[0] = *natp;
7990 		*natp = nat;
7991 		NBUMPSIDE(0, ns_bucketlen[hv]);
7992 
7993 		nat->nat_hnext[1] = NULL;
7994 		nat->nat_phnext[1] = NULL;
7995 		hv = nat->nat_hv[1] % softn->ipf_nat_table_sz;
7996 
7997 		natp = &softn->ipf_nat_table[1][hv];
7998 		if (*natp) {
7999 			(*natp)->nat_phnext[1] = &nat->nat_hnext[1];
8000 		} else {
8001 			NBUMPSIDE(1, ns_inuse);
8002 		}
8003 		nat->nat_phnext[1] = natp;
8004 		nat->nat_hnext[1] = *natp;
8005 		*natp = nat;
8006 		NBUMPSIDE(1, ns_bucketlen[hv]);
8007 	}
8008 	RWLOCK_EXIT(&softc->ipf_nat);
8009 
8010 	return (0);
8011 
8012 badrehash:
8013 	if (bucketlens[1] != NULL) {
8014 		KFREES(bucketlens[0], newsize * sizeof(u_int));
8015 	}
8016 	if (bucketlens[0] != NULL) {
8017 		KFREES(bucketlens[0], newsize * sizeof(u_int));
8018 	}
8019 	if (newtab[0] != NULL) {
8020 		KFREES(newtab[0], newsize * sizeof(nat_t *));
8021 	}
8022 	if (newtab[1] != NULL) {
8023 		KFREES(newtab[1], newsize * sizeof(nat_t *));
8024 	}
8025 	IPFERROR(error);
8026 	return (ENOMEM);
8027 }
8028 
8029 
8030 /* ------------------------------------------------------------------------ */
8031 /* Function:    ipf_nat_rehash_rules                                        */
8032 /* Returns:     int  - 0 = success, else failure			    */
8033 /* Parameters:  softc(I) - pointer to soft context main structure           */
8034 /*              t(I) - pointer to tunable                                   */
8035 /*              p(I) - pointer to new tuning data                           */
8036 /*                                                                          */
8037 /* All of the NAT rules hang off of a hash table that is searched with a    */
8038 /* hash on address after the netmask is applied.  There is a different table*/
8039 /* for both inbound rules (rdr) and outbound (map.)  The resizing will only */
8040 /* affect one of these two tables.                                          */
8041 /* ------------------------------------------------------------------------ */
8042 int
ipf_nat_rehash_rules(ipf_main_softc_t * softc,ipftuneable_t * t,ipftuneval_t * p)8043 ipf_nat_rehash_rules(ipf_main_softc_t *softc, ipftuneable_t *t,
8044 	ipftuneval_t *p)
8045 {
8046 	ipf_nat_softc_t *softn = softc->ipf_nat_soft;
8047 	ipnat_t **newtab, *np, ***old, **npp;
8048 	u_int newsize;
8049 	u_int mask;
8050 	u_int hv;
8051 
8052 	newsize = p->ipftu_int;
8053 	/*
8054 	 * In case there is nothing to do...
8055 	 */
8056 	if (newsize == *t->ipft_pint)
8057 		return (0);
8058 
8059 	/*
8060 	 * All inbound rules have the NAT_REDIRECT bit set in in_redir and
8061 	 * all outbound rules have either NAT_MAP or MAT_MAPBLK set.
8062 	 * This if statement allows for some more generic code to be below,
8063 	 * rather than two huge gobs of code that almost do the same thing.
8064 	 */
8065 	if (t->ipft_pint == &softn->ipf_nat_rdrrules_sz) {
8066 		old = &softn->ipf_nat_rdr_rules;
8067 		mask = NAT_REDIRECT;
8068 	} else {
8069 		old = &softn->ipf_nat_map_rules;
8070 		mask = NAT_MAP|NAT_MAPBLK;
8071 	}
8072 
8073 	KMALLOCS(newtab, ipnat_t **, newsize * sizeof(ipnat_t *));
8074 	if (newtab == NULL) {
8075 		IPFERROR(60067);
8076 		return (ENOMEM);
8077 	}
8078 
8079 	bzero((char *)newtab, newsize * sizeof(ipnat_t *));
8080 
8081 	WRITE_ENTER(&softc->ipf_nat);
8082 
8083 	if (*old != NULL) {
8084 		KFREES(*old, *t->ipft_pint * sizeof(ipnat_t **));
8085 	}
8086 	*old = newtab;
8087 	*t->ipft_pint = newsize;
8088 
8089 	for (np = softn->ipf_nat_list; np != NULL; np = np->in_next) {
8090 		if ((np->in_redir & mask) == 0)
8091 			continue;
8092 
8093 		if (np->in_redir & NAT_REDIRECT) {
8094 			np->in_rnext = NULL;
8095 			hv = np->in_hv[0] % newsize;
8096 			for (npp = newtab + hv; *npp != NULL; )
8097 				npp = &(*npp)->in_rnext;
8098 			np->in_prnext = npp;
8099 			*npp = np;
8100 		}
8101 		if (np->in_redir & NAT_MAP) {
8102 			np->in_mnext = NULL;
8103 			hv = np->in_hv[1] % newsize;
8104 			for (npp = newtab + hv; *npp != NULL; )
8105 				npp = &(*npp)->in_mnext;
8106 			np->in_pmnext = npp;
8107 			*npp = np;
8108 		}
8109 
8110 	}
8111 	RWLOCK_EXIT(&softc->ipf_nat);
8112 
8113 	return (0);
8114 }
8115 
8116 
8117 /* ------------------------------------------------------------------------ */
8118 /* Function:    ipf_nat_hostmap_rehash                                      */
8119 /* Returns:     int  - 0 = success, else failure			    */
8120 /* Parameters:  softc(I) - pointer to soft context main structure           */
8121 /*              t(I) - pointer to tunable                                   */
8122 /*              p(I) - pointer to new tuning data                           */
8123 /*                                                                          */
8124 /* Allocate and populate a new hash table that will contain a reference to  */
8125 /* all of the active IP# translations currently in place.                   */
8126 /* ------------------------------------------------------------------------ */
8127 int
ipf_nat_hostmap_rehash(ipf_main_softc_t * softc,ipftuneable_t * t,ipftuneval_t * p)8128 ipf_nat_hostmap_rehash(ipf_main_softc_t *softc, ipftuneable_t *t,
8129 	ipftuneval_t *p)
8130 {
8131 	ipf_nat_softc_t *softn = softc->ipf_nat_soft;
8132 	hostmap_t *hm, **newtab;
8133 	u_int newsize;
8134 	u_int hv;
8135 
8136 	newsize = p->ipftu_int;
8137 	/*
8138 	 * In case there is nothing to do...
8139 	 */
8140 	if (newsize == *t->ipft_pint)
8141 		return (0);
8142 
8143 	KMALLOCS(newtab, hostmap_t **, newsize * sizeof(hostmap_t *));
8144 	if (newtab == NULL) {
8145 		IPFERROR(60068);
8146 		return (ENOMEM);
8147 	}
8148 
8149 	bzero((char *)newtab, newsize * sizeof(hostmap_t *));
8150 
8151 	WRITE_ENTER(&softc->ipf_nat);
8152 	if (softn->ipf_hm_maptable != NULL) {
8153 		KFREES(softn->ipf_hm_maptable,
8154 		       softn->ipf_nat_hostmap_sz * sizeof(hostmap_t *));
8155 	}
8156 	softn->ipf_hm_maptable = newtab;
8157 	softn->ipf_nat_hostmap_sz = newsize;
8158 
8159 	for (hm = softn->ipf_hm_maplist; hm != NULL; hm = hm->hm_next) {
8160 		hv = hm->hm_hv % softn->ipf_nat_hostmap_sz;
8161 		hm->hm_hnext = softn->ipf_hm_maptable[hv];
8162 		hm->hm_phnext = softn->ipf_hm_maptable + hv;
8163 		if (softn->ipf_hm_maptable[hv] != NULL)
8164 			softn->ipf_hm_maptable[hv]->hm_phnext = &hm->hm_hnext;
8165 		softn->ipf_hm_maptable[hv] = hm;
8166 	}
8167 	RWLOCK_EXIT(&softc->ipf_nat);
8168 
8169 	return (0);
8170 }
8171 
8172 
8173 /* ------------------------------------------------------------------------ */
8174 /* Function:    ipf_nat_add_tq                                              */
8175 /* Parameters:  softc(I) - pointer to soft context main structure           */
8176 /*                                                                          */
8177 /* ------------------------------------------------------------------------ */
8178 ipftq_t *
ipf_nat_add_tq(ipf_main_softc_t * softc,int ttl)8179 ipf_nat_add_tq(ipf_main_softc_t *softc, int ttl)
8180 {
8181 	ipf_nat_softc_t *softs = softc->ipf_nat_soft;
8182 
8183 	return (ipf_addtimeoutqueue(softc, &softs->ipf_nat_utqe, ttl));
8184 }
8185 
8186 /* ------------------------------------------------------------------------ */
8187 /* Function:    ipf_nat_uncreate                                            */
8188 /* Returns:     Nil                                                         */
8189 /* Parameters:  fin(I) - pointer to packet information                      */
8190 /*                                                                          */
8191 /* This function is used to remove a NAT entry from the NAT table when we   */
8192 /* decide that the create was actually in error. It is thus assumed that    */
8193 /* fin_flx will have both FI_NATED and FI_NATNEW set. Because we're dealing */
8194 /* with the translated packet (not the original), we have to reverse the    */
8195 /* lookup. Although doing the lookup is expensive (relatively speaking), it */
8196 /* is not anticipated that this will be a frequent occurance for normal     */
8197 /* traffic patterns.                                                        */
8198 /* ------------------------------------------------------------------------ */
8199 void
ipf_nat_uncreate(fr_info_t * fin)8200 ipf_nat_uncreate(fr_info_t *fin)
8201 {
8202 	ipf_main_softc_t *softc = fin->fin_main_soft;
8203 	ipf_nat_softc_t *softn = softc->ipf_nat_soft;
8204 	int nflags;
8205 	nat_t *nat;
8206 
8207 	switch (fin->fin_p)
8208 	{
8209 	case IPPROTO_TCP :
8210 		nflags = IPN_TCP;
8211 		break;
8212 	case IPPROTO_UDP :
8213 		nflags = IPN_UDP;
8214 		break;
8215 	default :
8216 		nflags = 0;
8217 		break;
8218 	}
8219 
8220 	WRITE_ENTER(&softc->ipf_nat);
8221 
8222 	if (fin->fin_out == 0) {
8223 		nat = ipf_nat_outlookup(fin, nflags, (u_int)fin->fin_p,
8224 					fin->fin_dst, fin->fin_src);
8225 	} else {
8226 		nat = ipf_nat_inlookup(fin, nflags, (u_int)fin->fin_p,
8227 				       fin->fin_src, fin->fin_dst);
8228 	}
8229 
8230 	if (nat != NULL) {
8231 		NBUMPSIDE(fin->fin_out, ns_uncreate[0]);
8232 		ipf_nat_delete(softc, nat, NL_DESTROY);
8233 	} else {
8234 		NBUMPSIDE(fin->fin_out, ns_uncreate[1]);
8235 	}
8236 
8237 	RWLOCK_EXIT(&softc->ipf_nat);
8238 }
8239 
8240 
8241 /* ------------------------------------------------------------------------ */
8242 /* Function:    ipf_nat_cmp_rules                                           */
8243 /* Returns:     int   - 0 == success, else rules do not match.              */
8244 /* Parameters:  n1(I) - first rule to compare                               */
8245 /*              n2(I) - first rule to compare                               */
8246 /*                                                                          */
8247 /* Compare two rules using pointers to each rule. A straight bcmp will not  */
8248 /* work as some fields (such as in_dst, in_pkts) actually do change once    */
8249 /* the rule has been loaded into the kernel. Whilst this function returns   */
8250 /* various non-zero returns, they're strictly to aid in debugging. Use of   */
8251 /* this function should simply care if the result is zero or not.           */
8252 /* ------------------------------------------------------------------------ */
8253 static int
ipf_nat_cmp_rules(ipnat_t * n1,ipnat_t * n2)8254 ipf_nat_cmp_rules(ipnat_t *n1, ipnat_t *n2)
8255 {
8256 	if (n1->in_size != n2->in_size)
8257 		return (1);
8258 
8259 	if (bcmp((char *)&n1->in_v, (char *)&n2->in_v,
8260 		 offsetof(ipnat_t, in_ndst) - offsetof(ipnat_t, in_v)) != 0)
8261 		return (2);
8262 
8263 	if (bcmp((char *)&n1->in_tuc, (char *)&n2->in_tuc,
8264 		 n1->in_size - offsetof(ipnat_t, in_tuc)) != 0)
8265 		return (3);
8266 	if (n1->in_ndst.na_atype != n2->in_ndst.na_atype)
8267 		return (5);
8268 	if (n1->in_ndst.na_function != n2->in_ndst.na_function)
8269 		return (6);
8270 	if (bcmp((char *)&n1->in_ndst.na_addr, (char *)&n2->in_ndst.na_addr,
8271 		 sizeof(n1->in_ndst.na_addr)))
8272 		return (7);
8273 	if (n1->in_nsrc.na_atype != n2->in_nsrc.na_atype)
8274 		return (8);
8275 	if (n1->in_nsrc.na_function != n2->in_nsrc.na_function)
8276 		return (9);
8277 	if (bcmp((char *)&n1->in_nsrc.na_addr, (char *)&n2->in_nsrc.na_addr,
8278 		 sizeof(n1->in_nsrc.na_addr)))
8279 		return (10);
8280 	if (n1->in_odst.na_atype != n2->in_odst.na_atype)
8281 		return (11);
8282 	if (n1->in_odst.na_function != n2->in_odst.na_function)
8283 		return (12);
8284 	if (bcmp((char *)&n1->in_odst.na_addr, (char *)&n2->in_odst.na_addr,
8285 		 sizeof(n1->in_odst.na_addr)))
8286 		return (13);
8287 	if (n1->in_osrc.na_atype != n2->in_osrc.na_atype)
8288 		return (14);
8289 	if (n1->in_osrc.na_function != n2->in_osrc.na_function)
8290 		return (15);
8291 	if (bcmp((char *)&n1->in_osrc.na_addr, (char *)&n2->in_osrc.na_addr,
8292 		 sizeof(n1->in_osrc.na_addr)))
8293 		return (16);
8294 	return (0);
8295 }
8296 
8297 
8298 /* ------------------------------------------------------------------------ */
8299 /* Function:    ipf_nat_rule_init                                           */
8300 /* Returns:     int   - 0 == success, else rules do not match.              */
8301 /* Parameters:  softc(I) - pointer to soft context main structure           */
8302 /*              softn(I) - pointer to NAT context structure                 */
8303 /*              n(I)     - first rule to compare                            */
8304 /*                                                                          */
8305 /* ------------------------------------------------------------------------ */
8306 static int
ipf_nat_rule_init(ipf_main_softc_t * softc,ipf_nat_softc_t * softn,ipnat_t * n)8307 ipf_nat_rule_init(ipf_main_softc_t *softc, ipf_nat_softc_t *softn,
8308 	ipnat_t *n)
8309 {
8310 	int error = 0;
8311 
8312 	if ((n->in_flags & IPN_SIPRANGE) != 0)
8313 		n->in_nsrcatype = FRI_RANGE;
8314 
8315 	if ((n->in_flags & IPN_DIPRANGE) != 0)
8316 		n->in_ndstatype = FRI_RANGE;
8317 
8318 	if ((n->in_flags & IPN_SPLIT) != 0)
8319 		n->in_ndstatype = FRI_SPLIT;
8320 
8321 	if ((n->in_redir & (NAT_MAP|NAT_REWRITE|NAT_DIVERTUDP)) != 0)
8322 		n->in_spnext = n->in_spmin;
8323 
8324 	if ((n->in_redir & (NAT_REWRITE|NAT_DIVERTUDP)) != 0) {
8325 		n->in_dpnext = n->in_dpmin;
8326 	} else if (n->in_redir == NAT_REDIRECT) {
8327 		n->in_dpnext = n->in_dpmin;
8328 	}
8329 
8330 	n->in_stepnext = 0;
8331 
8332 	switch (n->in_v[0])
8333 	{
8334 	case 4 :
8335 		error = ipf_nat_ruleaddrinit(softc, softn, n);
8336 		if (error != 0)
8337 			return (error);
8338 		break;
8339 #ifdef USE_INET6
8340 	case 6 :
8341 		error = ipf_nat6_ruleaddrinit(softc, softn, n);
8342 		if (error != 0)
8343 			return (error);
8344 		break;
8345 #endif
8346 	default :
8347 		break;
8348 	}
8349 
8350 	if (n->in_redir == (NAT_DIVERTUDP|NAT_MAP)) {
8351 		/*
8352 		 * Prerecord whether or not the destination of the divert
8353 		 * is local or not to the interface the packet is going
8354 		 * to be sent out.
8355 		 */
8356 		n->in_dlocal = ipf_deliverlocal(softc, n->in_v[1],
8357 						n->in_ifps[1], &n->in_ndstip6);
8358 	}
8359 
8360 	return (error);
8361 }
8362 
8363 
8364 /* ------------------------------------------------------------------------ */
8365 /* Function:    ipf_nat_rule_fini                                           */
8366 /* Returns:     int   - 0 == success, else rules do not match.              */
8367 /* Parameters:  softc(I) - pointer to soft context main structure           */
8368 /*              n(I)     - rule to work on                                  */
8369 /*                                                                          */
8370 /* This function is used to release any objects that were referenced during */
8371 /* the rule initialisation. This is useful both when free'ing the rule and  */
8372 /* when handling ioctls that need to initialise these fields but not        */
8373 /* actually use them after the ioctl processing has finished.               */
8374 /* ------------------------------------------------------------------------ */
8375 static void
ipf_nat_rule_fini(ipf_main_softc_t * softc,ipnat_t * n)8376 ipf_nat_rule_fini(ipf_main_softc_t *softc, ipnat_t *n)
8377 {
8378 	if (n->in_odst.na_atype == FRI_LOOKUP && n->in_odst.na_ptr != NULL)
8379 		ipf_lookup_deref(softc, n->in_odst.na_type, n->in_odst.na_ptr);
8380 
8381 	if (n->in_osrc.na_atype == FRI_LOOKUP && n->in_osrc.na_ptr != NULL)
8382 		ipf_lookup_deref(softc, n->in_osrc.na_type, n->in_osrc.na_ptr);
8383 
8384 	if (n->in_ndst.na_atype == FRI_LOOKUP && n->in_ndst.na_ptr != NULL)
8385 		ipf_lookup_deref(softc, n->in_ndst.na_type, n->in_ndst.na_ptr);
8386 
8387 	if (n->in_nsrc.na_atype == FRI_LOOKUP && n->in_nsrc.na_ptr != NULL)
8388 		ipf_lookup_deref(softc, n->in_nsrc.na_type, n->in_nsrc.na_ptr);
8389 
8390 	if (n->in_divmp != NULL)
8391 		FREE_MB_T(n->in_divmp);
8392 }
8393