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