Home | History | Annotate | Line # | Download | only in netinet
ip_nat.c revision 1.1
      1  1.1  christos /*	$NetBSD: ip_nat.c,v 1.1 2012/03/23 20:36:59 christos Exp $	*/
      2  1.1  christos 
      3  1.1  christos /*
      4  1.1  christos  * Copyright (C) 2012 by Darren Reed.
      5  1.1  christos  *
      6  1.1  christos  * See the IPFILTER.LICENCE file for details on licencing.
      7  1.1  christos  */
      8  1.1  christos #if defined(KERNEL) || defined(_KERNEL)
      9  1.1  christos # undef KERNEL
     10  1.1  christos # undef ipf_nat_KERNEL
     11  1.1  christos # define        KERNEL	1
     12  1.1  christos # define        ipf_nat_KERNEL	1
     13  1.1  christos #endif
     14  1.1  christos #include <sys/errno.h>
     15  1.1  christos #include <sys/types.h>
     16  1.1  christos #include <sys/param.h>
     17  1.1  christos #include <sys/time.h>
     18  1.1  christos #include <sys/file.h>
     19  1.1  christos #if defined(_KERNEL) && \
     20  1.1  christos     (defined(__NetBSD_Version) && (__NetBSD_Version >= 399002000))
     21  1.1  christos # include <sys/kauth.h>
     22  1.1  christos #endif
     23  1.1  christos #if !defined(_KERNEL)
     24  1.1  christos # include <stdio.h>
     25  1.1  christos # include <string.h>
     26  1.1  christos # include <stdlib.h>
     27  1.1  christos # define ipf_nat_KERNEL
     28  1.1  christos # ifdef ipf_nat__OpenBSD__
     29  1.1  christos struct file;
     30  1.1  christos # endif
     31  1.1  christos # include <sys/uio.h>
     32  1.1  christos # undef ipf_nat_KERNEL
     33  1.1  christos #endif
     34  1.1  christos #if defined(_KERNEL) && \
     35  1.1  christos     defined(__FreeBSD_version) && (__FreeBSD_version >= 220000)
     36  1.1  christos # include <sys/filio.h>
     37  1.1  christos # include <sys/fcntl.h>
     38  1.1  christos #else
     39  1.1  christos # include <sys/ioctl.h>
     40  1.1  christos #endif
     41  1.1  christos #if !defined(AIX)
     42  1.1  christos # include <sys/fcntl.h>
     43  1.1  christos #endif
     44  1.1  christos #if !defined(linux)
     45  1.1  christos # include <sys/protosw.h>
     46  1.1  christos #endif
     47  1.1  christos #include <sys/socket.h>
     48  1.1  christos #if defined(_KERNEL)
     49  1.1  christos # include <sys/systm.h>
     50  1.1  christos # if !defined(__SVR4) && !defined(__svr4__)
     51  1.1  christos #  include <sys/mbuf.h>
     52  1.1  christos # endif
     53  1.1  christos #endif
     54  1.1  christos #if defined(__SVR4) || defined(__svr4__)
     55  1.1  christos # include <sys/filio.h>
     56  1.1  christos # include <sys/byteorder.h>
     57  1.1  christos # ifdef ipf_nat_KERNEL
     58  1.1  christos #  include <sys/dditypes.h>
     59  1.1  christos # endif
     60  1.1  christos # include <sys/stream.h>
     61  1.1  christos # include <sys/kmem.h>
     62  1.1  christos #endif
     63  1.1  christos #if ipf_nat__FreeBSD_version >= 300000
     64  1.1  christos # include <sys/queue.h>
     65  1.1  christos #endif
     66  1.1  christos #include <net/if.h>
     67  1.1  christos #if ipf_nat__FreeBSD_version >= 300000
     68  1.1  christos # include <net/if_var.h>
     69  1.1  christos #endif
     70  1.1  christos #ifdef sun
     71  1.1  christos # include <net/af.h>
     72  1.1  christos #endif
     73  1.1  christos #include <netinet/in.h>
     74  1.1  christos #include <netinet/in_systm.h>
     75  1.1  christos #include <netinet/ip.h>
     76  1.1  christos 
     77  1.1  christos #ifdef RFC1825
     78  1.1  christos # include <vpn/md5.h>
     79  1.1  christos # include <vpn/ipsec.h>
     80  1.1  christos extern struct ifnet vpnif;
     81  1.1  christos #endif
     82  1.1  christos 
     83  1.1  christos #if !defined(linux)
     84  1.1  christos # include <netinet/ip_var.h>
     85  1.1  christos #endif
     86  1.1  christos #include <netinet/tcp.h>
     87  1.1  christos #include <netinet/udp.h>
     88  1.1  christos #include <netinet/ip_icmp.h>
     89  1.1  christos #include "netinet/ip_compat.h"
     90  1.1  christos #include <netinet/tcpip.h>
     91  1.1  christos #include "netinet/ipl.h"
     92  1.1  christos #include "netinet/ip_fil.h"
     93  1.1  christos #include "netinet/ip_nat.h"
     94  1.1  christos #include "netinet/ip_frag.h"
     95  1.1  christos #include "netinet/ip_state.h"
     96  1.1  christos #include "netinet/ip_proxy.h"
     97  1.1  christos #include "netinet/ip_lookup.h"
     98  1.1  christos #include "netinet/ip_dstlist.h"
     99  1.1  christos #include "netinet/ip_sync.h"
    100  1.1  christos #if FREEBSD_GE_REV(300000)
    101  1.1  christos # include <sys/malloc.h>
    102  1.1  christos #endif
    103  1.1  christos #ifdef HAS_SYS_MD5_H
    104  1.1  christos # include <sys/md5.h>
    105  1.1  christos #else
    106  1.1  christos # include "md5.h"
    107  1.1  christos #endif
    108  1.1  christos /* END OF INCLUDES */
    109  1.1  christos 
    110  1.1  christos #undef	SOCKADDR_IN
    111  1.1  christos #define	SOCKADDR_IN	struct sockaddr_in
    112  1.1  christos 
    113  1.1  christos #if !defined(lint)
    114  1.1  christos static const char sccsid[] = "@(#)ip_nat.c	1.11 6/5/96 (C) 1995 Darren Reed";
    115  1.1  christos static const char rcsid[] = "@(#)Id";
    116  1.1  christos #endif
    117  1.1  christos 
    118  1.1  christos 
    119  1.1  christos #define	NATFSUM(n,v,f)	((v) == 4 ? (n)->f.in4.s_addr : (n)->f.i6[0] + \
    120  1.1  christos 			 (n)->f.i6[1] + (n)->f.i6[2] + (n)->f.i6[3])
    121  1.1  christos #define	NBUMP(x)	softn->(x)++
    122  1.1  christos #define	NBUMPD(x, y)	do { \
    123  1.1  christos 				softn->x.y++; \
    124  1.1  christos 				DT(y); \
    125  1.1  christos 			} while (0)
    126  1.1  christos #define	NBUMPSIDE(y,x)	softn->ipf_nat_stats.ns_side[y].x++
    127  1.1  christos #define	NBUMPSIDED(y,x)	do { softn->ipf_nat_stats.ns_side[y].x++; \
    128  1.1  christos 			     DT(x); } while (0)
    129  1.1  christos #define	NBUMPSIDEX(y,x,z) \
    130  1.1  christos 			do { softn->ipf_nat_stats.ns_side[y].x++; \
    131  1.1  christos 			     DT(z); } while (0)
    132  1.1  christos #define	NBUMPSIDEDF(y,x)do { softn->ipf_nat_stats.ns_side[y].x++; \
    133  1.1  christos 			     DT1(x, fr_info_t *, fin); } while (0)
    134  1.1  christos 
    135  1.1  christos frentry_t	ipfnatblock;
    136  1.1  christos 
    137  1.1  christos static ipftuneable_t ipf_nat_tuneables[] = {
    138  1.1  christos 	/* nat */
    139  1.1  christos 	{ { (void *)offsetof(ipf_nat_softc_t, ipf_nat_lock) },
    140  1.1  christos 		"nat_lock",	0,	1,
    141  1.1  christos 		stsizeof(ipf_nat_softc_t, ipf_nat_lock),
    142  1.1  christos 		IPFT_RDONLY,		NULL,	NULL },
    143  1.1  christos 	{ { (void *)offsetof(ipf_nat_softc_t, ipf_nat_table_sz) },
    144  1.1  christos 		"nat_table_size", 1,	0x7fffffff,
    145  1.1  christos 		stsizeof(ipf_nat_softc_t, ipf_nat_table_sz),
    146  1.1  christos 		0,			NULL,	ipf_nat_rehash },
    147  1.1  christos 	{ { (void *)offsetof(ipf_nat_softc_t, ipf_nat_table_max) },
    148  1.1  christos 		"nat_table_max", 1,	0x7fffffff,
    149  1.1  christos 		stsizeof(ipf_nat_softc_t, ipf_nat_table_max),
    150  1.1  christos 		0,			NULL,	NULL },
    151  1.1  christos 	{ { (void *)offsetof(ipf_nat_softc_t, ipf_nat_maprules_sz) },
    152  1.1  christos 		"nat_rules_size", 1,	0x7fffffff,
    153  1.1  christos 		stsizeof(ipf_nat_softc_t, ipf_nat_maprules_sz),
    154  1.1  christos 		0,			NULL,	ipf_nat_rehash_rules },
    155  1.1  christos 	{ { (void *)offsetof(ipf_nat_softc_t, ipf_nat_rdrrules_sz) },
    156  1.1  christos 		"rdr_rules_size", 1,	0x7fffffff,
    157  1.1  christos 		stsizeof(ipf_nat_softc_t, ipf_nat_rdrrules_sz),
    158  1.1  christos 		0,			NULL,	ipf_nat_rehash_rules },
    159  1.1  christos 	{ { (void *)offsetof(ipf_nat_softc_t, ipf_nat_hostmap_sz) },
    160  1.1  christos 		"hostmap_size",	1,	0x7fffffff,
    161  1.1  christos 		stsizeof(ipf_nat_softc_t, ipf_nat_hostmap_sz),
    162  1.1  christos 		0,			NULL,	ipf_nat_hostmap_rehash },
    163  1.1  christos 	{ { (void *)offsetof(ipf_nat_softc_t, ipf_nat_maxbucket) },
    164  1.1  christos 		"nat_maxbucket",1,	0x7fffffff,
    165  1.1  christos 		stsizeof(ipf_nat_softc_t, ipf_nat_maxbucket),
    166  1.1  christos 		0,			NULL,	NULL },
    167  1.1  christos 	{ { (void *)offsetof(ipf_nat_softc_t, ipf_nat_logging) },
    168  1.1  christos 		"nat_logging",	0,	1,
    169  1.1  christos 		stsizeof(ipf_nat_softc_t, ipf_nat_logging),
    170  1.1  christos 		0,			NULL,	NULL },
    171  1.1  christos 	{ { (void *)offsetof(ipf_nat_softc_t, ipf_nat_doflush) },
    172  1.1  christos 		"nat_doflush",	0,	1,
    173  1.1  christos 		stsizeof(ipf_nat_softc_t, ipf_nat_doflush),
    174  1.1  christos 		0,			NULL,	NULL },
    175  1.1  christos 	{ { (void *)offsetof(ipf_nat_softc_t, ipf_nat_table_wm_low) },
    176  1.1  christos 		"nat_table_wm_low",	1,	99,
    177  1.1  christos 		stsizeof(ipf_nat_softc_t, ipf_nat_table_wm_low),
    178  1.1  christos 		0,			NULL,	NULL },
    179  1.1  christos 	{ { (void *)offsetof(ipf_nat_softc_t, ipf_nat_table_wm_high) },
    180  1.1  christos 		"nat_table_wm_high",	2,	100,
    181  1.1  christos 		stsizeof(ipf_nat_softc_t, ipf_nat_table_wm_high),
    182  1.1  christos 		0,			NULL,	NULL },
    183  1.1  christos 	{ { 0 },
    184  1.1  christos 		NULL,			0,	0,
    185  1.1  christos 		0,
    186  1.1  christos 		0,			NULL,	NULL }
    187  1.1  christos };
    188  1.1  christos 
    189  1.1  christos /* ======================================================================== */
    190  1.1  christos /* How the NAT is organised and works.                                      */
    191  1.1  christos /*                                                                          */
    192  1.1  christos /* Inside (interface y) NAT       Outside (interface x)                     */
    193  1.1  christos /* -------------------- -+- -------------------------------------           */
    194  1.1  christos /* Packet going          |   out, processsed by ipf_nat_checkout() for x    */
    195  1.1  christos /* ------------>         |   ------------>                                  */
    196  1.1  christos /* src=10.1.1.1          |   src=192.1.1.1                                  */
    197  1.1  christos /*                       |                                                  */
    198  1.1  christos /*                       |   in, processed by ipf_nat_checkin() for x       */
    199  1.1  christos /* <------------         |   <------------                                  */
    200  1.1  christos /* dst=10.1.1.1          |   dst=192.1.1.1                                  */
    201  1.1  christos /* -------------------- -+- -------------------------------------           */
    202  1.1  christos /* ipf_nat_checkout() - changes ip_src and if required, sport               */
    203  1.1  christos /*             - creates a new mapping, if required.                        */
    204  1.1  christos /* ipf_nat_checkin()  - changes ip_dst and if required, dport               */
    205  1.1  christos /*                                                                          */
    206  1.1  christos /* In the NAT table, internal source is recorded as "in" and externally     */
    207  1.1  christos /* seen as "out".                                                           */
    208  1.1  christos /* ======================================================================== */
    209  1.1  christos 
    210  1.1  christos 
    211  1.1  christos #if SOLARIS && !defined(INSTANCES)
    212  1.1  christos extern	int		pfil_delayed_copy;
    213  1.1  christos #endif
    214  1.1  christos 
    215  1.1  christos static	int	ipf_nat_flush_entry __P((ipf_main_softc_t *, void *));
    216  1.1  christos static	int	ipf_nat_getent __P((ipf_main_softc_t *, caddr_t, int));
    217  1.1  christos static	int	ipf_nat_getsz __P((ipf_main_softc_t *, caddr_t, int));
    218  1.1  christos static	int	ipf_nat_putent __P((ipf_main_softc_t *, caddr_t, int));
    219  1.1  christos static	void	ipf_nat_add_active __P((int, u_32_t *));
    220  1.1  christos static	void	ipf_nat_add_map_mask __P((ipf_nat_softc_t *, int));
    221  1.1  christos static	void	ipf_nat_add_rdr_mask __P((ipf_nat_softc_t *, int));
    222  1.1  christos static	void	ipf_nat_addencap __P((ipf_nat_softc_t *, ipnat_t *));
    223  1.1  christos static	void	ipf_nat_addmap __P((ipf_nat_softc_t *, ipnat_t *));
    224  1.1  christos static	void	ipf_nat_addrdr __P((ipf_nat_softc_t *, ipnat_t *));
    225  1.1  christos static	int	ipf_nat_builddivertmp __P((ipf_nat_softc_t *, ipnat_t *));
    226  1.1  christos static	int	ipf_nat_clearlist __P((ipf_main_softc_t *, ipf_nat_softc_t *));
    227  1.1  christos static	int	ipf_nat_decap __P((fr_info_t *, nat_t *));
    228  1.1  christos static	void	ipf_nat_del_active __P((int, u_32_t *));
    229  1.1  christos static	void	ipf_nat_del_map_mask __P((ipf_nat_softc_t *, int));
    230  1.1  christos static	void	ipf_nat_del_rdr_mask __P((ipf_nat_softc_t *, int));
    231  1.1  christos static	int	ipf_nat_encapok __P((fr_info_t *, nat_t *));
    232  1.1  christos static	int	ipf_nat_extraflush __P((ipf_main_softc_t *, ipf_nat_softc_t *, int));
    233  1.1  christos static	int	ipf_nat_finalise __P((fr_info_t *, nat_t *));
    234  1.1  christos static	int	ipf_nat_flushtable __P((ipf_main_softc_t *, ipf_nat_softc_t *));
    235  1.1  christos static	void	ipf_nat_free_rule __P((ipf_main_softc_t *, ipf_nat_softc_t *, ipnat_t *));
    236  1.1  christos static	int	ipf_nat_getnext __P((ipf_main_softc_t *, ipftoken_t *,
    237  1.1  christos 				     ipfgeniter_t *, ipfobj_t *));
    238  1.1  christos static	int	ipf_nat_gettable __P((ipf_main_softc_t *, ipf_nat_softc_t *,
    239  1.1  christos 				      char *));
    240  1.1  christos static	hostmap_t *ipf_nat_hostmap __P((ipf_nat_softc_t *, ipnat_t *,
    241  1.1  christos 					struct in_addr, struct in_addr,
    242  1.1  christos 					struct in_addr, u_32_t));
    243  1.1  christos static	int	ipf_nat_icmpquerytype __P((int));
    244  1.1  christos static	int	ipf_nat_iterator __P((ipf_main_softc_t *, ipftoken_t *,
    245  1.1  christos 				      ipfgeniter_t *, ipfobj_t *));
    246  1.1  christos static	int	ipf_nat_match __P((fr_info_t *, ipnat_t *));
    247  1.1  christos static	int	ipf_nat_matcharray __P((nat_t *, int *, u_long));
    248  1.1  christos static	int	ipf_nat_matchencap __P((ipf_nat_softc_t *, fr_info_t *,
    249  1.1  christos 					ipnat_t *));
    250  1.1  christos static	int	ipf_nat_matchflush __P((ipf_main_softc_t *, ipf_nat_softc_t *,
    251  1.1  christos 					caddr_t));
    252  1.1  christos static	void	ipf_nat_mssclamp __P((tcphdr_t *, u_32_t, fr_info_t *,
    253  1.1  christos 				      u_short *));
    254  1.1  christos static	int	ipf_nat_newmap __P((fr_info_t *, nat_t *, natinfo_t *));
    255  1.1  christos static	int	ipf_nat_newdivert __P((fr_info_t *, nat_t *, natinfo_t *));
    256  1.1  christos static	int	ipf_nat_newrdr __P((fr_info_t *, nat_t *, natinfo_t *));
    257  1.1  christos static	int	ipf_nat_newrewrite __P((fr_info_t *, nat_t *, natinfo_t *));
    258  1.1  christos static	int	ipf_nat_nextaddr __P((fr_info_t *, nat_addr_t *, u_32_t *,
    259  1.1  christos 				      u_32_t *));
    260  1.1  christos static	int	ipf_nat_nextaddrinit __P((ipf_main_softc_t *, char *,
    261  1.1  christos 					  nat_addr_t *, int, void *));
    262  1.1  christos static	nat_t	*ipf_nat_rebuildencapicmp __P((fr_info_t *, nat_t *));
    263  1.1  christos static	int	ipf_nat_resolverule __P((ipf_main_softc_t *, ipnat_t *));
    264  1.1  christos static	int	ipf_nat_ruleaddrinit __P((ipf_main_softc_t *,
    265  1.1  christos 					  ipf_nat_softc_t *, ipnat_t *));
    266  1.1  christos static	int	ipf_nat_siocaddnat __P((ipf_main_softc_t *, ipf_nat_softc_t *,
    267  1.1  christos 					ipnat_t *, ipnat_t **, int));
    268  1.1  christos static	void	ipf_nat_siocdelnat __P((ipf_main_softc_t *, ipf_nat_softc_t *,
    269  1.1  christos 					ipnat_t *, ipnat_t **, int));
    270  1.1  christos static	void	ipf_nat_tabmove __P((ipf_nat_softc_t *, nat_t *));
    271  1.1  christos 
    272  1.1  christos /* ------------------------------------------------------------------------ */
    273  1.1  christos /* Function:    ipf_nat_main_load                                           */
    274  1.1  christos /* Returns:     int - 0 == success, -1 == failure                           */
    275  1.1  christos /* Parameters:  Nil                                                         */
    276  1.1  christos /*                                                                          */
    277  1.1  christos /* The only global NAT structure that needs to be initialised is the filter */
    278  1.1  christos /* rule that is used with blocking packets.                                 */
    279  1.1  christos /* ------------------------------------------------------------------------ */
    280  1.1  christos int
    281  1.1  christos ipf_nat_main_load()
    282  1.1  christos {
    283  1.1  christos 	bzero((char *)&ipfnatblock, sizeof(ipfnatblock));
    284  1.1  christos 	ipfnatblock.fr_flags = FR_BLOCK|FR_QUICK;
    285  1.1  christos 	ipfnatblock.fr_ref = 1;
    286  1.1  christos 
    287  1.1  christos 	return 0;
    288  1.1  christos }
    289  1.1  christos 
    290  1.1  christos 
    291  1.1  christos /* ------------------------------------------------------------------------ */
    292  1.1  christos /* Function:    ipf_nat_main_unload                                         */
    293  1.1  christos /* Returns:     int - 0 == success, -1 == failure                           */
    294  1.1  christos /* Parameters:  Nil                                                         */
    295  1.1  christos /*                                                                          */
    296  1.1  christos /* A null-op function that exists as a placeholder so that the flow in      */
    297  1.1  christos /* other functions is obvious.                                              */
    298  1.1  christos /* ------------------------------------------------------------------------ */
    299  1.1  christos int
    300  1.1  christos ipf_nat_main_unload()
    301  1.1  christos {
    302  1.1  christos 	return 0;
    303  1.1  christos }
    304  1.1  christos 
    305  1.1  christos 
    306  1.1  christos /* ------------------------------------------------------------------------ */
    307  1.1  christos /* Function:    ipf_nat_soft_create                                         */
    308  1.1  christos /* Returns:     void * - NULL = failure, else pointer to NAT context        */
    309  1.1  christos /* Parameters:  softc(I) - pointer to soft context main structure           */
    310  1.1  christos /*                                                                          */
    311  1.1  christos /* Allocate the initial soft context structure for NAT and populate it with */
    312  1.1  christos /* some default values. Creating the tables is left until we call _init so  */
    313  1.1  christos /* that sizes can be changed before we get under way.                       */
    314  1.1  christos /* ------------------------------------------------------------------------ */
    315  1.1  christos void *
    316  1.1  christos ipf_nat_soft_create(softc)
    317  1.1  christos 	ipf_main_softc_t *softc;
    318  1.1  christos {
    319  1.1  christos 	ipf_nat_softc_t *softn;
    320  1.1  christos 
    321  1.1  christos 	KMALLOC(softn, ipf_nat_softc_t *);
    322  1.1  christos 	if (softn == NULL)
    323  1.1  christos 		return NULL;
    324  1.1  christos 
    325  1.1  christos 	bzero((char *)softn, sizeof(*softn));
    326  1.1  christos 
    327  1.1  christos 	softn->ipf_nat_tune = ipf_tune_array_copy(softn,
    328  1.1  christos 						  sizeof(ipf_nat_tuneables),
    329  1.1  christos 						  ipf_nat_tuneables);
    330  1.1  christos 	if (softn->ipf_nat_tune == NULL) {
    331  1.1  christos 		ipf_nat_soft_destroy(softc, softn);
    332  1.1  christos 		return NULL;
    333  1.1  christos 	}
    334  1.1  christos 	if (ipf_tune_array_link(softc, softn->ipf_nat_tune) == -1) {
    335  1.1  christos 		ipf_nat_soft_destroy(softc, softn);
    336  1.1  christos 		return NULL;
    337  1.1  christos 	}
    338  1.1  christos 
    339  1.1  christos 	softn->ipf_nat_table_max = NAT_TABLE_MAX;
    340  1.1  christos 	softn->ipf_nat_table_sz = NAT_TABLE_SZ;
    341  1.1  christos 	softn->ipf_nat_maprules_sz = NAT_SIZE;
    342  1.1  christos 	softn->ipf_nat_rdrrules_sz = RDR_SIZE;
    343  1.1  christos 	softn->ipf_nat_hostmap_sz = HOSTMAP_SIZE;
    344  1.1  christos 	softn->ipf_nat_doflush = 0;
    345  1.1  christos #ifdef  IPFILTER_LOG
    346  1.1  christos 	softn->ipf_nat_logging = 1;
    347  1.1  christos #else
    348  1.1  christos 	softn->ipf_nat_logging = 0;
    349  1.1  christos #endif
    350  1.1  christos 
    351  1.1  christos 	softn->ipf_nat_defage = DEF_NAT_AGE;
    352  1.1  christos 	softn->ipf_nat_defipage = IPF_TTLVAL(60);
    353  1.1  christos 	softn->ipf_nat_deficmpage = IPF_TTLVAL(3);
    354  1.1  christos 	softn->ipf_nat_table_wm_high = 99;
    355  1.1  christos 	softn->ipf_nat_table_wm_low = 90;
    356  1.1  christos 
    357  1.1  christos 	return softn;
    358  1.1  christos }
    359  1.1  christos 
    360  1.1  christos /* ------------------------------------------------------------------------ */
    361  1.1  christos /* Function:    ipf_nat_soft_destroy                                        */
    362  1.1  christos /* Returns:     Nil                                                         */
    363  1.1  christos /* Parameters:  softc(I) - pointer to soft context main structure           */
    364  1.1  christos /*                                                                          */
    365  1.1  christos /* ------------------------------------------------------------------------ */
    366  1.1  christos void
    367  1.1  christos ipf_nat_soft_destroy(softc, arg)
    368  1.1  christos 	ipf_main_softc_t *softc;
    369  1.1  christos 	void *arg;
    370  1.1  christos {
    371  1.1  christos 	ipf_nat_softc_t *softn = arg;
    372  1.1  christos 
    373  1.1  christos 	if (softn->ipf_nat_tune != NULL) {
    374  1.1  christos 		ipf_tune_array_unlink(softc, softn->ipf_nat_tune);
    375  1.1  christos 		KFREES(softn->ipf_nat_tune, sizeof(ipf_nat_tuneables));
    376  1.1  christos 		softn->ipf_nat_tune = NULL;
    377  1.1  christos 	}
    378  1.1  christos 
    379  1.1  christos 	KFREE(softn);
    380  1.1  christos }
    381  1.1  christos 
    382  1.1  christos 
    383  1.1  christos /* ------------------------------------------------------------------------ */
    384  1.1  christos /* Function:    ipf_nat_init                                                */
    385  1.1  christos /* Returns:     int - 0 == success, -1 == failure                           */
    386  1.1  christos /* Parameters:  softc(I) - pointer to soft context main structure           */
    387  1.1  christos /*                                                                          */
    388  1.1  christos /* Initialise all of the NAT locks, tables and other structures.            */
    389  1.1  christos /* ------------------------------------------------------------------------ */
    390  1.1  christos int
    391  1.1  christos ipf_nat_soft_init(softc, arg)
    392  1.1  christos 	ipf_main_softc_t *softc;
    393  1.1  christos 	void *arg;
    394  1.1  christos {
    395  1.1  christos 	ipf_nat_softc_t *softn = arg;
    396  1.1  christos 	ipftq_t *tq;
    397  1.1  christos 	int i;
    398  1.1  christos 
    399  1.1  christos 	KMALLOCS(softn->ipf_nat_table[0], nat_t **, \
    400  1.1  christos 		 sizeof(nat_t *) * softn->ipf_nat_table_sz);
    401  1.1  christos 
    402  1.1  christos 	if (softn->ipf_nat_table[0] != NULL) {
    403  1.1  christos 		bzero((char *)softn->ipf_nat_table[0],
    404  1.1  christos 		      softn->ipf_nat_table_sz * sizeof(nat_t *));
    405  1.1  christos 	} else {
    406  1.1  christos 		return -1;
    407  1.1  christos 	}
    408  1.1  christos 
    409  1.1  christos 	KMALLOCS(softn->ipf_nat_table[1], nat_t **, \
    410  1.1  christos 		 sizeof(nat_t *) * softn->ipf_nat_table_sz);
    411  1.1  christos 
    412  1.1  christos 	if (softn->ipf_nat_table[1] != NULL) {
    413  1.1  christos 		bzero((char *)softn->ipf_nat_table[1],
    414  1.1  christos 		      softn->ipf_nat_table_sz * sizeof(nat_t *));
    415  1.1  christos 	} else {
    416  1.1  christos 		return -2;
    417  1.1  christos 	}
    418  1.1  christos 
    419  1.1  christos 	KMALLOCS(softn->ipf_nat_map_rules, ipnat_t **, \
    420  1.1  christos 		 sizeof(ipnat_t *) * softn->ipf_nat_maprules_sz);
    421  1.1  christos 
    422  1.1  christos 	if (softn->ipf_nat_map_rules != NULL) {
    423  1.1  christos 		bzero((char *)softn->ipf_nat_map_rules,
    424  1.1  christos 		      softn->ipf_nat_maprules_sz * sizeof(ipnat_t *));
    425  1.1  christos 	} else {
    426  1.1  christos 		return -3;
    427  1.1  christos 	}
    428  1.1  christos 
    429  1.1  christos 	KMALLOCS(softn->ipf_nat_rdr_rules, ipnat_t **, \
    430  1.1  christos 		 sizeof(ipnat_t *) * softn->ipf_nat_rdrrules_sz);
    431  1.1  christos 
    432  1.1  christos 	if (softn->ipf_nat_rdr_rules != NULL) {
    433  1.1  christos 		bzero((char *)softn->ipf_nat_rdr_rules,
    434  1.1  christos 		      softn->ipf_nat_rdrrules_sz * sizeof(ipnat_t *));
    435  1.1  christos 	} else {
    436  1.1  christos 		return -4;
    437  1.1  christos 	}
    438  1.1  christos 
    439  1.1  christos 	KMALLOCS(softn->ipf_hm_maptable, hostmap_t **, \
    440  1.1  christos 		 sizeof(hostmap_t *) * softn->ipf_nat_hostmap_sz);
    441  1.1  christos 
    442  1.1  christos 	if (softn->ipf_hm_maptable != NULL) {
    443  1.1  christos 		bzero((char *)softn->ipf_hm_maptable,
    444  1.1  christos 		      sizeof(hostmap_t *) * softn->ipf_nat_hostmap_sz);
    445  1.1  christos 	} else {
    446  1.1  christos 		return -5;
    447  1.1  christos 	}
    448  1.1  christos 	softn->ipf_hm_maplist = NULL;
    449  1.1  christos 
    450  1.1  christos 	KMALLOCS(softn->ipf_nat_stats.ns_side[0].ns_bucketlen, u_int *,
    451  1.1  christos 		 softn->ipf_nat_table_sz * sizeof(u_int));
    452  1.1  christos 
    453  1.1  christos 	if (softn->ipf_nat_stats.ns_side[0].ns_bucketlen == NULL) {
    454  1.1  christos 		return -6;
    455  1.1  christos 	}
    456  1.1  christos 	bzero((char *)softn->ipf_nat_stats.ns_side[0].ns_bucketlen,
    457  1.1  christos 	      softn->ipf_nat_table_sz * sizeof(u_int));
    458  1.1  christos 
    459  1.1  christos 	KMALLOCS(softn->ipf_nat_stats.ns_side[1].ns_bucketlen, u_int *,
    460  1.1  christos 		 softn->ipf_nat_table_sz * sizeof(u_int));
    461  1.1  christos 
    462  1.1  christos 	if (softn->ipf_nat_stats.ns_side[1].ns_bucketlen == NULL) {
    463  1.1  christos 		return -7;
    464  1.1  christos 	}
    465  1.1  christos 
    466  1.1  christos 	bzero((char *)softn->ipf_nat_stats.ns_side[1].ns_bucketlen,
    467  1.1  christos 	      softn->ipf_nat_table_sz * sizeof(u_int));
    468  1.1  christos 
    469  1.1  christos 	if (softn->ipf_nat_maxbucket == 0) {
    470  1.1  christos 		for (i = softn->ipf_nat_table_sz; i > 0; i >>= 1)
    471  1.1  christos 			softn->ipf_nat_maxbucket++;
    472  1.1  christos 		softn->ipf_nat_maxbucket *= 2;
    473  1.1  christos 	}
    474  1.1  christos 
    475  1.1  christos 	ipf_sttab_init(softc, softn->ipf_nat_tcptq);
    476  1.1  christos 	/*
    477  1.1  christos 	 * Increase this because we may have "keep state" following this too
    478  1.1  christos 	 * and packet storms can occur if this is removed too quickly.
    479  1.1  christos 	 */
    480  1.1  christos 	softn->ipf_nat_tcptq[IPF_TCPS_CLOSED].ifq_ttl = softc->ipf_tcplastack;
    481  1.1  christos 	softn->ipf_nat_tcptq[IPF_TCP_NSTATES - 1].ifq_next =
    482  1.1  christos 							&softn->ipf_nat_udptq;
    483  1.1  christos 
    484  1.1  christos 	IPFTQ_INIT(&softn->ipf_nat_udptq, softn->ipf_nat_defage,
    485  1.1  christos 		   "nat ipftq udp tab");
    486  1.1  christos 	softn->ipf_nat_udptq.ifq_next = &softn->ipf_nat_udpacktq;
    487  1.1  christos 
    488  1.1  christos 	IPFTQ_INIT(&softn->ipf_nat_udpacktq, softn->ipf_nat_defage,
    489  1.1  christos 		   "nat ipftq udpack tab");
    490  1.1  christos 	softn->ipf_nat_udpacktq.ifq_next = &softn->ipf_nat_icmptq;
    491  1.1  christos 
    492  1.1  christos 	IPFTQ_INIT(&softn->ipf_nat_icmptq, softn->ipf_nat_deficmpage,
    493  1.1  christos 		   "nat icmp ipftq tab");
    494  1.1  christos 	softn->ipf_nat_icmptq.ifq_next = &softn->ipf_nat_icmpacktq;
    495  1.1  christos 
    496  1.1  christos 	IPFTQ_INIT(&softn->ipf_nat_icmpacktq, softn->ipf_nat_defage,
    497  1.1  christos 		   "nat icmpack ipftq tab");
    498  1.1  christos 	softn->ipf_nat_icmpacktq.ifq_next = &softn->ipf_nat_iptq;
    499  1.1  christos 
    500  1.1  christos 	IPFTQ_INIT(&softn->ipf_nat_iptq, softn->ipf_nat_defipage,
    501  1.1  christos 		   "nat ip ipftq tab");
    502  1.1  christos 	softn->ipf_nat_iptq.ifq_next = &softn->ipf_nat_pending;
    503  1.1  christos 
    504  1.1  christos 	IPFTQ_INIT(&softn->ipf_nat_pending, 1, "nat pending ipftq tab");
    505  1.1  christos 	softn->ipf_nat_pending.ifq_next = NULL;
    506  1.1  christos 
    507  1.1  christos 	for (i = 0, tq = softn->ipf_nat_tcptq; i < IPF_TCP_NSTATES; i++, tq++) {
    508  1.1  christos 		if (tq->ifq_ttl < softn->ipf_nat_deficmpage)
    509  1.1  christos 			tq->ifq_ttl = softn->ipf_nat_deficmpage;
    510  1.1  christos #ifdef LARGE_NAT
    511  1.1  christos 		else if (tq->ifq_ttl > softn->ipf_nat_defage)
    512  1.1  christos 			tq->ifq_ttl = softn->ipf_nat_defage;
    513  1.1  christos #endif
    514  1.1  christos 	}
    515  1.1  christos 
    516  1.1  christos 	/*
    517  1.1  christos 	 * Increase this because we may have "keep state" following
    518  1.1  christos 	 * this too and packet storms can occur if this is removed
    519  1.1  christos 	 * too quickly.
    520  1.1  christos 	 */
    521  1.1  christos 	softn->ipf_nat_tcptq[IPF_TCPS_CLOSED].ifq_ttl = softc->ipf_tcplastack;
    522  1.1  christos 
    523  1.1  christos 	MUTEX_INIT(&softn->ipf_nat_new, "ipf nat new mutex");
    524  1.1  christos 	MUTEX_INIT(&softn->ipf_nat_io, "ipf nat io mutex");
    525  1.1  christos 
    526  1.1  christos 	softn->ipf_nat_inited = 1;
    527  1.1  christos 
    528  1.1  christos 	return 0;
    529  1.1  christos }
    530  1.1  christos 
    531  1.1  christos 
    532  1.1  christos /* ------------------------------------------------------------------------ */
    533  1.1  christos /* Function:    ipf_nat_soft_fini                                           */
    534  1.1  christos /* Returns:     Nil                                                         */
    535  1.1  christos /* Parameters:  softc(I) - pointer to soft context main structure           */
    536  1.1  christos /*                                                                          */
    537  1.1  christos /* Free all memory used by NAT structures allocated at runtime.             */
    538  1.1  christos /* ------------------------------------------------------------------------ */
    539  1.1  christos int
    540  1.1  christos ipf_nat_soft_fini(softc, arg)
    541  1.1  christos 	ipf_main_softc_t *softc;
    542  1.1  christos 	void *arg;
    543  1.1  christos {
    544  1.1  christos 	ipf_nat_softc_t *softn = arg;
    545  1.1  christos 	ipftq_t *ifq, *ifqnext;
    546  1.1  christos 
    547  1.1  christos 	(void) ipf_nat_clearlist(softc, softn);
    548  1.1  christos 	(void) ipf_nat_flushtable(softc, softn);
    549  1.1  christos 
    550  1.1  christos 	/*
    551  1.1  christos 	 * Proxy timeout queues are not cleaned here because although they
    552  1.1  christos 	 * exist on the NAT list, ipf_proxy_unload is called after unload
    553  1.1  christos 	 * and the proxies actually are responsible for them being created.
    554  1.1  christos 	 * Should the proxy timeouts have their own list?  There's no real
    555  1.1  christos 	 * justification as this is the only complication.
    556  1.1  christos 	 */
    557  1.1  christos 	for (ifq = softn->ipf_nat_utqe; ifq != NULL; ifq = ifqnext) {
    558  1.1  christos 		ifqnext = ifq->ifq_next;
    559  1.1  christos 		if (ipf_deletetimeoutqueue(ifq) == 0)
    560  1.1  christos 			ipf_freetimeoutqueue(softc, ifq);
    561  1.1  christos 	}
    562  1.1  christos 
    563  1.1  christos 	if (softn->ipf_nat_table[0] != NULL) {
    564  1.1  christos 		KFREES(softn->ipf_nat_table[0],
    565  1.1  christos 		       sizeof(nat_t *) * softn->ipf_nat_table_sz);
    566  1.1  christos 		softn->ipf_nat_table[0] = NULL;
    567  1.1  christos 	}
    568  1.1  christos 	if (softn->ipf_nat_table[1] != NULL) {
    569  1.1  christos 		KFREES(softn->ipf_nat_table[1],
    570  1.1  christos 		       sizeof(nat_t *) * softn->ipf_nat_table_sz);
    571  1.1  christos 		softn->ipf_nat_table[1] = NULL;
    572  1.1  christos 	}
    573  1.1  christos 	if (softn->ipf_nat_map_rules != NULL) {
    574  1.1  christos 		KFREES(softn->ipf_nat_map_rules,
    575  1.1  christos 		       sizeof(ipnat_t *) * softn->ipf_nat_maprules_sz);
    576  1.1  christos 		softn->ipf_nat_map_rules = NULL;
    577  1.1  christos 	}
    578  1.1  christos 	if (softn->ipf_nat_rdr_rules != NULL) {
    579  1.1  christos 		KFREES(softn->ipf_nat_rdr_rules,
    580  1.1  christos 		       sizeof(ipnat_t *) * softn->ipf_nat_rdrrules_sz);
    581  1.1  christos 		softn->ipf_nat_rdr_rules = NULL;
    582  1.1  christos 	}
    583  1.1  christos 	if (softn->ipf_hm_maptable != NULL) {
    584  1.1  christos 		KFREES(softn->ipf_hm_maptable,
    585  1.1  christos 		       sizeof(hostmap_t *) * softn->ipf_nat_hostmap_sz);
    586  1.1  christos 		softn->ipf_hm_maptable = NULL;
    587  1.1  christos 	}
    588  1.1  christos 	if (softn->ipf_nat_stats.ns_side[0].ns_bucketlen != NULL) {
    589  1.1  christos 		KFREES(softn->ipf_nat_stats.ns_side[0].ns_bucketlen,
    590  1.1  christos 		       sizeof(u_int) * softn->ipf_nat_table_sz);
    591  1.1  christos 		softn->ipf_nat_stats.ns_side[0].ns_bucketlen = NULL;
    592  1.1  christos 	}
    593  1.1  christos 	if (softn->ipf_nat_stats.ns_side[1].ns_bucketlen != NULL) {
    594  1.1  christos 		KFREES(softn->ipf_nat_stats.ns_side[1].ns_bucketlen,
    595  1.1  christos 		       sizeof(u_int) * softn->ipf_nat_table_sz);
    596  1.1  christos 		softn->ipf_nat_stats.ns_side[1].ns_bucketlen = NULL;
    597  1.1  christos 	}
    598  1.1  christos 
    599  1.1  christos 	if (softn->ipf_nat_inited == 1) {
    600  1.1  christos 		softn->ipf_nat_inited = 0;
    601  1.1  christos 		ipf_sttab_destroy(softn->ipf_nat_tcptq);
    602  1.1  christos 
    603  1.1  christos 		MUTEX_DESTROY(&softn->ipf_nat_new);
    604  1.1  christos 		MUTEX_DESTROY(&softn->ipf_nat_io);
    605  1.1  christos 
    606  1.1  christos 		MUTEX_DESTROY(&softn->ipf_nat_udptq.ifq_lock);
    607  1.1  christos 		MUTEX_DESTROY(&softn->ipf_nat_udpacktq.ifq_lock);
    608  1.1  christos 		MUTEX_DESTROY(&softn->ipf_nat_icmptq.ifq_lock);
    609  1.1  christos 		MUTEX_DESTROY(&softn->ipf_nat_icmpacktq.ifq_lock);
    610  1.1  christos 		MUTEX_DESTROY(&softn->ipf_nat_iptq.ifq_lock);
    611  1.1  christos 		MUTEX_DESTROY(&softn->ipf_nat_pending.ifq_lock);
    612  1.1  christos 	}
    613  1.1  christos 
    614  1.1  christos 	return 0;
    615  1.1  christos }
    616  1.1  christos 
    617  1.1  christos 
    618  1.1  christos /* ------------------------------------------------------------------------ */
    619  1.1  christos /* Function:    ipf_nat_setlock                                             */
    620  1.1  christos /* Returns:     Nil                                                         */
    621  1.1  christos /* Parameters:  arg(I) - pointer to soft state information                  */
    622  1.1  christos /*              tmp(I) - new lock value                                     */
    623  1.1  christos /*                                                                          */
    624  1.1  christos /* Set the "lock status" of NAT to the value in tmp.                        */
    625  1.1  christos /* ------------------------------------------------------------------------ */
    626  1.1  christos void
    627  1.1  christos ipf_nat_setlock(arg, tmp)
    628  1.1  christos 	void *arg;
    629  1.1  christos 	int tmp;
    630  1.1  christos {
    631  1.1  christos 	ipf_nat_softc_t *softn = arg;
    632  1.1  christos 
    633  1.1  christos 	softn->ipf_nat_lock = tmp;
    634  1.1  christos }
    635  1.1  christos 
    636  1.1  christos 
    637  1.1  christos /* ------------------------------------------------------------------------ */
    638  1.1  christos /* Function:    ipf_nat_add_active                                          */
    639  1.1  christos /* Returns:     Nil                                                         */
    640  1.1  christos /* Parameters:  bitcount(I) - number of bits set in the netmask             */
    641  1.1  christos /*              active(O)   - array to insert the new bitmask into          */
    642  1.1  christos /*                                                                          */
    643  1.1  christos /* Insert the 32bit bitmask represented by "bitcount" into the array of     */
    644  1.1  christos /* active netmasks at active[]. The array should never have a duplicate of  */
    645  1.1  christos /* any particular mask, except for "0".                                     */
    646  1.1  christos /* ------------------------------------------------------------------------ */
    647  1.1  christos static void
    648  1.1  christos ipf_nat_add_active(bitcount, active)
    649  1.1  christos 	int bitcount;
    650  1.1  christos 	u_32_t *active;
    651  1.1  christos {
    652  1.1  christos 	u_32_t mask = 0xffffffff << (32 - bitcount);
    653  1.1  christos 	int i;
    654  1.1  christos 
    655  1.1  christos 	for (i = 0; i < 33; i++) {
    656  1.1  christos 		if (ntohl(active[i]) < mask) {
    657  1.1  christos 			int j;
    658  1.1  christos 
    659  1.1  christos 			for (j = i + 1; j < 33; j++)
    660  1.1  christos 				active[j] = active[j - 1];
    661  1.1  christos 			active[i] = htonl(mask);
    662  1.1  christos 			break;
    663  1.1  christos 		}
    664  1.1  christos 	}
    665  1.1  christos }
    666  1.1  christos 
    667  1.1  christos 
    668  1.1  christos /* ------------------------------------------------------------------------ */
    669  1.1  christos /* Function:    ipf_nat_del_active                                          */
    670  1.1  christos /* Returns:     Nil                                                         */
    671  1.1  christos /* Parameters:  bitcount(I) - number of bits set in the netmask             */
    672  1.1  christos /*              active(O)   - array to remove the bitmask from              */
    673  1.1  christos /*                                                                          */
    674  1.1  christos /* REmove the 32bit bitmask represented by "bitcount" from the array of     */
    675  1.1  christos /* active netmasks at active[].                                             */
    676  1.1  christos /* ------------------------------------------------------------------------ */
    677  1.1  christos static void
    678  1.1  christos ipf_nat_del_active(bitcount, active)
    679  1.1  christos 	int bitcount;
    680  1.1  christos 	u_32_t *active;
    681  1.1  christos {
    682  1.1  christos 	u_32_t mask = htonl(0xffffffff << (32 - bitcount));
    683  1.1  christos 	int i;
    684  1.1  christos 
    685  1.1  christos 	for (i = 0; i < 33; i++) {
    686  1.1  christos 		if (active[i] == mask) {
    687  1.1  christos 			int j;
    688  1.1  christos 
    689  1.1  christos 			for (j = i + 1; j < 33; j++)
    690  1.1  christos 				active[j - 1] = active[j];
    691  1.1  christos 			break;
    692  1.1  christos 		}
    693  1.1  christos 	}
    694  1.1  christos }
    695  1.1  christos 
    696  1.1  christos 
    697  1.1  christos /* ------------------------------------------------------------------------ */
    698  1.1  christos /* Function:    ipf_nat_add_map_mask                                        */
    699  1.1  christos /* Returns:     Nil                                                         */
    700  1.1  christos /* Parameters:  softn(I)    - pointer to nat context information            */
    701  1.1  christos /*              bitcount(I) - bitcount of mask to add                       */
    702  1.1  christos /*                                                                          */
    703  1.1  christos /* When called, bitcount represents the mask of a new map rule that has     */
    704  1.1  christos /* just been added. This function inserts the bitmask into the array of     */
    705  1.1  christos /* masks to search when searching for a matching map rule for a packet.     */
    706  1.1  christos /* ------------------------------------------------------------------------ */
    707  1.1  christos static void
    708  1.1  christos ipf_nat_add_map_mask(softn, bitcount)
    709  1.1  christos 	ipf_nat_softc_t *softn;
    710  1.1  christos 	int bitcount;
    711  1.1  christos {
    712  1.1  christos 	ipf_nat_add_active(bitcount, softn->ipf_nat_map_active_masks);
    713  1.1  christos 	softn->ipf_nat_map_max++;
    714  1.1  christos }
    715  1.1  christos 
    716  1.1  christos 
    717  1.1  christos /* ------------------------------------------------------------------------ */
    718  1.1  christos /* Function:    ipf_nat_add_rdr_mask                                        */
    719  1.1  christos /* Returns:     Nil                                                         */
    720  1.1  christos /* Parameters:  softn(I)    - pointer to nat context information            */
    721  1.1  christos /*              bitcount(I) - bitcount of mask to add                       */
    722  1.1  christos /*                                                                          */
    723  1.1  christos /* When called, bitcount represents the mask of a new rdr rule that has     */
    724  1.1  christos /* just been added. This function inserts the bitmask into the array of     */
    725  1.1  christos /* masks to search when searching for a matching rdr rule for a packet.     */
    726  1.1  christos /* ------------------------------------------------------------------------ */
    727  1.1  christos static void
    728  1.1  christos ipf_nat_add_rdr_mask(softn, bitcount)
    729  1.1  christos 	ipf_nat_softc_t *softn;
    730  1.1  christos 	int bitcount;
    731  1.1  christos {
    732  1.1  christos 	ipf_nat_add_active(bitcount, softn->ipf_nat_rdr_active_masks);
    733  1.1  christos 	softn->ipf_nat_rdr_max++;
    734  1.1  christos }
    735  1.1  christos 
    736  1.1  christos 
    737  1.1  christos /* ------------------------------------------------------------------------ */
    738  1.1  christos /* Function:    ipf_nat_del_map_mask                                        */
    739  1.1  christos /* Returns:     Nil                                                         */
    740  1.1  christos /* Parameters:  softn(I)    - pointer to nat context information            */
    741  1.1  christos /*              bitcount(I) - bitcount of mask to add                       */
    742  1.1  christos /*                                                                          */
    743  1.1  christos /* This function performs the opposite action to ipf_nat_add_map_mask by    */
    744  1.1  christos /* removing the mask described by bitcount from the active mask array.      */
    745  1.1  christos /* ------------------------------------------------------------------------ */
    746  1.1  christos static void
    747  1.1  christos ipf_nat_del_map_mask(softn, bitcount)
    748  1.1  christos 	ipf_nat_softc_t *softn;
    749  1.1  christos 	int bitcount;
    750  1.1  christos {
    751  1.1  christos 	ipf_nat_del_active(bitcount, softn->ipf_nat_map_active_masks);
    752  1.1  christos 	softn->ipf_nat_map_max--;
    753  1.1  christos }
    754  1.1  christos 
    755  1.1  christos 
    756  1.1  christos /* ------------------------------------------------------------------------ */
    757  1.1  christos /* Function:    ipf_nat_del_rdr_mask                                        */
    758  1.1  christos /* Returns:     Nil                                                         */
    759  1.1  christos /* Parameters:  softn(I)    - pointer to nat context information            */
    760  1.1  christos /*              bitcount(I) - bitcount of mask to add                       */
    761  1.1  christos /*                                                                          */
    762  1.1  christos /* This function performs the opposite action to ipf_nat_add_rdr_mask by    */
    763  1.1  christos /* removing the mask described by bitcount from the active mask array.      */
    764  1.1  christos /* ------------------------------------------------------------------------ */
    765  1.1  christos static void
    766  1.1  christos ipf_nat_del_rdr_mask(softn, bitcount)
    767  1.1  christos 	ipf_nat_softc_t *softn;
    768  1.1  christos 	int bitcount;
    769  1.1  christos {
    770  1.1  christos 	ipf_nat_del_active(bitcount, softn->ipf_nat_rdr_active_masks);
    771  1.1  christos 	softn->ipf_nat_rdr_max--;
    772  1.1  christos }
    773  1.1  christos 
    774  1.1  christos 
    775  1.1  christos /* ------------------------------------------------------------------------ */
    776  1.1  christos /* Function:    ipf_nat_addrdr                                              */
    777  1.1  christos /* Returns:     Nil                                                         */
    778  1.1  christos /* Parameters:  n(I) - pointer to NAT rule to add                           */
    779  1.1  christos /*                                                                          */
    780  1.1  christos /* Adds a redirect rule to the hash table of redirect rules and the list of */
    781  1.1  christos /* loaded NAT rules.  Updates the bitmask indicating which netmasks are in  */
    782  1.1  christos /* use by redirect rules.                                                   */
    783  1.1  christos /* ------------------------------------------------------------------------ */
    784  1.1  christos static void
    785  1.1  christos ipf_nat_addrdr(softn, n)
    786  1.1  christos 	ipf_nat_softc_t *softn;
    787  1.1  christos 	ipnat_t *n;
    788  1.1  christos {
    789  1.1  christos 	ipnat_t **np;
    790  1.1  christos 	u_32_t j;
    791  1.1  christos 	u_int hv;
    792  1.1  christos 	u_int rhv;
    793  1.1  christos 	int k;
    794  1.1  christos 
    795  1.1  christos 	if (n->in_odstatype == FRI_NORMAL) {
    796  1.1  christos 		k = count4bits(n->in_odstmsk);
    797  1.1  christos 		softn->ipf_nat_rdr_masks[k]++;
    798  1.1  christos 		if (softn->ipf_nat_rdr_masks[k] == 1)
    799  1.1  christos 			ipf_nat_add_rdr_mask(softn, k);
    800  1.1  christos 		j = (n->in_odstaddr & n->in_odstmsk);
    801  1.1  christos 		rhv = NAT_HASH_FN(j, 0, 0xffffffff);
    802  1.1  christos 	} else {
    803  1.1  christos 		softn->ipf_nat_rdr_masks[0]++;
    804  1.1  christos 		if (softn->ipf_nat_rdr_masks[0] == 1)
    805  1.1  christos 			ipf_nat_add_rdr_mask(softn, 0);
    806  1.1  christos 		j = 0;
    807  1.1  christos 		rhv = 0;
    808  1.1  christos 	}
    809  1.1  christos 	hv = rhv % softn->ipf_nat_rdrrules_sz;
    810  1.1  christos 	np = softn->ipf_nat_rdr_rules + hv;
    811  1.1  christos 	while (*np != NULL)
    812  1.1  christos 		np = &(*np)->in_rnext;
    813  1.1  christos 	n->in_rnext = NULL;
    814  1.1  christos 	n->in_prnext = np;
    815  1.1  christos 	n->in_hv[0] = hv;
    816  1.1  christos 	*np = n;
    817  1.1  christos }
    818  1.1  christos 
    819  1.1  christos 
    820  1.1  christos /* ------------------------------------------------------------------------ */
    821  1.1  christos /* Function:    ipf_nat_addmap                                              */
    822  1.1  christos /* Returns:     Nil                                                         */
    823  1.1  christos /* Parameters:  n(I) - pointer to NAT rule to add                           */
    824  1.1  christos /*                                                                          */
    825  1.1  christos /* Adds a NAT map rule to the hash table of rules and the list of  loaded   */
    826  1.1  christos /* NAT rules.  Updates the bitmask indicating which netmasks are in use by  */
    827  1.1  christos /* redirect rules.                                                          */
    828  1.1  christos /* ------------------------------------------------------------------------ */
    829  1.1  christos static void
    830  1.1  christos ipf_nat_addmap(softn, n)
    831  1.1  christos 	ipf_nat_softc_t *softn;
    832  1.1  christos 	ipnat_t *n;
    833  1.1  christos {
    834  1.1  christos 	ipnat_t **np;
    835  1.1  christos 	u_32_t j;
    836  1.1  christos 	u_int hv;
    837  1.1  christos 	u_int rhv;
    838  1.1  christos 	int k;
    839  1.1  christos 
    840  1.1  christos 	if (n->in_osrcatype == FRI_NORMAL) {
    841  1.1  christos 		k = count4bits(n->in_osrcmsk);
    842  1.1  christos 		softn->ipf_nat_map_masks[k]++;
    843  1.1  christos 		if (softn->ipf_nat_map_masks[k] == 1)
    844  1.1  christos 			ipf_nat_add_map_mask(softn, k);
    845  1.1  christos 		j = (n->in_osrcaddr & n->in_osrcmsk);
    846  1.1  christos 		rhv = NAT_HASH_FN(j, 0, 0xffffffff);
    847  1.1  christos 	} else {
    848  1.1  christos 		softn->ipf_nat_map_masks[0]++;
    849  1.1  christos 		if (softn->ipf_nat_map_masks[0] == 1)
    850  1.1  christos 			ipf_nat_add_map_mask(softn, 0);
    851  1.1  christos 		j = 0;
    852  1.1  christos 		rhv = 0;
    853  1.1  christos 	}
    854  1.1  christos 	hv = rhv % softn->ipf_nat_maprules_sz;
    855  1.1  christos 	np = softn->ipf_nat_map_rules + hv;
    856  1.1  christos 	while (*np != NULL)
    857  1.1  christos 		np = &(*np)->in_mnext;
    858  1.1  christos 	n->in_mnext = NULL;
    859  1.1  christos 	n->in_pmnext = np;
    860  1.1  christos 	n->in_hv[1] = rhv;
    861  1.1  christos 	*np = n;
    862  1.1  christos }
    863  1.1  christos 
    864  1.1  christos 
    865  1.1  christos /* ------------------------------------------------------------------------ */
    866  1.1  christos /* Function:    ipf_nat_addencap                                            */
    867  1.1  christos /* Returns:     Nil                                                         */
    868  1.1  christos /* Parameters:  n(I) - pointer to NAT rule to add                           */
    869  1.1  christos /*                                                                          */
    870  1.1  christos /* Here we add in a pointer in the NAT rules hash table to match reply      */
    871  1.1  christos /* packets that are encapsulated.  For encap rules that are "out", what we  */
    872  1.1  christos /* will want to match upon will be the source address in the encap rule as  */
    873  1.1  christos /* this is what will become the destination in packets coming back to us.   */
    874  1.1  christos /* For encaps pointing in, it is still the same because it is still the     */
    875  1.1  christos /* reply packet we want to match.                                           */
    876  1.1  christos /* ------------------------------------------------------------------------ */
    877  1.1  christos static void
    878  1.1  christos ipf_nat_addencap(softn, n)
    879  1.1  christos 	ipf_nat_softc_t *softn;
    880  1.1  christos 	ipnat_t *n;
    881  1.1  christos {
    882  1.1  christos 	ipnat_t **np;
    883  1.1  christos 	u_32_t j;
    884  1.1  christos 	u_int hv, rhv;
    885  1.1  christos 	int k;
    886  1.1  christos 
    887  1.1  christos 	k = -1;
    888  1.1  christos 
    889  1.1  christos 	/*
    890  1.1  christos 	 * It is the new source address we're after...
    891  1.1  christos 	 */
    892  1.1  christos 	if (n->in_nsrcatype == FRI_NORMAL) {
    893  1.1  christos 		k = count4bits(n->in_nsrcmsk);
    894  1.1  christos 		j = (n->in_nsrcaddr & n->in_nsrcmsk);
    895  1.1  christos 		rhv = NAT_HASH_FN(j, 0, 0xffffffff);
    896  1.1  christos 	} else {
    897  1.1  christos 		j = 0;
    898  1.1  christos 		rhv = 0;
    899  1.1  christos 	}
    900  1.1  christos 
    901  1.1  christos 	/*
    902  1.1  christos 	 * And place the rules table entry in the reverse spot, so for out
    903  1.1  christos 	 * we use the rdr-links and for rdr, we use the map-links.  This is
    904  1.1  christos 	 * the reverse of how it is used elsewhere...
    905  1.1  christos 	 */
    906  1.1  christos 	if (n->in_redir & NAT_MAP) {
    907  1.1  christos 		softn->ipf_nat_rdr_masks[k]++;
    908  1.1  christos 		if (softn->ipf_nat_rdr_masks[k] == 1)
    909  1.1  christos 			ipf_nat_add_rdr_mask(softn, k);
    910  1.1  christos 		hv = rhv % softn->ipf_nat_maprules_sz;
    911  1.1  christos 		np = softn->ipf_nat_rdr_rules + hv;
    912  1.1  christos 		while (*np != NULL)
    913  1.1  christos 			np = &(*np)->in_rnext;
    914  1.1  christos 		n->in_rnext = NULL;
    915  1.1  christos 		n->in_prnext = np;
    916  1.1  christos 		n->in_hv[0] = rhv;
    917  1.1  christos 		*np = n;
    918  1.1  christos 	}
    919  1.1  christos 	if (n->in_redir & NAT_REDIRECT) {
    920  1.1  christos 		softn->ipf_nat_map_masks[k]++;
    921  1.1  christos 		if (softn->ipf_nat_map_masks[k] == 1)
    922  1.1  christos 			ipf_nat_add_map_mask(softn, k);
    923  1.1  christos 		hv = rhv % softn->ipf_nat_rdrrules_sz;
    924  1.1  christos 		np = softn->ipf_nat_map_rules + hv;
    925  1.1  christos 		while (*np != NULL)
    926  1.1  christos 			np = &(*np)->in_mnext;
    927  1.1  christos 		n->in_mnext = NULL;
    928  1.1  christos 		n->in_pmnext = np;
    929  1.1  christos 		n->in_hv[1] = rhv;
    930  1.1  christos 		*np = n;
    931  1.1  christos 	}
    932  1.1  christos 
    933  1.1  christos 	/* TRACE(n, hv, k) */
    934  1.1  christos }
    935  1.1  christos 
    936  1.1  christos 
    937  1.1  christos /* ------------------------------------------------------------------------ */
    938  1.1  christos /* Function:    ipf_nat_delrdr                                              */
    939  1.1  christos /* Returns:     Nil                                                         */
    940  1.1  christos /* Parameters:  n(I) - pointer to NAT rule to delete                        */
    941  1.1  christos /*                                                                          */
    942  1.1  christos /* Removes a redirect rule from the hash table of redirect rules.           */
    943  1.1  christos /* ------------------------------------------------------------------------ */
    944  1.1  christos void
    945  1.1  christos ipf_nat_delrdr(softn, n)
    946  1.1  christos 	ipf_nat_softc_t *softn;
    947  1.1  christos 	ipnat_t *n;
    948  1.1  christos {
    949  1.1  christos 	if (n->in_odstatype == FRI_NORMAL) {
    950  1.1  christos 		int k = count4bits(n->in_odstmsk);
    951  1.1  christos 		softn->ipf_nat_rdr_masks[k]--;
    952  1.1  christos 		if (softn->ipf_nat_rdr_masks[k] == 0)
    953  1.1  christos 			ipf_nat_del_rdr_mask(softn, k);
    954  1.1  christos 	} else {
    955  1.1  christos 		softn->ipf_nat_rdr_masks[0]--;
    956  1.1  christos 		if (softn->ipf_nat_rdr_masks[0] == 0)
    957  1.1  christos 			ipf_nat_del_rdr_mask(softn, 0);
    958  1.1  christos 	}
    959  1.1  christos 	if (n->in_rnext)
    960  1.1  christos 		n->in_rnext->in_prnext = n->in_prnext;
    961  1.1  christos 	*n->in_prnext = n->in_rnext;
    962  1.1  christos }
    963  1.1  christos 
    964  1.1  christos 
    965  1.1  christos /* ------------------------------------------------------------------------ */
    966  1.1  christos /* Function:    ipf_nat_delmap                                              */
    967  1.1  christos /* Returns:     Nil                                                         */
    968  1.1  christos /* Parameters:  n(I) - pointer to NAT rule to delete                        */
    969  1.1  christos /*                                                                          */
    970  1.1  christos /* Removes a NAT map rule from the hash table of NAT map rules.             */
    971  1.1  christos /* ------------------------------------------------------------------------ */
    972  1.1  christos void
    973  1.1  christos ipf_nat_delmap(softn, n)
    974  1.1  christos 	ipf_nat_softc_t *softn;
    975  1.1  christos 	ipnat_t *n;
    976  1.1  christos {
    977  1.1  christos 	if (n->in_osrcatype == FRI_NORMAL) {
    978  1.1  christos 		int k = count4bits(n->in_osrcmsk);
    979  1.1  christos 		softn->ipf_nat_map_masks[k]--;
    980  1.1  christos 		if (softn->ipf_nat_map_masks[k] == 0)
    981  1.1  christos 			ipf_nat_del_map_mask(softn, k);
    982  1.1  christos 	} else {
    983  1.1  christos 		softn->ipf_nat_map_masks[0]--;
    984  1.1  christos 		if (softn->ipf_nat_map_masks[0] == 0)
    985  1.1  christos 			ipf_nat_del_map_mask(softn, 0);
    986  1.1  christos 	}
    987  1.1  christos 	if (n->in_mnext != NULL)
    988  1.1  christos 		n->in_mnext->in_pmnext = n->in_pmnext;
    989  1.1  christos 	*n->in_pmnext = n->in_mnext;
    990  1.1  christos }
    991  1.1  christos 
    992  1.1  christos 
    993  1.1  christos /* ------------------------------------------------------------------------ */
    994  1.1  christos /* Function:    ipf_nat_hostmap                                             */
    995  1.1  christos /* Returns:     struct hostmap* - NULL if no hostmap could be created,      */
    996  1.1  christos /*                                else a pointer to the hostmapping to use  */
    997  1.1  christos /* Parameters:  np(I)   - pointer to NAT rule                               */
    998  1.1  christos /*              real(I) - real IP address                                   */
    999  1.1  christos /*              map(I)  - mapped IP address                                 */
   1000  1.1  christos /*              port(I) - destination port number                           */
   1001  1.1  christos /* Write Locks: ipf_nat                                                     */
   1002  1.1  christos /*                                                                          */
   1003  1.1  christos /* Check if an ip address has already been allocated for a given mapping    */
   1004  1.1  christos /* that is not doing port based translation.  If is not yet allocated, then */
   1005  1.1  christos /* create a new entry if a non-NULL NAT rule pointer has been supplied.     */
   1006  1.1  christos /* ------------------------------------------------------------------------ */
   1007  1.1  christos static struct hostmap *
   1008  1.1  christos ipf_nat_hostmap(softn, np, src, dst, map, port)
   1009  1.1  christos 	ipf_nat_softc_t *softn;
   1010  1.1  christos 	ipnat_t *np;
   1011  1.1  christos 	struct in_addr src;
   1012  1.1  christos 	struct in_addr dst;
   1013  1.1  christos 	struct in_addr map;
   1014  1.1  christos 	u_32_t port;
   1015  1.1  christos {
   1016  1.1  christos 	hostmap_t *hm;
   1017  1.1  christos 	u_int hv, rhv;
   1018  1.1  christos 
   1019  1.1  christos 	hv = (src.s_addr ^ dst.s_addr);
   1020  1.1  christos 	hv += src.s_addr;
   1021  1.1  christos 	hv += dst.s_addr;
   1022  1.1  christos 	rhv = hv;
   1023  1.1  christos 	hv %= softn->ipf_nat_hostmap_sz;
   1024  1.1  christos 	for (hm = softn->ipf_hm_maptable[hv]; hm; hm = hm->hm_hnext)
   1025  1.1  christos 		if ((hm->hm_osrcip.s_addr == src.s_addr) &&
   1026  1.1  christos 		    (hm->hm_odstip.s_addr == dst.s_addr) &&
   1027  1.1  christos 		    ((np == NULL) || (np == hm->hm_ipnat)) &&
   1028  1.1  christos 		    ((port == 0) || (port == hm->hm_port))) {
   1029  1.1  christos 			softn->ipf_nat_stats.ns_hm_addref++;
   1030  1.1  christos 			hm->hm_ref++;
   1031  1.1  christos 			return hm;
   1032  1.1  christos 		}
   1033  1.1  christos 
   1034  1.1  christos 	if (np == NULL) {
   1035  1.1  christos 		softn->ipf_nat_stats.ns_hm_nullnp++;
   1036  1.1  christos 		return NULL;
   1037  1.1  christos 	}
   1038  1.1  christos 
   1039  1.1  christos 	KMALLOC(hm, hostmap_t *);
   1040  1.1  christos 	if (hm) {
   1041  1.1  christos 		hm->hm_next = softn->ipf_hm_maplist;
   1042  1.1  christos 		hm->hm_pnext = &softn->ipf_hm_maplist;
   1043  1.1  christos 		if (softn->ipf_hm_maplist != NULL)
   1044  1.1  christos 			softn->ipf_hm_maplist->hm_pnext = &hm->hm_next;
   1045  1.1  christos 		softn->ipf_hm_maplist = hm;
   1046  1.1  christos 		hm->hm_hnext = softn->ipf_hm_maptable[hv];
   1047  1.1  christos 		hm->hm_phnext = softn->ipf_hm_maptable + hv;
   1048  1.1  christos 		if (softn->ipf_hm_maptable[hv] != NULL)
   1049  1.1  christos 			softn->ipf_hm_maptable[hv]->hm_phnext = &hm->hm_hnext;
   1050  1.1  christos 		softn->ipf_hm_maptable[hv] = hm;
   1051  1.1  christos 		hm->hm_ipnat = np;
   1052  1.1  christos 		hm->hm_osrcip = src;
   1053  1.1  christos 		hm->hm_odstip = dst;
   1054  1.1  christos 		hm->hm_nsrcip = map;
   1055  1.1  christos 		hm->hm_ndstip.s_addr = 0;
   1056  1.1  christos 		hm->hm_ref = 1;
   1057  1.1  christos 		hm->hm_port = port;
   1058  1.1  christos 		hm->hm_hv = rhv;
   1059  1.1  christos 		hm->hm_v = 4;
   1060  1.1  christos 		softn->ipf_nat_stats.ns_hm_new++;
   1061  1.1  christos 	} else {
   1062  1.1  christos 		softn->ipf_nat_stats.ns_hm_newfail++;
   1063  1.1  christos 	}
   1064  1.1  christos 	return hm;
   1065  1.1  christos }
   1066  1.1  christos 
   1067  1.1  christos 
   1068  1.1  christos /* ------------------------------------------------------------------------ */
   1069  1.1  christos /* Function:    ipf_nat_hostmapdel                                          */
   1070  1.1  christos /* Returns:     Nil                                                         */
   1071  1.1  christos /* Parameters:  hmp(I) - pointer to hostmap structure pointer               */
   1072  1.1  christos /* Write Locks: ipf_nat                                                     */
   1073  1.1  christos /*                                                                          */
   1074  1.1  christos /* Decrement the references to this hostmap structure by one.  If this      */
   1075  1.1  christos /* reaches zero then remove it and free it.                                 */
   1076  1.1  christos /* ------------------------------------------------------------------------ */
   1077  1.1  christos void
   1078  1.1  christos ipf_nat_hostmapdel(hmp)
   1079  1.1  christos 	struct hostmap **hmp;
   1080  1.1  christos {
   1081  1.1  christos 	struct hostmap *hm;
   1082  1.1  christos 
   1083  1.1  christos 	hm = *hmp;
   1084  1.1  christos 	*hmp = NULL;
   1085  1.1  christos 
   1086  1.1  christos 	hm->hm_ref--;
   1087  1.1  christos 	if (hm->hm_ref == 0) {
   1088  1.1  christos 		if (hm->hm_hnext)
   1089  1.1  christos 			hm->hm_hnext->hm_phnext = hm->hm_phnext;
   1090  1.1  christos 		*hm->hm_phnext = hm->hm_hnext;
   1091  1.1  christos 		if (hm->hm_next)
   1092  1.1  christos 			hm->hm_next->hm_pnext = hm->hm_pnext;
   1093  1.1  christos 		*hm->hm_pnext = hm->hm_next;
   1094  1.1  christos 		KFREE(hm);
   1095  1.1  christos 	}
   1096  1.1  christos }
   1097  1.1  christos 
   1098  1.1  christos 
   1099  1.1  christos /* ------------------------------------------------------------------------ */
   1100  1.1  christos /* Function:    ipf_fix_outcksum                                            */
   1101  1.1  christos /* Returns:     Nil                                                         */
   1102  1.1  christos /* Parameters:  fin(I) - pointer to packet information                      */
   1103  1.1  christos /*              sp(I)  - location of 16bit checksum to update               */
   1104  1.1  christos /*              n((I)  - amount to adjust checksum by                       */
   1105  1.1  christos /*                                                                          */
   1106  1.1  christos /* Adjusts the 16bit checksum by "n" for packets going out.                 */
   1107  1.1  christos /* ------------------------------------------------------------------------ */
   1108  1.1  christos void
   1109  1.1  christos ipf_fix_outcksum(fin, sp, n)
   1110  1.1  christos 	fr_info_t *fin;
   1111  1.1  christos 	u_short *sp;
   1112  1.1  christos 	u_32_t n;
   1113  1.1  christos {
   1114  1.1  christos 	u_short sumshort;
   1115  1.1  christos 	u_32_t sum1;
   1116  1.1  christos 
   1117  1.1  christos 	if (n == 0)
   1118  1.1  christos 		return;
   1119  1.1  christos 
   1120  1.1  christos 	if (n & NAT_HW_CKSUM) {
   1121  1.1  christos # if SOLARIS && defined(_KERNEL) && defined(NET_HCK_NONE)
   1122  1.1  christos 		*sp = (n + htons(fin->fin_dlen)) & 0xffff;
   1123  1.1  christos 		return;
   1124  1.1  christos #else
   1125  1.1  christos 		n &= 0xffff;
   1126  1.1  christos 		n += fin->fin_dlen;
   1127  1.1  christos 		n = (n & 0xffff) + (n >> 16);
   1128  1.1  christos 		*sp = n & 0xffff;
   1129  1.1  christos #endif
   1130  1.1  christos 		return;
   1131  1.1  christos 	}
   1132  1.1  christos 	sum1 = (~ntohs(*sp)) & 0xffff;
   1133  1.1  christos 	sum1 += (n);
   1134  1.1  christos 	sum1 = (sum1 >> 16) + (sum1 & 0xffff);
   1135  1.1  christos 	/* Again */
   1136  1.1  christos 	sum1 = (sum1 >> 16) + (sum1 & 0xffff);
   1137  1.1  christos 	sumshort = ~(u_short)sum1;
   1138  1.1  christos 	*(sp) = htons(sumshort);
   1139  1.1  christos }
   1140  1.1  christos 
   1141  1.1  christos 
   1142  1.1  christos /* ------------------------------------------------------------------------ */
   1143  1.1  christos /* Function:    ipf_fix_incksum                                             */
   1144  1.1  christos /* Returns:     Nil                                                         */
   1145  1.1  christos /* Parameters:  fin(I) - pointer to packet information                      */
   1146  1.1  christos /*              sp(I)  - location of 16bit checksum to update               */
   1147  1.1  christos /*              n((I)  - amount to adjust checksum by                       */
   1148  1.1  christos /*                                                                          */
   1149  1.1  christos /* Adjusts the 16bit checksum by "n" for packets going in.                  */
   1150  1.1  christos /* ------------------------------------------------------------------------ */
   1151  1.1  christos void
   1152  1.1  christos ipf_fix_incksum(fin, sp, n)
   1153  1.1  christos 	fr_info_t *fin;
   1154  1.1  christos 	u_short *sp;
   1155  1.1  christos 	u_32_t n;
   1156  1.1  christos {
   1157  1.1  christos 	u_short sumshort;
   1158  1.1  christos 	u_32_t sum1;
   1159  1.1  christos 
   1160  1.1  christos 	if (n == 0)
   1161  1.1  christos 		return;
   1162  1.1  christos 
   1163  1.1  christos 	if (n & NAT_HW_CKSUM) {
   1164  1.1  christos 		n &= 0xffff;
   1165  1.1  christos 		n += fin->fin_dlen;
   1166  1.1  christos 		n = (n & 0xffff) + (n >> 16);
   1167  1.1  christos 		*sp = n & 0xffff;
   1168  1.1  christos 		return;
   1169  1.1  christos 	}
   1170  1.1  christos 	sum1 = (~ntohs(*sp)) & 0xffff;
   1171  1.1  christos 	sum1 += ~(n) & 0xffff;
   1172  1.1  christos 	sum1 = (sum1 >> 16) + (sum1 & 0xffff);
   1173  1.1  christos 	/* Again */
   1174  1.1  christos 	sum1 = (sum1 >> 16) + (sum1 & 0xffff);
   1175  1.1  christos 	sumshort = ~(u_short)sum1;
   1176  1.1  christos 	*(sp) = htons(sumshort);
   1177  1.1  christos }
   1178  1.1  christos 
   1179  1.1  christos 
   1180  1.1  christos /* ------------------------------------------------------------------------ */
   1181  1.1  christos /* Function:    ipf_fix_datacksum                                           */
   1182  1.1  christos /* Returns:     Nil                                                         */
   1183  1.1  christos /* Parameters:  sp(I)  - location of 16bit checksum to update               */
   1184  1.1  christos /*              n((I)  - amount to adjust checksum by                       */
   1185  1.1  christos /*                                                                          */
   1186  1.1  christos /* Fix_datacksum is used *only* for the adjustments of checksums in the     */
   1187  1.1  christos /* data section of an IP packet.                                            */
   1188  1.1  christos /*                                                                          */
   1189  1.1  christos /* The only situation in which you need to do this is when NAT'ing an       */
   1190  1.1  christos /* ICMP error message. Such a message, contains in its body the IP header   */
   1191  1.1  christos /* of the original IP packet, that causes the error.                        */
   1192  1.1  christos /*                                                                          */
   1193  1.1  christos /* You can't use fix_incksum or fix_outcksum in that case, because for the  */
   1194  1.1  christos /* kernel the data section of the ICMP error is just data, and no special   */
   1195  1.1  christos /* processing like hardware cksum or ntohs processing have been done by the */
   1196  1.1  christos /* kernel on the data section.                                              */
   1197  1.1  christos /* ------------------------------------------------------------------------ */
   1198  1.1  christos void
   1199  1.1  christos ipf_fix_datacksum(sp, n)
   1200  1.1  christos 	u_short *sp;
   1201  1.1  christos 	u_32_t n;
   1202  1.1  christos {
   1203  1.1  christos 	u_short sumshort;
   1204  1.1  christos 	u_32_t sum1;
   1205  1.1  christos 
   1206  1.1  christos 	if (n == 0)
   1207  1.1  christos 		return;
   1208  1.1  christos 
   1209  1.1  christos 	sum1 = (~ntohs(*sp)) & 0xffff;
   1210  1.1  christos 	sum1 += (n);
   1211  1.1  christos 	sum1 = (sum1 >> 16) + (sum1 & 0xffff);
   1212  1.1  christos 	/* Again */
   1213  1.1  christos 	sum1 = (sum1 >> 16) + (sum1 & 0xffff);
   1214  1.1  christos 	sumshort = ~(u_short)sum1;
   1215  1.1  christos 	*(sp) = htons(sumshort);
   1216  1.1  christos }
   1217  1.1  christos 
   1218  1.1  christos 
   1219  1.1  christos /* ------------------------------------------------------------------------ */
   1220  1.1  christos /* Function:    ipf_nat_ioctl                                               */
   1221  1.1  christos /* Returns:     int - 0 == success, != 0 == failure                         */
   1222  1.1  christos /* Parameters:  data(I) - pointer to ioctl data                             */
   1223  1.1  christos /*              cmd(I)  - ioctl command integer                             */
   1224  1.1  christos /*              mode(I) - file mode bits used with open                     */
   1225  1.1  christos /*                                                                          */
   1226  1.1  christos /* Processes an ioctl call made to operate on the IP Filter NAT device.     */
   1227  1.1  christos /* ------------------------------------------------------------------------ */
   1228  1.1  christos int
   1229  1.1  christos ipf_nat_ioctl(softc, data, cmd, mode, uid, ctx)
   1230  1.1  christos 	ipf_main_softc_t *softc;
   1231  1.1  christos 	ioctlcmd_t cmd;
   1232  1.1  christos 	caddr_t data;
   1233  1.1  christos 	int mode, uid;
   1234  1.1  christos 	void *ctx;
   1235  1.1  christos {
   1236  1.1  christos 	ipf_nat_softc_t *softn = softc->ipf_nat_soft;
   1237  1.1  christos 	ipnat_t *nat, *nt, *n = NULL, **np = NULL;
   1238  1.1  christos 	int error = 0, ret, arg, getlock;
   1239  1.1  christos 	ipnat_t natd;
   1240  1.1  christos 	SPL_INT(s);
   1241  1.1  christos 
   1242  1.1  christos #if BSD_GE_YEAR(199306) && defined(_KERNEL)
   1243  1.1  christos # if NETBSD_GE_REV(399002000)
   1244  1.1  christos 	if ((mode & FWRITE) &&
   1245  1.1  christos 	     kauth_authorize_network(curlwp->l_cred, KAUTH_NETWORK_FIREWALL,
   1246  1.1  christos 				     KAUTH_REQ_NETWORK_FIREWALL_FW,
   1247  1.1  christos 				     NULL, NULL, NULL))
   1248  1.1  christos # else
   1249  1.1  christos #  if defined(__FreeBSD_version) && (__FreeBSD_version >= 500034)
   1250  1.1  christos 	if (securelevel_ge(curthread->td_ucred, 3) && (mode & FWRITE))
   1251  1.1  christos #  else
   1252  1.1  christos 	if ((securelevel >= 3) && (mode & FWRITE))
   1253  1.1  christos #  endif
   1254  1.1  christos 		return EPERM;
   1255  1.1  christos # endif
   1256  1.1  christos 	{
   1257  1.1  christos 		IPFERROR(60001);
   1258  1.1  christos 		return EPERM;
   1259  1.1  christos 	}
   1260  1.1  christos #endif
   1261  1.1  christos 
   1262  1.1  christos #if defined(__osf__) && defined(_KERNEL)
   1263  1.1  christos 	getlock = 0;
   1264  1.1  christos #else
   1265  1.1  christos 	getlock = (mode & NAT_LOCKHELD) ? 0 : 1;
   1266  1.1  christos #endif
   1267  1.1  christos 
   1268  1.1  christos 	nat = NULL;     /* XXX gcc -Wuninitialized */
   1269  1.1  christos 	nt = NULL;
   1270  1.1  christos 
   1271  1.1  christos 	if ((cmd == (ioctlcmd_t)SIOCADNAT) || (cmd == (ioctlcmd_t)SIOCRMNAT)) {
   1272  1.1  christos 		if (mode & NAT_SYSSPACE) {
   1273  1.1  christos 			bcopy(data, (char *)&natd, sizeof(natd));
   1274  1.1  christos 			nat = &natd;
   1275  1.1  christos 			error = 0;
   1276  1.1  christos 		} else {
   1277  1.1  christos 			bzero(&natd, sizeof(natd));
   1278  1.1  christos 			error = ipf_inobj(softc, data, NULL, &natd,
   1279  1.1  christos 					  IPFOBJ_IPNAT);
   1280  1.1  christos 			if (error != 0)
   1281  1.1  christos 				goto done;
   1282  1.1  christos 
   1283  1.1  christos 			if (natd.in_size < sizeof(ipnat_t)) {
   1284  1.1  christos 				error = EINVAL;
   1285  1.1  christos 				goto done;
   1286  1.1  christos 			}
   1287  1.1  christos 			KMALLOCS(nt, ipnat_t *, natd.in_size);
   1288  1.1  christos 			if (nt == NULL) {
   1289  1.1  christos 				IPFERROR(60070);
   1290  1.1  christos 				error = ENOMEM;
   1291  1.1  christos 				goto done;
   1292  1.1  christos 			}
   1293  1.1  christos 			bzero(nt, natd.in_size);
   1294  1.1  christos 			error = ipf_inobjsz(softc, data, nt, IPFOBJ_IPNAT,
   1295  1.1  christos 					    natd.in_size);
   1296  1.1  christos 			if (error)
   1297  1.1  christos 				goto done;
   1298  1.1  christos 			nat = nt;
   1299  1.1  christos 		}
   1300  1.1  christos 
   1301  1.1  christos 		/*
   1302  1.1  christos 		 * For add/delete, look to see if the NAT entry is
   1303  1.1  christos 		 * already present
   1304  1.1  christos 		 */
   1305  1.1  christos 		nat->in_flags &= IPN_USERFLAGS;
   1306  1.1  christos 		if ((nat->in_redir & NAT_MAPBLK) == 0) {
   1307  1.1  christos 			if (nat->in_osrcatype == FRI_NORMAL ||
   1308  1.1  christos 			    nat->in_osrcatype == FRI_NONE)
   1309  1.1  christos 				nat->in_osrcaddr &= nat->in_osrcmsk;
   1310  1.1  christos 			if (nat->in_odstatype == FRI_NORMAL ||
   1311  1.1  christos 			    nat->in_odstatype == FRI_NONE)
   1312  1.1  christos 				nat->in_odstaddr &= nat->in_odstmsk;
   1313  1.1  christos 			if ((nat->in_flags & (IPN_SPLIT|IPN_SIPRANGE)) == 0) {
   1314  1.1  christos 				if (nat->in_nsrcatype == FRI_NORMAL)
   1315  1.1  christos 					nat->in_nsrcaddr &= nat->in_nsrcmsk;
   1316  1.1  christos 				if (nat->in_ndstatype == FRI_NORMAL)
   1317  1.1  christos 					nat->in_ndstaddr &= nat->in_ndstmsk;
   1318  1.1  christos 			}
   1319  1.1  christos 		}
   1320  1.1  christos 		MUTEX_ENTER(&softn->ipf_nat_io);
   1321  1.1  christos 		for (np = &softn->ipf_nat_list; ((n = *np) != NULL);
   1322  1.1  christos 		     np = &n->in_next)
   1323  1.1  christos 			if (!bcmp((char *)&nat->in_v, (char *)&n->in_v,
   1324  1.1  christos 					IPN_CMPSIZ))
   1325  1.1  christos 				break;
   1326  1.1  christos 	}
   1327  1.1  christos 
   1328  1.1  christos 	switch (cmd)
   1329  1.1  christos 	{
   1330  1.1  christos #ifdef  IPFILTER_LOG
   1331  1.1  christos 	case SIOCIPFFB :
   1332  1.1  christos 	{
   1333  1.1  christos 		int tmp;
   1334  1.1  christos 
   1335  1.1  christos 		if (!(mode & FWRITE)) {
   1336  1.1  christos 			IPFERROR(60002);
   1337  1.1  christos 			error = EPERM;
   1338  1.1  christos 		} else {
   1339  1.1  christos 			tmp = ipf_log_clear(softc, IPL_LOGNAT);
   1340  1.1  christos 			error = BCOPYOUT(&tmp, data, sizeof(tmp));
   1341  1.1  christos 			if (error != 0) {
   1342  1.1  christos 				IPFERROR(60057);
   1343  1.1  christos 				error = EFAULT;
   1344  1.1  christos 			}
   1345  1.1  christos 		}
   1346  1.1  christos 		break;
   1347  1.1  christos 	}
   1348  1.1  christos 
   1349  1.1  christos 	case SIOCSETLG :
   1350  1.1  christos 		if (!(mode & FWRITE)) {
   1351  1.1  christos 			IPFERROR(60003);
   1352  1.1  christos 			error = EPERM;
   1353  1.1  christos 		} else {
   1354  1.1  christos 			error = BCOPYIN(data, &softn->ipf_nat_logging,
   1355  1.1  christos 					sizeof(softn->ipf_nat_logging));
   1356  1.1  christos 			if (error != 0)
   1357  1.1  christos 				error = EFAULT;
   1358  1.1  christos 		}
   1359  1.1  christos 		break;
   1360  1.1  christos 
   1361  1.1  christos 	case SIOCGETLG :
   1362  1.1  christos 		error = BCOPYOUT(&softn->ipf_nat_logging, data,
   1363  1.1  christos 				 sizeof(softn->ipf_nat_logging));
   1364  1.1  christos 		if (error != 0) {
   1365  1.1  christos 			IPFERROR(60004);
   1366  1.1  christos 			error = EFAULT;
   1367  1.1  christos 		}
   1368  1.1  christos 		break;
   1369  1.1  christos 
   1370  1.1  christos 	case FIONREAD :
   1371  1.1  christos 		arg = ipf_log_bytesused(softc, IPL_LOGNAT);
   1372  1.1  christos 		error = BCOPYOUT(&arg, data, sizeof(arg));
   1373  1.1  christos 		if (error != 0) {
   1374  1.1  christos 			IPFERROR(60005);
   1375  1.1  christos 			error = EFAULT;
   1376  1.1  christos 		}
   1377  1.1  christos 		break;
   1378  1.1  christos #endif
   1379  1.1  christos 	case SIOCADNAT :
   1380  1.1  christos 		if (!(mode & FWRITE)) {
   1381  1.1  christos 			IPFERROR(60006);
   1382  1.1  christos 			error = EPERM;
   1383  1.1  christos 		} else if (n != NULL) {
   1384  1.1  christos 			IPFERROR(60007);
   1385  1.1  christos 			error = EEXIST;
   1386  1.1  christos 		} else if (nt == NULL) {
   1387  1.1  christos 			IPFERROR(60008);
   1388  1.1  christos 			error = ENOMEM;
   1389  1.1  christos 		}
   1390  1.1  christos 		if (error != 0) {
   1391  1.1  christos 			MUTEX_EXIT(&softn->ipf_nat_io);
   1392  1.1  christos 			break;
   1393  1.1  christos 		}
   1394  1.1  christos 		if (nat != nt)
   1395  1.1  christos 			bcopy((char *)nat, (char *)nt, sizeof(*n));
   1396  1.1  christos 		error = ipf_nat_siocaddnat(softc, softn, nt, np, getlock);
   1397  1.1  christos 		MUTEX_EXIT(&softn->ipf_nat_io);
   1398  1.1  christos 		if (error == 0)
   1399  1.1  christos 			nt = NULL;
   1400  1.1  christos 		break;
   1401  1.1  christos 
   1402  1.1  christos 	case SIOCRMNAT :
   1403  1.1  christos 		if (!(mode & FWRITE)) {
   1404  1.1  christos 			IPFERROR(60009);
   1405  1.1  christos 			error = EPERM;
   1406  1.1  christos 			n = NULL;
   1407  1.1  christos 		} else if (n == NULL) {
   1408  1.1  christos 			IPFERROR(60010);
   1409  1.1  christos 			error = ESRCH;
   1410  1.1  christos 		}
   1411  1.1  christos 
   1412  1.1  christos 		if (error != 0) {
   1413  1.1  christos 			MUTEX_EXIT(&softn->ipf_nat_io);
   1414  1.1  christos 			break;
   1415  1.1  christos 		}
   1416  1.1  christos 		ipf_nat_siocdelnat(softc, softn, n, np, getlock);
   1417  1.1  christos 
   1418  1.1  christos 		MUTEX_EXIT(&softn->ipf_nat_io);
   1419  1.1  christos 		n = NULL;
   1420  1.1  christos 		break;
   1421  1.1  christos 
   1422  1.1  christos 	case SIOCGNATS :
   1423  1.1  christos 	    {
   1424  1.1  christos 		natstat_t *nsp = &softn->ipf_nat_stats;
   1425  1.1  christos 
   1426  1.1  christos 		nsp->ns_side[0].ns_table = softn->ipf_nat_table[0];
   1427  1.1  christos 		nsp->ns_side[1].ns_table = softn->ipf_nat_table[1];
   1428  1.1  christos 		nsp->ns_list = softn->ipf_nat_list;
   1429  1.1  christos 		nsp->ns_maptable = softn->ipf_hm_maptable;
   1430  1.1  christos 		nsp->ns_maplist = softn->ipf_hm_maplist;
   1431  1.1  christos 		nsp->ns_nattab_sz = softn->ipf_nat_table_sz;
   1432  1.1  christos 		nsp->ns_nattab_max = softn->ipf_nat_table_max;
   1433  1.1  christos 		nsp->ns_rultab_sz = softn->ipf_nat_maprules_sz;
   1434  1.1  christos 		nsp->ns_rdrtab_sz = softn->ipf_nat_rdrrules_sz;
   1435  1.1  christos 		nsp->ns_hostmap_sz = softn->ipf_nat_hostmap_sz;
   1436  1.1  christos 		nsp->ns_instances = softn->ipf_nat_instances;
   1437  1.1  christos 		nsp->ns_ticks = softc->ipf_ticks;
   1438  1.1  christos #ifdef IPFILTER_LOGGING
   1439  1.1  christos 		nsp->ns_log_ok = ipf_log_logok(softc, IPF_LOGNAT);
   1440  1.1  christos 		nsp->ns_log_fail = ipf_log_failures(softc, IPF_LOGNAT);
   1441  1.1  christos #else
   1442  1.1  christos 		nsp->ns_log_ok = 0;
   1443  1.1  christos 		nsp->ns_log_fail = 0;
   1444  1.1  christos #endif
   1445  1.1  christos 		error = ipf_outobj(softc, data, nsp, IPFOBJ_NATSTAT);
   1446  1.1  christos 		break;
   1447  1.1  christos 	    }
   1448  1.1  christos 
   1449  1.1  christos 	case SIOCGNATL :
   1450  1.1  christos 	    {
   1451  1.1  christos 		natlookup_t nl;
   1452  1.1  christos 
   1453  1.1  christos 		error = ipf_inobj(softc, data, NULL, &nl, IPFOBJ_NATLOOKUP);
   1454  1.1  christos 		if (error == 0) {
   1455  1.1  christos 			void *ptr;
   1456  1.1  christos 
   1457  1.1  christos 			if (getlock) {
   1458  1.1  christos 				READ_ENTER(&softc->ipf_nat);
   1459  1.1  christos 			}
   1460  1.1  christos 
   1461  1.1  christos 			switch (nl.nl_v)
   1462  1.1  christos 			{
   1463  1.1  christos 			case 4 :
   1464  1.1  christos 				ptr = ipf_nat_lookupredir(&nl);
   1465  1.1  christos 				break;
   1466  1.1  christos #ifdef USE_INET6
   1467  1.1  christos 			case 6 :
   1468  1.1  christos 				ptr = ipf_nat6_lookupredir(&nl);
   1469  1.1  christos 				break;
   1470  1.1  christos #endif
   1471  1.1  christos 			default:
   1472  1.1  christos 				ptr = NULL;
   1473  1.1  christos 				break;
   1474  1.1  christos 			}
   1475  1.1  christos 
   1476  1.1  christos 			if (getlock) {
   1477  1.1  christos 				RWLOCK_EXIT(&softc->ipf_nat);
   1478  1.1  christos 			}
   1479  1.1  christos 			if (ptr != NULL) {
   1480  1.1  christos 				error = ipf_outobj(softc, data, &nl,
   1481  1.1  christos 						   IPFOBJ_NATLOOKUP);
   1482  1.1  christos 			} else {
   1483  1.1  christos 				IPFERROR(60011);
   1484  1.1  christos 				error = ESRCH;
   1485  1.1  christos 			}
   1486  1.1  christos 		}
   1487  1.1  christos 		break;
   1488  1.1  christos 	    }
   1489  1.1  christos 
   1490  1.1  christos 	case SIOCIPFFL :	/* old SIOCFLNAT & SIOCCNATL */
   1491  1.1  christos 		if (!(mode & FWRITE)) {
   1492  1.1  christos 			IPFERROR(60012);
   1493  1.1  christos 			error = EPERM;
   1494  1.1  christos 			break;
   1495  1.1  christos 		}
   1496  1.1  christos 		if (getlock) {
   1497  1.1  christos 			WRITE_ENTER(&softc->ipf_nat);
   1498  1.1  christos 		}
   1499  1.1  christos 
   1500  1.1  christos 		error = BCOPYIN(data, &arg, sizeof(arg));
   1501  1.1  christos 		if (error != 0) {
   1502  1.1  christos 			IPFERROR(60013);
   1503  1.1  christos 			error = EFAULT;
   1504  1.1  christos 		} else {
   1505  1.1  christos 			if (arg == 0)
   1506  1.1  christos 				ret = ipf_nat_flushtable(softc, softn);
   1507  1.1  christos 			else if (arg == 1)
   1508  1.1  christos 				ret = ipf_nat_clearlist(softc, softn);
   1509  1.1  christos 			else
   1510  1.1  christos 				ret = ipf_nat_extraflush(softc, softn, arg);
   1511  1.1  christos 			ipf_proxy_flush(softc->ipf_proxy_soft, arg);
   1512  1.1  christos 		}
   1513  1.1  christos 
   1514  1.1  christos 		if (getlock) {
   1515  1.1  christos 			RWLOCK_EXIT(&softc->ipf_nat);
   1516  1.1  christos 		}
   1517  1.1  christos 		if (error == 0) {
   1518  1.1  christos 			error = BCOPYOUT(&ret, data, sizeof(ret));
   1519  1.1  christos 		}
   1520  1.1  christos 		break;
   1521  1.1  christos 
   1522  1.1  christos 	case SIOCMATCHFLUSH :
   1523  1.1  christos 		if (!(mode & FWRITE)) {
   1524  1.1  christos 			IPFERROR(60014);
   1525  1.1  christos 			error = EPERM;
   1526  1.1  christos 			break;
   1527  1.1  christos 		}
   1528  1.1  christos 		if (getlock) {
   1529  1.1  christos 			WRITE_ENTER(&softc->ipf_nat);
   1530  1.1  christos 		}
   1531  1.1  christos 
   1532  1.1  christos 		error = ipf_nat_matchflush(softc, softn, data);
   1533  1.1  christos 
   1534  1.1  christos 		if (getlock) {
   1535  1.1  christos 			RWLOCK_EXIT(&softc->ipf_nat);
   1536  1.1  christos 		}
   1537  1.1  christos 		break;
   1538  1.1  christos 
   1539  1.1  christos 	case SIOCPROXY :
   1540  1.1  christos 		error = ipf_proxy_ioctl(softc, data, cmd, mode, ctx);
   1541  1.1  christos 		break;
   1542  1.1  christos 
   1543  1.1  christos 	case SIOCSTLCK :
   1544  1.1  christos 		if (!(mode & FWRITE)) {
   1545  1.1  christos 			IPFERROR(60015);
   1546  1.1  christos 			error = EPERM;
   1547  1.1  christos 		} else {
   1548  1.1  christos 			error = ipf_lock(data, &softn->ipf_nat_lock);
   1549  1.1  christos 		}
   1550  1.1  christos 		break;
   1551  1.1  christos 
   1552  1.1  christos 	case SIOCSTPUT :
   1553  1.1  christos 		if ((mode & FWRITE) != 0) {
   1554  1.1  christos 			error = ipf_nat_putent(softc, data, getlock);
   1555  1.1  christos 		} else {
   1556  1.1  christos 			IPFERROR(60016);
   1557  1.1  christos 			error = EACCES;
   1558  1.1  christos 		}
   1559  1.1  christos 		break;
   1560  1.1  christos 
   1561  1.1  christos 	case SIOCSTGSZ :
   1562  1.1  christos 		if (softn->ipf_nat_lock) {
   1563  1.1  christos 			error = ipf_nat_getsz(softc, data, getlock);
   1564  1.1  christos 		} else {
   1565  1.1  christos 			IPFERROR(60017);
   1566  1.1  christos 			error = EACCES;
   1567  1.1  christos 		}
   1568  1.1  christos 		break;
   1569  1.1  christos 
   1570  1.1  christos 	case SIOCSTGET :
   1571  1.1  christos 		if (softn->ipf_nat_lock) {
   1572  1.1  christos 			error = ipf_nat_getent(softc, data, getlock);
   1573  1.1  christos 		} else {
   1574  1.1  christos 			IPFERROR(60018);
   1575  1.1  christos 			error = EACCES;
   1576  1.1  christos 		}
   1577  1.1  christos 		break;
   1578  1.1  christos 
   1579  1.1  christos 	case SIOCGENITER :
   1580  1.1  christos 	    {
   1581  1.1  christos 		ipfgeniter_t iter;
   1582  1.1  christos 		ipftoken_t *token;
   1583  1.1  christos 		ipfobj_t obj;
   1584  1.1  christos 
   1585  1.1  christos 		error = ipf_inobj(softc, data, &obj, &iter, IPFOBJ_GENITER);
   1586  1.1  christos 		if (error != 0)
   1587  1.1  christos 			break;
   1588  1.1  christos 
   1589  1.1  christos 		SPL_SCHED(s);
   1590  1.1  christos 		token = ipf_token_find(softc, iter.igi_type, uid, ctx);
   1591  1.1  christos 		if (token != NULL) {
   1592  1.1  christos 			error  = ipf_nat_iterator(softc, token, &iter, &obj);
   1593  1.1  christos 			WRITE_ENTER(&softc->ipf_tokens);
   1594  1.1  christos 			if (token->ipt_data == NULL)
   1595  1.1  christos 				ipf_token_free(softc, token);
   1596  1.1  christos 			else
   1597  1.1  christos 				ipf_token_deref(softc, token);
   1598  1.1  christos 			RWLOCK_EXIT(&softc->ipf_tokens);
   1599  1.1  christos 		}
   1600  1.1  christos 		SPL_X(s);
   1601  1.1  christos 		break;
   1602  1.1  christos 	    }
   1603  1.1  christos 
   1604  1.1  christos 	case SIOCIPFDELTOK :
   1605  1.1  christos 		error = BCOPYIN(data, &arg, sizeof(arg));
   1606  1.1  christos 		if (error == 0) {
   1607  1.1  christos 			SPL_SCHED(s);
   1608  1.1  christos 			error = ipf_token_del(softc, arg, uid, ctx);
   1609  1.1  christos 			SPL_X(s);
   1610  1.1  christos 		} else {
   1611  1.1  christos 			IPFERROR(60019);
   1612  1.1  christos 			error = EFAULT;
   1613  1.1  christos 		}
   1614  1.1  christos 		break;
   1615  1.1  christos 
   1616  1.1  christos 	case SIOCGTQTAB :
   1617  1.1  christos 		error = ipf_outobj(softc, data, softn->ipf_nat_tcptq,
   1618  1.1  christos 				   IPFOBJ_STATETQTAB);
   1619  1.1  christos 		break;
   1620  1.1  christos 
   1621  1.1  christos 	case SIOCGTABL :
   1622  1.1  christos 		error = ipf_nat_gettable(softc, softn, data);
   1623  1.1  christos 		break;
   1624  1.1  christos 
   1625  1.1  christos 	default :
   1626  1.1  christos 		IPFERROR(60020);
   1627  1.1  christos 		error = EINVAL;
   1628  1.1  christos 		break;
   1629  1.1  christos 	}
   1630  1.1  christos done:
   1631  1.1  christos 	if (nt != NULL)
   1632  1.1  christos 		KFREES(nt, nt->in_size);
   1633  1.1  christos 	return error;
   1634  1.1  christos }
   1635  1.1  christos 
   1636  1.1  christos 
   1637  1.1  christos /* ------------------------------------------------------------------------ */
   1638  1.1  christos /* Function:    ipf_nat_siocaddnat                                          */
   1639  1.1  christos /* Returns:     int - 0 == success, != 0 == failure                         */
   1640  1.1  christos /* Parameters:  n(I)       - pointer to new NAT rule                        */
   1641  1.1  christos /*              np(I)      - pointer to where to insert new NAT rule        */
   1642  1.1  christos /*              getlock(I) - flag indicating if lock on  is held            */
   1643  1.1  christos /* Mutex Locks: ipf_nat_io                                                   */
   1644  1.1  christos /*                                                                          */
   1645  1.1  christos /* Handle SIOCADNAT.  Resolve and calculate details inside the NAT rule     */
   1646  1.1  christos /* from information passed to the kernel, then add it  to the appropriate   */
   1647  1.1  christos /* NAT rule table(s).                                                       */
   1648  1.1  christos /* ------------------------------------------------------------------------ */
   1649  1.1  christos static int
   1650  1.1  christos ipf_nat_siocaddnat(softc, softn, n, np, getlock)
   1651  1.1  christos 	ipf_main_softc_t *softc;
   1652  1.1  christos 	ipf_nat_softc_t *softn;
   1653  1.1  christos 	ipnat_t *n, **np;
   1654  1.1  christos 	int getlock;
   1655  1.1  christos {
   1656  1.1  christos 	int error = 0;
   1657  1.1  christos 
   1658  1.1  christos 	/*
   1659  1.1  christos 	 * This combination of flags is incompatible because in_flags will
   1660  1.1  christos 	 * be checked for packets coming back in too.
   1661  1.1  christos 	 */
   1662  1.1  christos 	if ((n->in_flags & IPN_TCPUDP) && (n->in_redir & NAT_ENCAP)) {
   1663  1.1  christos 		IPFERROR(60021);
   1664  1.1  christos 		return EINVAL;
   1665  1.1  christos 	}
   1666  1.1  christos 
   1667  1.1  christos 	if (ipf_nat_resolverule(softc, n) != 0) {
   1668  1.1  christos 		IPFERROR(60022);
   1669  1.1  christos 		return ENOENT;
   1670  1.1  christos 	}
   1671  1.1  christos 
   1672  1.1  christos 	if ((n->in_age[0] == 0) && (n->in_age[1] != 0)) {
   1673  1.1  christos 		IPFERROR(60023);
   1674  1.1  christos 		return EINVAL;
   1675  1.1  christos 	}
   1676  1.1  christos 
   1677  1.1  christos 	n->in_use = 0;
   1678  1.1  christos 
   1679  1.1  christos 	if ((n->in_flags & IPN_SIPRANGE) != 0)
   1680  1.1  christos 		n->in_nsrcatype = FRI_RANGE;
   1681  1.1  christos 
   1682  1.1  christos 	if ((n->in_flags & IPN_DIPRANGE) != 0)
   1683  1.1  christos 		n->in_ndstatype = FRI_RANGE;
   1684  1.1  christos 
   1685  1.1  christos 	if ((n->in_flags & IPN_SPLIT) != 0)
   1686  1.1  christos 		n->in_ndstatype = FRI_SPLIT;
   1687  1.1  christos 
   1688  1.1  christos 	if ((n->in_redir & (NAT_MAP|NAT_REWRITE|NAT_DIVERTUDP)) != 0)
   1689  1.1  christos 		n->in_spnext = n->in_spmin;
   1690  1.1  christos 
   1691  1.1  christos 	if ((n->in_redir & (NAT_REWRITE|NAT_DIVERTUDP)) != 0) {
   1692  1.1  christos 		n->in_dpnext = n->in_dpmin;
   1693  1.1  christos 	} else if (n->in_redir == NAT_REDIRECT) {
   1694  1.1  christos 		n->in_dpnext = n->in_dpmin;
   1695  1.1  christos 	}
   1696  1.1  christos 
   1697  1.1  christos 	n->in_stepnext = 0;
   1698  1.1  christos 
   1699  1.1  christos 	switch (n->in_v[0])
   1700  1.1  christos 	{
   1701  1.1  christos 	case 4 :
   1702  1.1  christos 		error = ipf_nat_ruleaddrinit(softc, softn, n);
   1703  1.1  christos 		if (error != 0)
   1704  1.1  christos 			return error;
   1705  1.1  christos 		break;
   1706  1.1  christos #ifdef USE_INET6
   1707  1.1  christos 	case 6 :
   1708  1.1  christos 		error = ipf_nat6_ruleaddrinit(softc, softn, n);
   1709  1.1  christos 		if (error != 0)
   1710  1.1  christos 			return error;
   1711  1.1  christos 		break;
   1712  1.1  christos #endif
   1713  1.1  christos 	default :
   1714  1.1  christos 		break;
   1715  1.1  christos 	}
   1716  1.1  christos 
   1717  1.1  christos 	if (n->in_redir == (NAT_DIVERTUDP|NAT_MAP)) {
   1718  1.1  christos 		/*
   1719  1.1  christos 		 * Prerecord whether or not the destination of the divert
   1720  1.1  christos 		 * is local or not to the interface the packet is going
   1721  1.1  christos 		 * to be sent out.
   1722  1.1  christos 		 */
   1723  1.1  christos 		n->in_dlocal = ipf_deliverlocal(softc, n->in_v[1],
   1724  1.1  christos 						n->in_ifps[1], &n->in_ndstip6);
   1725  1.1  christos 	}
   1726  1.1  christos 
   1727  1.1  christos 	if (getlock) {
   1728  1.1  christos 		WRITE_ENTER(&softc->ipf_nat);
   1729  1.1  christos 	}
   1730  1.1  christos 	n->in_next = NULL;
   1731  1.1  christos 	*np = n;
   1732  1.1  christos 
   1733  1.1  christos 	if (n->in_redir & NAT_REDIRECT) {
   1734  1.1  christos 		n->in_flags &= ~IPN_NOTDST;
   1735  1.1  christos 		switch (n->in_v[0])
   1736  1.1  christos 		{
   1737  1.1  christos 		case 4 :
   1738  1.1  christos 			ipf_nat_addrdr(softn, n);
   1739  1.1  christos 			if (n->in_redir & NAT_ENCAP)
   1740  1.1  christos 				ipf_nat_addencap(softn, n);
   1741  1.1  christos 			break;
   1742  1.1  christos #ifdef USE_INET6
   1743  1.1  christos 		case 6 :
   1744  1.1  christos 			ipf_nat6_addrdr(softn, n);
   1745  1.1  christos 			if (n->in_redir & NAT_ENCAP)
   1746  1.1  christos 				ipf_nat6_addencap(softn, n);
   1747  1.1  christos 			break;
   1748  1.1  christos #endif
   1749  1.1  christos 		default :
   1750  1.1  christos 			break;
   1751  1.1  christos 		}
   1752  1.1  christos 		ATOMIC_INC32(softn->ipf_nat_stats.ns_rules_rdr);
   1753  1.1  christos 	}
   1754  1.1  christos 
   1755  1.1  christos 	if (n->in_redir & (NAT_MAP|NAT_MAPBLK)) {
   1756  1.1  christos 		n->in_flags &= ~IPN_NOTSRC;
   1757  1.1  christos 		switch (n->in_v[0])
   1758  1.1  christos 		{
   1759  1.1  christos 		case 4 :
   1760  1.1  christos 			ipf_nat_addmap(softn, n);
   1761  1.1  christos 			if (n->in_redir & NAT_ENCAP)
   1762  1.1  christos 				ipf_nat_addencap(softn, n);
   1763  1.1  christos 			break;
   1764  1.1  christos #ifdef USE_INET6
   1765  1.1  christos 		case 6 :
   1766  1.1  christos 			ipf_nat6_addmap(softn, n);
   1767  1.1  christos 			if (n->in_redir & NAT_ENCAP)
   1768  1.1  christos 				ipf_nat6_addencap(softn, n);
   1769  1.1  christos 			break;
   1770  1.1  christos #endif
   1771  1.1  christos 		default :
   1772  1.1  christos 			break;
   1773  1.1  christos 		}
   1774  1.1  christos 		ATOMIC_INC32(softn->ipf_nat_stats.ns_rules_map);
   1775  1.1  christos 	}
   1776  1.1  christos 
   1777  1.1  christos 	if (n->in_age[0] != 0)
   1778  1.1  christos 		n->in_tqehead[0] = ipf_addtimeoutqueue(softc,
   1779  1.1  christos 						       &softn->ipf_nat_utqe,
   1780  1.1  christos 						       n->in_age[0]);
   1781  1.1  christos 
   1782  1.1  christos 	if (n->in_age[1] != 0)
   1783  1.1  christos 		n->in_tqehead[1] = ipf_addtimeoutqueue(softc,
   1784  1.1  christos 						       &softn->ipf_nat_utqe,
   1785  1.1  christos 						       n->in_age[1]);
   1786  1.1  christos 
   1787  1.1  christos 	MUTEX_INIT(&n->in_lock, "ipnat rule lock");
   1788  1.1  christos 
   1789  1.1  christos 	n = NULL;
   1790  1.1  christos 	ATOMIC_INC32(softn->ipf_nat_stats.ns_rules);
   1791  1.1  christos #if SOLARIS && !defined(INSTANCES)
   1792  1.1  christos 	pfil_delayed_copy = 0;
   1793  1.1  christos #endif
   1794  1.1  christos 	if (getlock) {
   1795  1.1  christos 		RWLOCK_EXIT(&softc->ipf_nat);			/* WRITE */
   1796  1.1  christos 	}
   1797  1.1  christos 
   1798  1.1  christos 	return error;
   1799  1.1  christos }
   1800  1.1  christos 
   1801  1.1  christos 
   1802  1.1  christos static int
   1803  1.1  christos ipf_nat_ruleaddrinit(softc, softn, n)
   1804  1.1  christos 	ipf_main_softc_t *softc;
   1805  1.1  christos 	ipf_nat_softc_t *softn;
   1806  1.1  christos 	ipnat_t *n;
   1807  1.1  christos {
   1808  1.1  christos 	int idx, error;
   1809  1.1  christos 
   1810  1.1  christos 	if (n->in_redir == NAT_BIMAP) {
   1811  1.1  christos 		n->in_ndstaddr = n->in_osrcaddr;
   1812  1.1  christos 		n->in_ndstmsk = n->in_osrcmsk;
   1813  1.1  christos 		n->in_odstaddr = n->in_nsrcaddr;
   1814  1.1  christos 		n->in_odstmsk = n->in_nsrcmsk;
   1815  1.1  christos 
   1816  1.1  christos 	}
   1817  1.1  christos 
   1818  1.1  christos 	if (n->in_redir & NAT_REDIRECT)
   1819  1.1  christos 		idx = 1;
   1820  1.1  christos 	else
   1821  1.1  christos 		idx = 0;
   1822  1.1  christos 	/*
   1823  1.1  christos 	 * Initialise all of the address fields.
   1824  1.1  christos 	 */
   1825  1.1  christos 	error = ipf_nat_nextaddrinit(softc, n->in_names, &n->in_osrc, 1,
   1826  1.1  christos 				     n->in_ifps[idx]);
   1827  1.1  christos 	if (error != 0)
   1828  1.1  christos 		return error;
   1829  1.1  christos 
   1830  1.1  christos 	error = ipf_nat_nextaddrinit(softc, n->in_names, &n->in_odst, 1,
   1831  1.1  christos 				     n->in_ifps[idx]);
   1832  1.1  christos 	if (error != 0)
   1833  1.1  christos 		return error;
   1834  1.1  christos 
   1835  1.1  christos 	if ((n->in_nsrc.na_atype == FRI_LOOKUP) &&
   1836  1.1  christos 	    (n->in_nsrc.na_type != IPLT_DSTLIST)) {
   1837  1.1  christos 		IPFERROR(60069);
   1838  1.1  christos 		return EINVAL;
   1839  1.1  christos 	}
   1840  1.1  christos 	error = ipf_nat_nextaddrinit(softc, n->in_names, &n->in_nsrc, 1,
   1841  1.1  christos 				     n->in_ifps[idx]);
   1842  1.1  christos 	if (error != 0)
   1843  1.1  christos 		return error;
   1844  1.1  christos 
   1845  1.1  christos 	if ((n->in_ndst.na_atype == FRI_LOOKUP) &&
   1846  1.1  christos 	    (n->in_ndst.na_type != IPLT_DSTLIST)) {
   1847  1.1  christos 		IPFERROR(60071);
   1848  1.1  christos 		return EINVAL;
   1849  1.1  christos 	}
   1850  1.1  christos 	error = ipf_nat_nextaddrinit(softc, n->in_names, &n->in_ndst, 1,
   1851  1.1  christos 				     n->in_ifps[idx]);
   1852  1.1  christos 	if (error != 0)
   1853  1.1  christos 		return error;
   1854  1.1  christos 
   1855  1.1  christos 	if (n->in_redir & (NAT_ENCAP|NAT_DIVERTUDP))
   1856  1.1  christos 		ipf_nat_builddivertmp(softn, n);
   1857  1.1  christos 
   1858  1.1  christos 	return 0;
   1859  1.1  christos }
   1860  1.1  christos 
   1861  1.1  christos 
   1862  1.1  christos /* ------------------------------------------------------------------------ */
   1863  1.1  christos /* Function:    nat_resolvrule                                              */
   1864  1.1  christos /* Returns:     Nil                                                         */
   1865  1.1  christos /* Parameters:  n(I)  - pointer to NAT rule                                 */
   1866  1.1  christos /*                                                                          */
   1867  1.1  christos /* Handle SIOCADNAT.  Resolve and calculate details inside the NAT rule     */
   1868  1.1  christos /* from information passed to the kernel, then add it  to the appropriate   */
   1869  1.1  christos /* NAT rule table(s).                                                       */
   1870  1.1  christos /* ------------------------------------------------------------------------ */
   1871  1.1  christos static int
   1872  1.1  christos ipf_nat_resolverule(softc, n)
   1873  1.1  christos 	ipf_main_softc_t *softc;
   1874  1.1  christos 	ipnat_t *n;
   1875  1.1  christos {
   1876  1.1  christos 	char *base;
   1877  1.1  christos 
   1878  1.1  christos 	base = n->in_names;
   1879  1.1  christos 
   1880  1.1  christos 	n->in_ifps[0] = ipf_resolvenic(softc, base + n->in_ifnames[0],
   1881  1.1  christos 				       n->in_v[0]);
   1882  1.1  christos 
   1883  1.1  christos 	if (n->in_ifnames[1] == -1) {
   1884  1.1  christos 		n->in_ifnames[1] = n->in_ifnames[0];
   1885  1.1  christos 		n->in_ifps[1] = n->in_ifps[0];
   1886  1.1  christos 	} else {
   1887  1.1  christos 		n->in_ifps[1] = ipf_resolvenic(softc, base + n->in_ifnames[1],
   1888  1.1  christos 					       n->in_v[1]);
   1889  1.1  christos 	}
   1890  1.1  christos 
   1891  1.1  christos 	if (n->in_plabel != -1) {
   1892  1.1  christos 		if (n->in_redir & NAT_REDIRECT)
   1893  1.1  christos 			n->in_apr = ipf_proxy_lookup(softc->ipf_proxy_soft,
   1894  1.1  christos 						     n->in_pr[0],
   1895  1.1  christos 						     base + n->in_plabel);
   1896  1.1  christos 		else
   1897  1.1  christos 			n->in_apr = ipf_proxy_lookup(softc->ipf_proxy_soft,
   1898  1.1  christos 						     n->in_pr[1],
   1899  1.1  christos 						     base + n->in_plabel);
   1900  1.1  christos 		if (n->in_apr == NULL)
   1901  1.1  christos 			return -1;
   1902  1.1  christos 	}
   1903  1.1  christos 	return 0;
   1904  1.1  christos }
   1905  1.1  christos 
   1906  1.1  christos 
   1907  1.1  christos /* ------------------------------------------------------------------------ */
   1908  1.1  christos /* Function:    nat_siocdelnat                                              */
   1909  1.1  christos /* Returns:     int - 0 == success, != 0 == failure                         */
   1910  1.1  christos /* Parameters:  n(I)       - pointer to new NAT rule                        */
   1911  1.1  christos /*              np(I)      - pointer to where to insert new NAT rule        */
   1912  1.1  christos /*              getlock(I) - flag indicating if lock on  is held            */
   1913  1.1  christos /* Mutex Locks: ipf_nat_io                                                  */
   1914  1.1  christos /*                                                                          */
   1915  1.1  christos /* Handle SIOCADNAT.  Resolve and calculate details inside the NAT rule     */
   1916  1.1  christos /* from information passed to the kernel, then add it  to the appropriate   */
   1917  1.1  christos /* NAT rule table(s).                                                       */
   1918  1.1  christos /* ------------------------------------------------------------------------ */
   1919  1.1  christos static void
   1920  1.1  christos ipf_nat_siocdelnat(softc, softn, n, np, getlock)
   1921  1.1  christos 	ipf_main_softc_t *softc;
   1922  1.1  christos 	ipf_nat_softc_t *softn;
   1923  1.1  christos 	ipnat_t *n, **np;
   1924  1.1  christos 	int getlock;
   1925  1.1  christos {
   1926  1.1  christos #ifdef IPF_NAT6
   1927  1.1  christos 	int i;
   1928  1.1  christos #endif
   1929  1.1  christos 
   1930  1.1  christos 	if (getlock) {
   1931  1.1  christos 		WRITE_ENTER(&softc->ipf_nat);
   1932  1.1  christos 	}
   1933  1.1  christos 	if (n->in_redir & NAT_REDIRECT)
   1934  1.1  christos 		ipf_nat_delrdr(softn, n);
   1935  1.1  christos 	if (n->in_redir & (NAT_MAPBLK|NAT_MAP))
   1936  1.1  christos 		ipf_nat_delmap(softn, n);
   1937  1.1  christos 
   1938  1.1  christos 	if (n->in_tqehead[0] != NULL) {
   1939  1.1  christos 		if (ipf_deletetimeoutqueue(n->in_tqehead[0]) == 0) {
   1940  1.1  christos 			ipf_freetimeoutqueue(softc, n->in_tqehead[1]);
   1941  1.1  christos 		}
   1942  1.1  christos 	}
   1943  1.1  christos 
   1944  1.1  christos 	if (n->in_tqehead[1] != NULL) {
   1945  1.1  christos 		if (ipf_deletetimeoutqueue(n->in_tqehead[1]) == 0) {
   1946  1.1  christos 			ipf_freetimeoutqueue(softc, n->in_tqehead[1]);
   1947  1.1  christos 		}
   1948  1.1  christos 	}
   1949  1.1  christos 
   1950  1.1  christos 	*np = n->in_next;
   1951  1.1  christos 
   1952  1.1  christos 	if (n->in_use == 0) {
   1953  1.1  christos 		ipf_nat_free_rule(softc, softn, n);
   1954  1.1  christos 	} else {
   1955  1.1  christos 		n->in_flags |= IPN_DELETE;
   1956  1.1  christos 		n->in_next = NULL;
   1957  1.1  christos 	}
   1958  1.1  christos 	if (getlock) {
   1959  1.1  christos 		RWLOCK_EXIT(&softc->ipf_nat);			/* READ/WRITE */
   1960  1.1  christos 	}
   1961  1.1  christos }
   1962  1.1  christos 
   1963  1.1  christos 
   1964  1.1  christos static void
   1965  1.1  christos ipf_nat_free_rule(softc, softn, n)
   1966  1.1  christos 	ipf_main_softc_t *softc;
   1967  1.1  christos 	ipf_nat_softc_t *softn;
   1968  1.1  christos 	ipnat_t *n;
   1969  1.1  christos {
   1970  1.1  christos 	if (n->in_apr != NULL)
   1971  1.1  christos 		ipf_proxy_free(n->in_apr);
   1972  1.1  christos 
   1973  1.1  christos 	if (n->in_odst.na_atype == FRI_LOOKUP)
   1974  1.1  christos 		ipf_lookup_deref(softc, n->in_odst.na_type, n->in_odst.na_ptr);
   1975  1.1  christos 
   1976  1.1  christos 	if (n->in_osrc.na_atype == FRI_LOOKUP)
   1977  1.1  christos 		ipf_lookup_deref(softc, n->in_osrc.na_type, n->in_osrc.na_ptr);
   1978  1.1  christos 
   1979  1.1  christos 	if (n->in_ndst.na_atype == FRI_LOOKUP)
   1980  1.1  christos 		ipf_lookup_deref(softc, n->in_ndst.na_type, n->in_ndst.na_ptr);
   1981  1.1  christos 
   1982  1.1  christos 	if (n->in_nsrc.na_atype == FRI_LOOKUP)
   1983  1.1  christos 		ipf_lookup_deref(softc, n->in_nsrc.na_type, n->in_nsrc.na_ptr);
   1984  1.1  christos 
   1985  1.1  christos 	if (n->in_redir & NAT_REDIRECT) {
   1986  1.1  christos 		ATOMIC_DEC32(softn->ipf_nat_stats.ns_rules_rdr);
   1987  1.1  christos 	}
   1988  1.1  christos 	if (n->in_redir & (NAT_MAP|NAT_MAPBLK)) {
   1989  1.1  christos 		ATOMIC_DEC32(softn->ipf_nat_stats.ns_rules_map);
   1990  1.1  christos 	}
   1991  1.1  christos 
   1992  1.1  christos 	if (n->in_divmp != NULL) {
   1993  1.1  christos 		FREE_MB_T(n->in_divmp);
   1994  1.1  christos 	}
   1995  1.1  christos 	ATOMIC_DEC32(softn->ipf_nat_stats.ns_rules);
   1996  1.1  christos 
   1997  1.1  christos 	MUTEX_DESTROY(&n->in_lock);
   1998  1.1  christos 
   1999  1.1  christos 	KFREES(n, n->in_size);
   2000  1.1  christos 
   2001  1.1  christos #if SOLARIS && !defined(INSTANCES)
   2002  1.1  christos 	if (softn->ipf_nat_stats.ns_rules == 0)
   2003  1.1  christos 		pfil_delayed_copy = 1;
   2004  1.1  christos #endif
   2005  1.1  christos }
   2006  1.1  christos 
   2007  1.1  christos 
   2008  1.1  christos /* ------------------------------------------------------------------------ */
   2009  1.1  christos /* Function:    ipf_nat_getsz                                               */
   2010  1.1  christos /* Returns:     int - 0 == success, != 0 is the error value.                */
   2011  1.1  christos /* Parameters:  data(I)    - pointer to natget structure with kernel        */
   2012  1.1  christos /*                           pointer get the size of.                       */
   2013  1.1  christos /*              getlock(I) - flag indicating whether or not the caller      */
   2014  1.1  christos /*                           holds a lock on ipf_nat                        */
   2015  1.1  christos /*                                                                          */
   2016  1.1  christos /* Handle SIOCSTGSZ.                                                        */
   2017  1.1  christos /* Return the size of the nat list entry to be copied back to user space.   */
   2018  1.1  christos /* The size of the entry is stored in the ng_sz field and the enture natget */
   2019  1.1  christos /* structure is copied back to the user.                                    */
   2020  1.1  christos /* ------------------------------------------------------------------------ */
   2021  1.1  christos static int
   2022  1.1  christos ipf_nat_getsz(softc, data, getlock)
   2023  1.1  christos 	ipf_main_softc_t *softc;
   2024  1.1  christos 	caddr_t data;
   2025  1.1  christos 	int getlock;
   2026  1.1  christos {
   2027  1.1  christos 	ipf_nat_softc_t *softn = softc->ipf_nat_soft;
   2028  1.1  christos 	ap_session_t *aps;
   2029  1.1  christos 	nat_t *nat, *n;
   2030  1.1  christos 	natget_t ng;
   2031  1.1  christos 	int error;
   2032  1.1  christos 
   2033  1.1  christos 	error = BCOPYIN(data, &ng, sizeof(ng));
   2034  1.1  christos 	if (error != 0) {
   2035  1.1  christos 		IPFERROR(60024);
   2036  1.1  christos 		return EFAULT;
   2037  1.1  christos 	}
   2038  1.1  christos 
   2039  1.1  christos 	if (getlock) {
   2040  1.1  christos 		READ_ENTER(&softc->ipf_nat);
   2041  1.1  christos 	}
   2042  1.1  christos 
   2043  1.1  christos 	nat = ng.ng_ptr;
   2044  1.1  christos 	if (!nat) {
   2045  1.1  christos 		nat = softn->ipf_nat_instances;
   2046  1.1  christos 		ng.ng_sz = 0;
   2047  1.1  christos 		/*
   2048  1.1  christos 		 * Empty list so the size returned is 0.  Simple.
   2049  1.1  christos 		 */
   2050  1.1  christos 		if (nat == NULL) {
   2051  1.1  christos 			if (getlock) {
   2052  1.1  christos 				RWLOCK_EXIT(&softc->ipf_nat);
   2053  1.1  christos 			}
   2054  1.1  christos 			error = BCOPYOUT(&ng, data, sizeof(ng));
   2055  1.1  christos 			if (error != 0) {
   2056  1.1  christos 				IPFERROR(60025);
   2057  1.1  christos 				return EFAULT;
   2058  1.1  christos 			}
   2059  1.1  christos 			return 0;
   2060  1.1  christos 		}
   2061  1.1  christos 	} else {
   2062  1.1  christos 		/*
   2063  1.1  christos 		 * Make sure the pointer we're copying from exists in the
   2064  1.1  christos 		 * current list of entries.  Security precaution to prevent
   2065  1.1  christos 		 * copying of random kernel data.
   2066  1.1  christos 		 */
   2067  1.1  christos 		for (n = softn->ipf_nat_instances; n; n = n->nat_next)
   2068  1.1  christos 			if (n == nat)
   2069  1.1  christos 				break;
   2070  1.1  christos 		if (n == NULL) {
   2071  1.1  christos 			if (getlock) {
   2072  1.1  christos 				RWLOCK_EXIT(&softc->ipf_nat);
   2073  1.1  christos 			}
   2074  1.1  christos 			IPFERROR(60026);
   2075  1.1  christos 			return ESRCH;
   2076  1.1  christos 		}
   2077  1.1  christos 	}
   2078  1.1  christos 
   2079  1.1  christos 	/*
   2080  1.1  christos 	 * Incluse any space required for proxy data structures.
   2081  1.1  christos 	 */
   2082  1.1  christos 	ng.ng_sz = sizeof(nat_save_t);
   2083  1.1  christos 	aps = nat->nat_aps;
   2084  1.1  christos 	if (aps != NULL) {
   2085  1.1  christos 		ng.ng_sz += sizeof(ap_session_t) - 4;
   2086  1.1  christos 		if (aps->aps_data != 0)
   2087  1.1  christos 			ng.ng_sz += aps->aps_psiz;
   2088  1.1  christos 	}
   2089  1.1  christos 	if (getlock) {
   2090  1.1  christos 		RWLOCK_EXIT(&softc->ipf_nat);
   2091  1.1  christos 	}
   2092  1.1  christos 
   2093  1.1  christos 	error = BCOPYOUT(&ng, data, sizeof(ng));
   2094  1.1  christos 	if (error != 0) {
   2095  1.1  christos 		IPFERROR(60027);
   2096  1.1  christos 		return EFAULT;
   2097  1.1  christos 	}
   2098  1.1  christos 	return 0;
   2099  1.1  christos }
   2100  1.1  christos 
   2101  1.1  christos 
   2102  1.1  christos /* ------------------------------------------------------------------------ */
   2103  1.1  christos /* Function:    ipf_nat_getent                                              */
   2104  1.1  christos /* Returns:     int - 0 == success, != 0 is the error value.                */
   2105  1.1  christos /* Parameters:  data(I)    - pointer to natget structure with kernel pointer*/
   2106  1.1  christos /*                           to NAT structure to copy out.                  */
   2107  1.1  christos /*              getlock(I) - flag indicating whether or not the caller      */
   2108  1.1  christos /*                           holds a lock on ipf_nat                        */
   2109  1.1  christos /*                                                                          */
   2110  1.1  christos /* Handle SIOCSTGET.                                                        */
   2111  1.1  christos /* Copies out NAT entry to user space.  Any additional data held for a      */
   2112  1.1  christos /* proxy is also copied, as to is the NAT rule which was responsible for it */
   2113  1.1  christos /* ------------------------------------------------------------------------ */
   2114  1.1  christos static int
   2115  1.1  christos ipf_nat_getent(softc, data, getlock)
   2116  1.1  christos 	ipf_main_softc_t *softc;
   2117  1.1  christos 	caddr_t data;
   2118  1.1  christos 	int getlock;
   2119  1.1  christos {
   2120  1.1  christos 	ipf_nat_softc_t *softn = softc->ipf_nat_soft;
   2121  1.1  christos 	int error, outsize;
   2122  1.1  christos 	ap_session_t *aps;
   2123  1.1  christos 	nat_save_t *ipn, ipns;
   2124  1.1  christos 	nat_t *n, *nat;
   2125  1.1  christos 
   2126  1.1  christos 	error = ipf_inobj(softc, data, NULL, &ipns, IPFOBJ_NATSAVE);
   2127  1.1  christos 	if (error != 0)
   2128  1.1  christos 		return error;
   2129  1.1  christos 
   2130  1.1  christos 	if ((ipns.ipn_dsize < sizeof(ipns)) || (ipns.ipn_dsize > 81920)) {
   2131  1.1  christos 		IPFERROR(60028);
   2132  1.1  christos 		return EINVAL;
   2133  1.1  christos 	}
   2134  1.1  christos 
   2135  1.1  christos 	KMALLOCS(ipn, nat_save_t *, ipns.ipn_dsize);
   2136  1.1  christos 	if (ipn == NULL) {
   2137  1.1  christos 		IPFERROR(60029);
   2138  1.1  christos 		return ENOMEM;
   2139  1.1  christos 	}
   2140  1.1  christos 
   2141  1.1  christos 	if (getlock) {
   2142  1.1  christos 		READ_ENTER(&softc->ipf_nat);
   2143  1.1  christos 	}
   2144  1.1  christos 
   2145  1.1  christos 	ipn->ipn_dsize = ipns.ipn_dsize;
   2146  1.1  christos 	nat = ipns.ipn_next;
   2147  1.1  christos 	if (nat == NULL) {
   2148  1.1  christos 		nat = softn->ipf_nat_instances;
   2149  1.1  christos 		if (nat == NULL) {
   2150  1.1  christos 			if (softn->ipf_nat_instances == NULL) {
   2151  1.1  christos 				IPFERROR(60030);
   2152  1.1  christos 				error = ENOENT;
   2153  1.1  christos 			}
   2154  1.1  christos 			goto finished;
   2155  1.1  christos 		}
   2156  1.1  christos 	} else {
   2157  1.1  christos 		/*
   2158  1.1  christos 		 * Make sure the pointer we're copying from exists in the
   2159  1.1  christos 		 * current list of entries.  Security precaution to prevent
   2160  1.1  christos 		 * copying of random kernel data.
   2161  1.1  christos 		 */
   2162  1.1  christos 		for (n = softn->ipf_nat_instances; n; n = n->nat_next)
   2163  1.1  christos 			if (n == nat)
   2164  1.1  christos 				break;
   2165  1.1  christos 		if (n == NULL) {
   2166  1.1  christos 			IPFERROR(60031);
   2167  1.1  christos 			error = ESRCH;
   2168  1.1  christos 			goto finished;
   2169  1.1  christos 		}
   2170  1.1  christos 	}
   2171  1.1  christos 	ipn->ipn_next = nat->nat_next;
   2172  1.1  christos 
   2173  1.1  christos 	/*
   2174  1.1  christos 	 * Copy the NAT structure.
   2175  1.1  christos 	 */
   2176  1.1  christos 	bcopy((char *)nat, &ipn->ipn_nat, sizeof(*nat));
   2177  1.1  christos 
   2178  1.1  christos 	/*
   2179  1.1  christos 	 * If we have a pointer to the NAT rule it belongs to, save that too.
   2180  1.1  christos 	 */
   2181  1.1  christos 	if (nat->nat_ptr != NULL)
   2182  1.1  christos 		bcopy((char *)nat->nat_ptr, (char *)&ipn->ipn_ipnat,
   2183  1.1  christos 		      ipn->ipn_ipnat.in_size);
   2184  1.1  christos 
   2185  1.1  christos 	/*
   2186  1.1  christos 	 * If we also know the NAT entry has an associated filter rule,
   2187  1.1  christos 	 * save that too.
   2188  1.1  christos 	 */
   2189  1.1  christos 	if (nat->nat_fr != NULL)
   2190  1.1  christos 		bcopy((char *)nat->nat_fr, (char *)&ipn->ipn_fr,
   2191  1.1  christos 		      sizeof(ipn->ipn_fr));
   2192  1.1  christos 
   2193  1.1  christos 	/*
   2194  1.1  christos 	 * Last but not least, if there is an application proxy session set
   2195  1.1  christos 	 * up for this NAT entry, then copy that out too, including any
   2196  1.1  christos 	 * private data saved along side it by the proxy.
   2197  1.1  christos 	 */
   2198  1.1  christos 	aps = nat->nat_aps;
   2199  1.1  christos 	outsize = ipn->ipn_dsize - sizeof(*ipn) + sizeof(ipn->ipn_data);
   2200  1.1  christos 	if (aps != NULL) {
   2201  1.1  christos 		char *s;
   2202  1.1  christos 
   2203  1.1  christos 		if (outsize < sizeof(*aps)) {
   2204  1.1  christos 			IPFERROR(60032);
   2205  1.1  christos 			error = ENOBUFS;
   2206  1.1  christos 			goto finished;
   2207  1.1  christos 		}
   2208  1.1  christos 
   2209  1.1  christos 		s = ipn->ipn_data;
   2210  1.1  christos 		bcopy((char *)aps, s, sizeof(*aps));
   2211  1.1  christos 		s += sizeof(*aps);
   2212  1.1  christos 		outsize -= sizeof(*aps);
   2213  1.1  christos 		if ((aps->aps_data != NULL) && (outsize >= aps->aps_psiz))
   2214  1.1  christos 			bcopy(aps->aps_data, s, aps->aps_psiz);
   2215  1.1  christos 		else {
   2216  1.1  christos 			IPFERROR(60033);
   2217  1.1  christos 			error = ENOBUFS;
   2218  1.1  christos 		}
   2219  1.1  christos 	}
   2220  1.1  christos 	if (error == 0) {
   2221  1.1  christos 		if (getlock) {
   2222  1.1  christos 			READ_ENTER(&softc->ipf_nat);
   2223  1.1  christos 			getlock = 0;
   2224  1.1  christos 		}
   2225  1.1  christos 		error = ipf_outobjsz(softc, data, ipn, IPFOBJ_NATSAVE,
   2226  1.1  christos 				     ipns.ipn_dsize);
   2227  1.1  christos 	}
   2228  1.1  christos 
   2229  1.1  christos finished:
   2230  1.1  christos 	if (getlock) {
   2231  1.1  christos 		READ_ENTER(&softc->ipf_nat);
   2232  1.1  christos 	}
   2233  1.1  christos 	if (ipn != NULL) {
   2234  1.1  christos 		KFREES(ipn, ipns.ipn_dsize);
   2235  1.1  christos 	}
   2236  1.1  christos 	return error;
   2237  1.1  christos }
   2238  1.1  christos 
   2239  1.1  christos 
   2240  1.1  christos /* ------------------------------------------------------------------------ */
   2241  1.1  christos /* Function:    ipf_nat_putent                                              */
   2242  1.1  christos /* Returns:     int - 0 == success, != 0 is the error value.                */
   2243  1.1  christos /* Parameters:  data(I) -     pointer to natget structure with NAT          */
   2244  1.1  christos /*                            structure information to load into the kernel */
   2245  1.1  christos /*              getlock(I) - flag indicating whether or not a write lock    */
   2246  1.1  christos /*                           on  is already held.                    */
   2247  1.1  christos /*                                                                          */
   2248  1.1  christos /* Handle SIOCSTPUT.                                                        */
   2249  1.1  christos /* Loads a NAT table entry from user space, including a NAT rule, proxy and */
   2250  1.1  christos /* firewall rule data structures, if pointers to them indicate so.          */
   2251  1.1  christos /* ------------------------------------------------------------------------ */
   2252  1.1  christos static int
   2253  1.1  christos ipf_nat_putent(softc, data, getlock)
   2254  1.1  christos 	ipf_main_softc_t *softc;
   2255  1.1  christos 	caddr_t data;
   2256  1.1  christos 	int getlock;
   2257  1.1  christos {
   2258  1.1  christos 	ipf_nat_softc_t *softn = softc->ipf_nat_soft;
   2259  1.1  christos 	nat_save_t ipn, *ipnn;
   2260  1.1  christos 	ap_session_t *aps;
   2261  1.1  christos 	nat_t *n, *nat;
   2262  1.1  christos 	frentry_t *fr;
   2263  1.1  christos 	fr_info_t fin;
   2264  1.1  christos 	ipnat_t *in;
   2265  1.1  christos 	int error;
   2266  1.1  christos 
   2267  1.1  christos 	error = ipf_inobj(softc, data, NULL, &ipn, IPFOBJ_NATSAVE);
   2268  1.1  christos 	if (error != 0)
   2269  1.1  christos 		return error;
   2270  1.1  christos 
   2271  1.1  christos 	/*
   2272  1.1  christos 	 * Initialise early because of code at junkput label.
   2273  1.1  christos 	 */
   2274  1.1  christos 	n = NULL;
   2275  1.1  christos 	in = NULL;
   2276  1.1  christos 	aps = NULL;
   2277  1.1  christos 	nat = NULL;
   2278  1.1  christos 	ipnn = NULL;
   2279  1.1  christos 	fr = NULL;
   2280  1.1  christos 
   2281  1.1  christos 	/*
   2282  1.1  christos 	 * New entry, copy in the rest of the NAT entry if it's size is more
   2283  1.1  christos 	 * than just the nat_t structure.
   2284  1.1  christos 	 */
   2285  1.1  christos 	if (ipn.ipn_dsize > sizeof(ipn)) {
   2286  1.1  christos 		if (ipn.ipn_dsize > 81920) {
   2287  1.1  christos 			IPFERROR(60034);
   2288  1.1  christos 			error = ENOMEM;
   2289  1.1  christos 			goto junkput;
   2290  1.1  christos 		}
   2291  1.1  christos 
   2292  1.1  christos 		KMALLOCS(ipnn, nat_save_t *, ipn.ipn_dsize);
   2293  1.1  christos 		if (ipnn == NULL) {
   2294  1.1  christos 			IPFERROR(60035);
   2295  1.1  christos 			return ENOMEM;
   2296  1.1  christos 		}
   2297  1.1  christos 
   2298  1.1  christos 		bzero(ipnn, ipn.ipn_dsize);
   2299  1.1  christos 		error = ipf_inobjsz(softc, data, ipnn, IPFOBJ_NATSAVE,
   2300  1.1  christos 				    ipn.ipn_dsize);
   2301  1.1  christos 		if (error != 0) {
   2302  1.1  christos 			goto junkput;
   2303  1.1  christos 		}
   2304  1.1  christos 	} else
   2305  1.1  christos 		ipnn = &ipn;
   2306  1.1  christos 
   2307  1.1  christos 	KMALLOC(nat, nat_t *);
   2308  1.1  christos 	if (nat == NULL) {
   2309  1.1  christos 		IPFERROR(60037);
   2310  1.1  christos 		error = ENOMEM;
   2311  1.1  christos 		goto junkput;
   2312  1.1  christos 	}
   2313  1.1  christos 
   2314  1.1  christos 	bcopy((char *)&ipnn->ipn_nat, (char *)nat, sizeof(*nat));
   2315  1.1  christos 
   2316  1.1  christos 	switch (nat->nat_v[0])
   2317  1.1  christos 	{
   2318  1.1  christos 	case 4:
   2319  1.1  christos #ifdef USE_IENT6
   2320  1.1  christos 	case 6 :
   2321  1.1  christos #endif
   2322  1.1  christos 		break;
   2323  1.1  christos 	default :
   2324  1.1  christos 		IPFERROR(60061);
   2325  1.1  christos 		error = EPROTONOSUPPORT;
   2326  1.1  christos 		goto junkput;
   2327  1.1  christos 		/*NOTREACHED*/
   2328  1.1  christos 	}
   2329  1.1  christos 
   2330  1.1  christos 	/*
   2331  1.1  christos 	 * Initialize all these so that ipf_nat_delete() doesn't cause a crash.
   2332  1.1  christos 	 */
   2333  1.1  christos 	bzero((char *)nat, offsetof(struct nat, nat_tqe));
   2334  1.1  christos 	nat->nat_tqe.tqe_pnext = NULL;
   2335  1.1  christos 	nat->nat_tqe.tqe_next = NULL;
   2336  1.1  christos 	nat->nat_tqe.tqe_ifq = NULL;
   2337  1.1  christos 	nat->nat_tqe.tqe_parent = nat;
   2338  1.1  christos 
   2339  1.1  christos 	/*
   2340  1.1  christos 	 * Restore the rule associated with this nat session
   2341  1.1  christos 	 */
   2342  1.1  christos 	in = ipnn->ipn_nat.nat_ptr;
   2343  1.1  christos 	if (in != NULL) {
   2344  1.1  christos 		KMALLOCS(in, ipnat_t *, ipnn->ipn_ipnat.in_size);
   2345  1.1  christos 		nat->nat_ptr = in;
   2346  1.1  christos 		if (in == NULL) {
   2347  1.1  christos 			IPFERROR(60038);
   2348  1.1  christos 			error = ENOMEM;
   2349  1.1  christos 			goto junkput;
   2350  1.1  christos 		}
   2351  1.1  christos 		bcopy((char *)&ipnn->ipn_ipnat, (char *)in,
   2352  1.1  christos 		      ipnn->ipn_ipnat.in_size);
   2353  1.1  christos 		in->in_use = 1;
   2354  1.1  christos 		in->in_flags |= IPN_DELETE;
   2355  1.1  christos 
   2356  1.1  christos 		ATOMIC_INC32(softn->ipf_nat_stats.ns_rules);
   2357  1.1  christos 
   2358  1.1  christos 		if (ipf_nat_resolverule(softc, in) != 0) {
   2359  1.1  christos 			IPFERROR(60039);
   2360  1.1  christos 			error = ESRCH;
   2361  1.1  christos 			goto junkput;
   2362  1.1  christos 		}
   2363  1.1  christos 	}
   2364  1.1  christos 
   2365  1.1  christos 	/*
   2366  1.1  christos 	 * Check that the NAT entry doesn't already exist in the kernel.
   2367  1.1  christos 	 *
   2368  1.1  christos 	 * For NAT_OUTBOUND, we're lookup for a duplicate MAP entry.  To do
   2369  1.1  christos 	 * this, we check to see if the inbound combination of addresses and
   2370  1.1  christos 	 * ports is already known.  Similar logic is applied for NAT_INBOUND.
   2371  1.1  christos 	 *
   2372  1.1  christos 	 */
   2373  1.1  christos 	bzero((char *)&fin, sizeof(fin));
   2374  1.1  christos 	fin.fin_v = nat->nat_v[0];
   2375  1.1  christos 	fin.fin_p = nat->nat_pr[0];
   2376  1.1  christos 	fin.fin_rev = nat->nat_rev;
   2377  1.1  christos 	fin.fin_ifp = nat->nat_ifps[0];
   2378  1.1  christos 	fin.fin_data[0] = ntohs(nat->nat_ndport);
   2379  1.1  christos 	fin.fin_data[1] = ntohs(nat->nat_nsport);
   2380  1.1  christos 
   2381  1.1  christos 	switch (nat->nat_dir)
   2382  1.1  christos 	{
   2383  1.1  christos 	case NAT_OUTBOUND :
   2384  1.1  christos 	case NAT_ENCAPOUT :
   2385  1.1  christos 	case NAT_DIVERTOUT :
   2386  1.1  christos 		if (getlock) {
   2387  1.1  christos 			READ_ENTER(&softc->ipf_nat);
   2388  1.1  christos 		}
   2389  1.1  christos 
   2390  1.1  christos 		fin.fin_v = nat->nat_v[1];
   2391  1.1  christos 		if (nat->nat_v[1] == 4) {
   2392  1.1  christos 			n = ipf_nat_inlookup(&fin, nat->nat_flags, fin.fin_p,
   2393  1.1  christos 					     nat->nat_ndstip, nat->nat_nsrcip);
   2394  1.1  christos #ifdef USE_INET6
   2395  1.1  christos 		} else if (nat->nat_v[1] == 6) {
   2396  1.1  christos 			n = ipf_nat6_inlookup(&fin, nat->nat_flags, fin.fin_p,
   2397  1.1  christos 					      &nat->nat_ndst6.in6,
   2398  1.1  christos 					      &nat->nat_nsrc6.in6);
   2399  1.1  christos #endif
   2400  1.1  christos 		}
   2401  1.1  christos 
   2402  1.1  christos 		if (getlock) {
   2403  1.1  christos 			RWLOCK_EXIT(&softc->ipf_nat);
   2404  1.1  christos 		}
   2405  1.1  christos 		if (n != NULL) {
   2406  1.1  christos 			IPFERROR(60040);
   2407  1.1  christos 			error = EEXIST;
   2408  1.1  christos 			goto junkput;
   2409  1.1  christos 		}
   2410  1.1  christos 		break;
   2411  1.1  christos 
   2412  1.1  christos 	case NAT_INBOUND :
   2413  1.1  christos 	case NAT_ENCAPIN :
   2414  1.1  christos 	case NAT_DIVERTIN :
   2415  1.1  christos 		if (getlock) {
   2416  1.1  christos 			READ_ENTER(&softc->ipf_nat);
   2417  1.1  christos 		}
   2418  1.1  christos 
   2419  1.1  christos 		if (fin.fin_v == 4) {
   2420  1.1  christos 			n = ipf_nat_outlookup(&fin, nat->nat_flags, fin.fin_p,
   2421  1.1  christos 					      nat->nat_ndstip,
   2422  1.1  christos 					      nat->nat_nsrcip);
   2423  1.1  christos #ifdef USE_INET6
   2424  1.1  christos 		} else if (fin.fin_v == 6) {
   2425  1.1  christos 			n = ipf_nat6_outlookup(&fin, nat->nat_flags, fin.fin_p,
   2426  1.1  christos 					       &nat->nat_ndst6.in6,
   2427  1.1  christos 					       &nat->nat_nsrc6.in6);
   2428  1.1  christos #endif
   2429  1.1  christos 		}
   2430  1.1  christos 
   2431  1.1  christos 		if (getlock) {
   2432  1.1  christos 			RWLOCK_EXIT(&softc->ipf_nat);
   2433  1.1  christos 		}
   2434  1.1  christos 		if (n != NULL) {
   2435  1.1  christos 			IPFERROR(60041);
   2436  1.1  christos 			error = EEXIST;
   2437  1.1  christos 			goto junkput;
   2438  1.1  christos 		}
   2439  1.1  christos 		break;
   2440  1.1  christos 
   2441  1.1  christos 	default :
   2442  1.1  christos 		IPFERROR(60042);
   2443  1.1  christos 		error = EINVAL;
   2444  1.1  christos 		goto junkput;
   2445  1.1  christos 		break;
   2446  1.1  christos 	}
   2447  1.1  christos 
   2448  1.1  christos 	/*
   2449  1.1  christos 	 * Restore ap_session_t structure.  Include the private data allocated
   2450  1.1  christos 	 * if it was there.
   2451  1.1  christos 	 */
   2452  1.1  christos 	aps = nat->nat_aps;
   2453  1.1  christos 	if (aps != NULL) {
   2454  1.1  christos 		KMALLOC(aps, ap_session_t *);
   2455  1.1  christos 		nat->nat_aps = aps;
   2456  1.1  christos 		if (aps == NULL) {
   2457  1.1  christos 			IPFERROR(60043);
   2458  1.1  christos 			error = ENOMEM;
   2459  1.1  christos 			goto junkput;
   2460  1.1  christos 		}
   2461  1.1  christos 		bcopy(ipnn->ipn_data, (char *)aps, sizeof(*aps));
   2462  1.1  christos 		if (in != NULL)
   2463  1.1  christos 			aps->aps_apr = in->in_apr;
   2464  1.1  christos 		else
   2465  1.1  christos 			aps->aps_apr = NULL;
   2466  1.1  christos 		if (aps->aps_psiz != 0) {
   2467  1.1  christos 			if (aps->aps_psiz > 81920) {
   2468  1.1  christos 				IPFERROR(60044);
   2469  1.1  christos 				error = ENOMEM;
   2470  1.1  christos 				goto junkput;
   2471  1.1  christos 			}
   2472  1.1  christos 			KMALLOCS(aps->aps_data, void *, aps->aps_psiz);
   2473  1.1  christos 			if (aps->aps_data == NULL) {
   2474  1.1  christos 				IPFERROR(60045);
   2475  1.1  christos 				error = ENOMEM;
   2476  1.1  christos 				goto junkput;
   2477  1.1  christos 			}
   2478  1.1  christos 			bcopy(ipnn->ipn_data + sizeof(*aps), aps->aps_data,
   2479  1.1  christos 			      aps->aps_psiz);
   2480  1.1  christos 		} else {
   2481  1.1  christos 			aps->aps_psiz = 0;
   2482  1.1  christos 			aps->aps_data = NULL;
   2483  1.1  christos 		}
   2484  1.1  christos 	}
   2485  1.1  christos 
   2486  1.1  christos 	/*
   2487  1.1  christos 	 * If there was a filtering rule associated with this entry then
   2488  1.1  christos 	 * build up a new one.
   2489  1.1  christos 	 */
   2490  1.1  christos 	fr = nat->nat_fr;
   2491  1.1  christos 	if (fr != NULL) {
   2492  1.1  christos 		if ((nat->nat_flags & SI_NEWFR) != 0) {
   2493  1.1  christos 			KMALLOC(fr, frentry_t *);
   2494  1.1  christos 			nat->nat_fr = fr;
   2495  1.1  christos 			if (fr == NULL) {
   2496  1.1  christos 				IPFERROR(60046);
   2497  1.1  christos 				error = ENOMEM;
   2498  1.1  christos 				goto junkput;
   2499  1.1  christos 			}
   2500  1.1  christos 			ipnn->ipn_nat.nat_fr = fr;
   2501  1.1  christos 			fr->fr_ref = 1;
   2502  1.1  christos 			(void) ipf_outobj(softc, data, ipnn, IPFOBJ_NATSAVE);
   2503  1.1  christos 			bcopy((char *)&ipnn->ipn_fr, (char *)fr, sizeof(*fr));
   2504  1.1  christos 
   2505  1.1  christos 			fr->fr_ref = 1;
   2506  1.1  christos 			fr->fr_dsize = 0;
   2507  1.1  christos 			fr->fr_data = NULL;
   2508  1.1  christos 			fr->fr_type = FR_T_NONE;
   2509  1.1  christos 
   2510  1.1  christos 			MUTEX_NUKE(&fr->fr_lock);
   2511  1.1  christos 			MUTEX_INIT(&fr->fr_lock, "nat-filter rule lock");
   2512  1.1  christos 		} else {
   2513  1.1  christos 			if (getlock) {
   2514  1.1  christos 				READ_ENTER(&softc->ipf_nat);
   2515  1.1  christos 			}
   2516  1.1  christos 			for (n = softn->ipf_nat_instances; n; n = n->nat_next)
   2517  1.1  christos 				if (n->nat_fr == fr)
   2518  1.1  christos 					break;
   2519  1.1  christos 
   2520  1.1  christos 			if (n != NULL) {
   2521  1.1  christos 				MUTEX_ENTER(&fr->fr_lock);
   2522  1.1  christos 				fr->fr_ref++;
   2523  1.1  christos 				MUTEX_EXIT(&fr->fr_lock);
   2524  1.1  christos 			}
   2525  1.1  christos 			if (getlock) {
   2526  1.1  christos 				RWLOCK_EXIT(&softc->ipf_nat);
   2527  1.1  christos 			}
   2528  1.1  christos 
   2529  1.1  christos 			if (n == NULL) {
   2530  1.1  christos 				IPFERROR(60047);
   2531  1.1  christos 				error = ESRCH;
   2532  1.1  christos 				goto junkput;
   2533  1.1  christos 			}
   2534  1.1  christos 		}
   2535  1.1  christos 	}
   2536  1.1  christos 
   2537  1.1  christos 	if (ipnn != &ipn) {
   2538  1.1  christos 		KFREES(ipnn, ipn.ipn_dsize);
   2539  1.1  christos 		ipnn = NULL;
   2540  1.1  christos 	}
   2541  1.1  christos 
   2542  1.1  christos 	if (getlock) {
   2543  1.1  christos 		WRITE_ENTER(&softc->ipf_nat);
   2544  1.1  christos 	}
   2545  1.1  christos 
   2546  1.1  christos 	if (fin.fin_v == 4)
   2547  1.1  christos 		error = ipf_nat_finalise(&fin, nat);
   2548  1.1  christos #ifdef USE_INET6
   2549  1.1  christos 	else
   2550  1.1  christos 		error = ipf_nat6_finalise(&fin, nat);
   2551  1.1  christos #endif
   2552  1.1  christos 
   2553  1.1  christos 	if (getlock) {
   2554  1.1  christos 		RWLOCK_EXIT(&softc->ipf_nat);
   2555  1.1  christos 	}
   2556  1.1  christos 
   2557  1.1  christos 	if (error == 0)
   2558  1.1  christos 		return 0;
   2559  1.1  christos 
   2560  1.1  christos 	IPFERROR(60048);
   2561  1.1  christos 	error = ENOMEM;
   2562  1.1  christos 
   2563  1.1  christos junkput:
   2564  1.1  christos 	if (fr != NULL) {
   2565  1.1  christos 		(void) ipf_derefrule(softc, &fr);
   2566  1.1  christos 	}
   2567  1.1  christos 
   2568  1.1  christos 	if ((ipnn != NULL) && (ipnn != &ipn)) {
   2569  1.1  christos 		KFREES(ipnn, ipn.ipn_dsize);
   2570  1.1  christos 	}
   2571  1.1  christos 	if (nat != NULL) {
   2572  1.1  christos 		if (aps != NULL) {
   2573  1.1  christos 			if (aps->aps_data != NULL) {
   2574  1.1  christos 				KFREES(aps->aps_data, aps->aps_psiz);
   2575  1.1  christos 			}
   2576  1.1  christos 			KFREE(aps);
   2577  1.1  christos 		}
   2578  1.1  christos 		if (in != NULL) {
   2579  1.1  christos 			if (in->in_apr)
   2580  1.1  christos 				ipf_proxy_free(in->in_apr);
   2581  1.1  christos 			KFREES(in, in->in_size);
   2582  1.1  christos 		}
   2583  1.1  christos 		KFREE(nat);
   2584  1.1  christos 	}
   2585  1.1  christos 	return error;
   2586  1.1  christos }
   2587  1.1  christos 
   2588  1.1  christos 
   2589  1.1  christos /* ------------------------------------------------------------------------ */
   2590  1.1  christos /* Function:    ipf_nat_delete                                              */
   2591  1.1  christos /* Returns:     Nil                                                         */
   2592  1.1  christos /* Parameters:  natd(I)    - pointer to NAT structure to delete             */
   2593  1.1  christos /*              logtype(I) - type of LOG record to create before deleting   */
   2594  1.1  christos /* Write Lock:  ipf_nat                                                     */
   2595  1.1  christos /*                                                                          */
   2596  1.1  christos /* Delete a nat entry from the various lists and table.  If NAT logging is  */
   2597  1.1  christos /* enabled then generate a NAT log record for this event.                   */
   2598  1.1  christos /* ------------------------------------------------------------------------ */
   2599  1.1  christos void
   2600  1.1  christos ipf_nat_delete(softc, nat, logtype)
   2601  1.1  christos 	ipf_main_softc_t *softc;
   2602  1.1  christos 	struct nat *nat;
   2603  1.1  christos 	int logtype;
   2604  1.1  christos {
   2605  1.1  christos 	ipf_nat_softc_t *softn = softc->ipf_nat_soft;
   2606  1.1  christos 	int madeorphan = 0, bkt, removed = 0;
   2607  1.1  christos 	struct ipnat *ipn;
   2608  1.1  christos 
   2609  1.1  christos 	if (logtype != 0 && softn->ipf_nat_logging != 0)
   2610  1.1  christos 		ipf_nat_log(softc, softn, nat, logtype);
   2611  1.1  christos 
   2612  1.1  christos 	/*
   2613  1.1  christos 	 * Take it as a general indication that all the pointers are set if
   2614  1.1  christos 	 * nat_pnext is set.
   2615  1.1  christos 	 */
   2616  1.1  christos 	if (nat->nat_pnext != NULL) {
   2617  1.1  christos 		removed = 1;
   2618  1.1  christos 
   2619  1.1  christos 		bkt = nat->nat_hv[0] % softn->ipf_nat_table_sz;
   2620  1.1  christos 		softn->ipf_nat_stats.ns_side[0].ns_bucketlen[bkt]--;
   2621  1.1  christos 		if (softn->ipf_nat_stats.ns_side[0].ns_bucketlen[bkt] == 0) {
   2622  1.1  christos 			softn->ipf_nat_stats.ns_side[0].ns_inuse--;
   2623  1.1  christos 		}
   2624  1.1  christos 
   2625  1.1  christos 		bkt = nat->nat_hv[1] % softn->ipf_nat_table_sz;
   2626  1.1  christos 		softn->ipf_nat_stats.ns_side[1].ns_bucketlen[bkt]--;
   2627  1.1  christos 		if (softn->ipf_nat_stats.ns_side[1].ns_bucketlen[bkt] == 0) {
   2628  1.1  christos 			softn->ipf_nat_stats.ns_side[1].ns_inuse--;
   2629  1.1  christos 		}
   2630  1.1  christos 
   2631  1.1  christos 		*nat->nat_pnext = nat->nat_next;
   2632  1.1  christos 		if (nat->nat_next != NULL) {
   2633  1.1  christos 			nat->nat_next->nat_pnext = nat->nat_pnext;
   2634  1.1  christos 			nat->nat_next = NULL;
   2635  1.1  christos 		}
   2636  1.1  christos 		nat->nat_pnext = NULL;
   2637  1.1  christos 
   2638  1.1  christos 		*nat->nat_phnext[0] = nat->nat_hnext[0];
   2639  1.1  christos 		if (nat->nat_hnext[0] != NULL) {
   2640  1.1  christos 			nat->nat_hnext[0]->nat_phnext[0] = nat->nat_phnext[0];
   2641  1.1  christos 			nat->nat_hnext[0] = NULL;
   2642  1.1  christos 		}
   2643  1.1  christos 		nat->nat_phnext[0] = NULL;
   2644  1.1  christos 
   2645  1.1  christos 		*nat->nat_phnext[1] = nat->nat_hnext[1];
   2646  1.1  christos 		if (nat->nat_hnext[1] != NULL) {
   2647  1.1  christos 			nat->nat_hnext[1]->nat_phnext[1] = nat->nat_phnext[1];
   2648  1.1  christos 			nat->nat_hnext[1] = NULL;
   2649  1.1  christos 		}
   2650  1.1  christos 		nat->nat_phnext[1] = NULL;
   2651  1.1  christos 
   2652  1.1  christos 		if ((nat->nat_flags & SI_WILDP) != 0) {
   2653  1.1  christos 			ATOMIC_DEC32(softn->ipf_nat_stats.ns_wilds);
   2654  1.1  christos 		}
   2655  1.1  christos 		madeorphan = 1;
   2656  1.1  christos 	}
   2657  1.1  christos 
   2658  1.1  christos 	if (nat->nat_me != NULL) {
   2659  1.1  christos 		*nat->nat_me = NULL;
   2660  1.1  christos 		nat->nat_me = NULL;
   2661  1.1  christos 		nat->nat_ref--;
   2662  1.1  christos 	}
   2663  1.1  christos 
   2664  1.1  christos 	if (nat->nat_tqe.tqe_ifq != NULL) {
   2665  1.1  christos 		/*
   2666  1.1  christos 		 * No call to ipf_freetimeoutqueue() is made here, they are
   2667  1.1  christos 		 * garbage collected in ipf_nat_expire().
   2668  1.1  christos 		 */
   2669  1.1  christos 		(void) ipf_deletequeueentry(&nat->nat_tqe);
   2670  1.1  christos 	}
   2671  1.1  christos 
   2672  1.1  christos 	if (logtype == NL_EXPIRE)
   2673  1.1  christos 		softn->ipf_nat_stats.ns_expire++;
   2674  1.1  christos 
   2675  1.1  christos 	MUTEX_ENTER(&nat->nat_lock);
   2676  1.1  christos 	/*
   2677  1.1  christos 	 * NL_DESTROY should only be passed in when we've got nat_ref >= 2.
   2678  1.1  christos 	 * This happens when a nat'd packet is blocked and we want to throw
   2679  1.1  christos 	 * away the NAT session.
   2680  1.1  christos 	 */
   2681  1.1  christos 	if (logtype == NL_DESTROY) {
   2682  1.1  christos 		if (nat->nat_ref > 2) {
   2683  1.1  christos 			nat->nat_ref -= 2;
   2684  1.1  christos 			MUTEX_EXIT(&nat->nat_lock);
   2685  1.1  christos 			if (removed)
   2686  1.1  christos 				softn->ipf_nat_stats.ns_orphans++;
   2687  1.1  christos 			return;
   2688  1.1  christos 		}
   2689  1.1  christos 	} else if (nat->nat_ref > 1) {
   2690  1.1  christos 		nat->nat_ref--;
   2691  1.1  christos 		MUTEX_EXIT(&nat->nat_lock);
   2692  1.1  christos 		if (madeorphan == 1)
   2693  1.1  christos 			softn->ipf_nat_stats.ns_orphans++;
   2694  1.1  christos 		return;
   2695  1.1  christos 	}
   2696  1.1  christos 	MUTEX_EXIT(&nat->nat_lock);
   2697  1.1  christos 
   2698  1.1  christos 	nat->nat_ref = 0;
   2699  1.1  christos 
   2700  1.1  christos 	if (madeorphan == 0)
   2701  1.1  christos 		softn->ipf_nat_stats.ns_orphans--;
   2702  1.1  christos 
   2703  1.1  christos 	/*
   2704  1.1  christos 	 * At this point, nat_ref can be either 0 or -1
   2705  1.1  christos 	 */
   2706  1.1  christos 	softn->ipf_nat_stats.ns_proto[nat->nat_pr[0]]--;
   2707  1.1  christos 
   2708  1.1  christos 	if (nat->nat_sync)
   2709  1.1  christos 		ipf_sync_del_nat(softc->ipf_sync_soft,nat->nat_sync);
   2710  1.1  christos 
   2711  1.1  christos 	if (nat->nat_fr != NULL) {
   2712  1.1  christos 		(void) ipf_derefrule(softc, &nat->nat_fr);
   2713  1.1  christos 	}
   2714  1.1  christos 
   2715  1.1  christos 	if (nat->nat_hm != NULL) {
   2716  1.1  christos 		ipf_nat_hostmapdel(&nat->nat_hm);
   2717  1.1  christos 	}
   2718  1.1  christos 
   2719  1.1  christos 	/*
   2720  1.1  christos 	 * If there is an active reference from the nat entry to its parent
   2721  1.1  christos 	 * rule, decrement the rule's reference count and free it too if no
   2722  1.1  christos 	 * longer being used.
   2723  1.1  christos 	 */
   2724  1.1  christos 	ipn = nat->nat_ptr;
   2725  1.1  christos 	nat->nat_ptr = NULL;
   2726  1.1  christos 
   2727  1.1  christos 	if (ipn != NULL) {
   2728  1.1  christos 		ipf_nat_rulederef(softc, &ipn);
   2729  1.1  christos 	}
   2730  1.1  christos 
   2731  1.1  christos 	MUTEX_DESTROY(&nat->nat_lock);
   2732  1.1  christos 
   2733  1.1  christos 	aps_free(softc, softc->ipf_proxy_soft, nat->nat_aps);
   2734  1.1  christos 	softn->ipf_nat_stats.ns_active--;
   2735  1.1  christos 
   2736  1.1  christos 	/*
   2737  1.1  christos 	 * If there's a fragment table entry too for this nat entry, then
   2738  1.1  christos 	 * dereference that as well.  This is after nat_lock is released
   2739  1.1  christos 	 * because of Tru64.
   2740  1.1  christos 	 */
   2741  1.1  christos 	ipf_frag_natforget(softc, (void *)nat);
   2742  1.1  christos 
   2743  1.1  christos 	KFREE(nat);
   2744  1.1  christos }
   2745  1.1  christos 
   2746  1.1  christos 
   2747  1.1  christos /* ------------------------------------------------------------------------ */
   2748  1.1  christos /* Function:    ipf_nat_flushtable                                          */
   2749  1.1  christos /* Returns:     int - number of NAT rules deleted                           */
   2750  1.1  christos /* Parameters:  Nil                                                         */
   2751  1.1  christos /* Write Lock:  ipf_nat                                                     */
   2752  1.1  christos /*                                                                          */
   2753  1.1  christos /* Deletes all currently active NAT sessions.  In deleting each NAT entry a */
   2754  1.1  christos /* log record should be emitted in ipf_nat_delete() if NAT logging is       */
   2755  1.1  christos /* enabled.                                                                 */
   2756  1.1  christos /* ------------------------------------------------------------------------ */
   2757  1.1  christos /*
   2758  1.1  christos  * nat_flushtable - clear the NAT table of all mapping entries.
   2759  1.1  christos  */
   2760  1.1  christos static int
   2761  1.1  christos ipf_nat_flushtable(softc, softn)
   2762  1.1  christos 	ipf_main_softc_t *softc;
   2763  1.1  christos 	ipf_nat_softc_t *softn;
   2764  1.1  christos {
   2765  1.1  christos 	nat_t *nat;
   2766  1.1  christos 	int j = 0;
   2767  1.1  christos 
   2768  1.1  christos 	/*
   2769  1.1  christos 	 * ALL NAT mappings deleted, so lets just make the deletions
   2770  1.1  christos 	 * quicker.
   2771  1.1  christos 	 */
   2772  1.1  christos 	if (softn->ipf_nat_table[0] != NULL)
   2773  1.1  christos 		bzero((char *)softn->ipf_nat_table[0],
   2774  1.1  christos 		      sizeof(softn->ipf_nat_table[0]) *
   2775  1.1  christos 		      softn->ipf_nat_table_sz);
   2776  1.1  christos 	if (softn->ipf_nat_table[1] != NULL)
   2777  1.1  christos 		bzero((char *)softn->ipf_nat_table[1],
   2778  1.1  christos 		      sizeof(softn->ipf_nat_table[1]) *
   2779  1.1  christos 		      softn->ipf_nat_table_sz);
   2780  1.1  christos 
   2781  1.1  christos 	while ((nat = softn->ipf_nat_instances) != NULL) {
   2782  1.1  christos 		ipf_nat_delete(softc, nat, NL_FLUSH);
   2783  1.1  christos 		j++;
   2784  1.1  christos 	}
   2785  1.1  christos 
   2786  1.1  christos 	return j;
   2787  1.1  christos }
   2788  1.1  christos 
   2789  1.1  christos 
   2790  1.1  christos /* ------------------------------------------------------------------------ */
   2791  1.1  christos /* Function:    ipf_nat_clearlist                                           */
   2792  1.1  christos /* Returns:     int - number of NAT/RDR rules deleted                       */
   2793  1.1  christos /* Parameters:  Nil                                                         */
   2794  1.1  christos /*                                                                          */
   2795  1.1  christos /* Delete all rules in the current list of rules.  There is nothing elegant */
   2796  1.1  christos /* about this cleanup: simply free all entries on the list of rules and     */
   2797  1.1  christos /* clear out the tables used for hashed NAT rule lookups.                   */
   2798  1.1  christos /* ------------------------------------------------------------------------ */
   2799  1.1  christos static int
   2800  1.1  christos ipf_nat_clearlist(softc, softn)
   2801  1.1  christos 	ipf_main_softc_t *softc;
   2802  1.1  christos 	ipf_nat_softc_t *softn;
   2803  1.1  christos {
   2804  1.1  christos 	ipnat_t *n, **np = &softn->ipf_nat_list;
   2805  1.1  christos 	int i = 0;
   2806  1.1  christos 
   2807  1.1  christos 	if (softn->ipf_nat_map_rules != NULL) {
   2808  1.1  christos 		bzero((char *)softn->ipf_nat_map_rules,
   2809  1.1  christos 		      sizeof(*softn->ipf_nat_map_rules) *
   2810  1.1  christos 		      softn->ipf_nat_maprules_sz);
   2811  1.1  christos 	}
   2812  1.1  christos 	if (softn->ipf_nat_rdr_rules != NULL) {
   2813  1.1  christos 		bzero((char *)softn->ipf_nat_rdr_rules,
   2814  1.1  christos 		      sizeof(*softn->ipf_nat_rdr_rules) *
   2815  1.1  christos 		      softn->ipf_nat_rdrrules_sz);
   2816  1.1  christos 	}
   2817  1.1  christos 
   2818  1.1  christos 	while ((n = *np) != NULL) {
   2819  1.1  christos 		*np = n->in_next;
   2820  1.1  christos 		ipf_nat_delrule(softc, softn, n);
   2821  1.1  christos 		i++;
   2822  1.1  christos 	}
   2823  1.1  christos #if SOLARIS && !defined(INSTANCES)
   2824  1.1  christos 	pfil_delayed_copy = 1;
   2825  1.1  christos #endif
   2826  1.1  christos 	return i;
   2827  1.1  christos }
   2828  1.1  christos 
   2829  1.1  christos 
   2830  1.1  christos /* ------------------------------------------------------------------------ */
   2831  1.1  christos /* Function:    ipf_nat_delrule                                             */
   2832  1.1  christos /* Returns:     Nil                                                         */
   2833  1.1  christos /* Parameters:  np(I) - pointer to NAT rule to delete                       */
   2834  1.1  christos /*                                                                          */
   2835  1.1  christos /* ------------------------------------------------------------------------ */
   2836  1.1  christos void
   2837  1.1  christos ipf_nat_delrule(softc, softn, np)
   2838  1.1  christos 	ipf_main_softc_t *softc;
   2839  1.1  christos 	ipf_nat_softc_t *softn;
   2840  1.1  christos 	ipnat_t *np;
   2841  1.1  christos {
   2842  1.1  christos 	if (np->in_use == 0) {
   2843  1.1  christos 		ipf_nat_free_rule(softc, softn, np);
   2844  1.1  christos 	} else {
   2845  1.1  christos 		np->in_flags |= IPN_DELETE;
   2846  1.1  christos 		np->in_next = NULL;
   2847  1.1  christos 	}
   2848  1.1  christos 
   2849  1.1  christos }
   2850  1.1  christos 
   2851  1.1  christos 
   2852  1.1  christos /* ------------------------------------------------------------------------ */
   2853  1.1  christos /* Function:    ipf_nat_newmap                                              */
   2854  1.1  christos /* Returns:     int - -1 == error, 0 == success                             */
   2855  1.1  christos /* Parameters:  fin(I) - pointer to packet information                      */
   2856  1.1  christos /*              nat(I) - pointer to NAT entry                               */
   2857  1.1  christos /*              ni(I)  - pointer to structure with misc. information needed */
   2858  1.1  christos /*                       to create new NAT entry.                           */
   2859  1.1  christos /*                                                                          */
   2860  1.1  christos /* Given an empty NAT structure, populate it with new information about a   */
   2861  1.1  christos /* new NAT session, as defined by the matching NAT rule.                    */
   2862  1.1  christos /* ni.nai_ip is passed in uninitialised and must be set, in host byte order,*/
   2863  1.1  christos /* to the new IP address for the translation.                               */
   2864  1.1  christos /* ------------------------------------------------------------------------ */
   2865  1.1  christos static int
   2866  1.1  christos ipf_nat_newmap(fin, nat, ni)
   2867  1.1  christos 	fr_info_t *fin;
   2868  1.1  christos 	nat_t *nat;
   2869  1.1  christos 	natinfo_t *ni;
   2870  1.1  christos {
   2871  1.1  christos 	ipf_main_softc_t *softc = fin->fin_main_soft;
   2872  1.1  christos 	ipf_nat_softc_t *softn = softc->ipf_nat_soft;
   2873  1.1  christos 	u_short st_port, dport, sport, port, sp, dp;
   2874  1.1  christos 	struct in_addr in, inb;
   2875  1.1  christos 	hostmap_t *hm;
   2876  1.1  christos 	u_32_t flags;
   2877  1.1  christos 	u_32_t st_ip;
   2878  1.1  christos 	ipnat_t *np;
   2879  1.1  christos 	nat_t *natl;
   2880  1.1  christos 	int l;
   2881  1.1  christos 
   2882  1.1  christos 	/*
   2883  1.1  christos 	 * If it's an outbound packet which doesn't match any existing
   2884  1.1  christos 	 * record, then create a new port
   2885  1.1  christos 	 */
   2886  1.1  christos 	l = 0;
   2887  1.1  christos 	hm = NULL;
   2888  1.1  christos 	np = ni->nai_np;
   2889  1.1  christos 	st_ip = np->in_snip;
   2890  1.1  christos 	st_port = np->in_spnext;
   2891  1.1  christos 	flags = nat->nat_flags;
   2892  1.1  christos 
   2893  1.1  christos 	if (flags & IPN_ICMPQUERY) {
   2894  1.1  christos 		sport = fin->fin_data[1];
   2895  1.1  christos 		dport = 0;
   2896  1.1  christos 	} else {
   2897  1.1  christos 		sport = htons(fin->fin_data[0]);
   2898  1.1  christos 		dport = htons(fin->fin_data[1]);
   2899  1.1  christos 	}
   2900  1.1  christos 
   2901  1.1  christos 	/*
   2902  1.1  christos 	 * Do a loop until we either run out of entries to try or we find
   2903  1.1  christos 	 * a NAT mapping that isn't currently being used.  This is done
   2904  1.1  christos 	 * because the change to the source is not (usually) being fixed.
   2905  1.1  christos 	 */
   2906  1.1  christos 	do {
   2907  1.1  christos 		port = 0;
   2908  1.1  christos 		in.s_addr = htonl(np->in_snip);
   2909  1.1  christos 		if (l == 0) {
   2910  1.1  christos 			/*
   2911  1.1  christos 			 * Check to see if there is an existing NAT
   2912  1.1  christos 			 * setup for this IP address pair.
   2913  1.1  christos 			 */
   2914  1.1  christos 			hm = ipf_nat_hostmap(softn, np, fin->fin_src,
   2915  1.1  christos 					     fin->fin_dst, in, 0);
   2916  1.1  christos 			if (hm != NULL)
   2917  1.1  christos 				in.s_addr = hm->hm_nsrcip.s_addr;
   2918  1.1  christos 		} else if ((l == 1) && (hm != NULL)) {
   2919  1.1  christos 			ipf_nat_hostmapdel(&hm);
   2920  1.1  christos 		}
   2921  1.1  christos 		in.s_addr = ntohl(in.s_addr);
   2922  1.1  christos 
   2923  1.1  christos 		nat->nat_hm = hm;
   2924  1.1  christos 
   2925  1.1  christos 		if ((np->in_nsrcmsk == 0xffffffff) && (np->in_spnext == 0)) {
   2926  1.1  christos 			if (l > 0) {
   2927  1.1  christos 				NBUMPSIDEX(1, ns_exhausted, ns_exhausted_1);
   2928  1.1  christos 				return -1;
   2929  1.1  christos 			}
   2930  1.1  christos 		}
   2931  1.1  christos 
   2932  1.1  christos 		if (np->in_redir == NAT_BIMAP &&
   2933  1.1  christos 		    np->in_osrcmsk == np->in_nsrcmsk) {
   2934  1.1  christos 			/*
   2935  1.1  christos 			 * map the address block in a 1:1 fashion
   2936  1.1  christos 			 */
   2937  1.1  christos 			in.s_addr = np->in_nsrcaddr;
   2938  1.1  christos 			in.s_addr |= fin->fin_saddr & ~np->in_osrcmsk;
   2939  1.1  christos 			in.s_addr = ntohl(in.s_addr);
   2940  1.1  christos 
   2941  1.1  christos 		} else if (np->in_redir & NAT_MAPBLK) {
   2942  1.1  christos 			if ((l >= np->in_ppip) || ((l > 0) &&
   2943  1.1  christos 			     !(flags & IPN_TCPUDP))) {
   2944  1.1  christos 				NBUMPSIDEX(1, ns_exhausted, ns_exhausted_2);
   2945  1.1  christos 				return -1;
   2946  1.1  christos 			}
   2947  1.1  christos 			/*
   2948  1.1  christos 			 * map-block - Calculate destination address.
   2949  1.1  christos 			 */
   2950  1.1  christos 			in.s_addr = ntohl(fin->fin_saddr);
   2951  1.1  christos 			in.s_addr &= ntohl(~np->in_osrcmsk);
   2952  1.1  christos 			inb.s_addr = in.s_addr;
   2953  1.1  christos 			in.s_addr /= np->in_ippip;
   2954  1.1  christos 			in.s_addr &= ntohl(~np->in_nsrcmsk);
   2955  1.1  christos 			in.s_addr += ntohl(np->in_nsrcaddr);
   2956  1.1  christos 			/*
   2957  1.1  christos 			 * Calculate destination port.
   2958  1.1  christos 			 */
   2959  1.1  christos 			if ((flags & IPN_TCPUDP) &&
   2960  1.1  christos 			    (np->in_ppip != 0)) {
   2961  1.1  christos 				port = ntohs(sport) + l;
   2962  1.1  christos 				port %= np->in_ppip;
   2963  1.1  christos 				port += np->in_ppip *
   2964  1.1  christos 					(inb.s_addr % np->in_ippip);
   2965  1.1  christos 				port += MAPBLK_MINPORT;
   2966  1.1  christos 				port = htons(port);
   2967  1.1  christos 			}
   2968  1.1  christos 
   2969  1.1  christos 		} else if ((np->in_nsrcaddr == 0) &&
   2970  1.1  christos 			   (np->in_nsrcmsk == 0xffffffff)) {
   2971  1.1  christos 			i6addr_t in6;
   2972  1.1  christos 
   2973  1.1  christos 			/*
   2974  1.1  christos 			 * 0/32 - use the interface's IP address.
   2975  1.1  christos 			 */
   2976  1.1  christos 			if ((l > 0) ||
   2977  1.1  christos 			    ipf_ifpaddr(softc, 4, FRI_NORMAL, fin->fin_ifp,
   2978  1.1  christos 				       &in6, NULL) == -1) {
   2979  1.1  christos 				NBUMPSIDEX(1, ns_new_ifpaddr, ns_new_ifpaddr_1);
   2980  1.1  christos 				return -1;
   2981  1.1  christos 			}
   2982  1.1  christos 			in.s_addr = ntohl(in6.in4.s_addr);
   2983  1.1  christos 
   2984  1.1  christos 		} else if ((np->in_nsrcaddr == 0) && (np->in_nsrcmsk == 0)) {
   2985  1.1  christos 			/*
   2986  1.1  christos 			 * 0/0 - use the original source address/port.
   2987  1.1  christos 			 */
   2988  1.1  christos 			if (l > 0) {
   2989  1.1  christos 				NBUMPSIDEX(1, ns_exhausted, ns_exhausted_3);
   2990  1.1  christos 				return -1;
   2991  1.1  christos 			}
   2992  1.1  christos 			in.s_addr = ntohl(fin->fin_saddr);
   2993  1.1  christos 
   2994  1.1  christos 		} else if ((np->in_nsrcmsk != 0xffffffff) &&
   2995  1.1  christos 			   (np->in_spnext == 0) && ((l > 0) || (hm == NULL)))
   2996  1.1  christos 			np->in_snip++;
   2997  1.1  christos 
   2998  1.1  christos 		natl = NULL;
   2999  1.1  christos 
   3000  1.1  christos 		if ((flags & IPN_TCPUDP) &&
   3001  1.1  christos 		    ((np->in_redir & NAT_MAPBLK) == 0) &&
   3002  1.1  christos 		    (np->in_flags & IPN_AUTOPORTMAP)) {
   3003  1.1  christos 			/*
   3004  1.1  christos 			 * "ports auto" (without map-block)
   3005  1.1  christos 			 */
   3006  1.1  christos 			if ((l > 0) && (l % np->in_ppip == 0)) {
   3007  1.1  christos 				if ((l > np->in_ppip) &&
   3008  1.1  christos 				    np->in_nsrcmsk != 0xffffffff)
   3009  1.1  christos 					np->in_snip++;
   3010  1.1  christos 			}
   3011  1.1  christos 			if (np->in_ppip != 0) {
   3012  1.1  christos 				port = ntohs(sport);
   3013  1.1  christos 				port += (l % np->in_ppip);
   3014  1.1  christos 				port %= np->in_ppip;
   3015  1.1  christos 				port += np->in_ppip *
   3016  1.1  christos 					(ntohl(fin->fin_saddr) %
   3017  1.1  christos 					 np->in_ippip);
   3018  1.1  christos 				port += MAPBLK_MINPORT;
   3019  1.1  christos 				port = htons(port);
   3020  1.1  christos 			}
   3021  1.1  christos 
   3022  1.1  christos 		} else if (((np->in_redir & NAT_MAPBLK) == 0) &&
   3023  1.1  christos 			   (flags & IPN_TCPUDPICMP) && (np->in_spnext != 0)) {
   3024  1.1  christos 			/*
   3025  1.1  christos 			 * Standard port translation.  Select next port.
   3026  1.1  christos 			 */
   3027  1.1  christos 			if (np->in_flags & IPN_SEQUENTIAL) {
   3028  1.1  christos 				port = np->in_spnext;
   3029  1.1  christos 			} else {
   3030  1.1  christos 				port = ipf_random() % (np->in_spmax -
   3031  1.1  christos 						       np->in_spmin + 1);
   3032  1.1  christos 				port += np->in_spmin;
   3033  1.1  christos 			}
   3034  1.1  christos 			port = htons(port);
   3035  1.1  christos 			np->in_spnext++;
   3036  1.1  christos 
   3037  1.1  christos 			if (np->in_spnext > np->in_spmax) {
   3038  1.1  christos 				np->in_spnext = np->in_spmin;
   3039  1.1  christos 				if (np->in_nsrcmsk != 0xffffffff)
   3040  1.1  christos 					np->in_snip++;
   3041  1.1  christos 			}
   3042  1.1  christos 		}
   3043  1.1  christos 
   3044  1.1  christos 		if (np->in_flags & IPN_SIPRANGE) {
   3045  1.1  christos 			if (np->in_snip > ntohl(np->in_nsrcmsk))
   3046  1.1  christos 				np->in_snip = ntohl(np->in_nsrcaddr);
   3047  1.1  christos 		} else {
   3048  1.1  christos 			if ((np->in_nsrcmsk != 0xffffffff) &&
   3049  1.1  christos 			    ((np->in_snip + 1) & ntohl(np->in_nsrcmsk)) >
   3050  1.1  christos 			    ntohl(np->in_nsrcaddr))
   3051  1.1  christos 				np->in_snip = ntohl(np->in_nsrcaddr) + 1;
   3052  1.1  christos 		}
   3053  1.1  christos 
   3054  1.1  christos 		if ((port == 0) && (flags & (IPN_TCPUDPICMP|IPN_ICMPQUERY)))
   3055  1.1  christos 			port = sport;
   3056  1.1  christos 
   3057  1.1  christos 		/*
   3058  1.1  christos 		 * Here we do a lookup of the connection as seen from
   3059  1.1  christos 		 * the outside.  If an IP# pair already exists, try
   3060  1.1  christos 		 * again.  So if you have A->B becomes C->B, you can
   3061  1.1  christos 		 * also have D->E become C->E but not D->B causing
   3062  1.1  christos 		 * another C->B.  Also take protocol and ports into
   3063  1.1  christos 		 * account when determining whether a pre-existing
   3064  1.1  christos 		 * NAT setup will cause an external conflict where
   3065  1.1  christos 		 * this is appropriate.
   3066  1.1  christos 		 */
   3067  1.1  christos 		inb.s_addr = htonl(in.s_addr);
   3068  1.1  christos 		sp = fin->fin_data[0];
   3069  1.1  christos 		dp = fin->fin_data[1];
   3070  1.1  christos 		fin->fin_data[0] = fin->fin_data[1];
   3071  1.1  christos 		fin->fin_data[1] = ntohs(port);
   3072  1.1  christos 		natl = ipf_nat_inlookup(fin, flags & ~(SI_WILDP|NAT_SEARCH),
   3073  1.1  christos 					(u_int)fin->fin_p, fin->fin_dst, inb);
   3074  1.1  christos 		fin->fin_data[0] = sp;
   3075  1.1  christos 		fin->fin_data[1] = dp;
   3076  1.1  christos 
   3077  1.1  christos 		/*
   3078  1.1  christos 		 * Has the search wrapped around and come back to the
   3079  1.1  christos 		 * start ?
   3080  1.1  christos 		 */
   3081  1.1  christos 		if ((natl != NULL) &&
   3082  1.1  christos 		    (np->in_spnext != 0) && (st_port == np->in_spnext) &&
   3083  1.1  christos 		    (np->in_snip != 0) && (st_ip == np->in_snip)) {
   3084  1.1  christos 			NBUMPSIDED(1, ns_wrap);
   3085  1.1  christos 			return -1;
   3086  1.1  christos 		}
   3087  1.1  christos 		l++;
   3088  1.1  christos 	} while (natl != NULL);
   3089  1.1  christos 
   3090  1.1  christos 	/* Setup the NAT table */
   3091  1.1  christos 	nat->nat_osrcip = fin->fin_src;
   3092  1.1  christos 	nat->nat_nsrcaddr = htonl(in.s_addr);
   3093  1.1  christos 	nat->nat_odstip = fin->fin_dst;
   3094  1.1  christos 	nat->nat_ndstip = fin->fin_dst;
   3095  1.1  christos 	if (nat->nat_hm == NULL)
   3096  1.1  christos 		nat->nat_hm = ipf_nat_hostmap(softn, np, fin->fin_src,
   3097  1.1  christos 					      fin->fin_dst, nat->nat_nsrcip,
   3098  1.1  christos 					      0);
   3099  1.1  christos 
   3100  1.1  christos 	if (flags & IPN_TCPUDP) {
   3101  1.1  christos 		nat->nat_osport = sport;
   3102  1.1  christos 		nat->nat_nsport = port;	/* sport */
   3103  1.1  christos 		nat->nat_odport = dport;
   3104  1.1  christos 		nat->nat_ndport = dport;
   3105  1.1  christos 		((tcphdr_t *)fin->fin_dp)->th_sport = port;
   3106  1.1  christos 	} else if (flags & IPN_ICMPQUERY) {
   3107  1.1  christos 		nat->nat_oicmpid = fin->fin_data[1];
   3108  1.1  christos 		((icmphdr_t *)fin->fin_dp)->icmp_id = port;
   3109  1.1  christos 		nat->nat_nicmpid = port;
   3110  1.1  christos 	}
   3111  1.1  christos 	return 0;
   3112  1.1  christos }
   3113  1.1  christos 
   3114  1.1  christos 
   3115  1.1  christos /* ------------------------------------------------------------------------ */
   3116  1.1  christos /* Function:    ipf_nat_newrdr                                              */
   3117  1.1  christos /* Returns:     int - -1 == error, 0 == success (no move), 1 == success and */
   3118  1.1  christos /*                    allow rule to be moved if IPN_ROUNDR is set.          */
   3119  1.1  christos /* Parameters:  fin(I) - pointer to packet information                      */
   3120  1.1  christos /*              nat(I) - pointer to NAT entry                               */
   3121  1.1  christos /*              ni(I)  - pointer to structure with misc. information needed */
   3122  1.1  christos /*                       to create new NAT entry.                           */
   3123  1.1  christos /*                                                                          */
   3124  1.1  christos /* ni.nai_ip is passed in uninitialised and must be set, in host byte order,*/
   3125  1.1  christos /* to the new IP address for the translation.                               */
   3126  1.1  christos /* ------------------------------------------------------------------------ */
   3127  1.1  christos static int
   3128  1.1  christos ipf_nat_newrdr(fin, nat, ni)
   3129  1.1  christos 	fr_info_t *fin;
   3130  1.1  christos 	nat_t *nat;
   3131  1.1  christos 	natinfo_t *ni;
   3132  1.1  christos {
   3133  1.1  christos 	ipf_main_softc_t *softc = fin->fin_main_soft;
   3134  1.1  christos 	ipf_nat_softc_t *softn = softc->ipf_nat_soft;
   3135  1.1  christos 	u_short nport, dport, sport;
   3136  1.1  christos 	struct in_addr in, inb;
   3137  1.1  christos 	u_short sp, dp;
   3138  1.1  christos 	hostmap_t *hm;
   3139  1.1  christos 	u_32_t flags;
   3140  1.1  christos 	ipnat_t *np;
   3141  1.1  christos 	nat_t *natl;
   3142  1.1  christos 	int move;
   3143  1.1  christos 
   3144  1.1  christos 	move = 1;
   3145  1.1  christos 	hm = NULL;
   3146  1.1  christos 	in.s_addr = 0;
   3147  1.1  christos 	np = ni->nai_np;
   3148  1.1  christos 	flags = nat->nat_flags;
   3149  1.1  christos 
   3150  1.1  christos 	if (flags & IPN_ICMPQUERY) {
   3151  1.1  christos 		dport = fin->fin_data[1];
   3152  1.1  christos 		sport = 0;
   3153  1.1  christos 	} else {
   3154  1.1  christos 		sport = htons(fin->fin_data[0]);
   3155  1.1  christos 		dport = htons(fin->fin_data[1]);
   3156  1.1  christos 	}
   3157  1.1  christos 
   3158  1.1  christos 	/* TRACE sport, dport */
   3159  1.1  christos 
   3160  1.1  christos 
   3161  1.1  christos 	/*
   3162  1.1  christos 	 * If the matching rule has IPN_STICKY set, then we want to have the
   3163  1.1  christos 	 * same rule kick in as before.  Why would this happen?  If you have
   3164  1.1  christos 	 * a collection of rdr rules with "round-robin sticky", the current
   3165  1.1  christos 	 * packet might match a different one to the previous connection but
   3166  1.1  christos 	 * we want the same destination to be used.
   3167  1.1  christos 	 */
   3168  1.1  christos 	if (((np->in_flags & (IPN_ROUNDR|IPN_SPLIT)) != 0) &&
   3169  1.1  christos 	    ((np->in_flags & IPN_STICKY) != 0)) {
   3170  1.1  christos 		hm = ipf_nat_hostmap(softn, NULL, fin->fin_src, fin->fin_dst,
   3171  1.1  christos 				     in, (u_32_t)dport);
   3172  1.1  christos 		if (hm != NULL) {
   3173  1.1  christos 			in.s_addr = ntohl(hm->hm_ndstip.s_addr);
   3174  1.1  christos 			np = hm->hm_ipnat;
   3175  1.1  christos 			ni->nai_np = np;
   3176  1.1  christos 			move = 0;
   3177  1.1  christos 			ipf_nat_hostmapdel(&hm);
   3178  1.1  christos 		}
   3179  1.1  christos 	}
   3180  1.1  christos 
   3181  1.1  christos 	/*
   3182  1.1  christos 	 * Otherwise, it's an inbound packet. Most likely, we don't
   3183  1.1  christos 	 * want to rewrite source ports and source addresses. Instead,
   3184  1.1  christos 	 * we want to rewrite to a fixed internal address and fixed
   3185  1.1  christos 	 * internal port.
   3186  1.1  christos 	 */
   3187  1.1  christos 	if (np->in_flags & IPN_SPLIT) {
   3188  1.1  christos 		in.s_addr = np->in_dnip;
   3189  1.1  christos 
   3190  1.1  christos 		if ((np->in_flags & (IPN_ROUNDR|IPN_STICKY)) == IPN_STICKY) {
   3191  1.1  christos 			hm = ipf_nat_hostmap(softn, NULL, fin->fin_src,
   3192  1.1  christos 					     fin->fin_dst, in, (u_32_t)dport);
   3193  1.1  christos 			if (hm != NULL) {
   3194  1.1  christos 				in.s_addr = hm->hm_ndstip.s_addr;
   3195  1.1  christos 				move = 0;
   3196  1.1  christos 			}
   3197  1.1  christos 		}
   3198  1.1  christos 
   3199  1.1  christos 		if (hm == NULL || hm->hm_ref == 1) {
   3200  1.1  christos 			if (np->in_ndstaddr == htonl(in.s_addr)) {
   3201  1.1  christos 				np->in_dnip = ntohl(np->in_ndstmsk);
   3202  1.1  christos 				move = 0;
   3203  1.1  christos 			} else {
   3204  1.1  christos 				np->in_dnip = ntohl(np->in_ndstaddr);
   3205  1.1  christos 			}
   3206  1.1  christos 		}
   3207  1.1  christos 		if (hm != NULL)
   3208  1.1  christos 			ipf_nat_hostmapdel(&hm);
   3209  1.1  christos 
   3210  1.1  christos 	} else if ((np->in_ndstaddr == 0) && (np->in_ndstmsk == 0xffffffff)) {
   3211  1.1  christos 		i6addr_t in6;
   3212  1.1  christos 
   3213  1.1  christos 		/*
   3214  1.1  christos 		 * 0/32 - use the interface's IP address.
   3215  1.1  christos 		 */
   3216  1.1  christos 		if (ipf_ifpaddr(softc, 4, FRI_NORMAL, fin->fin_ifp,
   3217  1.1  christos 			       &in6, NULL) == -1) {
   3218  1.1  christos 			NBUMPSIDEX(0, ns_new_ifpaddr, ns_new_ifpaddr_2);
   3219  1.1  christos 			return -1;
   3220  1.1  christos 		}
   3221  1.1  christos 		in.s_addr = ntohl(in6.in4.s_addr);
   3222  1.1  christos 
   3223  1.1  christos 	} else if ((np->in_ndstaddr == 0) && (np->in_ndstmsk== 0)) {
   3224  1.1  christos 		/*
   3225  1.1  christos 		 * 0/0 - use the original destination address/port.
   3226  1.1  christos 		 */
   3227  1.1  christos 		in.s_addr = ntohl(fin->fin_daddr);
   3228  1.1  christos 
   3229  1.1  christos 	} else if (np->in_redir == NAT_BIMAP &&
   3230  1.1  christos 		   np->in_ndstmsk == np->in_odstmsk) {
   3231  1.1  christos 		/*
   3232  1.1  christos 		 * map the address block in a 1:1 fashion
   3233  1.1  christos 		 */
   3234  1.1  christos 		in.s_addr = np->in_ndstaddr;
   3235  1.1  christos 		in.s_addr |= fin->fin_daddr & ~np->in_ndstmsk;
   3236  1.1  christos 		in.s_addr = ntohl(in.s_addr);
   3237  1.1  christos 	} else {
   3238  1.1  christos 		in.s_addr = ntohl(np->in_ndstaddr);
   3239  1.1  christos 	}
   3240  1.1  christos 
   3241  1.1  christos 	if ((np->in_dpnext == 0) || ((flags & NAT_NOTRULEPORT) != 0))
   3242  1.1  christos 		nport = dport;
   3243  1.1  christos 	else {
   3244  1.1  christos 		/*
   3245  1.1  christos 		 * Whilst not optimized for the case where
   3246  1.1  christos 		 * pmin == pmax, the gain is not significant.
   3247  1.1  christos 		 */
   3248  1.1  christos 		if (((np->in_flags & IPN_FIXEDDPORT) == 0) &&
   3249  1.1  christos 		    (np->in_odport != np->in_dtop)) {
   3250  1.1  christos 			nport = ntohs(dport) - np->in_odport + np->in_dpmax;
   3251  1.1  christos 			nport = htons(nport);
   3252  1.1  christos 		} else {
   3253  1.1  christos 			nport = htons(np->in_dpnext);
   3254  1.1  christos 			np->in_dpnext++;
   3255  1.1  christos 			if (np->in_dpnext > np->in_dpmax)
   3256  1.1  christos 				np->in_dpnext = np->in_dpmin;
   3257  1.1  christos 		}
   3258  1.1  christos 	}
   3259  1.1  christos 
   3260  1.1  christos 	/*
   3261  1.1  christos 	 * When the redirect-to address is set to 0.0.0.0, just
   3262  1.1  christos 	 * assume a blank `forwarding' of the packet.  We don't
   3263  1.1  christos 	 * setup any translation for this either.
   3264  1.1  christos 	 */
   3265  1.1  christos 	if (in.s_addr == 0) {
   3266  1.1  christos 		if (nport == dport) {
   3267  1.1  christos 			NBUMPSIDED(0, ns_xlate_null);
   3268  1.1  christos 			return -1;
   3269  1.1  christos 		}
   3270  1.1  christos 		in.s_addr = ntohl(fin->fin_daddr);
   3271  1.1  christos 	}
   3272  1.1  christos 
   3273  1.1  christos 	/*
   3274  1.1  christos 	 * Check to see if this redirect mapping already exists and if
   3275  1.1  christos 	 * it does, return "failure" (allowing it to be created will just
   3276  1.1  christos 	 * cause one or both of these "connections" to stop working.)
   3277  1.1  christos 	 */
   3278  1.1  christos 	inb.s_addr = htonl(in.s_addr);
   3279  1.1  christos 	sp = fin->fin_data[0];
   3280  1.1  christos 	dp = fin->fin_data[1];
   3281  1.1  christos 	fin->fin_data[1] = fin->fin_data[0];
   3282  1.1  christos 	fin->fin_data[0] = ntohs(nport);
   3283  1.1  christos 	natl = ipf_nat_outlookup(fin, flags & ~(SI_WILDP|NAT_SEARCH),
   3284  1.1  christos 			     (u_int)fin->fin_p, inb, fin->fin_src);
   3285  1.1  christos 	fin->fin_data[0] = sp;
   3286  1.1  christos 	fin->fin_data[1] = dp;
   3287  1.1  christos 	if (natl != NULL) {
   3288  1.1  christos 		DT2(ns_new_xlate_exists, fr_info_t *, fin, nat_t *, natl);
   3289  1.1  christos 		NBUMPSIDE(0, ns_xlate_exists);
   3290  1.1  christos 		return -1;
   3291  1.1  christos 	}
   3292  1.1  christos 
   3293  1.1  christos 	nat->nat_ndstaddr = htonl(in.s_addr);
   3294  1.1  christos 	nat->nat_odstip = fin->fin_dst;
   3295  1.1  christos 	nat->nat_nsrcip = fin->fin_src;
   3296  1.1  christos 	nat->nat_osrcip = fin->fin_src;
   3297  1.1  christos 	if ((nat->nat_hm == NULL) && ((np->in_flags & IPN_STICKY) != 0))
   3298  1.1  christos 		nat->nat_hm = ipf_nat_hostmap(softn, np, fin->fin_src,
   3299  1.1  christos 					      fin->fin_dst, in, (u_32_t)dport);
   3300  1.1  christos 
   3301  1.1  christos 	if (flags & IPN_TCPUDP) {
   3302  1.1  christos 		nat->nat_odport = dport;
   3303  1.1  christos 		nat->nat_ndport = nport;
   3304  1.1  christos 		nat->nat_osport = sport;
   3305  1.1  christos 		nat->nat_nsport = sport;
   3306  1.1  christos 		((tcphdr_t *)fin->fin_dp)->th_dport = nport;
   3307  1.1  christos 	} else if (flags & IPN_ICMPQUERY) {
   3308  1.1  christos 		nat->nat_oicmpid = fin->fin_data[1];
   3309  1.1  christos 		((icmphdr_t *)fin->fin_dp)->icmp_id = nport;
   3310  1.1  christos 		nat->nat_nicmpid = nport;
   3311  1.1  christos 	}
   3312  1.1  christos 
   3313  1.1  christos 	return move;
   3314  1.1  christos }
   3315  1.1  christos 
   3316  1.1  christos /* ------------------------------------------------------------------------ */
   3317  1.1  christos /* Function:    ipf_nat_add                                                 */
   3318  1.1  christos /* Returns:     nat_t* - NULL == failure to create new NAT structure,       */
   3319  1.1  christos /*                       else pointer to new NAT structure                  */
   3320  1.1  christos /* Parameters:  fin(I)       - pointer to packet information                */
   3321  1.1  christos /*              np(I)        - pointer to NAT rule                          */
   3322  1.1  christos /*              natsave(I)   - pointer to where to store NAT struct pointer */
   3323  1.1  christos /*              flags(I)     - flags describing the current packet          */
   3324  1.1  christos /*              direction(I) - direction of packet (in/out)                 */
   3325  1.1  christos /* Write Lock:  ipf_nat                                                     */
   3326  1.1  christos /*                                                                          */
   3327  1.1  christos /* Attempts to create a new NAT entry.  Does not actually change the packet */
   3328  1.1  christos /* in any way.                                                              */
   3329  1.1  christos /*                                                                          */
   3330  1.1  christos /* This fucntion is in three main parts: (1) deal with creating a new NAT   */
   3331  1.1  christos /* structure for a "MAP" rule (outgoing NAT translation); (2) deal with     */
   3332  1.1  christos /* creating a new NAT structure for a "RDR" rule (incoming NAT translation) */
   3333  1.1  christos /* and (3) building that structure and putting it into the NAT table(s).    */
   3334  1.1  christos /*                                                                          */
   3335  1.1  christos /* NOTE: natsave should NOT be used top point back to an ipstate_t struct   */
   3336  1.1  christos /*       as it can result in memory being corrupted.                        */
   3337  1.1  christos /* ------------------------------------------------------------------------ */
   3338  1.1  christos nat_t *
   3339  1.1  christos ipf_nat_add(fin, np, natsave, flags, direction)
   3340  1.1  christos 	fr_info_t *fin;
   3341  1.1  christos 	ipnat_t *np;
   3342  1.1  christos 	nat_t **natsave;
   3343  1.1  christos 	u_int flags;
   3344  1.1  christos 	int direction;
   3345  1.1  christos {
   3346  1.1  christos 	ipf_main_softc_t *softc = fin->fin_main_soft;
   3347  1.1  christos 	ipf_nat_softc_t *softn = softc->ipf_nat_soft;
   3348  1.1  christos 	hostmap_t *hm = NULL;
   3349  1.1  christos 	nat_t *nat, *natl;
   3350  1.1  christos 	natstat_t *nsp;
   3351  1.1  christos 	u_int nflags;
   3352  1.1  christos 	natinfo_t ni;
   3353  1.1  christos 	int move;
   3354  1.1  christos 
   3355  1.1  christos 	nsp = &softn->ipf_nat_stats;
   3356  1.1  christos 
   3357  1.1  christos 	if ((nsp->ns_active * 100 / softn->ipf_nat_table_max) >
   3358  1.1  christos 	    softn->ipf_nat_table_wm_high) {
   3359  1.1  christos 		softn->ipf_nat_doflush = 1;
   3360  1.1  christos 	}
   3361  1.1  christos 
   3362  1.1  christos 	if (nsp->ns_active >= softn->ipf_nat_table_max) {
   3363  1.1  christos 		NBUMPSIDED(fin->fin_out, ns_table_max);
   3364  1.1  christos 		return NULL;
   3365  1.1  christos 	}
   3366  1.1  christos 
   3367  1.1  christos 	move = 1;
   3368  1.1  christos 	nflags = np->in_flags & flags;
   3369  1.1  christos 	nflags &= NAT_FROMRULE;
   3370  1.1  christos 
   3371  1.1  christos 	ni.nai_np = np;
   3372  1.1  christos 	ni.nai_dport = 0;
   3373  1.1  christos 	ni.nai_sport = 0;
   3374  1.1  christos 
   3375  1.1  christos 	/* Give me a new nat */
   3376  1.1  christos 	KMALLOC(nat, nat_t *);
   3377  1.1  christos 	if (nat == NULL) {
   3378  1.1  christos 		NBUMPSIDED(fin->fin_out, ns_memfail);
   3379  1.1  christos 		/*
   3380  1.1  christos 		 * Try to automatically tune the max # of entries in the
   3381  1.1  christos 		 * table allowed to be less than what will cause kmem_alloc()
   3382  1.1  christos 		 * to fail and try to eliminate panics due to out of memory
   3383  1.1  christos 		 * conditions arising.
   3384  1.1  christos 		 */
   3385  1.1  christos 		if ((softn->ipf_nat_table_max > softn->ipf_nat_table_sz) &&
   3386  1.1  christos 		    (nsp->ns_active > 100)) {
   3387  1.1  christos 			softn->ipf_nat_table_max = nsp->ns_active - 100;
   3388  1.1  christos 			printf("table_max reduced to %d\n",
   3389  1.1  christos 				softn->ipf_nat_table_max);
   3390  1.1  christos 		}
   3391  1.1  christos 		return NULL;
   3392  1.1  christos 	}
   3393  1.1  christos 
   3394  1.1  christos 	if (flags & IPN_ICMPQUERY) {
   3395  1.1  christos 		/*
   3396  1.1  christos 		 * In the ICMP query NAT code, we translate the ICMP id fields
   3397  1.1  christos 		 * to make them unique. This is indepedent of the ICMP type
   3398  1.1  christos 		 * (e.g. in the unlikely event that a host sends an echo and
   3399  1.1  christos 		 * an tstamp request with the same id, both packets will have
   3400  1.1  christos 		 * their ip address/id field changed in the same way).
   3401  1.1  christos 		 */
   3402  1.1  christos 		/* The icmp_id field is used by the sender to identify the
   3403  1.1  christos 		 * process making the icmp request. (the receiver justs
   3404  1.1  christos 		 * copies it back in its response). So, it closely matches
   3405  1.1  christos 		 * the concept of source port. We overlay sport, so we can
   3406  1.1  christos 		 * maximally reuse the existing code.
   3407  1.1  christos 		 */
   3408  1.1  christos 		ni.nai_sport = fin->fin_data[1];
   3409  1.1  christos 		ni.nai_dport = 0;
   3410  1.1  christos 	}
   3411  1.1  christos 
   3412  1.1  christos 	bzero((char *)nat, sizeof(*nat));
   3413  1.1  christos 	nat->nat_flags = flags;
   3414  1.1  christos 	nat->nat_redir = np->in_redir;
   3415  1.1  christos 	nat->nat_dir = direction;
   3416  1.1  christos 	nat->nat_pr[0] = fin->fin_p;
   3417  1.1  christos 	nat->nat_pr[1] = fin->fin_p;
   3418  1.1  christos 
   3419  1.1  christos 	/*
   3420  1.1  christos 	 * Search the current table for a match and create a new mapping
   3421  1.1  christos 	 * if there is none found.
   3422  1.1  christos 	 */
   3423  1.1  christos 	if (np->in_redir & (NAT_ENCAP|NAT_DIVERTUDP)) {
   3424  1.1  christos 		move = ipf_nat_newdivert(fin, nat, &ni);
   3425  1.1  christos 
   3426  1.1  christos 	} else if (np->in_redir & NAT_REWRITE) {
   3427  1.1  christos 		move = ipf_nat_newrewrite(fin, nat, &ni);
   3428  1.1  christos 
   3429  1.1  christos 	} else if (direction == NAT_OUTBOUND) {
   3430  1.1  christos 		/*
   3431  1.1  christos 		 * We can now arrange to call this for the same connection
   3432  1.1  christos 		 * because ipf_nat_new doesn't protect the code path into
   3433  1.1  christos 		 * this function.
   3434  1.1  christos 		 */
   3435  1.1  christos 		natl = ipf_nat_outlookup(fin, nflags, (u_int)fin->fin_p,
   3436  1.1  christos 				     fin->fin_src, fin->fin_dst);
   3437  1.1  christos 		if (natl != NULL) {
   3438  1.1  christos 			KFREE(nat);
   3439  1.1  christos 			nat = natl;
   3440  1.1  christos 			goto done;
   3441  1.1  christos 		}
   3442  1.1  christos 
   3443  1.1  christos 		move = ipf_nat_newmap(fin, nat, &ni);
   3444  1.1  christos 	} else {
   3445  1.1  christos 		/*
   3446  1.1  christos 		 * NAT_INBOUND is used for redirects rules
   3447  1.1  christos 		 */
   3448  1.1  christos 		natl = ipf_nat_inlookup(fin, nflags, (u_int)fin->fin_p,
   3449  1.1  christos 					fin->fin_src, fin->fin_dst);
   3450  1.1  christos 		if (natl != NULL) {
   3451  1.1  christos 			KFREE(nat);
   3452  1.1  christos 			nat = natl;
   3453  1.1  christos 			goto done;
   3454  1.1  christos 		}
   3455  1.1  christos 
   3456  1.1  christos 		move = ipf_nat_newrdr(fin, nat, &ni);
   3457  1.1  christos 	}
   3458  1.1  christos 	if (move == -1)
   3459  1.1  christos 		goto badnat;
   3460  1.1  christos 
   3461  1.1  christos 	np = ni.nai_np;
   3462  1.1  christos 
   3463  1.1  christos 	nat->nat_mssclamp = np->in_mssclamp;
   3464  1.1  christos 	nat->nat_me = natsave;
   3465  1.1  christos 	if (natsave != NULL)
   3466  1.1  christos 		*natsave = nat;
   3467  1.1  christos 	nat->nat_fr = fin->fin_fr;
   3468  1.1  christos 	nat->nat_rev = fin->fin_rev;
   3469  1.1  christos 	nat->nat_ptr = np;
   3470  1.1  christos 	nat->nat_dlocal = np->in_dlocal;
   3471  1.1  christos 
   3472  1.1  christos 	if ((np->in_apr != NULL) && ((nat->nat_flags & NAT_SLAVE) == 0))
   3473  1.1  christos 		if (ipf_proxy_new(fin, nat) == -1)
   3474  1.1  christos 			goto badnat;
   3475  1.1  christos 
   3476  1.1  christos 	nat->nat_ifps[0] = np->in_ifps[0];
   3477  1.1  christos 	if (np->in_ifps[0] != NULL) {
   3478  1.1  christos 		COPYIFNAME(np->in_v[0], np->in_ifps[0], nat->nat_ifnames[0]);
   3479  1.1  christos 	}
   3480  1.1  christos 
   3481  1.1  christos 	nat->nat_ifps[1] = np->in_ifps[1];
   3482  1.1  christos 	if (np->in_ifps[1] != NULL) {
   3483  1.1  christos 		COPYIFNAME(np->in_v[1], np->in_ifps[1], nat->nat_ifnames[1]);
   3484  1.1  christos 	}
   3485  1.1  christos 
   3486  1.1  christos 	if (ipf_nat_finalise(fin, nat) == -1) {
   3487  1.1  christos 		goto badnat;
   3488  1.1  christos 	}
   3489  1.1  christos 
   3490  1.1  christos 	np->in_use++;
   3491  1.1  christos 
   3492  1.1  christos 	if ((move == 1) && (np->in_flags & IPN_ROUNDR)) {
   3493  1.1  christos 		if ((np->in_redir & (NAT_REDIRECT|NAT_MAP)) == NAT_REDIRECT) {
   3494  1.1  christos 			ipf_nat_delrdr(softn, np);
   3495  1.1  christos 			ipf_nat_addrdr(softn, np);
   3496  1.1  christos 		} else if ((np->in_redir & (NAT_REDIRECT|NAT_MAP)) == NAT_MAP) {
   3497  1.1  christos 			ipf_nat_delmap(softn, np);
   3498  1.1  christos 			ipf_nat_addmap(softn, np);
   3499  1.1  christos 		}
   3500  1.1  christos 	}
   3501  1.1  christos 
   3502  1.1  christos 	if (flags & SI_WILDP)
   3503  1.1  christos 		nsp->ns_wilds++;
   3504  1.1  christos 	nsp->ns_proto[nat->nat_pr[0]]++;
   3505  1.1  christos 
   3506  1.1  christos 	goto done;
   3507  1.1  christos badnat:
   3508  1.1  christos 	DT2(ns_badnatnew, fr_info_t *, fin, nat_t *, nat);
   3509  1.1  christos 	NBUMPSIDE(fin->fin_out, ns_badnatnew);
   3510  1.1  christos 	if ((hm = nat->nat_hm) != NULL)
   3511  1.1  christos 		ipf_nat_hostmapdel(&hm);
   3512  1.1  christos 	KFREE(nat);
   3513  1.1  christos 	nat = NULL;
   3514  1.1  christos done:
   3515  1.1  christos 	if (nat != NULL && np != NULL)
   3516  1.1  christos 		np->in_hits++;
   3517  1.1  christos 	return nat;
   3518  1.1  christos }
   3519  1.1  christos 
   3520  1.1  christos 
   3521  1.1  christos /* ------------------------------------------------------------------------ */
   3522  1.1  christos /* Function:    ipf_nat_finalise                                            */
   3523  1.1  christos /* Returns:     int - 0 == sucess, -1 == failure                            */
   3524  1.1  christos /* Parameters:  fin(I) - pointer to packet information                      */
   3525  1.1  christos /*              nat(I) - pointer to NAT entry                               */
   3526  1.1  christos /* Write Lock:  ipf_nat                                                     */
   3527  1.1  christos /*                                                                          */
   3528  1.1  christos /* This is the tail end of constructing a new NAT entry and is the same     */
   3529  1.1  christos /* for both IPv4 and IPv6.                                                  */
   3530  1.1  christos /* ------------------------------------------------------------------------ */
   3531  1.1  christos /*ARGSUSED*/
   3532  1.1  christos static int
   3533  1.1  christos ipf_nat_finalise(fin, nat)
   3534  1.1  christos 	fr_info_t *fin;
   3535  1.1  christos 	nat_t *nat;
   3536  1.1  christos {
   3537  1.1  christos 	ipf_main_softc_t *softc = fin->fin_main_soft;
   3538  1.1  christos 	ipf_nat_softc_t *softn = softc->ipf_nat_soft;
   3539  1.1  christos 	u_32_t sum1, sum2, sumd;
   3540  1.1  christos 	frentry_t *fr;
   3541  1.1  christos 	u_32_t flags;
   3542  1.1  christos #if SOLARIS && defined(_KERNEL) && (SOLARIS2 >= 6) && defined(ICK_M_CTL_MAGIC)
   3543  1.1  christos 	qpktinfo_t *qpi = fin->fin_qpi;
   3544  1.1  christos #endif
   3545  1.1  christos 
   3546  1.1  christos 	flags = nat->nat_flags;
   3547  1.1  christos 
   3548  1.1  christos 	switch (nat->nat_pr[0])
   3549  1.1  christos 	{
   3550  1.1  christos 	case IPPROTO_ICMP :
   3551  1.1  christos 		sum1 = LONG_SUM(ntohs(nat->nat_osport));
   3552  1.1  christos 		sum2 = LONG_SUM(ntohs(nat->nat_nsport));
   3553  1.1  christos 		CALC_SUMD(sum1, sum2, sumd);
   3554  1.1  christos 		nat->nat_sumd[0] = (sumd & 0xffff) + (sumd >> 16);
   3555  1.1  christos 
   3556  1.1  christos 		break;
   3557  1.1  christos 
   3558  1.1  christos 	default :
   3559  1.1  christos 		sum1 = LONG_SUM(ntohl(nat->nat_osrcaddr) + \
   3560  1.1  christos 				ntohs(nat->nat_osport));
   3561  1.1  christos 		sum2 = LONG_SUM(ntohl(nat->nat_nsrcaddr) + \
   3562  1.1  christos 				ntohs(nat->nat_nsport));
   3563  1.1  christos 		CALC_SUMD(sum1, sum2, sumd);
   3564  1.1  christos 		nat->nat_sumd[0] = (sumd & 0xffff) + (sumd >> 16);
   3565  1.1  christos 
   3566  1.1  christos 		sum1 = LONG_SUM(ntohl(nat->nat_odstaddr) + \
   3567  1.1  christos 				ntohs(nat->nat_odport));
   3568  1.1  christos 		sum2 = LONG_SUM(ntohl(nat->nat_ndstaddr) + \
   3569  1.1  christos 				ntohs(nat->nat_ndport));
   3570  1.1  christos 		CALC_SUMD(sum1, sum2, sumd);
   3571  1.1  christos 		nat->nat_sumd[0] += (sumd & 0xffff) + (sumd >> 16);
   3572  1.1  christos 		break;
   3573  1.1  christos 	}
   3574  1.1  christos 
   3575  1.1  christos #if SOLARIS && defined(_KERNEL)
   3576  1.1  christos # if (SOLARIS2 >= 6) && defined(ICK_M_CTL_MAGIC)
   3577  1.1  christos 	if ((flags & IPN_TCP) && dohwcksum &&
   3578  1.1  christos 	    (((ill_t *)qpi->qpi_ill)->ill_ick.ick_magic == ICK_M_CTL_MAGIC)) {
   3579  1.1  christos 		sum1 = LONG_SUM(ntohl(nat->nat_nsrcaddr));
   3580  1.1  christos 		sum1 += LONG_SUM(ntohl(nat->nat_ndstaddr));
   3581  1.1  christos 		sum1 += 30;
   3582  1.1  christos 		sum1 = (sum1 & 0xffff) + (sum1 >> 16);
   3583  1.1  christos 		nat->nat_sumd[1] = NAT_HW_CKSUM|(sum1 & 0xffff);
   3584  1.1  christos 	} else
   3585  1.1  christos # endif
   3586  1.1  christos # if defined(NET_HCK_NONE)
   3587  1.1  christos 	if ((flags & IPN_TCPUDP) && dohwcksum) {
   3588  1.1  christos 		mblk_t *m = fin->fin_m;
   3589  1.1  christos 		u_int flags = net_ispartialchecksum(softc->ipf_nd_v4, m);
   3590  1.1  christos 
   3591  1.1  christos 		if (flags & NET_HCK_L4_PART) {
   3592  1.1  christos 			sum1 = LONG_SUM(ntohl(nat->nat_nsrcaddr));
   3593  1.1  christos 			sum1 += LONG_SUM(ntohl(nat->nat_ndstaddr));
   3594  1.1  christos 			sum1 += fin->fin_p;
   3595  1.1  christos 			sum1 = htons(sum1);
   3596  1.1  christos 
   3597  1.1  christos 			nat->nat_sumd[1] = (sum1 & 0xffff) + (sum1 >> 16);
   3598  1.1  christos 			nat->nat_sumd[1] |= NAT_HW_CKSUM;
   3599  1.1  christos 		} else if (flags & NET_HCK_L4_FULL) {
   3600  1.1  christos 			nat->nat_sumd[1] = NAT_HW_CKSUM;
   3601  1.1  christos 		} else {
   3602  1.1  christos 			nat->nat_sumd[1] = nat->nat_sumd[0];
   3603  1.1  christos 		}
   3604  1.1  christos 	} else
   3605  1.1  christos # endif
   3606  1.1  christos #endif
   3607  1.1  christos 		nat->nat_sumd[1] = nat->nat_sumd[0];
   3608  1.1  christos 
   3609  1.1  christos 	sum1 = LONG_SUM(ntohl(nat->nat_osrcaddr));
   3610  1.1  christos 	sum2 = LONG_SUM(ntohl(nat->nat_nsrcaddr));
   3611  1.1  christos 	CALC_SUMD(sum1, sum2, sumd);
   3612  1.1  christos 	nat->nat_ipsumd = (sumd & 0xffff) + (sumd >> 16);
   3613  1.1  christos 
   3614  1.1  christos 	sum1 = LONG_SUM(ntohl(nat->nat_odstaddr));
   3615  1.1  christos 	sum2 = LONG_SUM(ntohl(nat->nat_ndstaddr));
   3616  1.1  christos 	CALC_SUMD(sum1, sum2, sumd);
   3617  1.1  christos 	nat->nat_ipsumd += (sumd & 0xffff) + (sumd >> 16);
   3618  1.1  christos 
   3619  1.1  christos 	nat->nat_v[0] = 4;
   3620  1.1  christos 	nat->nat_v[1] = 4;
   3621  1.1  christos 
   3622  1.1  christos 	if ((nat->nat_ifps[0] != NULL) && (nat->nat_ifps[0] != (void *)-1)) {
   3623  1.1  christos 		nat->nat_mtu[0] = GETIFMTU_4(nat->nat_ifps[0]);
   3624  1.1  christos 	}
   3625  1.1  christos 
   3626  1.1  christos 	if ((nat->nat_ifps[1] != NULL) && (nat->nat_ifps[1] != (void *)-1)) {
   3627  1.1  christos 		nat->nat_mtu[1] = GETIFMTU_4(nat->nat_ifps[1]);
   3628  1.1  christos 	}
   3629  1.1  christos 
   3630  1.1  christos 	if ((nat->nat_flags & SI_CLONE) == 0)
   3631  1.1  christos 		nat->nat_sync = ipf_sync_new(softc, SMC_NAT, fin, nat);
   3632  1.1  christos 
   3633  1.1  christos 	if (ipf_nat_insert(softc, softn, nat) == 0) {
   3634  1.1  christos 		if (softn->ipf_nat_logging)
   3635  1.1  christos 			ipf_nat_log(softc, softn, nat, NL_NEW);
   3636  1.1  christos 		fr = nat->nat_fr;
   3637  1.1  christos 		if (fr != NULL) {
   3638  1.1  christos 			MUTEX_ENTER(&fr->fr_lock);
   3639  1.1  christos 			fr->fr_ref++;
   3640  1.1  christos 			MUTEX_EXIT(&fr->fr_lock);
   3641  1.1  christos 		}
   3642  1.1  christos 		return 0;
   3643  1.1  christos 	}
   3644  1.1  christos 
   3645  1.1  christos 	NBUMPSIDED(fin->fin_out, ns_unfinalised);
   3646  1.1  christos 	/*
   3647  1.1  christos 	 * nat_insert failed, so cleanup time...
   3648  1.1  christos 	 */
   3649  1.1  christos 	return -1;
   3650  1.1  christos }
   3651  1.1  christos 
   3652  1.1  christos 
   3653  1.1  christos /* ------------------------------------------------------------------------ */
   3654  1.1  christos /* Function:   ipf_nat_insert                                               */
   3655  1.1  christos /* Returns:    int - 0 == sucess, -1 == failure                             */
   3656  1.1  christos /* Parameters: nat(I) - pointer to NAT structure                            */
   3657  1.1  christos /*             rev(I) - flag indicating forward/reverse direction of packet */
   3658  1.1  christos /* Write Lock: ipf_nat                                                      */
   3659  1.1  christos /*                                                                          */
   3660  1.1  christos /* Insert a NAT entry into the hash tables for searching and add it to the  */
   3661  1.1  christos /* list of active NAT entries.  Adjust global counters when complete.       */
   3662  1.1  christos /* ------------------------------------------------------------------------ */
   3663  1.1  christos int
   3664  1.1  christos ipf_nat_insert(softc, softn, nat)
   3665  1.1  christos 	ipf_main_softc_t *softc;
   3666  1.1  christos 	ipf_nat_softc_t *softn;
   3667  1.1  christos 	nat_t *nat;
   3668  1.1  christos {
   3669  1.1  christos 	u_int hv0, hv1, rhv0, rhv1;
   3670  1.1  christos 	ipnat_t *in;
   3671  1.1  christos 	nat_t **natp;
   3672  1.1  christos 
   3673  1.1  christos 	/*
   3674  1.1  christos 	 * Try and return an error as early as possible, so calculate the hash
   3675  1.1  christos 	 * entry numbers first and then proceed.
   3676  1.1  christos 	 */
   3677  1.1  christos 	if ((nat->nat_flags & (SI_W_SPORT|SI_W_DPORT)) == 0) {
   3678  1.1  christos 		rhv0 = NAT_HASH_FN(nat->nat_osrcaddr, nat->nat_osport,
   3679  1.1  christos 				   0xffffffff);
   3680  1.1  christos 		rhv0 = NAT_HASH_FN(nat->nat_odstaddr, rhv0 + nat->nat_odport,
   3681  1.1  christos 				   0xffffffff);
   3682  1.1  christos 
   3683  1.1  christos 		/*
   3684  1.1  christos 		 * TRACE nat_osrcaddr, nat_osport, nat_odstaddr,
   3685  1.1  christos 		 * nat_odport, hv0
   3686  1.1  christos 		 */
   3687  1.1  christos 
   3688  1.1  christos 		rhv1 = NAT_HASH_FN(nat->nat_nsrcaddr, nat->nat_nsport,
   3689  1.1  christos 				   0xffffffff);
   3690  1.1  christos 		rhv1 = NAT_HASH_FN(nat->nat_ndstaddr, rhv1 + nat->nat_ndport,
   3691  1.1  christos 				   0xffffffff);
   3692  1.1  christos 		/*
   3693  1.1  christos 		 * TRACE nat_nsrcaddr, nat_nsport, nat_ndstaddr,
   3694  1.1  christos 		 * nat_ndport, hv1
   3695  1.1  christos 		 */
   3696  1.1  christos 	} else {
   3697  1.1  christos 		rhv0 = NAT_HASH_FN(nat->nat_osrcaddr, 0, 0xffffffff);
   3698  1.1  christos 		rhv0 = NAT_HASH_FN(nat->nat_odstaddr, rhv0, 0xffffffff);
   3699  1.1  christos 		/* TRACE nat_osrcaddr, nat_odstaddr, rhv0 */
   3700  1.1  christos 
   3701  1.1  christos 		rhv1 = NAT_HASH_FN(nat->nat_nsrcaddr, 0, 0xffffffff);
   3702  1.1  christos 		rhv1 = NAT_HASH_FN(nat->nat_ndstaddr, rhv1, 0xffffffff);
   3703  1.1  christos 		/* TRACE nat_nsrcaddr, nat_ndstaddr, rhv1 */
   3704  1.1  christos 	}
   3705  1.1  christos 	hv0 = rhv0 % softn->ipf_nat_table_sz;
   3706  1.1  christos 	hv1 = rhv1 % softn->ipf_nat_table_sz;
   3707  1.1  christos 
   3708  1.1  christos 	if (softn->ipf_nat_stats.ns_side[0].ns_bucketlen[hv0] >=
   3709  1.1  christos 	    softn->ipf_nat_maxbucket) {
   3710  1.1  christos 		DT1(ns_bucket_max_0, int,
   3711  1.1  christos 		    softn->ipf_nat_stats.ns_side[0].ns_bucketlen[hv0]);
   3712  1.1  christos 		NBUMPSIDE(0, ns_bucket_max);
   3713  1.1  christos 		return -1;
   3714  1.1  christos 	}
   3715  1.1  christos 
   3716  1.1  christos 	if (softn->ipf_nat_stats.ns_side[1].ns_bucketlen[hv1] >=
   3717  1.1  christos 	    softn->ipf_nat_maxbucket) {
   3718  1.1  christos 		DT1(ns_bucket_max_1, int,
   3719  1.1  christos 		    softn->ipf_nat_stats.ns_side[1].ns_bucketlen[hv1]);
   3720  1.1  christos 		NBUMPSIDE(1, ns_bucket_max);
   3721  1.1  christos 		return -1;
   3722  1.1  christos 	}
   3723  1.1  christos 
   3724  1.1  christos 	if (nat->nat_dir == NAT_INBOUND || nat->nat_dir == NAT_ENCAPIN ||
   3725  1.1  christos 	    nat->nat_dir == NAT_DIVERTIN) {
   3726  1.1  christos 		u_int swap;
   3727  1.1  christos 
   3728  1.1  christos 		swap = hv0;
   3729  1.1  christos 		hv0 = hv1;
   3730  1.1  christos 		hv1 = swap;
   3731  1.1  christos 	}
   3732  1.1  christos 	nat->nat_hv[0] = rhv0;
   3733  1.1  christos 	nat->nat_hv[1] = rhv1;
   3734  1.1  christos 
   3735  1.1  christos 	MUTEX_INIT(&nat->nat_lock, "nat entry lock");
   3736  1.1  christos 
   3737  1.1  christos 	in = nat->nat_ptr;
   3738  1.1  christos 	nat->nat_ref = nat->nat_me ? 2 : 1;
   3739  1.1  christos 
   3740  1.1  christos 	nat->nat_ifnames[0][LIFNAMSIZ - 1] = '\0';
   3741  1.1  christos 	nat->nat_ifps[0] = ipf_resolvenic(softc, nat->nat_ifnames[0], 4);
   3742  1.1  christos 
   3743  1.1  christos 	if (nat->nat_ifnames[1][0] != '\0') {
   3744  1.1  christos 		nat->nat_ifnames[1][LIFNAMSIZ - 1] = '\0';
   3745  1.1  christos 		nat->nat_ifps[1] = ipf_resolvenic(softc,
   3746  1.1  christos 						  nat->nat_ifnames[1], 4);
   3747  1.1  christos 	} else if (in->in_ifnames[1] != -1) {
   3748  1.1  christos 		char *name;
   3749  1.1  christos 
   3750  1.1  christos 		name = in->in_names + in->in_ifnames[1];
   3751  1.1  christos 		if (name[1] != '\0' && name[0] != '-' && name[0] != '*') {
   3752  1.1  christos 			(void) strncpy(nat->nat_ifnames[1],
   3753  1.1  christos 				       nat->nat_ifnames[0], LIFNAMSIZ);
   3754  1.1  christos 			nat->nat_ifnames[1][LIFNAMSIZ - 1] = '\0';
   3755  1.1  christos 			nat->nat_ifps[1] = nat->nat_ifps[0];
   3756  1.1  christos 		}
   3757  1.1  christos 	}
   3758  1.1  christos 	if ((nat->nat_ifps[0] != NULL) && (nat->nat_ifps[0] != (void *)-1)) {
   3759  1.1  christos 		nat->nat_mtu[0] = GETIFMTU_4(nat->nat_ifps[0]);
   3760  1.1  christos 	}
   3761  1.1  christos 	if ((nat->nat_ifps[1] != NULL) && (nat->nat_ifps[1] != (void *)-1)) {
   3762  1.1  christos 		nat->nat_mtu[1] = GETIFMTU_4(nat->nat_ifps[1]);
   3763  1.1  christos 	}
   3764  1.1  christos 
   3765  1.1  christos 	/*
   3766  1.1  christos 	 * The ordering of operations in the list and hash table insertion
   3767  1.1  christos 	 * is very important.  The last operation for each task should be
   3768  1.1  christos 	 * to update the top of the list, after all the "nexts" have been
   3769  1.1  christos 	 * done so that walking the list while it is being done does not
   3770  1.1  christos 	 * find strange pointers.
   3771  1.1  christos 	 *
   3772  1.1  christos 	 * Global list of NAT instances
   3773  1.1  christos 	 */
   3774  1.1  christos 	nat->nat_next = softn->ipf_nat_instances;
   3775  1.1  christos 	nat->nat_pnext = &softn->ipf_nat_instances;
   3776  1.1  christos 	if (softn->ipf_nat_instances)
   3777  1.1  christos 		softn->ipf_nat_instances->nat_pnext = &nat->nat_next;
   3778  1.1  christos 	softn->ipf_nat_instances = nat;
   3779  1.1  christos 
   3780  1.1  christos 	/*
   3781  1.1  christos 	 * Inbound hash table.
   3782  1.1  christos 	 */
   3783  1.1  christos 	natp = &softn->ipf_nat_table[0][hv0];
   3784  1.1  christos 	nat->nat_phnext[0] = natp;
   3785  1.1  christos 	nat->nat_hnext[0] = *natp;
   3786  1.1  christos 	if (*natp) {
   3787  1.1  christos 		(*natp)->nat_phnext[0] = &nat->nat_hnext[0];
   3788  1.1  christos 	} else {
   3789  1.1  christos 		NBUMPSIDE(0, ns_inuse);
   3790  1.1  christos 	}
   3791  1.1  christos 	*natp = nat;
   3792  1.1  christos 	NBUMPSIDE(0, ns_bucketlen[hv0]);
   3793  1.1  christos 
   3794  1.1  christos 	/*
   3795  1.1  christos 	 * Outbound hash table.
   3796  1.1  christos 	 */
   3797  1.1  christos 	natp = &softn->ipf_nat_table[1][hv1];
   3798  1.1  christos 	nat->nat_phnext[1] = natp;
   3799  1.1  christos 	nat->nat_hnext[1] = *natp;
   3800  1.1  christos 	if (*natp)
   3801  1.1  christos 		(*natp)->nat_phnext[1] = &nat->nat_hnext[1];
   3802  1.1  christos 	else {
   3803  1.1  christos 		NBUMPSIDE(1, ns_inuse);
   3804  1.1  christos 	}
   3805  1.1  christos 	*natp = nat;
   3806  1.1  christos 	NBUMPSIDE(1, ns_bucketlen[hv1]);
   3807  1.1  christos 
   3808  1.1  christos 	/* ---- */
   3809  1.1  christos 
   3810  1.1  christos 	ipf_nat_setqueue(softc, softn, nat);
   3811  1.1  christos 
   3812  1.1  christos 	if (nat->nat_dir & NAT_OUTBOUND) {
   3813  1.1  christos 		NBUMPSIDE(1, ns_added);
   3814  1.1  christos 	} else {
   3815  1.1  christos 		NBUMPSIDE(0, ns_added);
   3816  1.1  christos 	}
   3817  1.1  christos 	softn->ipf_nat_stats.ns_active++;
   3818  1.1  christos 	return 0;
   3819  1.1  christos }
   3820  1.1  christos 
   3821  1.1  christos 
   3822  1.1  christos /* ------------------------------------------------------------------------ */
   3823  1.1  christos /* Function:    ipf_nat_icmperrorlookup                                     */
   3824  1.1  christos /* Returns:     nat_t* - point to matching NAT structure                    */
   3825  1.1  christos /* Parameters:  fin(I) - pointer to packet information                      */
   3826  1.1  christos /*              dir(I) - direction of packet (in/out)                       */
   3827  1.1  christos /*                                                                          */
   3828  1.1  christos /* Check if the ICMP error message is related to an existing TCP, UDP or    */
   3829  1.1  christos /* ICMP query nat entry.  It is assumed that the packet is already of the   */
   3830  1.1  christos /* the required length.                                                     */
   3831  1.1  christos /* ------------------------------------------------------------------------ */
   3832  1.1  christos nat_t *
   3833  1.1  christos ipf_nat_icmperrorlookup(fin, dir)
   3834  1.1  christos 	fr_info_t *fin;
   3835  1.1  christos 	int dir;
   3836  1.1  christos {
   3837  1.1  christos 	ipf_main_softc_t *softc = fin->fin_main_soft;
   3838  1.1  christos 	ipf_nat_softc_t *softn = softc->ipf_nat_soft;
   3839  1.1  christos 	int flags = 0, type, minlen;
   3840  1.1  christos 	icmphdr_t *icmp, *orgicmp;
   3841  1.1  christos 	nat_stat_side_t *nside;
   3842  1.1  christos 	tcphdr_t *tcp = NULL;
   3843  1.1  christos 	u_short data[2];
   3844  1.1  christos 	nat_t *nat;
   3845  1.1  christos 	ip_t *oip;
   3846  1.1  christos 	u_int p;
   3847  1.1  christos 
   3848  1.1  christos 	icmp = fin->fin_dp;
   3849  1.1  christos 	type = icmp->icmp_type;
   3850  1.1  christos 	nside = &softn->ipf_nat_stats.ns_side[fin->fin_out];
   3851  1.1  christos 	/*
   3852  1.1  christos 	 * Does it at least have the return (basic) IP header ?
   3853  1.1  christos 	 * Only a basic IP header (no options) should be with an ICMP error
   3854  1.1  christos 	 * header.  Also, if it's not an error type, then return.
   3855  1.1  christos 	 */
   3856  1.1  christos 	if ((fin->fin_hlen != sizeof(ip_t)) || !(fin->fin_flx & FI_ICMPERR)) {
   3857  1.1  christos 		ATOMIC_INCL(nside->ns_icmp_basic);
   3858  1.1  christos 		return NULL;
   3859  1.1  christos 	}
   3860  1.1  christos 
   3861  1.1  christos 	/*
   3862  1.1  christos 	 * Check packet size
   3863  1.1  christos 	 */
   3864  1.1  christos 	oip = (ip_t *)((char *)fin->fin_dp + 8);
   3865  1.1  christos 	minlen = IP_HL(oip) << 2;
   3866  1.1  christos 	if ((minlen < sizeof(ip_t)) ||
   3867  1.1  christos 	    (fin->fin_plen < ICMPERR_IPICMPHLEN + minlen)) {
   3868  1.1  christos 		ATOMIC_INCL(nside->ns_icmp_size);
   3869  1.1  christos 		return NULL;
   3870  1.1  christos 	}
   3871  1.1  christos 
   3872  1.1  christos 	/*
   3873  1.1  christos 	 * Is the buffer big enough for all of it ?  It's the size of the IP
   3874  1.1  christos 	 * header claimed in the encapsulated part which is of concern.  It
   3875  1.1  christos 	 * may be too big to be in this buffer but not so big that it's
   3876  1.1  christos 	 * outside the ICMP packet, leading to TCP deref's causing problems.
   3877  1.1  christos 	 * This is possible because we don't know how big oip_hl is when we
   3878  1.1  christos 	 * do the pullup early in ipf_check() and thus can't gaurantee it is
   3879  1.1  christos 	 * all here now.
   3880  1.1  christos 	 */
   3881  1.1  christos #ifdef  ipf_nat_KERNEL
   3882  1.1  christos 	{
   3883  1.1  christos 	mb_t *m;
   3884  1.1  christos 
   3885  1.1  christos 	m = fin->fin_m;
   3886  1.1  christos # if defined(MENTAT)
   3887  1.1  christos 	if ((char *)oip + fin->fin_dlen - ICMPERR_ICMPHLEN >
   3888  1.1  christos 	    (char *)m->b_wptr) {
   3889  1.1  christos 		ATOMIC_INCL(nside->ns_icmp_mbuf);
   3890  1.1  christos 		return NULL;
   3891  1.1  christos 	}
   3892  1.1  christos # else
   3893  1.1  christos 	if ((char *)oip + fin->fin_dlen - ICMPERR_ICMPHLEN >
   3894  1.1  christos 	    (char *)fin->fin_ip + M_LEN(m)) {
   3895  1.1  christos 		ATOMIC_INCL(nside->ns_icmp_mbuf);
   3896  1.1  christos 		return NULL;
   3897  1.1  christos 	}
   3898  1.1  christos # endif
   3899  1.1  christos 	}
   3900  1.1  christos #endif
   3901  1.1  christos 
   3902  1.1  christos 	if (fin->fin_daddr != oip->ip_src.s_addr) {
   3903  1.1  christos 		ATOMIC_INCL(nside->ns_icmp_address);
   3904  1.1  christos 		return NULL;
   3905  1.1  christos 	}
   3906  1.1  christos 
   3907  1.1  christos 	p = oip->ip_p;
   3908  1.1  christos 	if (p == IPPROTO_TCP)
   3909  1.1  christos 		flags = IPN_TCP;
   3910  1.1  christos 	else if (p == IPPROTO_UDP)
   3911  1.1  christos 		flags = IPN_UDP;
   3912  1.1  christos 	else if (p == IPPROTO_ICMP) {
   3913  1.1  christos 		orgicmp = (icmphdr_t *)((char *)oip + (IP_HL(oip) << 2));
   3914  1.1  christos 
   3915  1.1  christos 		/* see if this is related to an ICMP query */
   3916  1.1  christos 		if (ipf_nat_icmpquerytype(orgicmp->icmp_type)) {
   3917  1.1  christos 			data[0] = fin->fin_data[0];
   3918  1.1  christos 			data[1] = fin->fin_data[1];
   3919  1.1  christos 			fin->fin_data[0] = 0;
   3920  1.1  christos 			fin->fin_data[1] = orgicmp->icmp_id;
   3921  1.1  christos 
   3922  1.1  christos 			flags = IPN_ICMPERR|IPN_ICMPQUERY;
   3923  1.1  christos 			/*
   3924  1.1  christos 			 * NOTE : dir refers to the direction of the original
   3925  1.1  christos 			 *        ip packet. By definition the icmp error
   3926  1.1  christos 			 *        message flows in the opposite direction.
   3927  1.1  christos 			 */
   3928  1.1  christos 			if (dir == NAT_INBOUND)
   3929  1.1  christos 				nat = ipf_nat_inlookup(fin, flags, p,
   3930  1.1  christos 						       oip->ip_dst,
   3931  1.1  christos 						       oip->ip_src);
   3932  1.1  christos 			else
   3933  1.1  christos 				nat = ipf_nat_outlookup(fin, flags, p,
   3934  1.1  christos 							oip->ip_dst,
   3935  1.1  christos 							oip->ip_src);
   3936  1.1  christos 			fin->fin_data[0] = data[0];
   3937  1.1  christos 			fin->fin_data[1] = data[1];
   3938  1.1  christos 			return nat;
   3939  1.1  christos 		}
   3940  1.1  christos 	}
   3941  1.1  christos 
   3942  1.1  christos 	if (flags & IPN_TCPUDP) {
   3943  1.1  christos 		minlen += 8;		/* + 64bits of data to get ports */
   3944  1.1  christos 		/* TRACE (fin,minlen) */
   3945  1.1  christos 		if (fin->fin_plen < ICMPERR_IPICMPHLEN + minlen) {
   3946  1.1  christos 			ATOMIC_INCL(nside->ns_icmp_short);
   3947  1.1  christos 			return NULL;
   3948  1.1  christos 		}
   3949  1.1  christos 
   3950  1.1  christos 		data[0] = fin->fin_data[0];
   3951  1.1  christos 		data[1] = fin->fin_data[1];
   3952  1.1  christos 		tcp = (tcphdr_t *)((char *)oip + (IP_HL(oip) << 2));
   3953  1.1  christos 		fin->fin_data[0] = ntohs(tcp->th_dport);
   3954  1.1  christos 		fin->fin_data[1] = ntohs(tcp->th_sport);
   3955  1.1  christos 
   3956  1.1  christos 		if (dir == NAT_INBOUND) {
   3957  1.1  christos 			nat = ipf_nat_inlookup(fin, flags, p, oip->ip_dst,
   3958  1.1  christos 					       oip->ip_src);
   3959  1.1  christos 		} else {
   3960  1.1  christos 			nat = ipf_nat_outlookup(fin, flags, p, oip->ip_dst,
   3961  1.1  christos 					    oip->ip_src);
   3962  1.1  christos 		}
   3963  1.1  christos 		fin->fin_data[0] = data[0];
   3964  1.1  christos 		fin->fin_data[1] = data[1];
   3965  1.1  christos 		return nat;
   3966  1.1  christos 	}
   3967  1.1  christos 	if (dir == NAT_INBOUND)
   3968  1.1  christos 		nat = ipf_nat_inlookup(fin, 0, p, oip->ip_dst, oip->ip_src);
   3969  1.1  christos 	else
   3970  1.1  christos 		nat = ipf_nat_outlookup(fin, 0, p, oip->ip_dst, oip->ip_src);
   3971  1.1  christos 
   3972  1.1  christos 	return nat;
   3973  1.1  christos }
   3974  1.1  christos 
   3975  1.1  christos 
   3976  1.1  christos /* ------------------------------------------------------------------------ */
   3977  1.1  christos /* Function:    ipf_nat_icmperror                                           */
   3978  1.1  christos /* Returns:     nat_t* - point to matching NAT structure                    */
   3979  1.1  christos /* Parameters:  fin(I)    - pointer to packet information                   */
   3980  1.1  christos /*              nflags(I) - NAT flags for this packet                       */
   3981  1.1  christos /*              dir(I)    - direction of packet (in/out)                    */
   3982  1.1  christos /*                                                                          */
   3983  1.1  christos /* Fix up an ICMP packet which is an error message for an existing NAT      */
   3984  1.1  christos /* session.  This will correct both packet header data and checksums.       */
   3985  1.1  christos /*                                                                          */
   3986  1.1  christos /* This should *ONLY* be used for incoming ICMP error packets to make sure  */
   3987  1.1  christos /* a NAT'd ICMP packet gets correctly recognised.                           */
   3988  1.1  christos /* ------------------------------------------------------------------------ */
   3989  1.1  christos nat_t *
   3990  1.1  christos ipf_nat_icmperror(fin, nflags, dir)
   3991  1.1  christos 	fr_info_t *fin;
   3992  1.1  christos 	u_int *nflags;
   3993  1.1  christos 	int dir;
   3994  1.1  christos {
   3995  1.1  christos 	ipf_main_softc_t *softc = fin->fin_main_soft;
   3996  1.1  christos 	ipf_nat_softc_t *softn = softc->ipf_nat_soft;
   3997  1.1  christos 	u_32_t sum1, sum2, sumd, sumd2;
   3998  1.1  christos 	struct in_addr a1, a2, a3, a4;
   3999  1.1  christos 	int flags, dlen, odst;
   4000  1.1  christos 	icmphdr_t *icmp;
   4001  1.1  christos 	u_short *csump;
   4002  1.1  christos 	tcphdr_t *tcp;
   4003  1.1  christos 	nat_t *nat;
   4004  1.1  christos 	ip_t *oip;
   4005  1.1  christos 	void *dp;
   4006  1.1  christos 
   4007  1.1  christos 	if ((fin->fin_flx & (FI_SHORT|FI_FRAGBODY))) {
   4008  1.1  christos 		NBUMPSIDED(fin->fin_out, ns_icmp_short);
   4009  1.1  christos 		return NULL;
   4010  1.1  christos 	}
   4011  1.1  christos 
   4012  1.1  christos 	/*
   4013  1.1  christos 	 * ipf_nat_icmperrorlookup() will return NULL for `defective' packets.
   4014  1.1  christos 	 */
   4015  1.1  christos 	if ((fin->fin_v != 4) || !(nat = ipf_nat_icmperrorlookup(fin, dir))) {
   4016  1.1  christos 		NBUMPSIDED(fin->fin_out, ns_icmp_notfound);
   4017  1.1  christos 		return NULL;
   4018  1.1  christos 	}
   4019  1.1  christos 
   4020  1.1  christos 	if (nat->nat_dir == NAT_ENCAPIN || nat->nat_dir == NAT_ENCAPOUT) {
   4021  1.1  christos 		/*
   4022  1.1  christos 		 * For ICMP replies to encapsulated packets, we need to
   4023  1.1  christos 		 * rebuild the ICMP reply completely to match the original
   4024  1.1  christos 		 * packet...
   4025  1.1  christos 		 */
   4026  1.1  christos 		if (ipf_nat_rebuildencapicmp(fin, nat) == 0)
   4027  1.1  christos 			return nat;
   4028  1.1  christos 		NBUMPSIDED(fin->fin_out, ns_icmp_rebuild);
   4029  1.1  christos 		return NULL;
   4030  1.1  christos 	}
   4031  1.1  christos 
   4032  1.1  christos 	tcp = NULL;
   4033  1.1  christos 	csump = NULL;
   4034  1.1  christos 	flags = 0;
   4035  1.1  christos 	sumd2 = 0;
   4036  1.1  christos 	*nflags = IPN_ICMPERR;
   4037  1.1  christos 	icmp = fin->fin_dp;
   4038  1.1  christos 	oip = (ip_t *)&icmp->icmp_ip;
   4039  1.1  christos 	dp = (((char *)oip) + (IP_HL(oip) << 2));
   4040  1.1  christos 	if (oip->ip_p == IPPROTO_TCP) {
   4041  1.1  christos 		tcp = (tcphdr_t *)dp;
   4042  1.1  christos 		csump = (u_short *)&tcp->th_sum;
   4043  1.1  christos 		flags = IPN_TCP;
   4044  1.1  christos 	} else if (oip->ip_p == IPPROTO_UDP) {
   4045  1.1  christos 		udphdr_t *udp;
   4046  1.1  christos 
   4047  1.1  christos 		udp = (udphdr_t *)dp;
   4048  1.1  christos 		tcp = (tcphdr_t *)dp;
   4049  1.1  christos 		csump = (u_short *)&udp->uh_sum;
   4050  1.1  christos 		flags = IPN_UDP;
   4051  1.1  christos 	} else if (oip->ip_p == IPPROTO_ICMP)
   4052  1.1  christos 		flags = IPN_ICMPQUERY;
   4053  1.1  christos 	dlen = fin->fin_plen - ((char *)dp - (char *)fin->fin_ip);
   4054  1.1  christos 
   4055  1.1  christos 	/*
   4056  1.1  christos 	 * Need to adjust ICMP header to include the real IP#'s and
   4057  1.1  christos 	 * port #'s.  Only apply a checksum change relative to the
   4058  1.1  christos 	 * IP address change as it will be modified again in ipf_nat_checkout
   4059  1.1  christos 	 * for both address and port.  Two checksum changes are
   4060  1.1  christos 	 * necessary for the two header address changes.  Be careful
   4061  1.1  christos 	 * to only modify the checksum once for the port # and twice
   4062  1.1  christos 	 * for the IP#.
   4063  1.1  christos 	 */
   4064  1.1  christos 
   4065  1.1  christos 	/*
   4066  1.1  christos 	 * Step 1
   4067  1.1  christos 	 * Fix the IP addresses in the offending IP packet. You also need
   4068  1.1  christos 	 * to adjust the IP header checksum of that offending IP packet.
   4069  1.1  christos 	 *
   4070  1.1  christos 	 * Normally, you would expect that the ICMP checksum of the
   4071  1.1  christos 	 * ICMP error message needs to be adjusted as well for the
   4072  1.1  christos 	 * IP address change in oip.
   4073  1.1  christos 	 * However, this is a NOP, because the ICMP checksum is
   4074  1.1  christos 	 * calculated over the complete ICMP packet, which includes the
   4075  1.1  christos 	 * changed oip IP addresses and oip->ip_sum. However, these
   4076  1.1  christos 	 * two changes cancel each other out (if the delta for
   4077  1.1  christos 	 * the IP address is x, then the delta for ip_sum is minus x),
   4078  1.1  christos 	 * so no change in the icmp_cksum is necessary.
   4079  1.1  christos 	 *
   4080  1.1  christos 	 * Inbound ICMP
   4081  1.1  christos 	 * ------------
   4082  1.1  christos 	 * MAP rule, SRC=a,DST=b -> SRC=c,DST=b
   4083  1.1  christos 	 * - response to outgoing packet (a,b)=>(c,b) (OIP_SRC=c,OIP_DST=b)
   4084  1.1  christos 	 * - OIP_SRC(c)=nat_newsrcip,          OIP_DST(b)=nat_newdstip
   4085  1.1  christos 	 *=> OIP_SRC(c)=nat_oldsrcip,          OIP_DST(b)=nat_olddstip
   4086  1.1  christos 	 *
   4087  1.1  christos 	 * RDR rule, SRC=a,DST=b -> SRC=a,DST=c
   4088  1.1  christos 	 * - response to outgoing packet (c,a)=>(b,a) (OIP_SRC=b,OIP_DST=a)
   4089  1.1  christos 	 * - OIP_SRC(b)=nat_olddstip,          OIP_DST(a)=nat_oldsrcip
   4090  1.1  christos 	 *=> OIP_SRC(b)=nat_newdstip,          OIP_DST(a)=nat_newsrcip
   4091  1.1  christos 	 *
   4092  1.1  christos 	 * REWRITE out rule, SRC=a,DST=b -> SRC=c,DST=d
   4093  1.1  christos 	 * - response to outgoing packet (a,b)=>(c,d) (OIP_SRC=c,OIP_DST=d)
   4094  1.1  christos 	 * - OIP_SRC(c)=nat_newsrcip,          OIP_DST(d)=nat_newdstip
   4095  1.1  christos 	 *=> OIP_SRC(c)=nat_oldsrcip,          OIP_DST(d)=nat_olddstip
   4096  1.1  christos 	 *
   4097  1.1  christos 	 * REWRITE in rule, SRC=a,DST=b -> SRC=c,DST=d
   4098  1.1  christos 	 * - response to outgoing packet (d,c)=>(b,a) (OIP_SRC=b,OIP_DST=a)
   4099  1.1  christos 	 * - OIP_SRC(b)=nat_olddstip,          OIP_DST(a)=nat_oldsrcip
   4100  1.1  christos 	 *=> OIP_SRC(b)=nat_newdstip,          OIP_DST(a)=nat_newsrcip
   4101  1.1  christos 	 *
   4102  1.1  christos 	 * Outbound ICMP
   4103  1.1  christos 	 * -------------
   4104  1.1  christos 	 * MAP rule, SRC=a,DST=b -> SRC=c,DST=b
   4105  1.1  christos 	 * - response to incoming packet (b,c)=>(b,a) (OIP_SRC=b,OIP_DST=a)
   4106  1.1  christos 	 * - OIP_SRC(b)=nat_olddstip,          OIP_DST(a)=nat_oldsrcip
   4107  1.1  christos 	 *=> OIP_SRC(b)=nat_newdstip,          OIP_DST(a)=nat_newsrcip
   4108  1.1  christos 	 *
   4109  1.1  christos 	 * RDR rule, SRC=a,DST=b -> SRC=a,DST=c
   4110  1.1  christos 	 * - response to incoming packet (a,b)=>(a,c) (OIP_SRC=a,OIP_DST=c)
   4111  1.1  christos 	 * - OIP_SRC(a)=nat_newsrcip,          OIP_DST(c)=nat_newdstip
   4112  1.1  christos 	 *=> OIP_SRC(a)=nat_oldsrcip,          OIP_DST(c)=nat_olddstip
   4113  1.1  christos 	 *
   4114  1.1  christos 	 * REWRITE out rule, SRC=a,DST=b -> SRC=c,DST=d
   4115  1.1  christos 	 * - response to incoming packet (d,c)=>(b,a) (OIP_SRC=c,OIP_DST=d)
   4116  1.1  christos 	 * - OIP_SRC(c)=nat_olddstip,          OIP_DST(d)=nat_oldsrcip
   4117  1.1  christos 	 *=> OIP_SRC(b)=nat_newdstip,          OIP_DST(a)=nat_newsrcip
   4118  1.1  christos 	 *
   4119  1.1  christos 	 * REWRITE in rule, SRC=a,DST=b -> SRC=c,DST=d
   4120  1.1  christos 	 * - response to incoming packet (a,b)=>(c,d) (OIP_SRC=b,OIP_DST=a)
   4121  1.1  christos 	 * - OIP_SRC(b)=nat_newsrcip,          OIP_DST(a)=nat_newdstip
   4122  1.1  christos 	 *=> OIP_SRC(a)=nat_oldsrcip,          OIP_DST(c)=nat_olddstip
   4123  1.1  christos 	 */
   4124  1.1  christos 
   4125  1.1  christos 	if (((fin->fin_out == 0) && ((nat->nat_redir & NAT_MAP) != 0)) ||
   4126  1.1  christos 	    ((fin->fin_out == 1) && ((nat->nat_redir & NAT_REDIRECT) != 0))) {
   4127  1.1  christos 		a1.s_addr = ntohl(nat->nat_osrcaddr);
   4128  1.1  christos 		a4.s_addr = ntohl(oip->ip_src.s_addr);
   4129  1.1  christos 		a3.s_addr = ntohl(nat->nat_odstaddr);
   4130  1.1  christos 		a2.s_addr = ntohl(oip->ip_dst.s_addr);
   4131  1.1  christos 		oip->ip_src.s_addr = htonl(a1.s_addr);
   4132  1.1  christos 		oip->ip_dst.s_addr = htonl(a3.s_addr);
   4133  1.1  christos 		odst = 1;
   4134  1.1  christos 	} else {
   4135  1.1  christos 		a1.s_addr = ntohl(nat->nat_ndstaddr);
   4136  1.1  christos 		a2.s_addr = ntohl(oip->ip_dst.s_addr);
   4137  1.1  christos 		a3.s_addr = ntohl(nat->nat_nsrcaddr);
   4138  1.1  christos 		a4.s_addr = ntohl(oip->ip_src.s_addr);
   4139  1.1  christos 		oip->ip_dst.s_addr = htonl(a3.s_addr);
   4140  1.1  christos 		oip->ip_src.s_addr = htonl(a1.s_addr);
   4141  1.1  christos 		odst = 0;
   4142  1.1  christos 	}
   4143  1.1  christos 	sumd = 0;
   4144  1.1  christos 	if ((a3.s_addr != a2.s_addr) || (a1.s_addr != a4.s_addr)) {
   4145  1.1  christos 		if (a3.s_addr > a2.s_addr)
   4146  1.1  christos 			sumd = a2.s_addr - a3.s_addr - 1;
   4147  1.1  christos 		else
   4148  1.1  christos 			sumd = a2.s_addr - a3.s_addr;
   4149  1.1  christos 		if (a1.s_addr > a4.s_addr)
   4150  1.1  christos 			sumd += a4.s_addr - a1.s_addr - 1;
   4151  1.1  christos 		else
   4152  1.1  christos 			sumd += a4.s_addr - a1.s_addr;
   4153  1.1  christos 		sumd = ~sumd;
   4154  1.1  christos 
   4155  1.1  christos 		ipf_fix_datacksum(&oip->ip_sum, sumd);
   4156  1.1  christos 	}
   4157  1.1  christos 
   4158  1.1  christos 	sumd2 = sumd;
   4159  1.1  christos 	sum1 = 0;
   4160  1.1  christos 	sum2 = 0;
   4161  1.1  christos 
   4162  1.1  christos 	/*
   4163  1.1  christos 	 * Fix UDP pseudo header checksum to compensate for the
   4164  1.1  christos 	 * IP address change.
   4165  1.1  christos 	 */
   4166  1.1  christos 	if (((flags & IPN_TCPUDP) != 0) && (dlen >= 4)) {
   4167  1.1  christos 		u_32_t sum3, sum4;
   4168  1.1  christos 		/*
   4169  1.1  christos 		 * Step 2 :
   4170  1.1  christos 		 * For offending TCP/UDP IP packets, translate the ports as
   4171  1.1  christos 		 * well, based on the NAT specification. Of course such
   4172  1.1  christos 		 * a change may be reflected in the ICMP checksum as well.
   4173  1.1  christos 		 *
   4174  1.1  christos 		 * Since the port fields are part of the TCP/UDP checksum
   4175  1.1  christos 		 * of the offending IP packet, you need to adjust that checksum
   4176  1.1  christos 		 * as well... except that the change in the port numbers should
   4177  1.1  christos 		 * be offset by the checksum change.  However, the TCP/UDP
   4178  1.1  christos 		 * checksum will also need to change if there has been an
   4179  1.1  christos 		 * IP address change.
   4180  1.1  christos 		 */
   4181  1.1  christos 		if (odst == 1) {
   4182  1.1  christos 			sum1 = ntohs(nat->nat_osport);
   4183  1.1  christos 			sum4 = ntohs(tcp->th_sport);
   4184  1.1  christos 			sum3 = ntohs(nat->nat_odport);
   4185  1.1  christos 			sum2 = ntohs(tcp->th_dport);
   4186  1.1  christos 
   4187  1.1  christos 			tcp->th_sport = htons(sum1);
   4188  1.1  christos 			tcp->th_dport = htons(sum3);
   4189  1.1  christos 		} else {
   4190  1.1  christos 			sum1 = ntohs(nat->nat_ndport);
   4191  1.1  christos 			sum2 = ntohs(tcp->th_dport);
   4192  1.1  christos 			sum3 = ntohs(nat->nat_nsport);
   4193  1.1  christos 			sum4 = ntohs(tcp->th_sport);
   4194  1.1  christos 
   4195  1.1  christos 			tcp->th_dport = htons(sum3);
   4196  1.1  christos 			tcp->th_sport = htons(sum1);
   4197  1.1  christos 		}
   4198  1.1  christos 		sumd += sum1 - sum4;
   4199  1.1  christos 		sumd += sum3 - sum2;
   4200  1.1  christos 
   4201  1.1  christos 		if (sumd != 0 || sumd2 != 0) {
   4202  1.1  christos 			/*
   4203  1.1  christos 			 * At this point, sumd is the delta to apply to the
   4204  1.1  christos 			 * TCP/UDP header, given the changes in both the IP
   4205  1.1  christos 			 * address and the ports and sumd2 is the delta to
   4206  1.1  christos 			 * apply to the ICMP header, given the IP address
   4207  1.1  christos 			 * change delta that may need to be applied to the
   4208  1.1  christos 			 * TCP/UDP checksum instead.
   4209  1.1  christos 			 *
   4210  1.1  christos 			 * If we will both the IP and TCP/UDP checksums
   4211  1.1  christos 			 * then the ICMP checksum changes by the address
   4212  1.1  christos 			 * delta applied to the TCP/UDP checksum.  If we
   4213  1.1  christos 			 * do not change the TCP/UDP checksum them we
   4214  1.1  christos 			 * apply the delta in ports to the ICMP checksum.
   4215  1.1  christos 			 */
   4216  1.1  christos 			if (oip->ip_p == IPPROTO_UDP) {
   4217  1.1  christos 				if ((dlen >= 8) && (*csump != 0)) {
   4218  1.1  christos 					ipf_fix_datacksum(csump, sumd);
   4219  1.1  christos 				} else {
   4220  1.1  christos 					sumd2 = sum4 - sum1;
   4221  1.1  christos 					if (sum1 > sum4)
   4222  1.1  christos 						sumd2--;
   4223  1.1  christos 					sumd2 += sum2 - sum3;
   4224  1.1  christos 					if (sum3 > sum2)
   4225  1.1  christos 						sumd2--;
   4226  1.1  christos 				}
   4227  1.1  christos 			} else if (oip->ip_p == IPPROTO_TCP) {
   4228  1.1  christos 				if (dlen >= 18) {
   4229  1.1  christos 					ipf_fix_datacksum(csump, sumd);
   4230  1.1  christos 				} else {
   4231  1.1  christos 					sumd2 = sum4 - sum1;
   4232  1.1  christos 					if (sum1 > sum4)
   4233  1.1  christos 						sumd2--;
   4234  1.1  christos 					sumd2 += sum2 - sum3;
   4235  1.1  christos 					if (sum3 > sum2)
   4236  1.1  christos 						sumd2--;
   4237  1.1  christos 				}
   4238  1.1  christos 			}
   4239  1.1  christos 			if (sumd2 != 0) {
   4240  1.1  christos 				sumd2 = (sumd2 & 0xffff) + (sumd2 >> 16);
   4241  1.1  christos 				sumd2 = (sumd2 & 0xffff) + (sumd2 >> 16);
   4242  1.1  christos 				sumd2 = (sumd2 & 0xffff) + (sumd2 >> 16);
   4243  1.1  christos 				ipf_fix_incksum(fin, &icmp->icmp_cksum, sumd2);
   4244  1.1  christos 			}
   4245  1.1  christos 		}
   4246  1.1  christos 	} else if (((flags & IPN_ICMPQUERY) != 0) && (dlen >= 8)) {
   4247  1.1  christos 		icmphdr_t *orgicmp;
   4248  1.1  christos 
   4249  1.1  christos 		/*
   4250  1.1  christos 		 * XXX - what if this is bogus hl and we go off the end ?
   4251  1.1  christos 		 * In this case, ipf_nat_icmperrorlookup() will have
   4252  1.1  christos 		 * returned NULL.
   4253  1.1  christos 		 */
   4254  1.1  christos 		orgicmp = (icmphdr_t *)dp;
   4255  1.1  christos 
   4256  1.1  christos 		if (odst == 1) {
   4257  1.1  christos 			if (orgicmp->icmp_id != nat->nat_osport) {
   4258  1.1  christos 
   4259  1.1  christos 				/*
   4260  1.1  christos 				 * Fix ICMP checksum (of the offening ICMP
   4261  1.1  christos 				 * query packet) to compensate the change
   4262  1.1  christos 				 * in the ICMP id of the offending ICMP
   4263  1.1  christos 				 * packet.
   4264  1.1  christos 				 *
   4265  1.1  christos 				 * Since you modify orgicmp->icmp_id with
   4266  1.1  christos 				 * a delta (say x) and you compensate that
   4267  1.1  christos 				 * in origicmp->icmp_cksum with a delta
   4268  1.1  christos 				 * minus x, you don't have to adjust the
   4269  1.1  christos 				 * overall icmp->icmp_cksum
   4270  1.1  christos 				 */
   4271  1.1  christos 				sum1 = ntohs(orgicmp->icmp_id);
   4272  1.1  christos 				sum2 = ntohs(nat->nat_osport);
   4273  1.1  christos 				CALC_SUMD(sum1, sum2, sumd);
   4274  1.1  christos 				orgicmp->icmp_id = nat->nat_oicmpid;
   4275  1.1  christos 				ipf_fix_datacksum(&orgicmp->icmp_cksum, sumd);
   4276  1.1  christos 			}
   4277  1.1  christos 		} /* nat_dir == NAT_INBOUND is impossible for icmp queries */
   4278  1.1  christos 	}
   4279  1.1  christos 	return nat;
   4280  1.1  christos }
   4281  1.1  christos 
   4282  1.1  christos 
   4283  1.1  christos /*
   4284  1.1  christos  *       MAP-IN    MAP-OUT   RDR-IN   RDR-OUT
   4285  1.1  christos  * osrc    X       == src    == src      X
   4286  1.1  christos  * odst    X       == dst    == dst      X
   4287  1.1  christos  * nsrc  == dst      X         X      == dst
   4288  1.1  christos  * ndst  == src      X         X      == src
   4289  1.1  christos  * MAP = NAT_OUTBOUND, RDR = NAT_INBOUND
   4290  1.1  christos  */
   4291  1.1  christos /*
   4292  1.1  christos  * NB: these lookups don't lock access to the list, it assumed that it has
   4293  1.1  christos  * already been done!
   4294  1.1  christos  */
   4295  1.1  christos /* ------------------------------------------------------------------------ */
   4296  1.1  christos /* Function:    ipf_nat_inlookup                                            */
   4297  1.1  christos /* Returns:     nat_t* - NULL == no match,                                  */
   4298  1.1  christos /*                       else pointer to matching NAT entry                 */
   4299  1.1  christos /* Parameters:  fin(I)    - pointer to packet information                   */
   4300  1.1  christos /*              flags(I)  - NAT flags for this packet                       */
   4301  1.1  christos /*              p(I)      - protocol for this packet                        */
   4302  1.1  christos /*              src(I)    - source IP address                               */
   4303  1.1  christos /*              mapdst(I) - destination IP address                          */
   4304  1.1  christos /*                                                                          */
   4305  1.1  christos /* Lookup a nat entry based on the mapped destination ip address/port and   */
   4306  1.1  christos /* real source address/port.  We use this lookup when receiving a packet,   */
   4307  1.1  christos /* we're looking for a table entry, based on the destination address.       */
   4308  1.1  christos /*                                                                          */
   4309  1.1  christos /* NOTE: THE PACKET BEING CHECKED (IF FOUND) HAS A MAPPING ALREADY.         */
   4310  1.1  christos /*                                                                          */
   4311  1.1  christos /* NOTE: IT IS ASSUMED THAT  IS ONLY HELD WITH A READ LOCK WHEN             */
   4312  1.1  christos /*       THIS FUNCTION IS CALLED WITH NAT_SEARCH SET IN nflags.             */
   4313  1.1  christos /*                                                                          */
   4314  1.1  christos /* flags   -> relevant are IPN_UDP/IPN_TCP/IPN_ICMPQUERY that indicate if   */
   4315  1.1  christos /*            the packet is of said protocol                                */
   4316  1.1  christos /* ------------------------------------------------------------------------ */
   4317  1.1  christos nat_t *
   4318  1.1  christos ipf_nat_inlookup(fin, flags, p, src, mapdst)
   4319  1.1  christos 	fr_info_t *fin;
   4320  1.1  christos 	u_int flags, p;
   4321  1.1  christos 	struct in_addr src , mapdst;
   4322  1.1  christos {
   4323  1.1  christos 	ipf_main_softc_t *softc = fin->fin_main_soft;
   4324  1.1  christos 	ipf_nat_softc_t *softn = softc->ipf_nat_soft;
   4325  1.1  christos 	u_short sport, dport;
   4326  1.1  christos 	grehdr_t *gre;
   4327  1.1  christos 	ipnat_t *ipn;
   4328  1.1  christos 	u_int sflags;
   4329  1.1  christos 	nat_t *nat;
   4330  1.1  christos 	int nflags;
   4331  1.1  christos 	u_32_t dst;
   4332  1.1  christos 	void *ifp;
   4333  1.1  christos 	u_int hv, rhv;
   4334  1.1  christos 
   4335  1.1  christos 	ifp = fin->fin_ifp;
   4336  1.1  christos 	gre = NULL;
   4337  1.1  christos 	dst = mapdst.s_addr;
   4338  1.1  christos 	sflags = flags & NAT_TCPUDPICMP;
   4339  1.1  christos 
   4340  1.1  christos 	switch (p)
   4341  1.1  christos 	{
   4342  1.1  christos 	case IPPROTO_TCP :
   4343  1.1  christos 	case IPPROTO_UDP :
   4344  1.1  christos 		sport = htons(fin->fin_data[0]);
   4345  1.1  christos 		dport = htons(fin->fin_data[1]);
   4346  1.1  christos 		break;
   4347  1.1  christos 	case IPPROTO_ICMP :
   4348  1.1  christos 		if (flags & IPN_ICMPERR) {
   4349  1.1  christos 			sport = fin->fin_data[1];
   4350  1.1  christos 			dport = 0;
   4351  1.1  christos 		} else {
   4352  1.1  christos 			dport = fin->fin_data[1];
   4353  1.1  christos 			sport = 0;
   4354  1.1  christos 		}
   4355  1.1  christos 		break;
   4356  1.1  christos 	default :
   4357  1.1  christos 		sport = 0;
   4358  1.1  christos 		dport = 0;
   4359  1.1  christos 		break;
   4360  1.1  christos 	}
   4361  1.1  christos 
   4362  1.1  christos 
   4363  1.1  christos 	if ((flags & SI_WILDP) != 0)
   4364  1.1  christos 		goto find_in_wild_ports;
   4365  1.1  christos 
   4366  1.1  christos 	rhv = NAT_HASH_FN(dst, dport, 0xffffffff);
   4367  1.1  christos 	rhv = NAT_HASH_FN(src.s_addr, rhv + sport, 0xffffffff);
   4368  1.1  christos 	hv = rhv % softn->ipf_nat_table_sz;
   4369  1.1  christos 	nat = softn->ipf_nat_table[1][hv];
   4370  1.1  christos 	/* TRACE dst, dport, src, sport, hv, nat */
   4371  1.1  christos 
   4372  1.1  christos 	for (; nat; nat = nat->nat_hnext[1]) {
   4373  1.1  christos 		if (nat->nat_ifps[0] != NULL) {
   4374  1.1  christos 			if ((ifp != NULL) && (ifp != nat->nat_ifps[0]))
   4375  1.1  christos 				continue;
   4376  1.1  christos 		}
   4377  1.1  christos 
   4378  1.1  christos 		if (nat->nat_pr[0] != p)
   4379  1.1  christos 			continue;
   4380  1.1  christos 
   4381  1.1  christos 		switch (nat->nat_dir)
   4382  1.1  christos 		{
   4383  1.1  christos 		case NAT_INBOUND :
   4384  1.1  christos 		case NAT_ENCAPIN :
   4385  1.1  christos 		case NAT_DIVERTIN :
   4386  1.1  christos 			if (nat->nat_v[0] != 4)
   4387  1.1  christos 				continue;
   4388  1.1  christos 			if (nat->nat_osrcaddr != src.s_addr ||
   4389  1.1  christos 			    nat->nat_odstaddr != dst)
   4390  1.1  christos 				continue;
   4391  1.1  christos 			if ((nat->nat_flags & IPN_TCPUDP) != 0) {
   4392  1.1  christos 				if (nat->nat_osport != sport)
   4393  1.1  christos 					continue;
   4394  1.1  christos 				if (nat->nat_odport != dport)
   4395  1.1  christos 					continue;
   4396  1.1  christos 
   4397  1.1  christos 			} else if (p == IPPROTO_ICMP) {
   4398  1.1  christos 				if (nat->nat_osport != dport) {
   4399  1.1  christos 					continue;
   4400  1.1  christos 				}
   4401  1.1  christos 			}
   4402  1.1  christos 			break;
   4403  1.1  christos 		case NAT_DIVERTOUT :
   4404  1.1  christos 			if (nat->nat_dlocal)
   4405  1.1  christos 				continue;
   4406  1.1  christos 		case NAT_OUTBOUND :
   4407  1.1  christos 		case NAT_ENCAPOUT :
   4408  1.1  christos 			if (nat->nat_v[1] != 4)
   4409  1.1  christos 				continue;
   4410  1.1  christos 			if (nat->nat_dlocal)
   4411  1.1  christos 				continue;
   4412  1.1  christos 			if (nat->nat_dlocal)
   4413  1.1  christos 				continue;
   4414  1.1  christos 			if (nat->nat_ndstaddr != src.s_addr ||
   4415  1.1  christos 			    nat->nat_nsrcaddr != dst)
   4416  1.1  christos 				continue;
   4417  1.1  christos 			if ((nat->nat_flags & IPN_TCPUDP) != 0) {
   4418  1.1  christos 				if (nat->nat_ndport != sport)
   4419  1.1  christos 					continue;
   4420  1.1  christos 				if (nat->nat_nsport != dport)
   4421  1.1  christos 					continue;
   4422  1.1  christos 
   4423  1.1  christos 			} else if (p == IPPROTO_ICMP) {
   4424  1.1  christos 				if (nat->nat_osport != dport) {
   4425  1.1  christos 					continue;
   4426  1.1  christos 				}
   4427  1.1  christos 			}
   4428  1.1  christos 			break;
   4429  1.1  christos 		}
   4430  1.1  christos 
   4431  1.1  christos 
   4432  1.1  christos 		if ((nat->nat_flags & IPN_TCPUDP) != 0) {
   4433  1.1  christos 			ipn = nat->nat_ptr;
   4434  1.1  christos 			if ((ipn != NULL) && (nat->nat_aps != NULL))
   4435  1.1  christos 				if (ipf_proxy_match(fin, nat) != 0)
   4436  1.1  christos 					continue;
   4437  1.1  christos 		}
   4438  1.1  christos 		if ((nat->nat_ifps[0] == NULL) && (ifp != NULL)) {
   4439  1.1  christos 			nat->nat_ifps[0] = ifp;
   4440  1.1  christos 			nat->nat_mtu[0] = GETIFMTU_4(ifp);
   4441  1.1  christos 		}
   4442  1.1  christos 		return nat;
   4443  1.1  christos 	}
   4444  1.1  christos 
   4445  1.1  christos 	/*
   4446  1.1  christos 	 * So if we didn't find it but there are wildcard members in the hash
   4447  1.1  christos 	 * table, go back and look for them.  We do this search and update here
   4448  1.1  christos 	 * because it is modifying the NAT table and we want to do this only
   4449  1.1  christos 	 * for the first packet that matches.  The exception, of course, is
   4450  1.1  christos 	 * for "dummy" (FI_IGNORE) lookups.
   4451  1.1  christos 	 */
   4452  1.1  christos find_in_wild_ports:
   4453  1.1  christos 	if (!(flags & NAT_TCPUDP) || !(flags & NAT_SEARCH)) {
   4454  1.1  christos 		NBUMPSIDEX(0, ns_lookup_miss, ns_lookup_miss_0);
   4455  1.1  christos 		return NULL;
   4456  1.1  christos 	}
   4457  1.1  christos 	if (softn->ipf_nat_stats.ns_wilds == 0) {
   4458  1.1  christos 		NBUMPSIDEX(0, ns_lookup_nowild, ns_lookup_nowild_0);
   4459  1.1  christos 		return NULL;
   4460  1.1  christos 	}
   4461  1.1  christos 
   4462  1.1  christos 	RWLOCK_EXIT(&softc->ipf_nat);
   4463  1.1  christos 
   4464  1.1  christos 	hv = NAT_HASH_FN(dst, 0, 0xffffffff);
   4465  1.1  christos 	hv = NAT_HASH_FN(src.s_addr, hv, softn->ipf_nat_table_sz);
   4466  1.1  christos 	WRITE_ENTER(&softc->ipf_nat);
   4467  1.1  christos 
   4468  1.1  christos 	nat = softn->ipf_nat_table[1][hv];
   4469  1.1  christos 	/* TRACE dst, src, hv, nat */
   4470  1.1  christos 	for (; nat; nat = nat->nat_hnext[1]) {
   4471  1.1  christos 		if (nat->nat_ifps[0] != NULL) {
   4472  1.1  christos 			if ((ifp != NULL) && (ifp != nat->nat_ifps[0]))
   4473  1.1  christos 				continue;
   4474  1.1  christos 		}
   4475  1.1  christos 
   4476  1.1  christos 		if (nat->nat_pr[0] != fin->fin_p)
   4477  1.1  christos 			continue;
   4478  1.1  christos 
   4479  1.1  christos 		switch (nat->nat_dir & (NAT_INBOUND|NAT_OUTBOUND))
   4480  1.1  christos 		{
   4481  1.1  christos 		case NAT_INBOUND :
   4482  1.1  christos 			if (nat->nat_v[0] != 4)
   4483  1.1  christos 				continue;
   4484  1.1  christos 			if (nat->nat_osrcaddr != src.s_addr ||
   4485  1.1  christos 			    nat->nat_odstaddr != dst)
   4486  1.1  christos 				continue;
   4487  1.1  christos 			break;
   4488  1.1  christos 		case NAT_OUTBOUND :
   4489  1.1  christos 			if (nat->nat_v[1] != 4)
   4490  1.1  christos 				continue;
   4491  1.1  christos 			if (nat->nat_ndstaddr != src.s_addr ||
   4492  1.1  christos 			    nat->nat_nsrcaddr != dst)
   4493  1.1  christos 				continue;
   4494  1.1  christos 			break;
   4495  1.1  christos 		}
   4496  1.1  christos 
   4497  1.1  christos 		nflags = nat->nat_flags;
   4498  1.1  christos 		if (!(nflags & (NAT_TCPUDP|SI_WILDP)))
   4499  1.1  christos 			continue;
   4500  1.1  christos 
   4501  1.1  christos 		if (ipf_nat_wildok(nat, (int)sport, (int)dport, nflags,
   4502  1.1  christos 				   NAT_INBOUND) == 1) {
   4503  1.1  christos 			if ((fin->fin_flx & FI_IGNORE) != 0)
   4504  1.1  christos 				break;
   4505  1.1  christos 			if ((nflags & SI_CLONE) != 0) {
   4506  1.1  christos 				nat = ipf_nat_clone(fin, nat);
   4507  1.1  christos 				if (nat == NULL)
   4508  1.1  christos 					break;
   4509  1.1  christos 			} else {
   4510  1.1  christos 				MUTEX_ENTER(&softn->ipf_nat_new);
   4511  1.1  christos 				softn->ipf_nat_stats.ns_wilds--;
   4512  1.1  christos 				MUTEX_EXIT(&softn->ipf_nat_new);
   4513  1.1  christos 			}
   4514  1.1  christos 
   4515  1.1  christos 			if (nat->nat_dir == NAT_INBOUND) {
   4516  1.1  christos 				if (nat->nat_osport == 0) {
   4517  1.1  christos 					nat->nat_osport = sport;
   4518  1.1  christos 					nat->nat_nsport = sport;
   4519  1.1  christos 				}
   4520  1.1  christos 				if (nat->nat_odport == 0) {
   4521  1.1  christos 					nat->nat_odport = dport;
   4522  1.1  christos 					nat->nat_ndport = dport;
   4523  1.1  christos 				}
   4524  1.1  christos 			} else if (nat->nat_dir == NAT_OUTBOUND) {
   4525  1.1  christos 				if (nat->nat_osport == 0) {
   4526  1.1  christos 					nat->nat_osport = dport;
   4527  1.1  christos 					nat->nat_nsport = dport;
   4528  1.1  christos 				}
   4529  1.1  christos 				if (nat->nat_odport == 0) {
   4530  1.1  christos 					nat->nat_odport = sport;
   4531  1.1  christos 					nat->nat_ndport = sport;
   4532  1.1  christos 				}
   4533  1.1  christos 			}
   4534  1.1  christos 			if ((nat->nat_ifps[0] == NULL) && (ifp != NULL)) {
   4535  1.1  christos 				nat->nat_ifps[0] = ifp;
   4536  1.1  christos 				nat->nat_mtu[0] = GETIFMTU_4(ifp);
   4537  1.1  christos 			}
   4538  1.1  christos 			nat->nat_flags &= ~(SI_W_DPORT|SI_W_SPORT);
   4539  1.1  christos 			ipf_nat_tabmove(softn, nat);
   4540  1.1  christos 			break;
   4541  1.1  christos 		}
   4542  1.1  christos 	}
   4543  1.1  christos 
   4544  1.1  christos 	MUTEX_DOWNGRADE(&softc->ipf_nat);
   4545  1.1  christos 
   4546  1.1  christos 	if (nat == NULL) {
   4547  1.1  christos 		NBUMPSIDE(0, ns_lookup_miss);
   4548  1.1  christos 	}
   4549  1.1  christos 	return nat;
   4550  1.1  christos }
   4551  1.1  christos 
   4552  1.1  christos 
   4553  1.1  christos /* ------------------------------------------------------------------------ */
   4554  1.1  christos /* Function:    ipf_nat_tabmove                                             */
   4555  1.1  christos /* Returns:     Nil                                                         */
   4556  1.1  christos /* Parameters:  nat(I) - pointer to NAT structure                           */
   4557  1.1  christos /* Write Lock:  ipf_nat                                                     */
   4558  1.1  christos /*                                                                          */
   4559  1.1  christos /* This function is only called for TCP/UDP NAT table entries where the     */
   4560  1.1  christos /* original was placed in the table without hashing on the ports and we now */
   4561  1.1  christos /* want to include hashing on port numbers.                                 */
   4562  1.1  christos /* ------------------------------------------------------------------------ */
   4563  1.1  christos static void
   4564  1.1  christos ipf_nat_tabmove(softn, nat)
   4565  1.1  christos 	ipf_nat_softc_t *softn;
   4566  1.1  christos 	nat_t *nat;
   4567  1.1  christos {
   4568  1.1  christos 	u_int hv0, hv1, rhv0, rhv1;
   4569  1.1  christos 	natstat_t *nsp;
   4570  1.1  christos 	nat_t **natp;
   4571  1.1  christos 
   4572  1.1  christos 	if (nat->nat_flags & SI_CLONE)
   4573  1.1  christos 		return;
   4574  1.1  christos 
   4575  1.1  christos 	nsp = &softn->ipf_nat_stats;
   4576  1.1  christos 	/*
   4577  1.1  christos 	 * Remove the NAT entry from the old location
   4578  1.1  christos 	 */
   4579  1.1  christos 	if (nat->nat_hnext[0])
   4580  1.1  christos 		nat->nat_hnext[0]->nat_phnext[0] = nat->nat_phnext[0];
   4581  1.1  christos 	*nat->nat_phnext[0] = nat->nat_hnext[0];
   4582  1.1  christos 	nsp->ns_side[0].ns_bucketlen[nat->nat_hv[0] %
   4583  1.1  christos 				     softn->ipf_nat_table_sz]--;
   4584  1.1  christos 
   4585  1.1  christos 	if (nat->nat_hnext[1])
   4586  1.1  christos 		nat->nat_hnext[1]->nat_phnext[1] = nat->nat_phnext[1];
   4587  1.1  christos 	*nat->nat_phnext[1] = nat->nat_hnext[1];
   4588  1.1  christos 	nsp->ns_side[1].ns_bucketlen[nat->nat_hv[1] %
   4589  1.1  christos 				     softn->ipf_nat_table_sz]--;
   4590  1.1  christos 
   4591  1.1  christos 	/*
   4592  1.1  christos 	 * Add into the NAT table in the new position
   4593  1.1  christos 	 */
   4594  1.1  christos 	rhv0 = NAT_HASH_FN(nat->nat_osrcaddr, nat->nat_osport, 0xffffffff);
   4595  1.1  christos 	rhv0 = NAT_HASH_FN(nat->nat_odstaddr, rhv0 + nat->nat_odport,
   4596  1.1  christos 			   0xffffffff);
   4597  1.1  christos 	rhv1 = NAT_HASH_FN(nat->nat_nsrcaddr, nat->nat_nsport, 0xffffffff);
   4598  1.1  christos 	rhv1 = NAT_HASH_FN(nat->nat_ndstaddr, rhv1 + nat->nat_ndport,
   4599  1.1  christos 			   0xffffffff);
   4600  1.1  christos 
   4601  1.1  christos 	hv0 = rhv0 % softn->ipf_nat_table_sz;
   4602  1.1  christos 	hv1 = rhv1 % softn->ipf_nat_table_sz;
   4603  1.1  christos 
   4604  1.1  christos 	if (nat->nat_dir == NAT_INBOUND || nat->nat_dir == NAT_ENCAPIN ||
   4605  1.1  christos 	    nat->nat_dir == NAT_DIVERTIN) {
   4606  1.1  christos 		u_int swap;
   4607  1.1  christos 
   4608  1.1  christos 		swap = hv0;
   4609  1.1  christos 		hv0 = hv1;
   4610  1.1  christos 		hv1 = swap;
   4611  1.1  christos 	}
   4612  1.1  christos 
   4613  1.1  christos 	/* TRACE nat_osrcaddr, nat_osport, nat_odstaddr, nat_odport, hv0 */
   4614  1.1  christos 	/* TRACE nat_nsrcaddr, nat_nsport, nat_ndstaddr, nat_ndport, hv1 */
   4615  1.1  christos 
   4616  1.1  christos 	nat->nat_hv[0] = rhv0;
   4617  1.1  christos 	natp = &softn->ipf_nat_table[0][hv0];
   4618  1.1  christos 	if (*natp)
   4619  1.1  christos 		(*natp)->nat_phnext[0] = &nat->nat_hnext[0];
   4620  1.1  christos 	nat->nat_phnext[0] = natp;
   4621  1.1  christos 	nat->nat_hnext[0] = *natp;
   4622  1.1  christos 	*natp = nat;
   4623  1.1  christos 	nsp->ns_side[0].ns_bucketlen[hv0]++;
   4624  1.1  christos 
   4625  1.1  christos 	nat->nat_hv[1] = rhv1;
   4626  1.1  christos 	natp = &softn->ipf_nat_table[1][hv1];
   4627  1.1  christos 	if (*natp)
   4628  1.1  christos 		(*natp)->nat_phnext[1] = &nat->nat_hnext[1];
   4629  1.1  christos 	nat->nat_phnext[1] = natp;
   4630  1.1  christos 	nat->nat_hnext[1] = *natp;
   4631  1.1  christos 	*natp = nat;
   4632  1.1  christos 	nsp->ns_side[1].ns_bucketlen[hv1]++;
   4633  1.1  christos }
   4634  1.1  christos 
   4635  1.1  christos 
   4636  1.1  christos /* ------------------------------------------------------------------------ */
   4637  1.1  christos /* Function:    ipf_nat_outlookup                                           */
   4638  1.1  christos /* Returns:     nat_t* - NULL == no match,                                  */
   4639  1.1  christos /*                       else pointer to matching NAT entry                 */
   4640  1.1  christos /* Parameters:  fin(I)   - pointer to packet information                    */
   4641  1.1  christos /*              flags(I) - NAT flags for this packet                        */
   4642  1.1  christos /*              p(I)     - protocol for this packet                         */
   4643  1.1  christos /*              src(I)   - source IP address                                */
   4644  1.1  christos /*              dst(I)   - destination IP address                           */
   4645  1.1  christos /*              rw(I)    - 1 == write lock on  held, 0 == read lock.        */
   4646  1.1  christos /*                                                                          */
   4647  1.1  christos /* Lookup a nat entry based on the source 'real' ip address/port and        */
   4648  1.1  christos /* destination address/port.  We use this lookup when sending a packet out, */
   4649  1.1  christos /* we're looking for a table entry, based on the source address.            */
   4650  1.1  christos /*                                                                          */
   4651  1.1  christos /* NOTE: THE PACKET BEING CHECKED (IF FOUND) HAS A MAPPING ALREADY.         */
   4652  1.1  christos /*                                                                          */
   4653  1.1  christos /* NOTE: IT IS ASSUMED THAT  IS ONLY HELD WITH A READ LOCK WHEN             */
   4654  1.1  christos /*       THIS FUNCTION IS CALLED WITH NAT_SEARCH SET IN nflags.             */
   4655  1.1  christos /*                                                                          */
   4656  1.1  christos /* flags   -> relevant are IPN_UDP/IPN_TCP/IPN_ICMPQUERY that indicate if   */
   4657  1.1  christos /*            the packet is of said protocol                                */
   4658  1.1  christos /* ------------------------------------------------------------------------ */
   4659  1.1  christos nat_t *
   4660  1.1  christos ipf_nat_outlookup(fin, flags, p, src, dst)
   4661  1.1  christos 	fr_info_t *fin;
   4662  1.1  christos 	u_int flags, p;
   4663  1.1  christos 	struct in_addr src , dst;
   4664  1.1  christos {
   4665  1.1  christos 	ipf_main_softc_t *softc = fin->fin_main_soft;
   4666  1.1  christos 	ipf_nat_softc_t *softn = softc->ipf_nat_soft;
   4667  1.1  christos 	u_short sport, dport;
   4668  1.1  christos 	u_int sflags;
   4669  1.1  christos 	ipnat_t *ipn;
   4670  1.1  christos 	nat_t *nat;
   4671  1.1  christos 	void *ifp;
   4672  1.1  christos 	u_int hv;
   4673  1.1  christos 
   4674  1.1  christos 	ifp = fin->fin_ifp;
   4675  1.1  christos 	sflags = flags & IPN_TCPUDPICMP;
   4676  1.1  christos 	sport = 0;
   4677  1.1  christos 	dport = 0;
   4678  1.1  christos 
   4679  1.1  christos 	switch (p)
   4680  1.1  christos 	{
   4681  1.1  christos 	case IPPROTO_TCP :
   4682  1.1  christos 	case IPPROTO_UDP :
   4683  1.1  christos 		sport = htons(fin->fin_data[0]);
   4684  1.1  christos 		dport = htons(fin->fin_data[1]);
   4685  1.1  christos 		break;
   4686  1.1  christos 	case IPPROTO_ICMP :
   4687  1.1  christos 		if (flags & IPN_ICMPERR)
   4688  1.1  christos 			sport = fin->fin_data[1];
   4689  1.1  christos 		else
   4690  1.1  christos 			dport = fin->fin_data[1];
   4691  1.1  christos 		break;
   4692  1.1  christos 	default :
   4693  1.1  christos 		break;
   4694  1.1  christos 	}
   4695  1.1  christos 
   4696  1.1  christos 	if ((flags & SI_WILDP) != 0)
   4697  1.1  christos 		goto find_out_wild_ports;
   4698  1.1  christos 
   4699  1.1  christos 	hv = NAT_HASH_FN(src.s_addr, sport, 0xffffffff);
   4700  1.1  christos 	hv = NAT_HASH_FN(dst.s_addr, hv + dport, softn->ipf_nat_table_sz);
   4701  1.1  christos 	nat = softn->ipf_nat_table[0][hv];
   4702  1.1  christos 
   4703  1.1  christos 	/* TRACE src, sport, dst, dport, hv, nat */
   4704  1.1  christos 
   4705  1.1  christos 	for (; nat; nat = nat->nat_hnext[0]) {
   4706  1.1  christos 		if (nat->nat_ifps[1] != NULL) {
   4707  1.1  christos 			if ((ifp != NULL) && (ifp != nat->nat_ifps[1]))
   4708  1.1  christos 				continue;
   4709  1.1  christos 		}
   4710  1.1  christos 
   4711  1.1  christos 		if (nat->nat_pr[1] != p)
   4712  1.1  christos 			continue;
   4713  1.1  christos 
   4714  1.1  christos 		switch (nat->nat_dir)
   4715  1.1  christos 		{
   4716  1.1  christos 		case NAT_INBOUND :
   4717  1.1  christos 		case NAT_ENCAPIN :
   4718  1.1  christos 		case NAT_DIVERTIN :
   4719  1.1  christos 			if (nat->nat_v[1] != 4)
   4720  1.1  christos 				continue;
   4721  1.1  christos 			if (nat->nat_ndstaddr != src.s_addr ||
   4722  1.1  christos 			    nat->nat_nsrcaddr != dst.s_addr)
   4723  1.1  christos 				continue;
   4724  1.1  christos 
   4725  1.1  christos 			if ((nat->nat_flags & IPN_TCPUDP) != 0) {
   4726  1.1  christos 				if (nat->nat_ndport != sport)
   4727  1.1  christos 					continue;
   4728  1.1  christos 				if (nat->nat_nsport != dport)
   4729  1.1  christos 					continue;
   4730  1.1  christos 
   4731  1.1  christos 			} else if (p == IPPROTO_ICMP) {
   4732  1.1  christos 				if (nat->nat_osport != dport) {
   4733  1.1  christos 					continue;
   4734  1.1  christos 				}
   4735  1.1  christos 			}
   4736  1.1  christos 			break;
   4737  1.1  christos 		case NAT_OUTBOUND :
   4738  1.1  christos 		case NAT_ENCAPOUT :
   4739  1.1  christos 		case NAT_DIVERTOUT :
   4740  1.1  christos 			if (nat->nat_v[0] != 4)
   4741  1.1  christos 				continue;
   4742  1.1  christos 			if (nat->nat_osrcaddr != src.s_addr ||
   4743  1.1  christos 			    nat->nat_odstaddr != dst.s_addr)
   4744  1.1  christos 				continue;
   4745  1.1  christos 
   4746  1.1  christos 			if ((nat->nat_flags & IPN_TCPUDP) != 0) {
   4747  1.1  christos 				if (nat->nat_odport != dport)
   4748  1.1  christos 					continue;
   4749  1.1  christos 				if (nat->nat_osport != sport)
   4750  1.1  christos 					continue;
   4751  1.1  christos 
   4752  1.1  christos 			} else if (p == IPPROTO_ICMP) {
   4753  1.1  christos 				if (nat->nat_osport != dport) {
   4754  1.1  christos 					continue;
   4755  1.1  christos 				}
   4756  1.1  christos 			}
   4757  1.1  christos 			break;
   4758  1.1  christos 		}
   4759  1.1  christos 
   4760  1.1  christos 		ipn = nat->nat_ptr;
   4761  1.1  christos 		if ((ipn != NULL) && (nat->nat_aps != NULL))
   4762  1.1  christos 			if (ipf_proxy_match(fin, nat) != 0)
   4763  1.1  christos 				continue;
   4764  1.1  christos 
   4765  1.1  christos 		if ((nat->nat_ifps[1] == NULL) && (ifp != NULL)) {
   4766  1.1  christos 			nat->nat_ifps[1] = ifp;
   4767  1.1  christos 			nat->nat_mtu[1] = GETIFMTU_4(ifp);
   4768  1.1  christos 		}
   4769  1.1  christos 		return nat;
   4770  1.1  christos 	}
   4771  1.1  christos 
   4772  1.1  christos 	/*
   4773  1.1  christos 	 * So if we didn't find it but there are wildcard members in the hash
   4774  1.1  christos 	 * table, go back and look for them.  We do this search and update here
   4775  1.1  christos 	 * because it is modifying the NAT table and we want to do this only
   4776  1.1  christos 	 * for the first packet that matches.  The exception, of course, is
   4777  1.1  christos 	 * for "dummy" (FI_IGNORE) lookups.
   4778  1.1  christos 	 */
   4779  1.1  christos find_out_wild_ports:
   4780  1.1  christos 	if (!(flags & NAT_TCPUDP) || !(flags & NAT_SEARCH)) {
   4781  1.1  christos 		NBUMPSIDEX(1, ns_lookup_miss, ns_lookup_miss_1);
   4782  1.1  christos 		return NULL;
   4783  1.1  christos 	}
   4784  1.1  christos 	if (softn->ipf_nat_stats.ns_wilds == 0) {
   4785  1.1  christos 		NBUMPSIDEX(1, ns_lookup_nowild, ns_lookup_nowild_1);
   4786  1.1  christos 		return NULL;
   4787  1.1  christos 	}
   4788  1.1  christos 
   4789  1.1  christos 	RWLOCK_EXIT(&softc->ipf_nat);
   4790  1.1  christos 
   4791  1.1  christos 	hv = NAT_HASH_FN(src.s_addr, 0, 0xffffffff);
   4792  1.1  christos 	hv = NAT_HASH_FN(dst.s_addr, hv, softn->ipf_nat_table_sz);
   4793  1.1  christos 
   4794  1.1  christos 	WRITE_ENTER(&softc->ipf_nat);
   4795  1.1  christos 
   4796  1.1  christos 	nat = softn->ipf_nat_table[0][hv];
   4797  1.1  christos 	for (; nat; nat = nat->nat_hnext[0]) {
   4798  1.1  christos 		if (nat->nat_ifps[1] != NULL) {
   4799  1.1  christos 			if ((ifp != NULL) && (ifp != nat->nat_ifps[1]))
   4800  1.1  christos 				continue;
   4801  1.1  christos 		}
   4802  1.1  christos 
   4803  1.1  christos 		if (nat->nat_pr[1] != fin->fin_p)
   4804  1.1  christos 			continue;
   4805  1.1  christos 
   4806  1.1  christos 		switch (nat->nat_dir & (NAT_INBOUND|NAT_OUTBOUND))
   4807  1.1  christos 		{
   4808  1.1  christos 		case NAT_INBOUND :
   4809  1.1  christos 			if (nat->nat_v[1] != 4)
   4810  1.1  christos 				continue;
   4811  1.1  christos 			if (nat->nat_ndstaddr != src.s_addr ||
   4812  1.1  christos 			    nat->nat_nsrcaddr != dst.s_addr)
   4813  1.1  christos 				continue;
   4814  1.1  christos 			break;
   4815  1.1  christos 		case NAT_OUTBOUND :
   4816  1.1  christos 			if (nat->nat_v[0] != 4)
   4817  1.1  christos 				continue;
   4818  1.1  christos 			if (nat->nat_osrcaddr != src.s_addr ||
   4819  1.1  christos 			    nat->nat_odstaddr != dst.s_addr)
   4820  1.1  christos 				continue;
   4821  1.1  christos 			break;
   4822  1.1  christos 		}
   4823  1.1  christos 
   4824  1.1  christos 		if (!(nat->nat_flags & (NAT_TCPUDP|SI_WILDP)))
   4825  1.1  christos 			continue;
   4826  1.1  christos 
   4827  1.1  christos 		if (ipf_nat_wildok(nat, (int)sport, (int)dport, nat->nat_flags,
   4828  1.1  christos 				   NAT_OUTBOUND) == 1) {
   4829  1.1  christos 			if ((fin->fin_flx & FI_IGNORE) != 0)
   4830  1.1  christos 				break;
   4831  1.1  christos 			if ((nat->nat_flags & SI_CLONE) != 0) {
   4832  1.1  christos 				nat = ipf_nat_clone(fin, nat);
   4833  1.1  christos 				if (nat == NULL)
   4834  1.1  christos 					break;
   4835  1.1  christos 			} else {
   4836  1.1  christos 				MUTEX_ENTER(&softn->ipf_nat_new);
   4837  1.1  christos 				softn->ipf_nat_stats.ns_wilds--;
   4838  1.1  christos 				MUTEX_EXIT(&softn->ipf_nat_new);
   4839  1.1  christos 			}
   4840  1.1  christos 
   4841  1.1  christos 			if (nat->nat_dir == NAT_OUTBOUND) {
   4842  1.1  christos 				if (nat->nat_osport == 0) {
   4843  1.1  christos 					nat->nat_osport = sport;
   4844  1.1  christos 					nat->nat_nsport = sport;
   4845  1.1  christos 				}
   4846  1.1  christos 				if (nat->nat_odport == 0) {
   4847  1.1  christos 					nat->nat_odport = dport;
   4848  1.1  christos 					nat->nat_ndport = dport;
   4849  1.1  christos 				}
   4850  1.1  christos 			} else if (nat->nat_dir == NAT_INBOUND) {
   4851  1.1  christos 				if (nat->nat_osport == 0) {
   4852  1.1  christos 					nat->nat_osport = dport;
   4853  1.1  christos 					nat->nat_nsport = dport;
   4854  1.1  christos 				}
   4855  1.1  christos 				if (nat->nat_odport == 0) {
   4856  1.1  christos 					nat->nat_odport = sport;
   4857  1.1  christos 					nat->nat_ndport = sport;
   4858  1.1  christos 				}
   4859  1.1  christos 			}
   4860  1.1  christos 			if ((nat->nat_ifps[1] == NULL) && (ifp != NULL)) {
   4861  1.1  christos 				nat->nat_ifps[1] = ifp;
   4862  1.1  christos 				nat->nat_mtu[1] = GETIFMTU_4(ifp);
   4863  1.1  christos 			}
   4864  1.1  christos 			nat->nat_flags &= ~(SI_W_DPORT|SI_W_SPORT);
   4865  1.1  christos 			ipf_nat_tabmove(softn, nat);
   4866  1.1  christos 			break;
   4867  1.1  christos 		}
   4868  1.1  christos 	}
   4869  1.1  christos 
   4870  1.1  christos 	MUTEX_DOWNGRADE(&softc->ipf_nat);
   4871  1.1  christos 
   4872  1.1  christos 	if (nat == NULL) {
   4873  1.1  christos 		NBUMPSIDE(1, ns_lookup_miss);
   4874  1.1  christos 	}
   4875  1.1  christos 	return nat;
   4876  1.1  christos }
   4877  1.1  christos 
   4878  1.1  christos 
   4879  1.1  christos /* ------------------------------------------------------------------------ */
   4880  1.1  christos /* Function:    ipf_nat_lookupredir                                         */
   4881  1.1  christos /* Returns:     nat_t* - NULL == no match,                                  */
   4882  1.1  christos /*                       else pointer to matching NAT entry                 */
   4883  1.1  christos /* Parameters:  np(I) - pointer to description of packet to find NAT table  */
   4884  1.1  christos /*                      entry for.                                          */
   4885  1.1  christos /*                                                                          */
   4886  1.1  christos /* Lookup the NAT tables to search for a matching redirect                  */
   4887  1.1  christos /* The contents of natlookup_t should imitate those found in a packet that  */
   4888  1.1  christos /* would be translated - ie a packet coming in for RDR or going out for MAP.*/
   4889  1.1  christos /* We can do the lookup in one of two ways, imitating an inbound or         */
   4890  1.1  christos /* outbound  packet.  By default we assume outbound, unless IPN_IN is set.  */
   4891  1.1  christos /* For IN, the fields are set as follows:                                   */
   4892  1.1  christos /*     nl_real* = source information                                        */
   4893  1.1  christos /*     nl_out* = destination information (translated)                       */
   4894  1.1  christos /* For an out packet, the fields are set like this:                         */
   4895  1.1  christos /*     nl_in* = source information (untranslated)                           */
   4896  1.1  christos /*     nl_out* = destination information (translated)                       */
   4897  1.1  christos /* ------------------------------------------------------------------------ */
   4898  1.1  christos nat_t *
   4899  1.1  christos ipf_nat_lookupredir(np)
   4900  1.1  christos 	natlookup_t *np;
   4901  1.1  christos {
   4902  1.1  christos 	fr_info_t fi;
   4903  1.1  christos 	nat_t *nat;
   4904  1.1  christos 
   4905  1.1  christos 	bzero((char *)&fi, sizeof(fi));
   4906  1.1  christos 	if (np->nl_flags & IPN_IN) {
   4907  1.1  christos 		fi.fin_data[0] = ntohs(np->nl_realport);
   4908  1.1  christos 		fi.fin_data[1] = ntohs(np->nl_outport);
   4909  1.1  christos 	} else {
   4910  1.1  christos 		fi.fin_data[0] = ntohs(np->nl_inport);
   4911  1.1  christos 		fi.fin_data[1] = ntohs(np->nl_outport);
   4912  1.1  christos 	}
   4913  1.1  christos 	if (np->nl_flags & IPN_TCP)
   4914  1.1  christos 		fi.fin_p = IPPROTO_TCP;
   4915  1.1  christos 	else if (np->nl_flags & IPN_UDP)
   4916  1.1  christos 		fi.fin_p = IPPROTO_UDP;
   4917  1.1  christos 	else if (np->nl_flags & (IPN_ICMPERR|IPN_ICMPQUERY))
   4918  1.1  christos 		fi.fin_p = IPPROTO_ICMP;
   4919  1.1  christos 
   4920  1.1  christos 	/*
   4921  1.1  christos 	 * We can do two sorts of lookups:
   4922  1.1  christos 	 * - IPN_IN: we have the `real' and `out' address, look for `in'.
   4923  1.1  christos 	 * - default: we have the `in' and `out' address, look for `real'.
   4924  1.1  christos 	 */
   4925  1.1  christos 	if (np->nl_flags & IPN_IN) {
   4926  1.1  christos 		if ((nat = ipf_nat_inlookup(&fi, np->nl_flags, fi.fin_p,
   4927  1.1  christos 					    np->nl_realip, np->nl_outip))) {
   4928  1.1  christos 			np->nl_inip = nat->nat_odstip;
   4929  1.1  christos 			np->nl_inport = nat->nat_odport;
   4930  1.1  christos 		}
   4931  1.1  christos 	} else {
   4932  1.1  christos 		/*
   4933  1.1  christos 		 * If nl_inip is non null, this is a lookup based on the real
   4934  1.1  christos 		 * ip address. Else, we use the fake.
   4935  1.1  christos 		 */
   4936  1.1  christos 		if ((nat = ipf_nat_outlookup(&fi, np->nl_flags, fi.fin_p,
   4937  1.1  christos 					 np->nl_inip, np->nl_outip))) {
   4938  1.1  christos 
   4939  1.1  christos 			if ((np->nl_flags & IPN_FINDFORWARD) != 0) {
   4940  1.1  christos 				fr_info_t fin;
   4941  1.1  christos 				bzero((char *)&fin, sizeof(fin));
   4942  1.1  christos 				fin.fin_p = nat->nat_pr[0];
   4943  1.1  christos 				fin.fin_data[0] = ntohs(nat->nat_ndport);
   4944  1.1  christos 				fin.fin_data[1] = ntohs(nat->nat_nsport);
   4945  1.1  christos 				if (ipf_nat_inlookup(&fin, np->nl_flags,
   4946  1.1  christos 						     fin.fin_p, nat->nat_ndstip,
   4947  1.1  christos 						     nat->nat_nsrcip) != NULL) {
   4948  1.1  christos 					np->nl_flags &= ~IPN_FINDFORWARD;
   4949  1.1  christos 				}
   4950  1.1  christos 			}
   4951  1.1  christos 
   4952  1.1  christos 			np->nl_realip = nat->nat_ndstip;
   4953  1.1  christos 			np->nl_realport = nat->nat_ndport;
   4954  1.1  christos 		}
   4955  1.1  christos  	}
   4956  1.1  christos 
   4957  1.1  christos 	return nat;
   4958  1.1  christos }
   4959  1.1  christos 
   4960  1.1  christos 
   4961  1.1  christos /* ------------------------------------------------------------------------ */
   4962  1.1  christos /* Function:    ipf_nat_match                                               */
   4963  1.1  christos /* Returns:     int - 0 == no match, 1 == match                             */
   4964  1.1  christos /* Parameters:  fin(I)   - pointer to packet information                    */
   4965  1.1  christos /*              np(I)    - pointer to NAT rule                              */
   4966  1.1  christos /*                                                                          */
   4967  1.1  christos /* Pull the matching of a packet against a NAT rule out of that complex     */
   4968  1.1  christos /* loop inside ipf_nat_checkin() and lay it out properly in its own function. */
   4969  1.1  christos /* ------------------------------------------------------------------------ */
   4970  1.1  christos static int
   4971  1.1  christos ipf_nat_match(fin, np)
   4972  1.1  christos 	fr_info_t *fin;
   4973  1.1  christos 	ipnat_t *np;
   4974  1.1  christos {
   4975  1.1  christos 	ipf_main_softc_t *softc = fin->fin_main_soft;
   4976  1.1  christos 	ipf_nat_softc_t *softn = softc->ipf_nat_soft;
   4977  1.1  christos 	frtuc_t *ft;
   4978  1.1  christos 	int match;
   4979  1.1  christos 
   4980  1.1  christos 	if ((fin->fin_p == IPPROTO_IPIP) && (np->in_redir & NAT_ENCAP))
   4981  1.1  christos 		return ipf_nat_matchencap(softn, fin, np);
   4982  1.1  christos 
   4983  1.1  christos 	match = 0;
   4984  1.1  christos 	switch (np->in_osrcatype)
   4985  1.1  christos 	{
   4986  1.1  christos 	case FRI_NORMAL :
   4987  1.1  christos 		match = ((fin->fin_saddr & np->in_osrcmsk) != np->in_osrcaddr);
   4988  1.1  christos 		break;
   4989  1.1  christos 	case FRI_LOOKUP :
   4990  1.1  christos 		match = (*np->in_osrcfunc)(softc, np->in_osrcptr,
   4991  1.1  christos 					   4, &fin->fin_saddr, fin->fin_plen);
   4992  1.1  christos 		break;
   4993  1.1  christos 	}
   4994  1.1  christos 	match ^= ((np->in_flags & IPN_NOTSRC) != 0);
   4995  1.1  christos 	if (match)
   4996  1.1  christos 		return 0;
   4997  1.1  christos 
   4998  1.1  christos 	match = 0;
   4999  1.1  christos 	switch (np->in_odstatype)
   5000  1.1  christos 	{
   5001  1.1  christos 	case FRI_NORMAL :
   5002  1.1  christos 		match = ((fin->fin_daddr & np->in_odstmsk) != np->in_odstaddr);
   5003  1.1  christos 		break;
   5004  1.1  christos 	case FRI_LOOKUP :
   5005  1.1  christos 		match = (*np->in_odstfunc)(softc, np->in_odstptr,
   5006  1.1  christos 					   4, &fin->fin_daddr, fin->fin_plen);
   5007  1.1  christos 		break;
   5008  1.1  christos 	}
   5009  1.1  christos 
   5010  1.1  christos 	match ^= ((np->in_flags & IPN_NOTDST) != 0);
   5011  1.1  christos 	if (match)
   5012  1.1  christos 		return 0;
   5013  1.1  christos 
   5014  1.1  christos 	ft = &np->in_tuc;
   5015  1.1  christos 	if (!(fin->fin_flx & FI_TCPUDP) ||
   5016  1.1  christos 	    (fin->fin_flx & (FI_SHORT|FI_FRAGBODY))) {
   5017  1.1  christos 		if (ft->ftu_scmp || ft->ftu_dcmp)
   5018  1.1  christos 			return 0;
   5019  1.1  christos 		return 1;
   5020  1.1  christos 	}
   5021  1.1  christos 
   5022  1.1  christos 	return ipf_tcpudpchk(&fin->fin_fi, ft);
   5023  1.1  christos }
   5024  1.1  christos 
   5025  1.1  christos 
   5026  1.1  christos /* ------------------------------------------------------------------------ */
   5027  1.1  christos /* Function:    ipf_nat_update                                              */
   5028  1.1  christos /* Returns:     Nil                                                         */
   5029  1.1  christos /* Parameters:  fin(I)   - pointer to packet information                    */
   5030  1.1  christos /*              nat(I)    - pointer to NAT structure                        */
   5031  1.1  christos /*                                                                          */
   5032  1.1  christos /* Updates the lifetime of a NAT table entry for non-TCP packets.  Must be  */
   5033  1.1  christos /* called with fin_rev updated - i.e. after calling ipf_nat_proto().        */
   5034  1.1  christos /*                                                                          */
   5035  1.1  christos /* This *MUST* be called after ipf_nat_proto() as it expects fin_rev to     */
   5036  1.1  christos /* already be set.                                                          */
   5037  1.1  christos /* ------------------------------------------------------------------------ */
   5038  1.1  christos void
   5039  1.1  christos ipf_nat_update(fin, nat)
   5040  1.1  christos 	fr_info_t *fin;
   5041  1.1  christos 	nat_t *nat;
   5042  1.1  christos {
   5043  1.1  christos 	ipf_main_softc_t *softc = fin->fin_main_soft;
   5044  1.1  christos 	ipf_nat_softc_t *softn = softc->ipf_nat_soft;
   5045  1.1  christos 	ipftq_t *ifq, *ifq2;
   5046  1.1  christos 	ipftqent_t *tqe;
   5047  1.1  christos 	ipnat_t *np = nat->nat_ptr;
   5048  1.1  christos 
   5049  1.1  christos 	tqe = &nat->nat_tqe;
   5050  1.1  christos 	ifq = tqe->tqe_ifq;
   5051  1.1  christos 
   5052  1.1  christos 	/*
   5053  1.1  christos 	 * We allow over-riding of NAT timeouts from NAT rules, even for
   5054  1.1  christos 	 * TCP, however, if it is TCP and there is no rule timeout set,
   5055  1.1  christos 	 * then do not update the timeout here.
   5056  1.1  christos 	 */
   5057  1.1  christos 	if (np != NULL) {
   5058  1.1  christos 		np->in_bytes[fin->fin_rev] += fin->fin_plen;
   5059  1.1  christos 		ifq2 = np->in_tqehead[fin->fin_rev];
   5060  1.1  christos 	} else {
   5061  1.1  christos 		ifq2 = NULL;
   5062  1.1  christos 	}
   5063  1.1  christos 
   5064  1.1  christos 	if (nat->nat_pr[0] == IPPROTO_TCP && ifq2 == NULL) {
   5065  1.1  christos 		(void) ipf_tcp_age(&nat->nat_tqe, fin, softn->ipf_nat_tcptq,
   5066  1.1  christos 				   0, 2);
   5067  1.1  christos 	} else {
   5068  1.1  christos 		if (ifq2 == NULL) {
   5069  1.1  christos 			if (nat->nat_pr[0] == IPPROTO_UDP)
   5070  1.1  christos 				ifq2 = fin->fin_rev ? &softn->ipf_nat_udpacktq :
   5071  1.1  christos 						      &softn->ipf_nat_udptq;
   5072  1.1  christos 			else if (nat->nat_pr[0] == IPPROTO_ICMP)
   5073  1.1  christos 				ifq2 = fin->fin_rev ? &softn->ipf_nat_icmpacktq:
   5074  1.1  christos 						      &softn->ipf_nat_icmptq;
   5075  1.1  christos 			else
   5076  1.1  christos 				ifq2 = &softn->ipf_nat_iptq;
   5077  1.1  christos 		}
   5078  1.1  christos 
   5079  1.1  christos 		ipf_movequeue(softc->ipf_ticks, tqe, ifq, ifq2);
   5080  1.1  christos 	}
   5081  1.1  christos }
   5082  1.1  christos 
   5083  1.1  christos 
   5084  1.1  christos /* ------------------------------------------------------------------------ */
   5085  1.1  christos /* Function:    ipf_nat_ipfout                                              */
   5086  1.1  christos /* Returns:     frentry_t* - NULL (packet may have been translated, let it  */
   5087  1.1  christos /*                           pass), &ipfnatblock - block/drop the packet.   */
   5088  1.1  christos /* Parameters:  fin(I)   - pointer to packet information                    */
   5089  1.1  christos /*              passp(I) - point to filtering result flags                  */
   5090  1.1  christos /*                                                                          */
   5091  1.1  christos /* This is purely and simply a wrapper around ipf_nat_checkout for the sole */
   5092  1.1  christos /* reason of being able to activate NAT from an ipf rule using "call-now".  */
   5093  1.1  christos /* ------------------------------------------------------------------------ */
   5094  1.1  christos frentry_t *
   5095  1.1  christos ipf_nat_ipfout(fin, passp)
   5096  1.1  christos 	fr_info_t *fin;
   5097  1.1  christos 	u_32_t *passp;
   5098  1.1  christos {
   5099  1.1  christos 	frentry_t *fr = fin->fin_fr;
   5100  1.1  christos 
   5101  1.1  christos 	if (fin->fin_v == 6) {
   5102  1.1  christos #ifdef USE_INET6
   5103  1.1  christos 		return ipf_nat6_ipfout(fin, passp);
   5104  1.1  christos #else
   5105  1.1  christos 		return NULL;
   5106  1.1  christos #endif
   5107  1.1  christos 	}
   5108  1.1  christos 
   5109  1.1  christos 	switch (ipf_nat_checkout(fin, passp))
   5110  1.1  christos 	{
   5111  1.1  christos 	case -1 :
   5112  1.1  christos 		fr = &ipfnatblock;
   5113  1.1  christos 		MUTEX_ENTER(&fr->fr_lock);
   5114  1.1  christos 		fr->fr_ref++;
   5115  1.1  christos 		MUTEX_EXIT(&fr->fr_lock);
   5116  1.1  christos 		return fr;
   5117  1.1  christos 
   5118  1.1  christos 	case 0 :
   5119  1.1  christos 		break;
   5120  1.1  christos 
   5121  1.1  christos 	case 1 :
   5122  1.1  christos 		/*
   5123  1.1  christos 		 * Returing NULL causes this rule to be "ignored" but
   5124  1.1  christos 		 * it has actually had an influence on the packet so we
   5125  1.1  christos 		 * increment counters for it.
   5126  1.1  christos 		 */
   5127  1.1  christos 		fr->fr_bytes += (U_QUAD_T)fin->fin_plen;
   5128  1.1  christos 		fr->fr_hits++;
   5129  1.1  christos 		break;
   5130  1.1  christos 	}
   5131  1.1  christos 
   5132  1.1  christos 	return NULL;
   5133  1.1  christos }
   5134  1.1  christos 
   5135  1.1  christos 
   5136  1.1  christos /* ------------------------------------------------------------------------ */
   5137  1.1  christos /* Function:    ipf_nat_checkout                                            */
   5138  1.1  christos /* Returns:     int - -1 == packet failed NAT checks so block it,           */
   5139  1.1  christos /*                     0 == no packet translation occurred,                 */
   5140  1.1  christos /*                     1 == packet was successfully translated.             */
   5141  1.1  christos /* Parameters:  fin(I)   - pointer to packet information                    */
   5142  1.1  christos /*              passp(I) - pointer to filtering result flags                */
   5143  1.1  christos /*                                                                          */
   5144  1.1  christos /* Check to see if an outcoming packet should be changed.  ICMP packets are */
   5145  1.1  christos /* first checked to see if they match an existing entry (if an error),      */
   5146  1.1  christos /* otherwise a search of the current NAT table is made.  If neither results */
   5147  1.1  christos /* in a match then a search for a matching NAT rule is made.  Create a new  */
   5148  1.1  christos /* NAT entry if a we matched a NAT rule.  Lastly, actually change the       */
   5149  1.1  christos /* packet header(s) as required.                                            */
   5150  1.1  christos /* ------------------------------------------------------------------------ */
   5151  1.1  christos int
   5152  1.1  christos ipf_nat_checkout(fin, passp)
   5153  1.1  christos 	fr_info_t *fin;
   5154  1.1  christos 	u_32_t *passp;
   5155  1.1  christos {
   5156  1.1  christos 	ipnat_t *np = NULL, *npnext;
   5157  1.1  christos 	struct ifnet *ifp, *sifp;
   5158  1.1  christos 	ipf_main_softc_t *softc;
   5159  1.1  christos 	ipf_nat_softc_t *softn;
   5160  1.1  christos 	icmphdr_t *icmp = NULL;
   5161  1.1  christos 	tcphdr_t *tcp = NULL;
   5162  1.1  christos 	int rval, natfailed;
   5163  1.1  christos 	u_int nflags = 0;
   5164  1.1  christos 	u_32_t ipa, iph;
   5165  1.1  christos 	int natadd = 1;
   5166  1.1  christos 	frentry_t *fr;
   5167  1.1  christos 	nat_t *nat;
   5168  1.1  christos 
   5169  1.1  christos 	if (fin->fin_v == 6) {
   5170  1.1  christos #ifdef USE_INET6
   5171  1.1  christos 		return ipf_nat6_checkout(fin, passp);
   5172  1.1  christos #else
   5173  1.1  christos 		return 0;
   5174  1.1  christos #endif
   5175  1.1  christos 	}
   5176  1.1  christos 
   5177  1.1  christos 	softc = fin->fin_main_soft;
   5178  1.1  christos 	softn = softc->ipf_nat_soft;
   5179  1.1  christos 
   5180  1.1  christos 	if (softn->ipf_nat_lock != 0)
   5181  1.1  christos 		return 0;
   5182  1.1  christos 	if (softn->ipf_nat_stats.ns_rules == 0 &&
   5183  1.1  christos 	    softn->ipf_nat_instances == NULL)
   5184  1.1  christos 		return 0;
   5185  1.1  christos 
   5186  1.1  christos 	natfailed = 0;
   5187  1.1  christos 	fr = fin->fin_fr;
   5188  1.1  christos 	sifp = fin->fin_ifp;
   5189  1.1  christos 	if (fr != NULL) {
   5190  1.1  christos 		ifp = fr->fr_tifs[fin->fin_rev].fd_ptr;
   5191  1.1  christos 		if ((ifp != NULL) && (ifp != (void *)-1))
   5192  1.1  christos 			fin->fin_ifp = ifp;
   5193  1.1  christos 	}
   5194  1.1  christos 	ifp = fin->fin_ifp;
   5195  1.1  christos 
   5196  1.1  christos 	if (!(fin->fin_flx & FI_SHORT) && (fin->fin_off == 0)) {
   5197  1.1  christos 		switch (fin->fin_p)
   5198  1.1  christos 		{
   5199  1.1  christos 		case IPPROTO_TCP :
   5200  1.1  christos 			nflags = IPN_TCP;
   5201  1.1  christos 			break;
   5202  1.1  christos 		case IPPROTO_UDP :
   5203  1.1  christos 			nflags = IPN_UDP;
   5204  1.1  christos 			break;
   5205  1.1  christos 		case IPPROTO_ICMP :
   5206  1.1  christos 			icmp = fin->fin_dp;
   5207  1.1  christos 
   5208  1.1  christos 			/*
   5209  1.1  christos 			 * This is an incoming packet, so the destination is
   5210  1.1  christos 			 * the icmp_id and the source port equals 0
   5211  1.1  christos 			 */
   5212  1.1  christos 			if ((fin->fin_flx & FI_ICMPQUERY) != 0)
   5213  1.1  christos 				nflags = IPN_ICMPQUERY;
   5214  1.1  christos 			break;
   5215  1.1  christos 		default :
   5216  1.1  christos 			break;
   5217  1.1  christos 		}
   5218  1.1  christos 
   5219  1.1  christos 		if ((nflags & IPN_TCPUDP))
   5220  1.1  christos 			tcp = fin->fin_dp;
   5221  1.1  christos 	}
   5222  1.1  christos 
   5223  1.1  christos 	ipa = fin->fin_saddr;
   5224  1.1  christos 
   5225  1.1  christos 	READ_ENTER(&softc->ipf_nat);
   5226  1.1  christos 
   5227  1.1  christos 	if ((fin->fin_p == IPPROTO_ICMP) && !(nflags & IPN_ICMPQUERY) &&
   5228  1.1  christos 	    (nat = ipf_nat_icmperror(fin, &nflags, NAT_OUTBOUND)))
   5229  1.1  christos 		/*EMPTY*/;
   5230  1.1  christos 	else if ((fin->fin_flx & FI_FRAG) && (nat = ipf_frag_natknown(fin)))
   5231  1.1  christos 		natadd = 0;
   5232  1.1  christos 	else if ((nat = ipf_nat_outlookup(fin, nflags|NAT_SEARCH,
   5233  1.1  christos 				      (u_int)fin->fin_p, fin->fin_src,
   5234  1.1  christos 				      fin->fin_dst))) {
   5235  1.1  christos 		nflags = nat->nat_flags;
   5236  1.1  christos 	} else if (fin->fin_off == 0) {
   5237  1.1  christos 		u_32_t hv, msk, nmsk = 0;
   5238  1.1  christos 
   5239  1.1  christos 		/*
   5240  1.1  christos 		 * If there is no current entry in the nat table for this IP#,
   5241  1.1  christos 		 * create one for it (if there is a matching rule).
   5242  1.1  christos 		 */
   5243  1.1  christos maskloop:
   5244  1.1  christos 		msk = softn->ipf_nat_map_active_masks[nmsk];
   5245  1.1  christos 		iph = ipa & msk;
   5246  1.1  christos 		hv = NAT_HASH_FN(iph, 0, softn->ipf_nat_maprules_sz);
   5247  1.1  christos retry_roundrobin:
   5248  1.1  christos 		for (np = softn->ipf_nat_map_rules[hv]; np; np = npnext) {
   5249  1.1  christos 			npnext = np->in_mnext;
   5250  1.1  christos 			if ((np->in_ifps[1] && (np->in_ifps[1] != ifp)))
   5251  1.1  christos 				continue;
   5252  1.1  christos 			if (np->in_v[0] != 4)
   5253  1.1  christos 				continue;
   5254  1.1  christos 			if (np->in_pr[1] && (np->in_pr[1] != fin->fin_p))
   5255  1.1  christos 				continue;
   5256  1.1  christos 			if ((np->in_flags & IPN_RF) &&
   5257  1.1  christos 			    !(np->in_flags & nflags))
   5258  1.1  christos 				continue;
   5259  1.1  christos 			if (np->in_flags & IPN_FILTER) {
   5260  1.1  christos 				switch (ipf_nat_match(fin, np))
   5261  1.1  christos 				{
   5262  1.1  christos 				case 0 :
   5263  1.1  christos 					continue;
   5264  1.1  christos 				case -1 :
   5265  1.1  christos 					rval = -1;
   5266  1.1  christos 					goto outmatchfail;
   5267  1.1  christos 				case 1 :
   5268  1.1  christos 				default :
   5269  1.1  christos 					break;
   5270  1.1  christos 				}
   5271  1.1  christos 			} else if ((ipa & np->in_osrcmsk) != np->in_osrcaddr)
   5272  1.1  christos 				continue;
   5273  1.1  christos 
   5274  1.1  christos 			if ((fr != NULL) &&
   5275  1.1  christos 			    !ipf_matchtag(&np->in_tag, &fr->fr_nattag))
   5276  1.1  christos 				continue;
   5277  1.1  christos 
   5278  1.1  christos 			if (np->in_plabel != -1) {
   5279  1.1  christos 				if (((np->in_flags & IPN_FILTER) == 0) &&
   5280  1.1  christos 				    (np->in_odport != fin->fin_data[1]))
   5281  1.1  christos 					continue;
   5282  1.1  christos 				if (ipf_proxy_ok(fin, tcp, np) == 0)
   5283  1.1  christos 					continue;
   5284  1.1  christos 			}
   5285  1.1  christos 
   5286  1.1  christos 			if (np->in_flags & IPN_NO) {
   5287  1.1  christos 				np->in_hits++;
   5288  1.1  christos 				break;
   5289  1.1  christos 			}
   5290  1.1  christos 			MUTEX_ENTER(&softn->ipf_nat_new);
   5291  1.1  christos 			/*
   5292  1.1  christos 			 * If we've matched a round-robin rule but it has
   5293  1.1  christos 			 * moved in the list since we got it, start over as
   5294  1.1  christos 			 * this is now no longer correct.
   5295  1.1  christos 			 */
   5296  1.1  christos 			if (npnext != np->in_mnext) {
   5297  1.1  christos 				if ((np->in_flags & IPN_ROUNDR) != 0) {
   5298  1.1  christos 					MUTEX_EXIT(&softn->ipf_nat_new);
   5299  1.1  christos 					goto retry_roundrobin;
   5300  1.1  christos 				}
   5301  1.1  christos 				npnext = np->in_mnext;
   5302  1.1  christos 			}
   5303  1.1  christos 
   5304  1.1  christos 			nat = ipf_nat_add(fin, np, NULL, nflags, NAT_OUTBOUND);
   5305  1.1  christos 			MUTEX_EXIT(&softn->ipf_nat_new);
   5306  1.1  christos 			if (nat != NULL) {
   5307  1.1  christos 				natfailed = 0;
   5308  1.1  christos 				break;
   5309  1.1  christos 			}
   5310  1.1  christos 			natfailed = -1;
   5311  1.1  christos 		}
   5312  1.1  christos 		if ((np == NULL) && (nmsk < softn->ipf_nat_map_max)) {
   5313  1.1  christos 			nmsk++;
   5314  1.1  christos 			goto maskloop;
   5315  1.1  christos 		}
   5316  1.1  christos 	}
   5317  1.1  christos 
   5318  1.1  christos 	if (nat != NULL) {
   5319  1.1  christos 		rval = ipf_nat_out(fin, nat, natadd, nflags);
   5320  1.1  christos 		if (rval == 1) {
   5321  1.1  christos 			MUTEX_ENTER(&nat->nat_lock);
   5322  1.1  christos 			ipf_nat_update(fin, nat);
   5323  1.1  christos 			nat->nat_bytes[1] += fin->fin_plen;
   5324  1.1  christos 			nat->nat_pkts[1]++;
   5325  1.1  christos 			fin->fin_pktnum = nat->nat_pkts[1];
   5326  1.1  christos 			MUTEX_EXIT(&nat->nat_lock);
   5327  1.1  christos 		}
   5328  1.1  christos 	} else
   5329  1.1  christos 		rval = natfailed;
   5330  1.1  christos outmatchfail:
   5331  1.1  christos 	RWLOCK_EXIT(&softc->ipf_nat);
   5332  1.1  christos 
   5333  1.1  christos 	switch (rval)
   5334  1.1  christos 	{
   5335  1.1  christos 	case -1 :
   5336  1.1  christos 		if (passp != NULL) {
   5337  1.1  christos 			DT1(frb_natv4out, fr_info_t *, fin);
   5338  1.1  christos 			NBUMPSIDED(1, ns_drop);
   5339  1.1  christos 			*passp = FR_BLOCK;
   5340  1.1  christos 			fin->fin_reason = FRB_NATV4OUT;
   5341  1.1  christos 		}
   5342  1.1  christos 		fin->fin_flx |= FI_BADNAT;
   5343  1.1  christos 		NBUMPSIDED(1, ns_badnat);
   5344  1.1  christos 		break;
   5345  1.1  christos 	case 0 :
   5346  1.1  christos 		NBUMPSIDE(1, ns_ignored);
   5347  1.1  christos 		break;
   5348  1.1  christos 	case 1 :
   5349  1.1  christos 		NBUMPSIDE(1, ns_translated);
   5350  1.1  christos 		break;
   5351  1.1  christos 	}
   5352  1.1  christos 	fin->fin_ifp = sifp;
   5353  1.1  christos 	return rval;
   5354  1.1  christos }
   5355  1.1  christos 
   5356  1.1  christos /* ------------------------------------------------------------------------ */
   5357  1.1  christos /* Function:    ipf_nat_out                                                 */
   5358  1.1  christos /* Returns:     int - -1 == packet failed NAT checks so block it,           */
   5359  1.1  christos /*                     1 == packet was successfully translated.             */
   5360  1.1  christos /* Parameters:  fin(I)    - pointer to packet information                   */
   5361  1.1  christos /*              nat(I)    - pointer to NAT structure                        */
   5362  1.1  christos /*              natadd(I) - flag indicating if it is safe to add frag cache */
   5363  1.1  christos /*              nflags(I) - NAT flags set for this packet                   */
   5364  1.1  christos /*                                                                          */
   5365  1.1  christos /* Translate a packet coming "out" on an interface.                         */
   5366  1.1  christos /* ------------------------------------------------------------------------ */
   5367  1.1  christos int
   5368  1.1  christos ipf_nat_out(fin, nat, natadd, nflags)
   5369  1.1  christos 	fr_info_t *fin;
   5370  1.1  christos 	nat_t *nat;
   5371  1.1  christos 	int natadd;
   5372  1.1  christos 	u_32_t nflags;
   5373  1.1  christos {
   5374  1.1  christos 	ipf_main_softc_t *softc = fin->fin_main_soft;
   5375  1.1  christos 	ipf_nat_softc_t *softn = softc->ipf_nat_soft;
   5376  1.1  christos 	icmphdr_t *icmp;
   5377  1.1  christos 	tcphdr_t *tcp;
   5378  1.1  christos 	ipnat_t *np;
   5379  1.1  christos 	int skip;
   5380  1.1  christos 	int i;
   5381  1.1  christos 
   5382  1.1  christos 	tcp = NULL;
   5383  1.1  christos 	icmp = NULL;
   5384  1.1  christos 	np = nat->nat_ptr;
   5385  1.1  christos 
   5386  1.1  christos 	if ((natadd != 0) && (fin->fin_flx & FI_FRAG) && (np != NULL))
   5387  1.1  christos 		(void) ipf_frag_natnew(softc, fin, 0, nat);
   5388  1.1  christos 
   5389  1.1  christos 	/*
   5390  1.1  christos 	 * Fix up checksums, not by recalculating them, but
   5391  1.1  christos 	 * simply computing adjustments.
   5392  1.1  christos 	 * This is only done for STREAMS based IP implementations where the
   5393  1.1  christos 	 * checksum has already been calculated by IP.  In all other cases,
   5394  1.1  christos 	 * IPFilter is called before the checksum needs calculating so there
   5395  1.1  christos 	 * is no call to modify whatever is in the header now.
   5396  1.1  christos 	 */
   5397  1.1  christos 	if (nflags == IPN_ICMPERR) {
   5398  1.1  christos 		u_32_t s1, s2, sumd, msumd;
   5399  1.1  christos 
   5400  1.1  christos 		s1 = LONG_SUM(ntohl(fin->fin_saddr));
   5401  1.1  christos 		if (nat->nat_dir == NAT_OUTBOUND) {
   5402  1.1  christos 			s2 = LONG_SUM(ntohl(nat->nat_nsrcaddr));
   5403  1.1  christos 		} else {
   5404  1.1  christos 			s2 = LONG_SUM(ntohl(nat->nat_odstaddr));
   5405  1.1  christos 		}
   5406  1.1  christos 		CALC_SUMD(s1, s2, sumd);
   5407  1.1  christos 		msumd = sumd;
   5408  1.1  christos 
   5409  1.1  christos 		s1 = LONG_SUM(ntohl(fin->fin_daddr));
   5410  1.1  christos 		if (nat->nat_dir == NAT_OUTBOUND) {
   5411  1.1  christos 			s2 = LONG_SUM(ntohl(nat->nat_ndstaddr));
   5412  1.1  christos 		} else {
   5413  1.1  christos 			s2 = LONG_SUM(ntohl(nat->nat_osrcaddr));
   5414  1.1  christos 		}
   5415  1.1  christos 		CALC_SUMD(s1, s2, sumd);
   5416  1.1  christos 		msumd += sumd;
   5417  1.1  christos 
   5418  1.1  christos 		ipf_fix_outcksum(fin, &fin->fin_ip->ip_sum, msumd);
   5419  1.1  christos 	}
   5420  1.1  christos #if !defined(_KERNEL) || defined(MENTAT) || defined(__sgi) || \
   5421  1.1  christos     defined(linux) || defined(BRIDGE_IPF)
   5422  1.1  christos 	else {
   5423  1.1  christos 		/*
   5424  1.1  christos 		 * Strictly speaking, this isn't necessary on BSD
   5425  1.1  christos 		 * kernels because they do checksum calculation after
   5426  1.1  christos 		 * this code has run BUT if ipfilter is being used
   5427  1.1  christos 		 * to do NAT as a bridge, that code doesn't exist.
   5428  1.1  christos 		 */
   5429  1.1  christos 		switch (nat->nat_dir)
   5430  1.1  christos 		{
   5431  1.1  christos 		case NAT_OUTBOUND :
   5432  1.1  christos 			ipf_fix_outcksum(fin, &fin->fin_ip->ip_sum,
   5433  1.1  christos 				     nat->nat_ipsumd);
   5434  1.1  christos 			break;
   5435  1.1  christos 
   5436  1.1  christos 		case NAT_INBOUND :
   5437  1.1  christos 			ipf_fix_incksum(fin, &fin->fin_ip->ip_sum,
   5438  1.1  christos 				    nat->nat_ipsumd);
   5439  1.1  christos 			break;
   5440  1.1  christos 
   5441  1.1  christos 		default :
   5442  1.1  christos 			break;
   5443  1.1  christos 		}
   5444  1.1  christos 	}
   5445  1.1  christos #endif
   5446  1.1  christos 
   5447  1.1  christos 	/*
   5448  1.1  christos 	 * Address assignment is after the checksum modification because
   5449  1.1  christos 	 * we are using the address in the packet for determining the
   5450  1.1  christos 	 * correct checksum offset (the ICMP error could be coming from
   5451  1.1  christos 	 * anyone...)
   5452  1.1  christos 	 */
   5453  1.1  christos 	switch (nat->nat_dir)
   5454  1.1  christos 	{
   5455  1.1  christos 	case NAT_OUTBOUND :
   5456  1.1  christos 		fin->fin_ip->ip_src = nat->nat_nsrcip;
   5457  1.1  christos 		fin->fin_saddr = nat->nat_nsrcaddr;
   5458  1.1  christos 		fin->fin_ip->ip_dst = nat->nat_ndstip;
   5459  1.1  christos 		fin->fin_daddr = nat->nat_ndstaddr;
   5460  1.1  christos 		break;
   5461  1.1  christos 
   5462  1.1  christos 	case NAT_INBOUND :
   5463  1.1  christos 		fin->fin_ip->ip_src = nat->nat_odstip;
   5464  1.1  christos 		fin->fin_saddr = nat->nat_ndstaddr;
   5465  1.1  christos 		fin->fin_ip->ip_dst = nat->nat_osrcip;
   5466  1.1  christos 		fin->fin_daddr = nat->nat_nsrcaddr;
   5467  1.1  christos 		break;
   5468  1.1  christos 
   5469  1.1  christos 	case NAT_ENCAPIN :
   5470  1.1  christos 		fin->fin_flx |= FI_ENCAP;
   5471  1.1  christos 	case NAT_DIVERTIN :
   5472  1.1  christos 	    {
   5473  1.1  christos 		mb_t *m;
   5474  1.1  christos 
   5475  1.1  christos 		skip = ipf_nat_decap(fin, nat);
   5476  1.1  christos 		if (skip <= 0) {
   5477  1.1  christos 			NBUMPSIDED(1, ns_decap_fail);
   5478  1.1  christos 			return -1;
   5479  1.1  christos 		}
   5480  1.1  christos 
   5481  1.1  christos 		m = fin->fin_m;
   5482  1.1  christos 
   5483  1.1  christos #if defined(MENTAT) && defined(_KERNEL)
   5484  1.1  christos 		m->b_rptr += skip;
   5485  1.1  christos #else
   5486  1.1  christos 		m->m_data += skip;
   5487  1.1  christos 		m->m_len -= skip;
   5488  1.1  christos 
   5489  1.1  christos # ifdef M_PKTHDR
   5490  1.1  christos 		if (m->m_flags & M_PKTHDR)
   5491  1.1  christos 			m->m_pkthdr.len -= skip;
   5492  1.1  christos # endif
   5493  1.1  christos #endif
   5494  1.1  christos 
   5495  1.1  christos 		MUTEX_ENTER(&nat->nat_lock);
   5496  1.1  christos 		ipf_nat_update(fin, nat);
   5497  1.1  christos 		MUTEX_EXIT(&nat->nat_lock);
   5498  1.1  christos 		fin->fin_flx |= FI_NATED;
   5499  1.1  christos 		if (np != NULL && np->in_tag.ipt_num[0] != 0)
   5500  1.1  christos 			fin->fin_nattag = &np->in_tag;
   5501  1.1  christos 		return 1;
   5502  1.1  christos 		/* NOTREACHED */
   5503  1.1  christos 	    }
   5504  1.1  christos 
   5505  1.1  christos 	case NAT_ENCAPOUT :
   5506  1.1  christos 	    {
   5507  1.1  christos 		u_32_t s1, s2, sumd;
   5508  1.1  christos 		ip_t *ip;
   5509  1.1  christos 		mb_t *m;
   5510  1.1  christos 
   5511  1.1  christos 		if (ipf_nat_encapok(fin, nat) == -1)
   5512  1.1  christos 			return -1;
   5513  1.1  christos 
   5514  1.1  christos 		m = M_DUP(np->in_divmp);
   5515  1.1  christos 		if (m == NULL) {
   5516  1.1  christos 			NBUMPSIDED(1, ns_encap_dup);
   5517  1.1  christos 			return -1;
   5518  1.1  christos 		}
   5519  1.1  christos 
   5520  1.1  christos 		ip = MTOD(m, ip_t *);
   5521  1.1  christos 		/* TRACE (fin,ip) */
   5522  1.1  christos 		ip->ip_off = (fin->fin_ip->ip_off & htons(IP_DF));
   5523  1.1  christos 		ip->ip_id = htons(ipf_nextipid(fin));
   5524  1.1  christos 		ip->ip_len = htons(fin->fin_plen + sizeof(ip_t));
   5525  1.1  christos 		s1 = 0;
   5526  1.1  christos 		/*
   5527  1.1  christos 		 * We subtract 40 here because ip_len has already been set
   5528  1.1  christos 		 * to this value when the template checksum is created.
   5529  1.1  christos 		 */
   5530  1.1  christos 		s2 = ntohs(ip->ip_id) + ntohs(ip->ip_len) - sizeof(ip_t);
   5531  1.1  christos 		s2 += ntohs(ip->ip_off) & IP_DF;
   5532  1.1  christos 		/* TRACE (s1,s2,ip) */
   5533  1.1  christos 		CALC_SUMD(s1, s2, sumd);
   5534  1.1  christos 		/* TRACE (sumd) */
   5535  1.1  christos 
   5536  1.1  christos #if !defined(_KERNEL) || defined(MENTAT) || defined(__sgi) || \
   5537  1.1  christos     defined(linux) || defined(BRIDGE_IPF)
   5538  1.1  christos 		ipf_fix_outcksum(fin, &ip->ip_sum, sumd);
   5539  1.1  christos #endif
   5540  1.1  christos 		/* TRACE (ip) */
   5541  1.1  christos 
   5542  1.1  christos 		PREP_MB_T(fin, m);
   5543  1.1  christos 
   5544  1.1  christos 		fin->fin_ip = ip;
   5545  1.1  christos 		fin->fin_plen += sizeof(ip_t);	/* UDP + new IPv4 hdr */
   5546  1.1  christos 		fin->fin_dlen += sizeof(ip_t);	/* UDP + old IPv4 hdr */
   5547  1.1  christos 		fin->fin_flx |= FI_ENCAP;
   5548  1.1  christos 
   5549  1.1  christos 		nflags &= ~IPN_TCPUDPICMP;
   5550  1.1  christos 
   5551  1.1  christos 		break;
   5552  1.1  christos 	    }
   5553  1.1  christos 	case NAT_DIVERTOUT :
   5554  1.1  christos 	    {
   5555  1.1  christos 		u_32_t s1, s2, sumd;
   5556  1.1  christos 		udphdr_t *uh;
   5557  1.1  christos 		ip_t *ip;
   5558  1.1  christos 		mb_t *m;
   5559  1.1  christos 
   5560  1.1  christos 		m = M_DUP(np->in_divmp);
   5561  1.1  christos 		if (m == NULL) {
   5562  1.1  christos 			NBUMPSIDED(1, ns_divert_dup);
   5563  1.1  christos 			return -1;
   5564  1.1  christos 		}
   5565  1.1  christos 
   5566  1.1  christos 		ip = MTOD(m, ip_t *);
   5567  1.1  christos 		ip->ip_id = htons(ipf_nextipid(fin));
   5568  1.1  christos 		s2 = ntohs(ip->ip_id);
   5569  1.1  christos 
   5570  1.1  christos 		s1 = ip->ip_len;
   5571  1.1  christos 		ip->ip_len = ntohs(ip->ip_len);
   5572  1.1  christos 		ip->ip_len += fin->fin_plen;
   5573  1.1  christos 		ip->ip_len = htons(ip->ip_len);
   5574  1.1  christos 		s2 += ntohs(ip->ip_len);
   5575  1.1  christos 		CALC_SUMD(s1, s2, sumd);
   5576  1.1  christos 
   5577  1.1  christos 		uh = (udphdr_t *)(ip + 1);
   5578  1.1  christos 		uh->uh_ulen += fin->fin_plen;
   5579  1.1  christos 		uh->uh_ulen = htons(uh->uh_ulen);
   5580  1.1  christos #if !defined(_KERNEL) || defined(MENTAT) || defined(__sgi) || \
   5581  1.1  christos     defined(linux) || defined(BRIDGE_IPF)
   5582  1.1  christos 		ipf_fix_outcksum(fin, &ip->ip_sum, sumd);
   5583  1.1  christos #endif
   5584  1.1  christos 
   5585  1.1  christos 		PREP_MB_T(fin, m);
   5586  1.1  christos 
   5587  1.1  christos 		fin->fin_src = ip->ip_src;
   5588  1.1  christos 		fin->fin_dst = ip->ip_dst;
   5589  1.1  christos 		fin->fin_ip = ip;
   5590  1.1  christos 		fin->fin_plen += sizeof(ip_t) + 8;	/* UDP + IPv4 hdr */
   5591  1.1  christos 		fin->fin_dlen += sizeof(ip_t) + 8;	/* UDP + IPv4 hdr */
   5592  1.1  christos 
   5593  1.1  christos 		nflags &= ~IPN_TCPUDPICMP;
   5594  1.1  christos 
   5595  1.1  christos 		break;
   5596  1.1  christos 	    }
   5597  1.1  christos 
   5598  1.1  christos 	default :
   5599  1.1  christos 		break;
   5600  1.1  christos 	}
   5601  1.1  christos 
   5602  1.1  christos 	if (!(fin->fin_flx & FI_SHORT) && (fin->fin_off == 0)) {
   5603  1.1  christos 		u_short *csump;
   5604  1.1  christos 
   5605  1.1  christos 		if ((nat->nat_nsport != 0) && (nflags & IPN_TCPUDP)) {
   5606  1.1  christos 			tcp = fin->fin_dp;
   5607  1.1  christos 
   5608  1.1  christos 			switch (nat->nat_dir)
   5609  1.1  christos 			{
   5610  1.1  christos 			case NAT_OUTBOUND :
   5611  1.1  christos 				tcp->th_sport = nat->nat_nsport;
   5612  1.1  christos 				fin->fin_data[0] = ntohs(nat->nat_nsport);
   5613  1.1  christos 				tcp->th_dport = nat->nat_ndport;
   5614  1.1  christos 				fin->fin_data[0] = ntohs(nat->nat_ndport);
   5615  1.1  christos 				break;
   5616  1.1  christos 
   5617  1.1  christos 			case NAT_INBOUND :
   5618  1.1  christos 				tcp->th_sport = nat->nat_odport;
   5619  1.1  christos 				fin->fin_data[0] = ntohs(nat->nat_odport);
   5620  1.1  christos 				tcp->th_dport = nat->nat_osport;
   5621  1.1  christos 				fin->fin_data[0] = ntohs(nat->nat_osport);
   5622  1.1  christos 				break;
   5623  1.1  christos 			}
   5624  1.1  christos 		}
   5625  1.1  christos 
   5626  1.1  christos 		if ((nat->nat_nsport != 0) && (nflags & IPN_ICMPQUERY)) {
   5627  1.1  christos 			icmp = fin->fin_dp;
   5628  1.1  christos 			icmp->icmp_id = nat->nat_nicmpid;
   5629  1.1  christos 		}
   5630  1.1  christos 
   5631  1.1  christos 		csump = ipf_nat_proto(fin, nat, nflags);
   5632  1.1  christos 
   5633  1.1  christos 		/*
   5634  1.1  christos 		 * The above comments do not hold for layer 4 (or higher)
   5635  1.1  christos 		 * checksums...
   5636  1.1  christos 		 */
   5637  1.1  christos 		if (csump != NULL) {
   5638  1.1  christos 			if (nat->nat_dir == NAT_OUTBOUND)
   5639  1.1  christos 				ipf_fix_outcksum(fin, csump, nat->nat_sumd[1]);
   5640  1.1  christos 			else
   5641  1.1  christos 				ipf_fix_incksum(fin, csump, nat->nat_sumd[1]);
   5642  1.1  christos 		}
   5643  1.1  christos 	}
   5644  1.1  christos 
   5645  1.1  christos 	ipf_sync_update(softc, SMC_NAT, fin, nat->nat_sync);
   5646  1.1  christos 	/* ------------------------------------------------------------- */
   5647  1.1  christos 	/* A few quick notes:                                            */
   5648  1.1  christos 	/*      Following are test conditions prior to calling the       */
   5649  1.1  christos 	/*      ipf_proxy_check routine.                                 */
   5650  1.1  christos 	/*                                                               */
   5651  1.1  christos 	/*      A NULL tcp indicates a non TCP/UDP packet.  When dealing */
   5652  1.1  christos 	/*      with a redirect rule, we attempt to match the packet's   */
   5653  1.1  christos 	/*      source port against in_dport, otherwise we'd compare the */
   5654  1.1  christos 	/*      packet's destination.                                    */
   5655  1.1  christos 	/* ------------------------------------------------------------- */
   5656  1.1  christos 	if ((np != NULL) && (np->in_apr != NULL)) {
   5657  1.1  christos 		i = ipf_proxy_check(fin, nat);
   5658  1.1  christos 		if (i == 0)
   5659  1.1  christos 			i = 1;
   5660  1.1  christos 		else if (i == -1) {
   5661  1.1  christos 			NBUMPSIDED(1, ns_ipf_proxy_fail);
   5662  1.1  christos 		}
   5663  1.1  christos 	} else {
   5664  1.1  christos 		i = 1;
   5665  1.1  christos 	}
   5666  1.1  christos 	fin->fin_flx |= FI_NATED;
   5667  1.1  christos 	return i;
   5668  1.1  christos }
   5669  1.1  christos 
   5670  1.1  christos 
   5671  1.1  christos /* ------------------------------------------------------------------------ */
   5672  1.1  christos /* Function:    ipf_nat_ipfin                                               */
   5673  1.1  christos /* Returns:     frentry_t* - NULL (packet may have been translated, let it  */
   5674  1.1  christos /*                           pass), &ipfnatblock - block/drop the packet.   */
   5675  1.1  christos /* Parameters:  fin(I)   - pointer to packet information                    */
   5676  1.1  christos /*              passp(I) - point to filtering result flags                  */
   5677  1.1  christos /*                                                                          */
   5678  1.1  christos /* This is purely and simply a wrapper around ipf_nat_checkin for the sole  */
   5679  1.1  christos /* reason of being able to activate NAT from an ipf rule using "call-now".  */
   5680  1.1  christos /* ------------------------------------------------------------------------ */
   5681  1.1  christos frentry_t *
   5682  1.1  christos ipf_nat_ipfin(fin, passp)
   5683  1.1  christos 	fr_info_t *fin;
   5684  1.1  christos 	u_32_t *passp;
   5685  1.1  christos {
   5686  1.1  christos 	frentry_t *fr = fin->fin_fr;
   5687  1.1  christos 
   5688  1.1  christos 	if (fin->fin_v == 6) {
   5689  1.1  christos #ifdef USE_INET6
   5690  1.1  christos 		return ipf_nat6_ipfin(fin, passp);
   5691  1.1  christos #else
   5692  1.1  christos 		return NULL;
   5693  1.1  christos #endif
   5694  1.1  christos 	}
   5695  1.1  christos 
   5696  1.1  christos 	switch (ipf_nat_checkin(fin, passp))
   5697  1.1  christos 	{
   5698  1.1  christos 	case -1 :
   5699  1.1  christos 		fr = &ipfnatblock;
   5700  1.1  christos 		MUTEX_ENTER(&fr->fr_lock);
   5701  1.1  christos 		fr->fr_ref++;
   5702  1.1  christos 		MUTEX_EXIT(&fr->fr_lock);
   5703  1.1  christos 		return fr;
   5704  1.1  christos 
   5705  1.1  christos 	case 0 :
   5706  1.1  christos 		break;
   5707  1.1  christos 
   5708  1.1  christos 	case 1 :
   5709  1.1  christos 		/*
   5710  1.1  christos 		 * Returing NULL causes this rule to be "ignored" but
   5711  1.1  christos 		 * it has actually had an influence on the packet so we
   5712  1.1  christos 		 * increment counters for it.
   5713  1.1  christos 		 */
   5714  1.1  christos 		fr->fr_bytes += (U_QUAD_T)fin->fin_plen;
   5715  1.1  christos 		fr->fr_hits++;
   5716  1.1  christos 		break;
   5717  1.1  christos 	}
   5718  1.1  christos 
   5719  1.1  christos 	return NULL;
   5720  1.1  christos }
   5721  1.1  christos 
   5722  1.1  christos 
   5723  1.1  christos /* ------------------------------------------------------------------------ */
   5724  1.1  christos /* Function:    ipf_nat_checkin                                             */
   5725  1.1  christos /* Returns:     int - -1 == packet failed NAT checks so block it,           */
   5726  1.1  christos /*                     0 == no packet translation occurred,                 */
   5727  1.1  christos /*                     1 == packet was successfully translated.             */
   5728  1.1  christos /* Parameters:  fin(I)   - pointer to packet information                    */
   5729  1.1  christos /*              passp(I) - pointer to filtering result flags                */
   5730  1.1  christos /*                                                                          */
   5731  1.1  christos /* Check to see if an incoming packet should be changed.  ICMP packets are  */
   5732  1.1  christos /* first checked to see if they match an existing entry (if an error),      */
   5733  1.1  christos /* otherwise a search of the current NAT table is made.  If neither results */
   5734  1.1  christos /* in a match then a search for a matching NAT rule is made.  Create a new  */
   5735  1.1  christos /* NAT entry if a we matched a NAT rule.  Lastly, actually change the       */
   5736  1.1  christos /* packet header(s) as required.                                            */
   5737  1.1  christos /* ------------------------------------------------------------------------ */
   5738  1.1  christos int
   5739  1.1  christos ipf_nat_checkin(fin, passp)
   5740  1.1  christos 	fr_info_t *fin;
   5741  1.1  christos 	u_32_t *passp;
   5742  1.1  christos {
   5743  1.1  christos 	ipf_main_softc_t *softc;
   5744  1.1  christos 	ipf_nat_softc_t *softn;
   5745  1.1  christos 	u_int nflags, natadd;
   5746  1.1  christos 	ipnat_t *np, *npnext;
   5747  1.1  christos 	int rval, natfailed;
   5748  1.1  christos 	struct ifnet *ifp;
   5749  1.1  christos 	struct in_addr in;
   5750  1.1  christos 	icmphdr_t *icmp;
   5751  1.1  christos 	tcphdr_t *tcp;
   5752  1.1  christos 	u_short dport;
   5753  1.1  christos 	nat_t *nat;
   5754  1.1  christos 	u_32_t iph;
   5755  1.1  christos 
   5756  1.1  christos 	softc = fin->fin_main_soft;
   5757  1.1  christos 	softn = softc->ipf_nat_soft;
   5758  1.1  christos 
   5759  1.1  christos 	if (softn->ipf_nat_lock != 0)
   5760  1.1  christos 		return 0;
   5761  1.1  christos 	if (softn->ipf_nat_stats.ns_rules == 0 &&
   5762  1.1  christos 	    softn->ipf_nat_instances == NULL)
   5763  1.1  christos 		return 0;
   5764  1.1  christos 
   5765  1.1  christos 	tcp = NULL;
   5766  1.1  christos 	icmp = NULL;
   5767  1.1  christos 	dport = 0;
   5768  1.1  christos 	natadd = 1;
   5769  1.1  christos 	nflags = 0;
   5770  1.1  christos 	natfailed = 0;
   5771  1.1  christos 	ifp = fin->fin_ifp;
   5772  1.1  christos 
   5773  1.1  christos 	if (!(fin->fin_flx & FI_SHORT) && (fin->fin_off == 0)) {
   5774  1.1  christos 		switch (fin->fin_p)
   5775  1.1  christos 		{
   5776  1.1  christos 		case IPPROTO_TCP :
   5777  1.1  christos 			nflags = IPN_TCP;
   5778  1.1  christos 			break;
   5779  1.1  christos 		case IPPROTO_UDP :
   5780  1.1  christos 			nflags = IPN_UDP;
   5781  1.1  christos 			break;
   5782  1.1  christos 		case IPPROTO_ICMP :
   5783  1.1  christos 			icmp = fin->fin_dp;
   5784  1.1  christos 
   5785  1.1  christos 			/*
   5786  1.1  christos 			 * This is an incoming packet, so the destination is
   5787  1.1  christos 			 * the icmp_id and the source port equals 0
   5788  1.1  christos 			 */
   5789  1.1  christos 			if ((fin->fin_flx & FI_ICMPQUERY) != 0) {
   5790  1.1  christos 				nflags = IPN_ICMPQUERY;
   5791  1.1  christos 				dport = icmp->icmp_id;
   5792  1.1  christos 			} break;
   5793  1.1  christos 		default :
   5794  1.1  christos 			break;
   5795  1.1  christos 		}
   5796  1.1  christos 
   5797  1.1  christos 		if ((nflags & IPN_TCPUDP)) {
   5798  1.1  christos 			tcp = fin->fin_dp;
   5799  1.1  christos 			dport = fin->fin_data[1];
   5800  1.1  christos 		}
   5801  1.1  christos 	}
   5802  1.1  christos 
   5803  1.1  christos 	in = fin->fin_dst;
   5804  1.1  christos 
   5805  1.1  christos 	READ_ENTER(&softc->ipf_nat);
   5806  1.1  christos 
   5807  1.1  christos 	if ((fin->fin_p == IPPROTO_ICMP) && !(nflags & IPN_ICMPQUERY) &&
   5808  1.1  christos 	    (nat = ipf_nat_icmperror(fin, &nflags, NAT_INBOUND)))
   5809  1.1  christos 		/*EMPTY*/;
   5810  1.1  christos 	else if ((fin->fin_flx & FI_FRAG) && (nat = ipf_frag_natknown(fin)))
   5811  1.1  christos 		natadd = 0;
   5812  1.1  christos 	else if ((nat = ipf_nat_inlookup(fin, nflags|NAT_SEARCH,
   5813  1.1  christos 					 (u_int)fin->fin_p,
   5814  1.1  christos 					 fin->fin_src, in))) {
   5815  1.1  christos 		nflags = nat->nat_flags;
   5816  1.1  christos 	} else if (fin->fin_off == 0) {
   5817  1.1  christos 		u_32_t hv, msk, rmsk = 0;
   5818  1.1  christos 
   5819  1.1  christos 		/*
   5820  1.1  christos 		 * If there is no current entry in the nat table for this IP#,
   5821  1.1  christos 		 * create one for it (if there is a matching rule).
   5822  1.1  christos 		 */
   5823  1.1  christos maskloop:
   5824  1.1  christos 		msk = softn->ipf_nat_rdr_active_masks[rmsk];
   5825  1.1  christos 		iph = in.s_addr & msk;
   5826  1.1  christos 		hv = NAT_HASH_FN(iph, 0, softn->ipf_nat_rdrrules_sz);
   5827  1.1  christos retry_roundrobin:
   5828  1.1  christos 		/* TRACE (iph,msk,rmsk,hv,softn->ipf_nat_rdrrules_sz) */
   5829  1.1  christos 		for (np = softn->ipf_nat_rdr_rules[hv]; np; np = npnext) {
   5830  1.1  christos 			npnext = np->in_rnext;
   5831  1.1  christos 			if (np->in_ifps[0] && (np->in_ifps[0] != ifp))
   5832  1.1  christos 				continue;
   5833  1.1  christos 			if (np->in_v[0] != 4)
   5834  1.1  christos 				continue;
   5835  1.1  christos 			if (np->in_pr[0] && (np->in_pr[0] != fin->fin_p))
   5836  1.1  christos 				continue;
   5837  1.1  christos 			if ((np->in_flags & IPN_RF) && !(np->in_flags & nflags))
   5838  1.1  christos 				continue;
   5839  1.1  christos 			if (np->in_flags & IPN_FILTER) {
   5840  1.1  christos 				switch (ipf_nat_match(fin, np))
   5841  1.1  christos 				{
   5842  1.1  christos 				case 0 :
   5843  1.1  christos 					continue;
   5844  1.1  christos 				case -1 :
   5845  1.1  christos 					rval = -1;
   5846  1.1  christos 					goto inmatchfail;
   5847  1.1  christos 				case 1 :
   5848  1.1  christos 				default :
   5849  1.1  christos 					break;
   5850  1.1  christos 				}
   5851  1.1  christos 			} else {
   5852  1.1  christos 				if ((in.s_addr & np->in_odstmsk) !=
   5853  1.1  christos 				    np->in_odstaddr)
   5854  1.1  christos 					continue;
   5855  1.1  christos 				if (np->in_odport &&
   5856  1.1  christos 				    ((np->in_dtop < dport) ||
   5857  1.1  christos 				     (dport < np->in_odport)))
   5858  1.1  christos 					continue;
   5859  1.1  christos 			}
   5860  1.1  christos 
   5861  1.1  christos 			if (np->in_plabel != -1) {
   5862  1.1  christos 				if (!ipf_proxy_ok(fin, tcp, np)) {
   5863  1.1  christos 					continue;
   5864  1.1  christos 				}
   5865  1.1  christos 			}
   5866  1.1  christos 
   5867  1.1  christos 			if (np->in_flags & IPN_NO) {
   5868  1.1  christos 				np->in_hits++;
   5869  1.1  christos 				break;
   5870  1.1  christos 			}
   5871  1.1  christos 
   5872  1.1  christos 			MUTEX_ENTER(&softn->ipf_nat_new);
   5873  1.1  christos 			/*
   5874  1.1  christos 			 * If we've matched a round-robin rule but it has
   5875  1.1  christos 			 * moved in the list since we got it, start over as
   5876  1.1  christos 			 * this is now no longer correct.
   5877  1.1  christos 			 */
   5878  1.1  christos 			if (npnext != np->in_rnext) {
   5879  1.1  christos 				if ((np->in_flags & IPN_ROUNDR) != 0) {
   5880  1.1  christos 					MUTEX_EXIT(&softn->ipf_nat_new);
   5881  1.1  christos 					goto retry_roundrobin;
   5882  1.1  christos 				}
   5883  1.1  christos 				npnext = np->in_rnext;
   5884  1.1  christos 			}
   5885  1.1  christos 
   5886  1.1  christos 			nat = ipf_nat_add(fin, np, NULL, nflags, NAT_INBOUND);
   5887  1.1  christos 			MUTEX_EXIT(&softn->ipf_nat_new);
   5888  1.1  christos 			if (nat != NULL) {
   5889  1.1  christos 				natfailed = 0;
   5890  1.1  christos 				break;
   5891  1.1  christos 			}
   5892  1.1  christos 			natfailed = -1;
   5893  1.1  christos 		}
   5894  1.1  christos 
   5895  1.1  christos 		if ((np == NULL) && (rmsk < softn->ipf_nat_rdr_max)) {
   5896  1.1  christos 			rmsk++;
   5897  1.1  christos 			goto maskloop;
   5898  1.1  christos 		}
   5899  1.1  christos 	}
   5900  1.1  christos 	if (nat != NULL) {
   5901  1.1  christos 		rval = ipf_nat_in(fin, nat, natadd, nflags);
   5902  1.1  christos 		if (rval == 1) {
   5903  1.1  christos 			MUTEX_ENTER(&nat->nat_lock);
   5904  1.1  christos 			ipf_nat_update(fin, nat);
   5905  1.1  christos 			nat->nat_bytes[0] += fin->fin_plen;
   5906  1.1  christos 			nat->nat_pkts[0]++;
   5907  1.1  christos 			fin->fin_pktnum = nat->nat_pkts[0];
   5908  1.1  christos 			MUTEX_EXIT(&nat->nat_lock);
   5909  1.1  christos 		}
   5910  1.1  christos 	} else
   5911  1.1  christos 		rval = natfailed;
   5912  1.1  christos inmatchfail:
   5913  1.1  christos 	RWLOCK_EXIT(&softc->ipf_nat);
   5914  1.1  christos 
   5915  1.1  christos 	switch (rval)
   5916  1.1  christos 	{
   5917  1.1  christos 	case -1 :
   5918  1.1  christos 		if (passp != NULL) {
   5919  1.1  christos 			DT1(frb_natv4in, fr_info_t *, fin);
   5920  1.1  christos 			NBUMPSIDED(0, ns_drop);
   5921  1.1  christos 			*passp = FR_BLOCK;
   5922  1.1  christos 			fin->fin_reason = FRB_NATV4IN;
   5923  1.1  christos 		}
   5924  1.1  christos 		fin->fin_flx |= FI_BADNAT;
   5925  1.1  christos 		NBUMPSIDED(0, ns_badnat);
   5926  1.1  christos 		break;
   5927  1.1  christos 	case 0 :
   5928  1.1  christos 		NBUMPSIDE(0, ns_ignored);
   5929  1.1  christos 		break;
   5930  1.1  christos 	case 1 :
   5931  1.1  christos 		NBUMPSIDE(0, ns_translated);
   5932  1.1  christos 		break;
   5933  1.1  christos 	}
   5934  1.1  christos 	return rval;
   5935  1.1  christos }
   5936  1.1  christos 
   5937  1.1  christos 
   5938  1.1  christos /* ------------------------------------------------------------------------ */
   5939  1.1  christos /* Function:    ipf_nat_in                                                  */
   5940  1.1  christos /* Returns:     int - -1 == packet failed NAT checks so block it,           */
   5941  1.1  christos /*                     1 == packet was successfully translated.             */
   5942  1.1  christos /* Parameters:  fin(I)    - pointer to packet information                   */
   5943  1.1  christos /*              nat(I)    - pointer to NAT structure                        */
   5944  1.1  christos /*              natadd(I) - flag indicating if it is safe to add frag cache */
   5945  1.1  christos /*              nflags(I) - NAT flags set for this packet                   */
   5946  1.1  christos /* Locks Held:  ipf_nat(READ)                                               */
   5947  1.1  christos /*                                                                          */
   5948  1.1  christos /* Translate a packet coming "in" on an interface.                          */
   5949  1.1  christos /* ------------------------------------------------------------------------ */
   5950  1.1  christos int
   5951  1.1  christos ipf_nat_in(fin, nat, natadd, nflags)
   5952  1.1  christos 	fr_info_t *fin;
   5953  1.1  christos 	nat_t *nat;
   5954  1.1  christos 	int natadd;
   5955  1.1  christos 	u_32_t nflags;
   5956  1.1  christos {
   5957  1.1  christos 	ipf_main_softc_t *softc = fin->fin_main_soft;
   5958  1.1  christos 	ipf_nat_softc_t *softn = softc->ipf_nat_soft;
   5959  1.1  christos 	u_32_t sumd, ipsumd, sum1, sum2;
   5960  1.1  christos 	icmphdr_t *icmp;
   5961  1.1  christos 	tcphdr_t *tcp;
   5962  1.1  christos 	ipnat_t *np;
   5963  1.1  christos 	int skip;
   5964  1.1  christos 	int i;
   5965  1.1  christos 
   5966  1.1  christos 	tcp = NULL;
   5967  1.1  christos 	np = nat->nat_ptr;
   5968  1.1  christos 	fin->fin_fr = nat->nat_fr;
   5969  1.1  christos 
   5970  1.1  christos 	if (np != NULL) {
   5971  1.1  christos 		if ((natadd != 0) && (fin->fin_flx & FI_FRAG))
   5972  1.1  christos 			(void) ipf_frag_natnew(softc, fin, 0, nat);
   5973  1.1  christos 
   5974  1.1  christos 	/* ------------------------------------------------------------- */
   5975  1.1  christos 	/* A few quick notes:                                            */
   5976  1.1  christos 	/*      Following are test conditions prior to calling the       */
   5977  1.1  christos 	/*      ipf_proxy_check routine.                                 */
   5978  1.1  christos 	/*                                                               */
   5979  1.1  christos 	/*      A NULL tcp indicates a non TCP/UDP packet.  When dealing */
   5980  1.1  christos 	/*      with a map rule, we attempt to match the packet's        */
   5981  1.1  christos 	/*      source port against in_dport, otherwise we'd compare the */
   5982  1.1  christos 	/*      packet's destination.                                    */
   5983  1.1  christos 	/* ------------------------------------------------------------- */
   5984  1.1  christos 		if (np->in_apr != NULL) {
   5985  1.1  christos 			i = ipf_proxy_check(fin, nat);
   5986  1.1  christos 			if (i == -1) {
   5987  1.1  christos 				NBUMPSIDED(0, ns_ipf_proxy_fail);
   5988  1.1  christos 				return -1;
   5989  1.1  christos 			}
   5990  1.1  christos 		}
   5991  1.1  christos 	}
   5992  1.1  christos 
   5993  1.1  christos 	ipf_sync_update(softc, SMC_NAT, fin, nat->nat_sync);
   5994  1.1  christos 
   5995  1.1  christos 	ipsumd = nat->nat_ipsumd;
   5996  1.1  christos 	/*
   5997  1.1  christos 	 * Fix up checksums, not by recalculating them, but
   5998  1.1  christos 	 * simply computing adjustments.
   5999  1.1  christos 	 * Why only do this for some platforms on inbound packets ?
   6000  1.1  christos 	 * Because for those that it is done, IP processing is yet to happen
   6001  1.1  christos 	 * and so the IPv4 header checksum has not yet been evaluated.
   6002  1.1  christos 	 * Perhaps it should always be done for the benefit of things like
   6003  1.1  christos 	 * fast forwarding (so that it doesn't need to be recomputed) but with
   6004  1.1  christos 	 * header checksum offloading, perhaps it is a moot point.
   6005  1.1  christos 	 */
   6006  1.1  christos 
   6007  1.1  christos 	switch (nat->nat_dir)
   6008  1.1  christos 	{
   6009  1.1  christos 	case NAT_INBOUND :
   6010  1.1  christos 		if ((fin->fin_flx & FI_ICMPERR) == 0) {
   6011  1.1  christos 			fin->fin_ip->ip_src = nat->nat_nsrcip;
   6012  1.1  christos 			fin->fin_saddr = nat->nat_nsrcaddr;
   6013  1.1  christos 		} else {
   6014  1.1  christos 			sum1 = nat->nat_osrcaddr;
   6015  1.1  christos 			sum2 = nat->nat_nsrcaddr;
   6016  1.1  christos 			CALC_SUMD(sum1, sum2, sumd);
   6017  1.1  christos 			ipsumd -= sumd;
   6018  1.1  christos 		}
   6019  1.1  christos 		fin->fin_ip->ip_dst = nat->nat_ndstip;
   6020  1.1  christos 		fin->fin_daddr = nat->nat_ndstaddr;
   6021  1.1  christos #if !defined(_KERNEL) || defined(MENTAT) || defined(__sgi) || \
   6022  1.1  christos      defined(__osf__) || defined(linux)
   6023  1.1  christos 		ipf_fix_outcksum(fin, &fin->fin_ip->ip_sum, ipsumd);
   6024  1.1  christos #endif
   6025  1.1  christos 		break;
   6026  1.1  christos 
   6027  1.1  christos 	case NAT_OUTBOUND :
   6028  1.1  christos 		if ((fin->fin_flx & FI_ICMPERR) == 0) {
   6029  1.1  christos 			fin->fin_ip->ip_src = nat->nat_odstip;
   6030  1.1  christos 			fin->fin_saddr = nat->nat_odstaddr;
   6031  1.1  christos 		} else {
   6032  1.1  christos 			sum1 = nat->nat_odstaddr;
   6033  1.1  christos 			sum2 = nat->nat_ndstaddr;
   6034  1.1  christos 			CALC_SUMD(sum1, sum2, sumd);
   6035  1.1  christos 			ipsumd -= sumd;
   6036  1.1  christos 		}
   6037  1.1  christos 		fin->fin_ip->ip_dst = nat->nat_osrcip;
   6038  1.1  christos 		fin->fin_daddr = nat->nat_osrcaddr;
   6039  1.1  christos #if !defined(_KERNEL) || defined(MENTAT) || defined(__sgi) || \
   6040  1.1  christos      defined(__osf__) || defined(linux)
   6041  1.1  christos 		ipf_fix_incksum(fin, &fin->fin_ip->ip_sum, ipsumd);
   6042  1.1  christos #endif
   6043  1.1  christos 		break;
   6044  1.1  christos 
   6045  1.1  christos 	case NAT_ENCAPIN :
   6046  1.1  christos 	    {
   6047  1.1  christos 		ip_t *ip;
   6048  1.1  christos 		mb_t *m;
   6049  1.1  christos 
   6050  1.1  christos 		/*
   6051  1.1  christos 		 * XXX
   6052  1.1  christos 		 * This is not necessarily true.  What we need to know here
   6053  1.1  christos 		 * is the MTU of the interface out which the packets will go
   6054  1.1  christos 		 * and this won't be nat_ifps[1] because that is where we
   6055  1.1  christos 		 * send packets after stripping off stuff - what's needed
   6056  1.1  christos 		 * here is the MTU of the interface for the route to the
   6057  1.1  christos 		 * destination of the outer header.
   6058  1.1  christos 		 */
   6059  1.1  christos 		if (ipf_nat_encapok(fin, nat) == -1)
   6060  1.1  christos 			return -1;
   6061  1.1  christos 
   6062  1.1  christos 		m = M_DUP(np->in_divmp);
   6063  1.1  christos 		if (m == NULL) {
   6064  1.1  christos 			NBUMPSIDED(0, ns_encap_dup);
   6065  1.1  christos 			return -1;
   6066  1.1  christos 		}
   6067  1.1  christos 
   6068  1.1  christos 		ip = MTOD(m, ip_t *);
   6069  1.1  christos 		ip->ip_id = htons(ipf_nextipid(fin));
   6070  1.1  christos 		sum1 = ntohs(ip->ip_len);
   6071  1.1  christos 		ip->ip_len = htons(fin->fin_plen + sizeof(ip_t));
   6072  1.1  christos 		sum2 = ntohs(ip->ip_id) + ntohs(ip->ip_len);
   6073  1.1  christos 		CALC_SUMD(sum1, sum2, sumd);
   6074  1.1  christos 
   6075  1.1  christos #if !defined(_KERNEL) || defined(MENTAT) || defined(__sgi) || \
   6076  1.1  christos      defined(__osf__) || defined(linux)
   6077  1.1  christos 		ipf_fix_outcksum(fin, &ip->ip_sum, sumd);
   6078  1.1  christos #endif
   6079  1.1  christos 
   6080  1.1  christos 		PREP_MB_T(fin, m);
   6081  1.1  christos 
   6082  1.1  christos 		fin->fin_ip = ip;
   6083  1.1  christos 		fin->fin_plen += sizeof(ip_t);	/* UDP + new IPv4 hdr */
   6084  1.1  christos 		fin->fin_dlen += sizeof(ip_t);	/* UDP + old IPv4 hdr */
   6085  1.1  christos 		fin->fin_flx |= FI_ENCAP;
   6086  1.1  christos 
   6087  1.1  christos 		nflags &= ~IPN_TCPUDPICMP;
   6088  1.1  christos 
   6089  1.1  christos 		break;
   6090  1.1  christos 	    }
   6091  1.1  christos 
   6092  1.1  christos 	case NAT_DIVERTIN :
   6093  1.1  christos 	    {
   6094  1.1  christos 		udphdr_t *uh;
   6095  1.1  christos 		ip_t *ip;
   6096  1.1  christos 		mb_t *m;
   6097  1.1  christos 
   6098  1.1  christos 		m = M_DUP(np->in_divmp);
   6099  1.1  christos 		if (m == NULL) {
   6100  1.1  christos 			NBUMPSIDED(0, ns_divert_dup);
   6101  1.1  christos 			return -1;
   6102  1.1  christos 		}
   6103  1.1  christos 
   6104  1.1  christos 		ip = MTOD(m, ip_t *);
   6105  1.1  christos 		ip->ip_id = htons(ipf_nextipid(fin));
   6106  1.1  christos 		sum1 = ntohs(ip->ip_len);
   6107  1.1  christos 		ip->ip_len = ntohs(ip->ip_len);
   6108  1.1  christos 		ip->ip_len += fin->fin_plen;
   6109  1.1  christos 		ip->ip_len = htons(ip->ip_len);
   6110  1.1  christos 
   6111  1.1  christos 		uh = (udphdr_t *)(ip + 1);
   6112  1.1  christos 		uh->uh_ulen += fin->fin_plen;
   6113  1.1  christos 		uh->uh_ulen = htons(uh->uh_ulen);
   6114  1.1  christos 
   6115  1.1  christos 		sum2 = ntohs(ip->ip_id) + ntohs(ip->ip_len);
   6116  1.1  christos 		sum2 += ntohs(ip->ip_off) & IP_DF;
   6117  1.1  christos 		CALC_SUMD(sum1, sum2, sumd);
   6118  1.1  christos 
   6119  1.1  christos #if !defined(_KERNEL) || defined(MENTAT) || defined(__sgi) || \
   6120  1.1  christos      defined(__osf__) || defined(linux)
   6121  1.1  christos 		ipf_fix_outcksum(fin, &ip->ip_sum, sumd);
   6122  1.1  christos #endif
   6123  1.1  christos 		PREP_MB_T(fin, m);
   6124  1.1  christos 
   6125  1.1  christos 		fin->fin_ip = ip;
   6126  1.1  christos 		fin->fin_plen += sizeof(ip_t) + 8;	/* UDP + new IPv4 hdr */
   6127  1.1  christos 		fin->fin_dlen += sizeof(ip_t) + 8;	/* UDP + old IPv4 hdr */
   6128  1.1  christos 
   6129  1.1  christos 		nflags &= ~IPN_TCPUDPICMP;
   6130  1.1  christos 
   6131  1.1  christos 		break;
   6132  1.1  christos 	    }
   6133  1.1  christos 
   6134  1.1  christos 	case NAT_ENCAPOUT :
   6135  1.1  christos 		fin->fin_flx |= FI_ENCAP;
   6136  1.1  christos 	case NAT_DIVERTOUT :
   6137  1.1  christos 	    {
   6138  1.1  christos 		mb_t *m;
   6139  1.1  christos 
   6140  1.1  christos 		skip = ipf_nat_decap(fin, nat);
   6141  1.1  christos 		if (skip <= 0) {
   6142  1.1  christos 			NBUMPSIDED(0, ns_decap_fail);
   6143  1.1  christos 			return -1;
   6144  1.1  christos 		}
   6145  1.1  christos 
   6146  1.1  christos 		m = fin->fin_m;
   6147  1.1  christos 
   6148  1.1  christos #if defined(MENTAT) && defined(_KERNEL)
   6149  1.1  christos 		m->b_rptr += skip;
   6150  1.1  christos #else
   6151  1.1  christos 		m->m_data += skip;
   6152  1.1  christos 		m->m_len -= skip;
   6153  1.1  christos 
   6154  1.1  christos # ifdef M_PKTHDR
   6155  1.1  christos 		if (m->m_flags & M_PKTHDR)
   6156  1.1  christos 			m->m_pkthdr.len -= skip;
   6157  1.1  christos # endif
   6158  1.1  christos #endif
   6159  1.1  christos 
   6160  1.1  christos 		ipf_nat_update(fin, nat);
   6161  1.1  christos 		nflags &= ~IPN_TCPUDPICMP;
   6162  1.1  christos 		fin->fin_flx |= FI_NATED;
   6163  1.1  christos 		if (np != NULL && np->in_tag.ipt_num[0] != 0)
   6164  1.1  christos 			fin->fin_nattag = &np->in_tag;
   6165  1.1  christos 		return 1;
   6166  1.1  christos 		/* NOTREACHED */
   6167  1.1  christos 	    }
   6168  1.1  christos 	}
   6169  1.1  christos 	if (nflags & IPN_TCPUDP)
   6170  1.1  christos 		tcp = fin->fin_dp;
   6171  1.1  christos 
   6172  1.1  christos 	if (!(fin->fin_flx & FI_SHORT) && (fin->fin_off == 0)) {
   6173  1.1  christos 		u_short *csump;
   6174  1.1  christos 
   6175  1.1  christos 		if ((nat->nat_odport != 0) && (nflags & IPN_TCPUDP)) {
   6176  1.1  christos 			switch (nat->nat_dir)
   6177  1.1  christos 			{
   6178  1.1  christos 			case NAT_INBOUND :
   6179  1.1  christos 				tcp->th_sport = nat->nat_nsport;
   6180  1.1  christos 				fin->fin_data[0] = ntohs(nat->nat_nsport);
   6181  1.1  christos 				tcp->th_dport = nat->nat_ndport;
   6182  1.1  christos 				fin->fin_data[1] = ntohs(nat->nat_ndport);
   6183  1.1  christos 				break;
   6184  1.1  christos 
   6185  1.1  christos 			case NAT_OUTBOUND :
   6186  1.1  christos 				tcp->th_sport = nat->nat_odport;
   6187  1.1  christos 				fin->fin_data[0] = ntohs(nat->nat_odport);
   6188  1.1  christos 				tcp->th_dport = nat->nat_osport;
   6189  1.1  christos 				fin->fin_data[1] = ntohs(nat->nat_osport);
   6190  1.1  christos 				break;
   6191  1.1  christos 			}
   6192  1.1  christos 		}
   6193  1.1  christos 
   6194  1.1  christos 
   6195  1.1  christos 		if ((nat->nat_odport != 0) && (nflags & IPN_ICMPQUERY)) {
   6196  1.1  christos 			icmp = fin->fin_dp;
   6197  1.1  christos 
   6198  1.1  christos 			icmp->icmp_id = nat->nat_nicmpid;
   6199  1.1  christos 		}
   6200  1.1  christos 
   6201  1.1  christos 		csump = ipf_nat_proto(fin, nat, nflags);
   6202  1.1  christos 
   6203  1.1  christos 		/*
   6204  1.1  christos 		 * The above comments do not hold for layer 4 (or higher)
   6205  1.1  christos 		 * checksums...
   6206  1.1  christos 		 */
   6207  1.1  christos 		if (csump != NULL) {
   6208  1.1  christos 			if (nat->nat_dir == NAT_OUTBOUND)
   6209  1.1  christos 				ipf_fix_incksum(fin, csump, nat->nat_sumd[0]);
   6210  1.1  christos 			else
   6211  1.1  christos 				ipf_fix_outcksum(fin, csump, nat->nat_sumd[0]);
   6212  1.1  christos 		}
   6213  1.1  christos 	}
   6214  1.1  christos 
   6215  1.1  christos 	fin->fin_flx |= FI_NATED;
   6216  1.1  christos 	if (np != NULL && np->in_tag.ipt_num[0] != 0)
   6217  1.1  christos 		fin->fin_nattag = &np->in_tag;
   6218  1.1  christos 	return 1;
   6219  1.1  christos }
   6220  1.1  christos 
   6221  1.1  christos 
   6222  1.1  christos /* ------------------------------------------------------------------------ */
   6223  1.1  christos /* Function:    ipf_nat_proto                                               */
   6224  1.1  christos /* Returns:     u_short* - pointer to transport header checksum to update,  */
   6225  1.1  christos /*                         NULL if the transport protocol is not recognised */
   6226  1.1  christos /*                         as needing a checksum update.                    */
   6227  1.1  christos /* Parameters:  fin(I)    - pointer to packet information                   */
   6228  1.1  christos /*              nat(I)    - pointer to NAT structure                        */
   6229  1.1  christos /*              nflags(I) - NAT flags set for this packet                   */
   6230  1.1  christos /*                                                                          */
   6231  1.1  christos /* Return the pointer to the checksum field for each protocol so understood.*/
   6232  1.1  christos /* If support for making other changes to a protocol header is required,    */
   6233  1.1  christos /* that is not strictly 'address' translation, such as clamping the MSS in  */
   6234  1.1  christos /* TCP down to a specific value, then do it from here.                      */
   6235  1.1  christos /* ------------------------------------------------------------------------ */
   6236  1.1  christos u_short *
   6237  1.1  christos ipf_nat_proto(fin, nat, nflags)
   6238  1.1  christos 	fr_info_t *fin;
   6239  1.1  christos 	nat_t *nat;
   6240  1.1  christos 	u_int nflags;
   6241  1.1  christos {
   6242  1.1  christos 	icmphdr_t *icmp;
   6243  1.1  christos 	u_short *csump;
   6244  1.1  christos 	tcphdr_t *tcp;
   6245  1.1  christos 	udphdr_t *udp;
   6246  1.1  christos 
   6247  1.1  christos 	csump = NULL;
   6248  1.1  christos 	if (fin->fin_out == 0) {
   6249  1.1  christos 		fin->fin_rev = (nat->nat_dir & NAT_OUTBOUND);
   6250  1.1  christos 	} else {
   6251  1.1  christos 		fin->fin_rev = ((nat->nat_dir & NAT_OUTBOUND) == 0);
   6252  1.1  christos 	}
   6253  1.1  christos 
   6254  1.1  christos 	switch (fin->fin_p)
   6255  1.1  christos 	{
   6256  1.1  christos 	case IPPROTO_TCP :
   6257  1.1  christos 		tcp = fin->fin_dp;
   6258  1.1  christos 
   6259  1.1  christos 		if ((nflags & IPN_TCP) != 0)
   6260  1.1  christos 			csump = &tcp->th_sum;
   6261  1.1  christos 
   6262  1.1  christos 		/*
   6263  1.1  christos 		 * Do a MSS CLAMPING on a SYN packet,
   6264  1.1  christos 		 * only deal IPv4 for now.
   6265  1.1  christos 		 */
   6266  1.1  christos 		if ((nat->nat_mssclamp != 0) && (tcp->th_flags & TH_SYN) != 0)
   6267  1.1  christos 			ipf_nat_mssclamp(tcp, nat->nat_mssclamp, fin, csump);
   6268  1.1  christos 
   6269  1.1  christos 		break;
   6270  1.1  christos 
   6271  1.1  christos 	case IPPROTO_UDP :
   6272  1.1  christos 		udp = fin->fin_dp;
   6273  1.1  christos 
   6274  1.1  christos 		if ((nflags & IPN_UDP) != 0) {
   6275  1.1  christos 			if (udp->uh_sum != 0)
   6276  1.1  christos 				csump = &udp->uh_sum;
   6277  1.1  christos 		}
   6278  1.1  christos 		break;
   6279  1.1  christos 
   6280  1.1  christos 	case IPPROTO_ICMP :
   6281  1.1  christos 		icmp = fin->fin_dp;
   6282  1.1  christos 
   6283  1.1  christos 		if ((nflags & IPN_ICMPQUERY) != 0) {
   6284  1.1  christos 			if (icmp->icmp_cksum != 0)
   6285  1.1  christos 				csump = &icmp->icmp_cksum;
   6286  1.1  christos 		}
   6287  1.1  christos 		break;
   6288  1.1  christos 	}
   6289  1.1  christos 	return csump;
   6290  1.1  christos }
   6291  1.1  christos 
   6292  1.1  christos 
   6293  1.1  christos /* ------------------------------------------------------------------------ */
   6294  1.1  christos /* Function:    ipf_nat_expire                                              */
   6295  1.1  christos /* Returns:     Nil                                                         */
   6296  1.1  christos /* Parameters:  Nil                                                         */
   6297  1.1  christos /*                                                                          */
   6298  1.1  christos /* Check all of the timeout queues for entries at the top which need to be  */
   6299  1.1  christos /* expired.                                                                 */
   6300  1.1  christos /* ------------------------------------------------------------------------ */
   6301  1.1  christos void
   6302  1.1  christos ipf_nat_expire(softc)
   6303  1.1  christos 	ipf_main_softc_t *softc;
   6304  1.1  christos {
   6305  1.1  christos 	ipf_nat_softc_t *softn = softc->ipf_nat_soft;
   6306  1.1  christos 	ipftq_t *ifq, *ifqnext;
   6307  1.1  christos 	ipftqent_t *tqe, *tqn;
   6308  1.1  christos 	int i;
   6309  1.1  christos 	SPL_INT(s);
   6310  1.1  christos 
   6311  1.1  christos 	SPL_NET(s);
   6312  1.1  christos 	WRITE_ENTER(&softc->ipf_nat);
   6313  1.1  christos 	for (ifq = softn->ipf_nat_tcptq, i = 0; ifq != NULL;
   6314  1.1  christos 	     ifq = ifq->ifq_next) {
   6315  1.1  christos 		for (tqn = ifq->ifq_head; ((tqe = tqn) != NULL); i++) {
   6316  1.1  christos 			if (tqe->tqe_die > softc->ipf_ticks)
   6317  1.1  christos 				break;
   6318  1.1  christos 			tqn = tqe->tqe_next;
   6319  1.1  christos 			ipf_nat_delete(softc, tqe->tqe_parent, NL_EXPIRE);
   6320  1.1  christos 		}
   6321  1.1  christos 	}
   6322  1.1  christos 
   6323  1.1  christos 	for (ifq = softn->ipf_nat_utqe; ifq != NULL; ifq = ifq->ifq_next) {
   6324  1.1  christos 		for (tqn = ifq->ifq_head; ((tqe = tqn) != NULL); i++) {
   6325  1.1  christos 			if (tqe->tqe_die > softc->ipf_ticks)
   6326  1.1  christos 				break;
   6327  1.1  christos 			tqn = tqe->tqe_next;
   6328  1.1  christos 			ipf_nat_delete(softc, tqe->tqe_parent, NL_EXPIRE);
   6329  1.1  christos 		}
   6330  1.1  christos 	}
   6331  1.1  christos 
   6332  1.1  christos 	for (ifq = softn->ipf_nat_utqe; ifq != NULL; ifq = ifqnext) {
   6333  1.1  christos 		ifqnext = ifq->ifq_next;
   6334  1.1  christos 
   6335  1.1  christos 		if (((ifq->ifq_flags & IFQF_DELETE) != 0) &&
   6336  1.1  christos 		    (ifq->ifq_ref == 0)) {
   6337  1.1  christos 			ipf_freetimeoutqueue(softc, ifq);
   6338  1.1  christos 		}
   6339  1.1  christos 	}
   6340  1.1  christos 
   6341  1.1  christos 	if (softn->ipf_nat_doflush != 0) {
   6342  1.1  christos 		ipf_nat_extraflush(softc, softn, 2);
   6343  1.1  christos 		softn->ipf_nat_doflush = 0;
   6344  1.1  christos 	}
   6345  1.1  christos 
   6346  1.1  christos 	RWLOCK_EXIT(&softc->ipf_nat);
   6347  1.1  christos 	SPL_X(s);
   6348  1.1  christos }
   6349  1.1  christos 
   6350  1.1  christos 
   6351  1.1  christos /* ------------------------------------------------------------------------ */
   6352  1.1  christos /* Function:    ipf_nat_sync                                                */
   6353  1.1  christos /* Returns:     Nil                                                         */
   6354  1.1  christos /* Parameters:  ifp(I) - pointer to network interface                       */
   6355  1.1  christos /*                                                                          */
   6356  1.1  christos /* Walk through all of the currently active NAT sessions, looking for those */
   6357  1.1  christos /* which need to have their translated address updated.                     */
   6358  1.1  christos /* ------------------------------------------------------------------------ */
   6359  1.1  christos void
   6360  1.1  christos ipf_nat_sync(softc, ifp)
   6361  1.1  christos 	ipf_main_softc_t *softc;
   6362  1.1  christos 	void *ifp;
   6363  1.1  christos {
   6364  1.1  christos 	ipf_nat_softc_t *softn = softc->ipf_nat_soft;
   6365  1.1  christos 	u_32_t sum1, sum2, sumd;
   6366  1.1  christos 	i6addr_t in;
   6367  1.1  christos 	ipnat_t *n;
   6368  1.1  christos 	nat_t *nat;
   6369  1.1  christos 	void *ifp2;
   6370  1.1  christos 	int idx;
   6371  1.1  christos 	SPL_INT(s);
   6372  1.1  christos 
   6373  1.1  christos 	if (softc->ipf_running <= 0)
   6374  1.1  christos 		return;
   6375  1.1  christos 
   6376  1.1  christos 	/*
   6377  1.1  christos 	 * Change IP addresses for NAT sessions for any protocol except TCP
   6378  1.1  christos 	 * since it will break the TCP connection anyway.  The only rules
   6379  1.1  christos 	 * which will get changed are those which are "map ... -> 0/32",
   6380  1.1  christos 	 * where the rule specifies the address is taken from the interface.
   6381  1.1  christos 	 */
   6382  1.1  christos 	SPL_NET(s);
   6383  1.1  christos 	WRITE_ENTER(&softc->ipf_nat);
   6384  1.1  christos 
   6385  1.1  christos 	if (softc->ipf_running <= 0) {
   6386  1.1  christos 		RWLOCK_EXIT(&softc->ipf_nat);
   6387  1.1  christos 		return;
   6388  1.1  christos 	}
   6389  1.1  christos 
   6390  1.1  christos 	for (nat = softn->ipf_nat_instances; nat; nat = nat->nat_next) {
   6391  1.1  christos 		if ((nat->nat_flags & IPN_TCP) != 0)
   6392  1.1  christos 			continue;
   6393  1.1  christos 
   6394  1.1  christos 		n = nat->nat_ptr;
   6395  1.1  christos 		if (n != NULL) {
   6396  1.1  christos 			if (n->in_v[1] == 4) {
   6397  1.1  christos 				if (n->in_redir & NAT_MAP) {
   6398  1.1  christos 					if ((n->in_nsrcaddr != 0) ||
   6399  1.1  christos 					    (n->in_nsrcmsk != 0xffffffff))
   6400  1.1  christos 						continue;
   6401  1.1  christos 				} else if (n->in_redir & NAT_REDIRECT) {
   6402  1.1  christos 					if ((n->in_ndstaddr != 0) ||
   6403  1.1  christos 					    (n->in_ndstmsk != 0xffffffff))
   6404  1.1  christos 						continue;
   6405  1.1  christos 				}
   6406  1.1  christos 			}
   6407  1.1  christos #ifdef USE_INET6
   6408  1.1  christos 			if (n->in_v[1] == 4) {
   6409  1.1  christos 				if (n->in_redir & NAT_MAP) {
   6410  1.1  christos 					if (!IP6_ISZERO(&n->in_nsrcaddr) ||
   6411  1.1  christos 					    !IP6_ISONES(&n->in_nsrcmsk))
   6412  1.1  christos 						continue;
   6413  1.1  christos 				} else if (n->in_redir & NAT_REDIRECT) {
   6414  1.1  christos 					if (!IP6_ISZERO(&n->in_ndstaddr) ||
   6415  1.1  christos 					    !IP6_ISONES(&n->in_ndstmsk))
   6416  1.1  christos 						continue;
   6417  1.1  christos 				}
   6418  1.1  christos 			}
   6419  1.1  christos #endif
   6420  1.1  christos 		}
   6421  1.1  christos 
   6422  1.1  christos 		if (((ifp == NULL) || (ifp == nat->nat_ifps[0]) ||
   6423  1.1  christos 		     (ifp == nat->nat_ifps[1]))) {
   6424  1.1  christos 			nat->nat_ifps[0] = GETIFP(nat->nat_ifnames[0],
   6425  1.1  christos 						  nat->nat_v[0]);
   6426  1.1  christos 			if ((nat->nat_ifps[0] != NULL) &&
   6427  1.1  christos 			    (nat->nat_ifps[0] != (void *)-1)) {
   6428  1.1  christos 				nat->nat_mtu[0] = GETIFMTU_4(nat->nat_ifps[0]);
   6429  1.1  christos 			}
   6430  1.1  christos 			if (nat->nat_ifnames[1][0] != '\0') {
   6431  1.1  christos 				nat->nat_ifps[1] = GETIFP(nat->nat_ifnames[1],
   6432  1.1  christos 							  nat->nat_v[1]);
   6433  1.1  christos 			} else {
   6434  1.1  christos 				nat->nat_ifps[1] = nat->nat_ifps[0];
   6435  1.1  christos 			}
   6436  1.1  christos 			if ((nat->nat_ifps[1] != NULL) &&
   6437  1.1  christos 			    (nat->nat_ifps[1] != (void *)-1)) {
   6438  1.1  christos 				nat->nat_mtu[1] = GETIFMTU_4(nat->nat_ifps[1]);
   6439  1.1  christos 			}
   6440  1.1  christos 			ifp2 = nat->nat_ifps[0];
   6441  1.1  christos 			if (ifp2 == NULL)
   6442  1.1  christos 				continue;
   6443  1.1  christos 
   6444  1.1  christos 			/*
   6445  1.1  christos 			 * Change the map-to address to be the same as the
   6446  1.1  christos 			 * new one.
   6447  1.1  christos 			 */
   6448  1.1  christos 			sum1 = NATFSUM(nat, nat->nat_v[1], nat_nsrc6);
   6449  1.1  christos 			if (ipf_ifpaddr(softc, nat->nat_v[0], FRI_NORMAL, ifp2,
   6450  1.1  christos 				       &in, NULL) != -1) {
   6451  1.1  christos 				if (nat->nat_v[0] == 4)
   6452  1.1  christos 					nat->nat_nsrcip = in.in4;
   6453  1.1  christos 			}
   6454  1.1  christos 			sum2 = NATFSUM(nat, nat->nat_v[1], nat_nsrc6);
   6455  1.1  christos 
   6456  1.1  christos 			if (sum1 == sum2)
   6457  1.1  christos 				continue;
   6458  1.1  christos 			/*
   6459  1.1  christos 			 * Readjust the checksum adjustment to take into
   6460  1.1  christos 			 * account the new IP#.
   6461  1.1  christos 			 */
   6462  1.1  christos 			CALC_SUMD(sum1, sum2, sumd);
   6463  1.1  christos 			/* XXX - dont change for TCP when solaris does
   6464  1.1  christos 			 * hardware checksumming.
   6465  1.1  christos 			 */
   6466  1.1  christos 			sumd += nat->nat_sumd[0];
   6467  1.1  christos 			nat->nat_sumd[0] = (sumd & 0xffff) + (sumd >> 16);
   6468  1.1  christos 			nat->nat_sumd[1] = nat->nat_sumd[0];
   6469  1.1  christos 		}
   6470  1.1  christos 	}
   6471  1.1  christos 
   6472  1.1  christos 	for (n = softn->ipf_nat_list; (n != NULL); n = n->in_next) {
   6473  1.1  christos 		char *base = n->in_names;
   6474  1.1  christos 
   6475  1.1  christos 		if ((ifp == NULL) || (n->in_ifps[0] == ifp))
   6476  1.1  christos 			n->in_ifps[0] = ipf_resolvenic(softc,
   6477  1.1  christos 						       base + n->in_ifnames[0],
   6478  1.1  christos 						       n->in_v[0]);
   6479  1.1  christos 		if ((ifp == NULL) || (n->in_ifps[1] == ifp))
   6480  1.1  christos 			n->in_ifps[1] = ipf_resolvenic(softc,
   6481  1.1  christos 						       base + n->in_ifnames[1],
   6482  1.1  christos 						       n->in_v[1]);
   6483  1.1  christos 
   6484  1.1  christos 		if (n->in_redir & NAT_REDIRECT)
   6485  1.1  christos 			idx = 1;
   6486  1.1  christos 		else
   6487  1.1  christos 			idx = 0;
   6488  1.1  christos 
   6489  1.1  christos 		if (((ifp == NULL) || (n->in_ifps[idx] == ifp)) &&
   6490  1.1  christos 		    (n->in_ifps[idx] != NULL &&
   6491  1.1  christos 		     n->in_ifps[idx] != (void *)-1)) {
   6492  1.1  christos 
   6493  1.1  christos 			ipf_nat_nextaddrinit(softc, n->in_names, &n->in_osrc,
   6494  1.1  christos 					     0, n->in_ifps[idx]);
   6495  1.1  christos 			ipf_nat_nextaddrinit(softc, n->in_names, &n->in_odst,
   6496  1.1  christos 					     0, n->in_ifps[idx]);
   6497  1.1  christos 			ipf_nat_nextaddrinit(softc, n->in_names, &n->in_nsrc,
   6498  1.1  christos 					     0, n->in_ifps[idx]);
   6499  1.1  christos 			ipf_nat_nextaddrinit(softc, n->in_names, &n->in_ndst,
   6500  1.1  christos 					     0, n->in_ifps[idx]);
   6501  1.1  christos 		}
   6502  1.1  christos 	}
   6503  1.1  christos 	RWLOCK_EXIT(&softc->ipf_nat);
   6504  1.1  christos 	SPL_X(s);
   6505  1.1  christos }
   6506  1.1  christos 
   6507  1.1  christos 
   6508  1.1  christos /* ------------------------------------------------------------------------ */
   6509  1.1  christos /* Function:    ipf_nat_icmpquerytype                                       */
   6510  1.1  christos /* Returns:     int - 1 == success, 0 == failure                            */
   6511  1.1  christos /* Parameters:  icmptype(I) - ICMP type number                              */
   6512  1.1  christos /*                                                                          */
   6513  1.1  christos /* Tests to see if the ICMP type number passed is a query/response type or  */
   6514  1.1  christos /* not.                                                                     */
   6515  1.1  christos /* ------------------------------------------------------------------------ */
   6516  1.1  christos static int
   6517  1.1  christos ipf_nat_icmpquerytype(icmptype)
   6518  1.1  christos 	int icmptype;
   6519  1.1  christos {
   6520  1.1  christos 
   6521  1.1  christos 	/*
   6522  1.1  christos 	 * For the ICMP query NAT code, it is essential that both the query
   6523  1.1  christos 	 * and the reply match on the NAT rule. Because the NAT structure
   6524  1.1  christos 	 * does not keep track of the icmptype, and a single NAT structure
   6525  1.1  christos 	 * is used for all icmp types with the same src, dest and id, we
   6526  1.1  christos 	 * simply define the replies as queries as well. The funny thing is,
   6527  1.1  christos 	 * altough it seems silly to call a reply a query, this is exactly
   6528  1.1  christos 	 * as it is defined in the IPv4 specification
   6529  1.1  christos 	 */
   6530  1.1  christos 	switch (icmptype)
   6531  1.1  christos 	{
   6532  1.1  christos 	case ICMP_ECHOREPLY:
   6533  1.1  christos 	case ICMP_ECHO:
   6534  1.1  christos 	/* route aedvertisement/solliciation is currently unsupported: */
   6535  1.1  christos 	/* it would require rewriting the ICMP data section            */
   6536  1.1  christos 	case ICMP_TSTAMP:
   6537  1.1  christos 	case ICMP_TSTAMPREPLY:
   6538  1.1  christos 	case ICMP_IREQ:
   6539  1.1  christos 	case ICMP_IREQREPLY:
   6540  1.1  christos 	case ICMP_MASKREQ:
   6541  1.1  christos 	case ICMP_MASKREPLY:
   6542  1.1  christos 		return 1;
   6543  1.1  christos 	default:
   6544  1.1  christos 		return 0;
   6545  1.1  christos 	}
   6546  1.1  christos }
   6547  1.1  christos 
   6548  1.1  christos 
   6549  1.1  christos /* ------------------------------------------------------------------------ */
   6550  1.1  christos /* Function:    nat_log                                                     */
   6551  1.1  christos /* Returns:     Nil                                                         */
   6552  1.1  christos /* Parameters:  nat(I)    - pointer to NAT structure                        */
   6553  1.1  christos /*              action(I) - action related to NAT structure being performed */
   6554  1.1  christos /*                                                                          */
   6555  1.1  christos /* Creates a NAT log entry.                                                 */
   6556  1.1  christos /* ------------------------------------------------------------------------ */
   6557  1.1  christos void
   6558  1.1  christos ipf_nat_log(softc, softn, nat, action)
   6559  1.1  christos 	ipf_main_softc_t *softc;
   6560  1.1  christos 	ipf_nat_softc_t *softn;
   6561  1.1  christos 	struct nat *nat;
   6562  1.1  christos 	u_int action;
   6563  1.1  christos {
   6564  1.1  christos #ifdef	IPFILTER_LOG
   6565  1.1  christos # ifndef LARGE_NAT
   6566  1.1  christos 	struct ipnat *np;
   6567  1.1  christos 	int rulen;
   6568  1.1  christos # endif
   6569  1.1  christos 	struct natlog natl;
   6570  1.1  christos 	void *items[1];
   6571  1.1  christos 	size_t sizes[1];
   6572  1.1  christos 	int types[1];
   6573  1.1  christos 
   6574  1.1  christos 	bcopy((char *)&nat->nat_osrc6, (char *)&natl.nl_osrcip,
   6575  1.1  christos 	      sizeof(natl.nl_osrcip));
   6576  1.1  christos 	bcopy((char *)&nat->nat_nsrc6, (char *)&natl.nl_nsrcip,
   6577  1.1  christos 	      sizeof(natl.nl_nsrcip));
   6578  1.1  christos 	bcopy((char *)&nat->nat_odst6, (char *)&natl.nl_odstip,
   6579  1.1  christos 	      sizeof(natl.nl_odstip));
   6580  1.1  christos 	bcopy((char *)&nat->nat_ndst6, (char *)&natl.nl_ndstip,
   6581  1.1  christos 	      sizeof(natl.nl_ndstip));
   6582  1.1  christos 
   6583  1.1  christos 	natl.nl_bytes[0] = nat->nat_bytes[0];
   6584  1.1  christos 	natl.nl_bytes[1] = nat->nat_bytes[1];
   6585  1.1  christos 	natl.nl_pkts[0] = nat->nat_pkts[0];
   6586  1.1  christos 	natl.nl_pkts[1] = nat->nat_pkts[1];
   6587  1.1  christos 	natl.nl_odstport = nat->nat_odport;
   6588  1.1  christos 	natl.nl_osrcport = nat->nat_osport;
   6589  1.1  christos 	natl.nl_nsrcport = nat->nat_nsport;
   6590  1.1  christos 	natl.nl_ndstport = nat->nat_ndport;
   6591  1.1  christos 	natl.nl_p[0] = nat->nat_pr[0];
   6592  1.1  christos 	natl.nl_p[1] = nat->nat_pr[1];
   6593  1.1  christos 	natl.nl_v[0] = nat->nat_v[0];
   6594  1.1  christos 	natl.nl_v[1] = nat->nat_v[1];
   6595  1.1  christos 	natl.nl_type = nat->nat_redir;
   6596  1.1  christos 	natl.nl_action = action;
   6597  1.1  christos 	natl.nl_rule = -1;
   6598  1.1  christos 
   6599  1.1  christos 	bcopy(nat->nat_ifnames[0], natl.nl_ifnames[0],
   6600  1.1  christos 	      sizeof(nat->nat_ifnames[0]));
   6601  1.1  christos 	bcopy(nat->nat_ifnames[1], natl.nl_ifnames[1],
   6602  1.1  christos 	      sizeof(nat->nat_ifnames[1]));
   6603  1.1  christos 
   6604  1.1  christos # ifndef LARGE_NAT
   6605  1.1  christos 	if (nat->nat_ptr != NULL) {
   6606  1.1  christos 		for (rulen = 0, np = softn->ipf_nat_list; np != NULL;
   6607  1.1  christos 		     np = np->in_next, rulen++)
   6608  1.1  christos 			if (np == nat->nat_ptr) {
   6609  1.1  christos 				natl.nl_rule = rulen;
   6610  1.1  christos 				break;
   6611  1.1  christos 			}
   6612  1.1  christos 	}
   6613  1.1  christos # endif
   6614  1.1  christos 	items[0] = &natl;
   6615  1.1  christos 	sizes[0] = sizeof(natl);
   6616  1.1  christos 	types[0] = 0;
   6617  1.1  christos 
   6618  1.1  christos 	(void) ipf_log_items(softc, IPL_LOGNAT, NULL, items, sizes, types, 1);
   6619  1.1  christos #endif
   6620  1.1  christos }
   6621  1.1  christos 
   6622  1.1  christos 
   6623  1.1  christos #if defined(__OpenBSD__)
   6624  1.1  christos /* ------------------------------------------------------------------------ */
   6625  1.1  christos /* Function:    ipf_nat_ifdetach                                            */
   6626  1.1  christos /* Returns:     Nil                                                         */
   6627  1.1  christos /* Parameters:  ifp(I) - pointer to network interface                       */
   6628  1.1  christos /*                                                                          */
   6629  1.1  christos /* Compatibility interface for OpenBSD to trigger the correct updating of   */
   6630  1.1  christos /* interface references within IPFilter.                                    */
   6631  1.1  christos /* ------------------------------------------------------------------------ */
   6632  1.1  christos void
   6633  1.1  christos ipf_nat_ifdetach(ifp)
   6634  1.1  christos 	void *ifp;
   6635  1.1  christos {
   6636  1.1  christos 	ipf_main_softc_t *softc;
   6637  1.1  christos 
   6638  1.1  christos 	softc = ipf_get_softc(0);
   6639  1.1  christos 
   6640  1.1  christos 	ipf_sync(ifp);
   6641  1.1  christos 	return;
   6642  1.1  christos }
   6643  1.1  christos #endif
   6644  1.1  christos 
   6645  1.1  christos 
   6646  1.1  christos /* ------------------------------------------------------------------------ */
   6647  1.1  christos /* Function:    ipf_nat_rulederef                                           */
   6648  1.1  christos /* Returns:     Nil                                                         */
   6649  1.1  christos /* Parameters:  isp(I) - pointer to pointer to NAT rule                     */
   6650  1.1  christos /* Write Locks: ipf_nat                                                     */
   6651  1.1  christos /*                                                                          */
   6652  1.1  christos /* ------------------------------------------------------------------------ */
   6653  1.1  christos void
   6654  1.1  christos ipf_nat_rulederef(softc, inp)
   6655  1.1  christos 	ipf_main_softc_t *softc;
   6656  1.1  christos 	ipnat_t **inp;
   6657  1.1  christos {
   6658  1.1  christos 	ipf_nat_softc_t *softn = softc->ipf_nat_soft;
   6659  1.1  christos 	ipnat_t *in;
   6660  1.1  christos 
   6661  1.1  christos 	in = *inp;
   6662  1.1  christos 	*inp = NULL;
   6663  1.1  christos 	in->in_space++;
   6664  1.1  christos 	in->in_use--;
   6665  1.1  christos 	if (in->in_use == 0 && (in->in_flags & IPN_DELETE)) {
   6666  1.1  christos 		ipf_nat_free_rule(softc, softn, in);
   6667  1.1  christos 	}
   6668  1.1  christos }
   6669  1.1  christos 
   6670  1.1  christos 
   6671  1.1  christos /* ------------------------------------------------------------------------ */
   6672  1.1  christos /* Function:    ipf_nat_deref                                               */
   6673  1.1  christos /* Returns:     Nil                                                         */
   6674  1.1  christos /* Parameters:  isp(I) - pointer to pointer to NAT table entry              */
   6675  1.1  christos /*                                                                          */
   6676  1.1  christos /* Decrement the reference counter for this NAT table entry and free it if  */
   6677  1.1  christos /* there are no more things using it.                                       */
   6678  1.1  christos /*                                                                          */
   6679  1.1  christos /* IF nat_ref == 1 when this function is called, then we have an orphan nat */
   6680  1.1  christos /* structure *because* it only gets called on paths _after_ nat_ref has been*/
   6681  1.1  christos /* incremented.  If nat_ref == 1 then we shouldn't decrement it here        */
   6682  1.1  christos /* because nat_delete() will do that and send nat_ref to -1.                */
   6683  1.1  christos /*                                                                          */
   6684  1.1  christos /* Holding the lock on nat_lock is required to serialise nat_delete() being */
   6685  1.1  christos /* called from a NAT flush ioctl with a deref happening because of a packet.*/
   6686  1.1  christos /* ------------------------------------------------------------------------ */
   6687  1.1  christos void
   6688  1.1  christos ipf_nat_deref(softc, natp)
   6689  1.1  christos 	ipf_main_softc_t *softc;
   6690  1.1  christos 	nat_t **natp;
   6691  1.1  christos {
   6692  1.1  christos 	nat_t *nat;
   6693  1.1  christos 
   6694  1.1  christos 	nat = *natp;
   6695  1.1  christos 	*natp = NULL;
   6696  1.1  christos 
   6697  1.1  christos 	MUTEX_ENTER(&nat->nat_lock);
   6698  1.1  christos 	if (nat->nat_ref > 1) {
   6699  1.1  christos 		nat->nat_ref--;
   6700  1.1  christos 		MUTEX_EXIT(&nat->nat_lock);
   6701  1.1  christos 		return;
   6702  1.1  christos 	}
   6703  1.1  christos 	MUTEX_EXIT(&nat->nat_lock);
   6704  1.1  christos 
   6705  1.1  christos 	WRITE_ENTER(&softc->ipf_nat);
   6706  1.1  christos 	ipf_nat_delete(softc, nat, NL_EXPIRE);
   6707  1.1  christos 	RWLOCK_EXIT(&softc->ipf_nat);
   6708  1.1  christos }
   6709  1.1  christos 
   6710  1.1  christos 
   6711  1.1  christos /* ------------------------------------------------------------------------ */
   6712  1.1  christos /* Function:    ipf_nat_clone                                               */
   6713  1.1  christos /* Returns:     ipstate_t* - NULL == cloning failed,                        */
   6714  1.1  christos /*                           else pointer to new state structure            */
   6715  1.1  christos /* Parameters:  fin(I) - pointer to packet information                      */
   6716  1.1  christos /*              is(I)  - pointer to master state structure                  */
   6717  1.1  christos /* Write Lock:  ipf_nat                                                     */
   6718  1.1  christos /*                                                                          */
   6719  1.1  christos /* Create a "duplcate" state table entry from the master.                   */
   6720  1.1  christos /* ------------------------------------------------------------------------ */
   6721  1.1  christos nat_t *
   6722  1.1  christos ipf_nat_clone(fin, nat)
   6723  1.1  christos 	fr_info_t *fin;
   6724  1.1  christos 	nat_t *nat;
   6725  1.1  christos {
   6726  1.1  christos 	ipf_main_softc_t *softc = fin->fin_main_soft;
   6727  1.1  christos 	ipf_nat_softc_t *softn = softc->ipf_nat_soft;
   6728  1.1  christos 	frentry_t *fr;
   6729  1.1  christos 	nat_t *clone;
   6730  1.1  christos 	ipnat_t *np;
   6731  1.1  christos 
   6732  1.1  christos 	KMALLOC(clone, nat_t *);
   6733  1.1  christos 	if (clone == NULL) {
   6734  1.1  christos 		NBUMPSIDED(fin->fin_out, ns_clone_nomem);
   6735  1.1  christos 		return NULL;
   6736  1.1  christos 	}
   6737  1.1  christos 	bcopy((char *)nat, (char *)clone, sizeof(*clone));
   6738  1.1  christos 
   6739  1.1  christos 	MUTEX_NUKE(&clone->nat_lock);
   6740  1.1  christos 
   6741  1.1  christos 	clone->nat_rev = fin->fin_rev;
   6742  1.1  christos 	clone->nat_aps = NULL;
   6743  1.1  christos 	/*
   6744  1.1  christos 	 * Initialize all these so that ipf_nat_delete() doesn't cause a crash.
   6745  1.1  christos 	 */
   6746  1.1  christos 	clone->nat_tqe.tqe_pnext = NULL;
   6747  1.1  christos 	clone->nat_tqe.tqe_next = NULL;
   6748  1.1  christos 	clone->nat_tqe.tqe_ifq = NULL;
   6749  1.1  christos 	clone->nat_tqe.tqe_parent = clone;
   6750  1.1  christos 
   6751  1.1  christos 	clone->nat_flags &= ~SI_CLONE;
   6752  1.1  christos 	clone->nat_flags |= SI_CLONED;
   6753  1.1  christos 
   6754  1.1  christos 	if (clone->nat_hm)
   6755  1.1  christos 		clone->nat_hm->hm_ref++;
   6756  1.1  christos 
   6757  1.1  christos 	if (ipf_nat_insert(softc, softn, clone) == -1) {
   6758  1.1  christos 		KFREE(clone);
   6759  1.1  christos 		NBUMPSIDED(fin->fin_out, ns_insert_fail);
   6760  1.1  christos 		return NULL;
   6761  1.1  christos 	}
   6762  1.1  christos 
   6763  1.1  christos 	np = clone->nat_ptr;
   6764  1.1  christos 	if (np != NULL) {
   6765  1.1  christos 		if (softn->ipf_nat_logging)
   6766  1.1  christos 			ipf_nat_log(softc, softn, clone, NL_CLONE);
   6767  1.1  christos 		np->in_use++;
   6768  1.1  christos 	}
   6769  1.1  christos 	fr = clone->nat_fr;
   6770  1.1  christos 	if (fr != NULL) {
   6771  1.1  christos 		MUTEX_ENTER(&fr->fr_lock);
   6772  1.1  christos 		fr->fr_ref++;
   6773  1.1  christos 		MUTEX_EXIT(&fr->fr_lock);
   6774  1.1  christos 	}
   6775  1.1  christos 
   6776  1.1  christos 
   6777  1.1  christos 	/*
   6778  1.1  christos 	 * Because the clone is created outside the normal loop of things and
   6779  1.1  christos 	 * TCP has special needs in terms of state, initialise the timeout
   6780  1.1  christos 	 * state of the new NAT from here.
   6781  1.1  christos 	 */
   6782  1.1  christos 	if (clone->nat_pr[0] == IPPROTO_TCP) {
   6783  1.1  christos 		(void) ipf_tcp_age(&clone->nat_tqe, fin, softn->ipf_nat_tcptq,
   6784  1.1  christos 				   clone->nat_flags, 2);
   6785  1.1  christos 	}
   6786  1.1  christos 	clone->nat_sync = ipf_sync_new(softc, SMC_NAT, fin, clone);
   6787  1.1  christos 	if (softn->ipf_nat_logging)
   6788  1.1  christos 		ipf_nat_log(softc, softn, clone, NL_CLONE);
   6789  1.1  christos 	return clone;
   6790  1.1  christos }
   6791  1.1  christos 
   6792  1.1  christos 
   6793  1.1  christos /* ------------------------------------------------------------------------ */
   6794  1.1  christos /* Function:   ipf_nat_wildok                                               */
   6795  1.1  christos /* Returns:    int - 1 == packet's ports match wildcards                    */
   6796  1.1  christos /*                   0 == packet's ports don't match wildcards              */
   6797  1.1  christos /* Parameters: nat(I)   - NAT entry                                         */
   6798  1.1  christos /*             sport(I) - source port                                       */
   6799  1.1  christos /*             dport(I) - destination port                                  */
   6800  1.1  christos /*             flags(I) - wildcard flags                                    */
   6801  1.1  christos /*             dir(I)   - packet direction                                  */
   6802  1.1  christos /*                                                                          */
   6803  1.1  christos /* Use NAT entry and packet direction to determine which combination of     */
   6804  1.1  christos /* wildcard flags should be used.                                           */
   6805  1.1  christos /* ------------------------------------------------------------------------ */
   6806  1.1  christos int
   6807  1.1  christos ipf_nat_wildok(nat, sport, dport, flags, dir)
   6808  1.1  christos 	nat_t *nat;
   6809  1.1  christos 	int sport, dport, flags, dir;
   6810  1.1  christos {
   6811  1.1  christos 	/*
   6812  1.1  christos 	 * When called by       dir is set to
   6813  1.1  christos 	 * nat_inlookup         NAT_INBOUND (0)
   6814  1.1  christos 	 * nat_outlookup        NAT_OUTBOUND (1)
   6815  1.1  christos 	 *
   6816  1.1  christos 	 * We simply combine the packet's direction in dir with the original
   6817  1.1  christos 	 * "intended" direction of that NAT entry in nat->nat_dir to decide
   6818  1.1  christos 	 * which combination of wildcard flags to allow.
   6819  1.1  christos 	 */
   6820  1.1  christos 	switch ((dir << 1) | (nat->nat_dir & (NAT_INBOUND|NAT_OUTBOUND)))
   6821  1.1  christos 	{
   6822  1.1  christos 	case 3: /* outbound packet / outbound entry */
   6823  1.1  christos 		if (((nat->nat_osport == sport) ||
   6824  1.1  christos 		    (flags & SI_W_SPORT)) &&
   6825  1.1  christos 		    ((nat->nat_odport == dport) ||
   6826  1.1  christos 		    (flags & SI_W_DPORT)))
   6827  1.1  christos 			return 1;
   6828  1.1  christos 		break;
   6829  1.1  christos 	case 2: /* outbound packet / inbound entry */
   6830  1.1  christos 		if (((nat->nat_osport == dport) ||
   6831  1.1  christos 		    (flags & SI_W_SPORT)) &&
   6832  1.1  christos 		    ((nat->nat_odport == sport) ||
   6833  1.1  christos 		    (flags & SI_W_DPORT)))
   6834  1.1  christos 			return 1;
   6835  1.1  christos 		break;
   6836  1.1  christos 	case 1: /* inbound packet / outbound entry */
   6837  1.1  christos 		if (((nat->nat_osport == dport) ||
   6838  1.1  christos 		    (flags & SI_W_SPORT)) &&
   6839  1.1  christos 		    ((nat->nat_odport == sport) ||
   6840  1.1  christos 		    (flags & SI_W_DPORT)))
   6841  1.1  christos 			return 1;
   6842  1.1  christos 		break;
   6843  1.1  christos 	case 0: /* inbound packet / inbound entry */
   6844  1.1  christos 		if (((nat->nat_osport == sport) ||
   6845  1.1  christos 		    (flags & SI_W_SPORT)) &&
   6846  1.1  christos 		    ((nat->nat_odport == dport) ||
   6847  1.1  christos 		    (flags & SI_W_DPORT)))
   6848  1.1  christos 			return 1;
   6849  1.1  christos 		break;
   6850  1.1  christos 	default:
   6851  1.1  christos 		break;
   6852  1.1  christos 	}
   6853  1.1  christos 
   6854  1.1  christos 	return(0);
   6855  1.1  christos }
   6856  1.1  christos 
   6857  1.1  christos 
   6858  1.1  christos /* ------------------------------------------------------------------------ */
   6859  1.1  christos /* Function:    nat_mssclamp                                                */
   6860  1.1  christos /* Returns:     Nil                                                         */
   6861  1.1  christos /* Parameters:  tcp(I)    - pointer to TCP header                           */
   6862  1.1  christos /*              maxmss(I) - value to clamp the TCP MSS to                   */
   6863  1.1  christos /*              fin(I)    - pointer to packet information                   */
   6864  1.1  christos /*              csump(I)  - pointer to TCP checksum                         */
   6865  1.1  christos /*                                                                          */
   6866  1.1  christos /* Check for MSS option and clamp it if necessary.  If found and changed,   */
   6867  1.1  christos /* then the TCP header checksum will be updated to reflect the change in    */
   6868  1.1  christos /* the MSS.                                                                 */
   6869  1.1  christos /* ------------------------------------------------------------------------ */
   6870  1.1  christos static void
   6871  1.1  christos ipf_nat_mssclamp(tcp, maxmss, fin, csump)
   6872  1.1  christos 	tcphdr_t *tcp;
   6873  1.1  christos 	u_32_t maxmss;
   6874  1.1  christos 	fr_info_t *fin;
   6875  1.1  christos 	u_short *csump;
   6876  1.1  christos {
   6877  1.1  christos 	u_char *cp, *ep, opt;
   6878  1.1  christos 	int hlen, advance;
   6879  1.1  christos 	u_32_t mss, sumd;
   6880  1.1  christos 
   6881  1.1  christos 	hlen = TCP_OFF(tcp) << 2;
   6882  1.1  christos 	if (hlen > sizeof(*tcp)) {
   6883  1.1  christos 		cp = (u_char *)tcp + sizeof(*tcp);
   6884  1.1  christos 		ep = (u_char *)tcp + hlen;
   6885  1.1  christos 
   6886  1.1  christos 		while (cp < ep) {
   6887  1.1  christos 			opt = cp[0];
   6888  1.1  christos 			if (opt == TCPOPT_EOL)
   6889  1.1  christos 				break;
   6890  1.1  christos 			else if (opt == TCPOPT_NOP) {
   6891  1.1  christos 				cp++;
   6892  1.1  christos 				continue;
   6893  1.1  christos 			}
   6894  1.1  christos 
   6895  1.1  christos 			if (cp + 1 >= ep)
   6896  1.1  christos 				break;
   6897  1.1  christos 			advance = cp[1];
   6898  1.1  christos 			if ((cp + advance > ep) || (advance <= 0))
   6899  1.1  christos 				break;
   6900  1.1  christos 			switch (opt)
   6901  1.1  christos 			{
   6902  1.1  christos 			case TCPOPT_MAXSEG:
   6903  1.1  christos 				if (advance != 4)
   6904  1.1  christos 					break;
   6905  1.1  christos 				mss = cp[2] * 256 + cp[3];
   6906  1.1  christos 				if (mss > maxmss) {
   6907  1.1  christos 					cp[2] = maxmss / 256;
   6908  1.1  christos 					cp[3] = maxmss & 0xff;
   6909  1.1  christos 					CALC_SUMD(mss, maxmss, sumd);
   6910  1.1  christos 					ipf_fix_outcksum(fin, csump, sumd);
   6911  1.1  christos 				}
   6912  1.1  christos 				break;
   6913  1.1  christos 			default:
   6914  1.1  christos 				/* ignore unknown options */
   6915  1.1  christos 				break;
   6916  1.1  christos 			}
   6917  1.1  christos 
   6918  1.1  christos 			cp += advance;
   6919  1.1  christos 		}
   6920  1.1  christos 	}
   6921  1.1  christos }
   6922  1.1  christos 
   6923  1.1  christos 
   6924  1.1  christos /* ------------------------------------------------------------------------ */
   6925  1.1  christos /* Function:    softn->ipf_nat_setqueue                                            */
   6926  1.1  christos /* Returns:     Nil                                                         */
   6927  1.1  christos /* Parameters:  nat(I)- pointer to NAT structure                            */
   6928  1.1  christos /*              rev(I) - forward(0) or reverse(1) direction                 */
   6929  1.1  christos /* Locks:       ipf_nat (read or write)                                     */
   6930  1.1  christos /*                                                                          */
   6931  1.1  christos /* Put the NAT entry on its default queue entry, using rev as a helped in   */
   6932  1.1  christos /* determining which queue it should be placed on.                          */
   6933  1.1  christos /* ------------------------------------------------------------------------ */
   6934  1.1  christos void
   6935  1.1  christos ipf_nat_setqueue(softc, softn, nat)
   6936  1.1  christos 	ipf_main_softc_t *softc;
   6937  1.1  christos 	ipf_nat_softc_t *softn;
   6938  1.1  christos 	nat_t *nat;
   6939  1.1  christos {
   6940  1.1  christos 	ipftq_t *oifq, *nifq;
   6941  1.1  christos 	int rev = nat->nat_rev;
   6942  1.1  christos 
   6943  1.1  christos 	if (nat->nat_ptr != NULL)
   6944  1.1  christos 		nifq = nat->nat_ptr->in_tqehead[rev];
   6945  1.1  christos 	else
   6946  1.1  christos 		nifq = NULL;
   6947  1.1  christos 
   6948  1.1  christos 	if (nifq == NULL) {
   6949  1.1  christos 		switch (nat->nat_pr[0])
   6950  1.1  christos 		{
   6951  1.1  christos 		case IPPROTO_UDP :
   6952  1.1  christos 			nifq = &softn->ipf_nat_udptq;
   6953  1.1  christos 			break;
   6954  1.1  christos 		case IPPROTO_ICMP :
   6955  1.1  christos 			nifq = &softn->ipf_nat_icmptq;
   6956  1.1  christos 			break;
   6957  1.1  christos 		case IPPROTO_TCP :
   6958  1.1  christos 			nifq = softn->ipf_nat_tcptq +
   6959  1.1  christos 			       nat->nat_tqe.tqe_state[rev];
   6960  1.1  christos 			break;
   6961  1.1  christos 		default :
   6962  1.1  christos 			nifq = &softn->ipf_nat_iptq;
   6963  1.1  christos 			break;
   6964  1.1  christos 		}
   6965  1.1  christos 	}
   6966  1.1  christos 
   6967  1.1  christos 	oifq = nat->nat_tqe.tqe_ifq;
   6968  1.1  christos 	/*
   6969  1.1  christos 	 * If it's currently on a timeout queue, move it from one queue to
   6970  1.1  christos 	 * another, else put it on the end of the newly determined queue.
   6971  1.1  christos 	 */
   6972  1.1  christos 	if (oifq != NULL)
   6973  1.1  christos 		ipf_movequeue(softc->ipf_ticks, &nat->nat_tqe, oifq, nifq);
   6974  1.1  christos 	else
   6975  1.1  christos 		ipf_queueappend(softc->ipf_ticks, &nat->nat_tqe, nifq, nat);
   6976  1.1  christos 	return;
   6977  1.1  christos }
   6978  1.1  christos 
   6979  1.1  christos 
   6980  1.1  christos /* ------------------------------------------------------------------------ */
   6981  1.1  christos /* Function:    nat_getnext                                                 */
   6982  1.1  christos /* Returns:     int - 0 == ok, else error                                   */
   6983  1.1  christos /* Parameters:  t(I)   - pointer to ipftoken structure                      */
   6984  1.1  christos /*              itp(I) - pointer to ipfgeniter_t structure                  */
   6985  1.1  christos /*                                                                          */
   6986  1.1  christos /* Fetch the next nat/ipnat structure pointer from the linked list and      */
   6987  1.1  christos /* copy it out to the storage space pointed to by itp_data.  The next item  */
   6988  1.1  christos /* in the list to look at is put back in the ipftoken struture.             */
   6989  1.1  christos /* ------------------------------------------------------------------------ */
   6990  1.1  christos static int
   6991  1.1  christos ipf_nat_getnext(softc, t, itp, objp)
   6992  1.1  christos 	ipf_main_softc_t *softc;
   6993  1.1  christos 	ipftoken_t *t;
   6994  1.1  christos 	ipfgeniter_t *itp;
   6995  1.1  christos 	ipfobj_t *objp;
   6996  1.1  christos {
   6997  1.1  christos 	ipf_nat_softc_t *softn = softc->ipf_nat_soft;
   6998  1.1  christos 	hostmap_t *hm, *nexthm = NULL, zerohm;
   6999  1.1  christos 	ipnat_t *ipn, *nextipnat = NULL, zeroipn;
   7000  1.1  christos 	nat_t *nat, *nextnat = NULL, zeronat;
   7001  1.1  christos 	int error = 0;
   7002  1.1  christos 	void *nnext;
   7003  1.1  christos 
   7004  1.1  christos 	if (itp->igi_nitems != 1) {
   7005  1.1  christos 		IPFERROR(60075);
   7006  1.1  christos 		return ENOSPC;
   7007  1.1  christos 	}
   7008  1.1  christos 
   7009  1.1  christos 	READ_ENTER(&softc->ipf_nat);
   7010  1.1  christos 
   7011  1.1  christos 	switch (itp->igi_type)
   7012  1.1  christos 	{
   7013  1.1  christos 	case IPFGENITER_HOSTMAP :
   7014  1.1  christos 		hm = t->ipt_data;
   7015  1.1  christos 		if (hm == NULL) {
   7016  1.1  christos 			nexthm = softn->ipf_hm_maplist;
   7017  1.1  christos 		} else {
   7018  1.1  christos 			nexthm = hm->hm_next;
   7019  1.1  christos 		}
   7020  1.1  christos 		if (nexthm != NULL) {
   7021  1.1  christos 			ATOMIC_INC32(nexthm->hm_ref);
   7022  1.1  christos 			t->ipt_data = nexthm;
   7023  1.1  christos 		} else {
   7024  1.1  christos 			bzero(&zerohm, sizeof(zerohm));
   7025  1.1  christos 			nexthm = &zerohm;
   7026  1.1  christos 			t->ipt_data = NULL;
   7027  1.1  christos 		}
   7028  1.1  christos 		nnext = nexthm->hm_next;
   7029  1.1  christos 		break;
   7030  1.1  christos 
   7031  1.1  christos 	case IPFGENITER_IPNAT :
   7032  1.1  christos 		ipn = t->ipt_data;
   7033  1.1  christos 		if (ipn == NULL) {
   7034  1.1  christos 			nextipnat = softn->ipf_nat_list;
   7035  1.1  christos 		} else {
   7036  1.1  christos 			nextipnat = ipn->in_next;
   7037  1.1  christos 		}
   7038  1.1  christos 		if (nextipnat != NULL) {
   7039  1.1  christos 			ATOMIC_INC32(nextipnat->in_use);
   7040  1.1  christos 			t->ipt_data = nextipnat;
   7041  1.1  christos 		} else {
   7042  1.1  christos 			bzero(&zeroipn, sizeof(zeroipn));
   7043  1.1  christos 			nextipnat = &zeroipn;
   7044  1.1  christos 			t->ipt_data = NULL;
   7045  1.1  christos 		}
   7046  1.1  christos 		nnext = nextipnat->in_next;
   7047  1.1  christos 		break;
   7048  1.1  christos 
   7049  1.1  christos 	case IPFGENITER_NAT :
   7050  1.1  christos 		nat = t->ipt_data;
   7051  1.1  christos 		if (nat == NULL) {
   7052  1.1  christos 			nextnat = softn->ipf_nat_instances;
   7053  1.1  christos 		} else {
   7054  1.1  christos 			nextnat = nat->nat_next;
   7055  1.1  christos 		}
   7056  1.1  christos 		if (nextnat != NULL) {
   7057  1.1  christos 			MUTEX_ENTER(&nextnat->nat_lock);
   7058  1.1  christos 			nextnat->nat_ref++;
   7059  1.1  christos 			MUTEX_EXIT(&nextnat->nat_lock);
   7060  1.1  christos 			t->ipt_data = nextnat;
   7061  1.1  christos 		} else {
   7062  1.1  christos 			bzero(&zeronat, sizeof(zeronat));
   7063  1.1  christos 			nextnat = &zeronat;
   7064  1.1  christos 			t->ipt_data = NULL;
   7065  1.1  christos 		}
   7066  1.1  christos 		nnext = nextnat->nat_next;
   7067  1.1  christos 		break;
   7068  1.1  christos 
   7069  1.1  christos 	default :
   7070  1.1  christos 		RWLOCK_EXIT(&softc->ipf_nat);
   7071  1.1  christos 		IPFERROR(60055);
   7072  1.1  christos 		return EINVAL;
   7073  1.1  christos 	}
   7074  1.1  christos 
   7075  1.1  christos 	RWLOCK_EXIT(&softc->ipf_nat);
   7076  1.1  christos 
   7077  1.1  christos 	objp->ipfo_ptr = itp->igi_data;
   7078  1.1  christos 
   7079  1.1  christos 	switch (itp->igi_type)
   7080  1.1  christos 	{
   7081  1.1  christos 	case IPFGENITER_HOSTMAP :
   7082  1.1  christos 		error = COPYOUT(nexthm, objp->ipfo_ptr, sizeof(*nexthm));
   7083  1.1  christos 		if (error != 0) {
   7084  1.1  christos 			IPFERROR(60049);
   7085  1.1  christos 			error = EFAULT;
   7086  1.1  christos 		}
   7087  1.1  christos 		if (hm != NULL) {
   7088  1.1  christos 			WRITE_ENTER(&softc->ipf_nat);
   7089  1.1  christos 			ipf_nat_hostmapdel(&hm);
   7090  1.1  christos 			RWLOCK_EXIT(&softc->ipf_nat);
   7091  1.1  christos 		}
   7092  1.1  christos 		break;
   7093  1.1  christos 
   7094  1.1  christos 	case IPFGENITER_IPNAT :
   7095  1.1  christos 		objp->ipfo_size = nextipnat->in_size;
   7096  1.1  christos 		objp->ipfo_type = IPFOBJ_IPNAT;
   7097  1.1  christos 		error = ipf_outobjk(softc, objp, nextipnat);
   7098  1.1  christos 		if (ipn != NULL) {
   7099  1.1  christos 			WRITE_ENTER(&softc->ipf_nat);
   7100  1.1  christos 			ipf_nat_rulederef(softc, &ipn);
   7101  1.1  christos 			RWLOCK_EXIT(&softc->ipf_nat);
   7102  1.1  christos 		}
   7103  1.1  christos 		break;
   7104  1.1  christos 
   7105  1.1  christos 	case IPFGENITER_NAT :
   7106  1.1  christos 		objp->ipfo_size = sizeof(nat_t);
   7107  1.1  christos 		objp->ipfo_type = IPFOBJ_NAT;
   7108  1.1  christos 		error = ipf_outobjk(softc, objp, nextnat);
   7109  1.1  christos 		if (nat != NULL)
   7110  1.1  christos 			ipf_nat_deref(softc, &nat);
   7111  1.1  christos 
   7112  1.1  christos 		break;
   7113  1.1  christos 	}
   7114  1.1  christos 
   7115  1.1  christos 	if (nnext == NULL)
   7116  1.1  christos 		ipf_token_mark_complete(t);
   7117  1.1  christos 
   7118  1.1  christos 	return error;
   7119  1.1  christos }
   7120  1.1  christos 
   7121  1.1  christos 
   7122  1.1  christos /* ------------------------------------------------------------------------ */
   7123  1.1  christos /* Function:    nat_extraflush                                              */
   7124  1.1  christos /* Returns:     int - 0 == success, -1 == failure                           */
   7125  1.1  christos /* Parameters:  which(I) - how to flush the active NAT table                */
   7126  1.1  christos /* Write Locks: ipf_nat                                                     */
   7127  1.1  christos /*                                                                          */
   7128  1.1  christos /* Flush nat tables.  Three actions currently defined:                      */
   7129  1.1  christos /* which == 0 : flush all nat table entries                                 */
   7130  1.1  christos /* which == 1 : flush TCP connections which have started to close but are   */
   7131  1.1  christos /*	      stuck for some reason.                                        */
   7132  1.1  christos /* which == 2 : flush TCP connections which have been idle for a long time, */
   7133  1.1  christos /*	      starting at > 4 days idle and working back in successive half-*/
   7134  1.1  christos /*	      days to at most 12 hours old.  If this fails to free enough   */
   7135  1.1  christos /*            slots then work backwards in half hour slots to 30 minutes.   */
   7136  1.1  christos /*            If that too fails, then work backwards in 30 second intervals */
   7137  1.1  christos /*            for the last 30 minutes to at worst 30 seconds idle.          */
   7138  1.1  christos /* ------------------------------------------------------------------------ */
   7139  1.1  christos static int
   7140  1.1  christos ipf_nat_extraflush(softc, softn, which)
   7141  1.1  christos 	ipf_main_softc_t *softc;
   7142  1.1  christos 	ipf_nat_softc_t *softn;
   7143  1.1  christos 	int which;
   7144  1.1  christos {
   7145  1.1  christos 	nat_t *nat, **natp;
   7146  1.1  christos 	ipftqent_t *tqn;
   7147  1.1  christos 	ipftq_t *ifq;
   7148  1.1  christos 	int removed;
   7149  1.1  christos 	SPL_INT(s);
   7150  1.1  christos 
   7151  1.1  christos 	removed = 0;
   7152  1.1  christos 
   7153  1.1  christos 	SPL_NET(s);
   7154  1.1  christos 	switch (which)
   7155  1.1  christos 	{
   7156  1.1  christos 	case 0 :
   7157  1.1  christos 		softn->ipf_nat_stats.ns_flush_all++;
   7158  1.1  christos 		/*
   7159  1.1  christos 		 * Style 0 flush removes everything...
   7160  1.1  christos 		 */
   7161  1.1  christos 		for (natp = &softn->ipf_nat_instances;
   7162  1.1  christos 		     ((nat = *natp) != NULL); ) {
   7163  1.1  christos 			ipf_nat_delete(softc, nat, NL_FLUSH);
   7164  1.1  christos 			removed++;
   7165  1.1  christos 		}
   7166  1.1  christos 		break;
   7167  1.1  christos 
   7168  1.1  christos 	case 1 :
   7169  1.1  christos 		softn->ipf_nat_stats.ns_flush_closing++;
   7170  1.1  christos 		/*
   7171  1.1  christos 		 * Since we're only interested in things that are closing,
   7172  1.1  christos 		 * we can start with the appropriate timeout queue.
   7173  1.1  christos 		 */
   7174  1.1  christos 		for (ifq = softn->ipf_nat_tcptq + IPF_TCPS_CLOSE_WAIT;
   7175  1.1  christos 		     ifq != NULL; ifq = ifq->ifq_next) {
   7176  1.1  christos 
   7177  1.1  christos 			for (tqn = ifq->ifq_head; tqn != NULL; ) {
   7178  1.1  christos 				nat = tqn->tqe_parent;
   7179  1.1  christos 				tqn = tqn->tqe_next;
   7180  1.1  christos 				if (nat->nat_pr[0] != IPPROTO_TCP ||
   7181  1.1  christos 				    nat->nat_pr[1] != IPPROTO_TCP)
   7182  1.1  christos 					break;
   7183  1.1  christos 				ipf_nat_delete(softc, nat, NL_EXPIRE);
   7184  1.1  christos 				removed++;
   7185  1.1  christos 			}
   7186  1.1  christos 		}
   7187  1.1  christos 
   7188  1.1  christos 		/*
   7189  1.1  christos 		 * Also need to look through the user defined queues.
   7190  1.1  christos 		 */
   7191  1.1  christos 		for (ifq = softn->ipf_nat_utqe; ifq != NULL;
   7192  1.1  christos 		     ifq = ifq->ifq_next) {
   7193  1.1  christos 			for (tqn = ifq->ifq_head; tqn != NULL; ) {
   7194  1.1  christos 				nat = tqn->tqe_parent;
   7195  1.1  christos 				tqn = tqn->tqe_next;
   7196  1.1  christos 				if (nat->nat_pr[0] != IPPROTO_TCP ||
   7197  1.1  christos 				    nat->nat_pr[1] != IPPROTO_TCP)
   7198  1.1  christos 					continue;
   7199  1.1  christos 
   7200  1.1  christos 				if ((nat->nat_tcpstate[0] >
   7201  1.1  christos 				     IPF_TCPS_ESTABLISHED) &&
   7202  1.1  christos 				    (nat->nat_tcpstate[1] >
   7203  1.1  christos 				     IPF_TCPS_ESTABLISHED)) {
   7204  1.1  christos 					ipf_nat_delete(softc, nat, NL_EXPIRE);
   7205  1.1  christos 					removed++;
   7206  1.1  christos 				}
   7207  1.1  christos 			}
   7208  1.1  christos 		}
   7209  1.1  christos 		break;
   7210  1.1  christos 
   7211  1.1  christos 		/*
   7212  1.1  christos 		 * Args 5-11 correspond to flushing those particular states
   7213  1.1  christos 		 * for TCP connections.
   7214  1.1  christos 		 */
   7215  1.1  christos 	case IPF_TCPS_CLOSE_WAIT :
   7216  1.1  christos 	case IPF_TCPS_FIN_WAIT_1 :
   7217  1.1  christos 	case IPF_TCPS_CLOSING :
   7218  1.1  christos 	case IPF_TCPS_LAST_ACK :
   7219  1.1  christos 	case IPF_TCPS_FIN_WAIT_2 :
   7220  1.1  christos 	case IPF_TCPS_TIME_WAIT :
   7221  1.1  christos 	case IPF_TCPS_CLOSED :
   7222  1.1  christos 		softn->ipf_nat_stats.ns_flush_state++;
   7223  1.1  christos 		tqn = softn->ipf_nat_tcptq[which].ifq_head;
   7224  1.1  christos 		while (tqn != NULL) {
   7225  1.1  christos 			nat = tqn->tqe_parent;
   7226  1.1  christos 			tqn = tqn->tqe_next;
   7227  1.1  christos 			ipf_nat_delete(softc, nat, NL_FLUSH);
   7228  1.1  christos 			removed++;
   7229  1.1  christos 		}
   7230  1.1  christos 		break;
   7231  1.1  christos 
   7232  1.1  christos 	default :
   7233  1.1  christos 		if (which < 30)
   7234  1.1  christos 			break;
   7235  1.1  christos 
   7236  1.1  christos 		softn->ipf_nat_stats.ns_flush_timeout++;
   7237  1.1  christos 		/*
   7238  1.1  christos 		 * Take a large arbitrary number to mean the number of seconds
   7239  1.1  christos 		 * for which which consider to be the maximum value we'll allow
   7240  1.1  christos 		 * the expiration to be.
   7241  1.1  christos 		 */
   7242  1.1  christos 		which = IPF_TTLVAL(which);
   7243  1.1  christos 		for (natp = &softn->ipf_nat_instances;
   7244  1.1  christos 		     ((nat = *natp) != NULL); ) {
   7245  1.1  christos 			if (softc->ipf_ticks - nat->nat_touched > which) {
   7246  1.1  christos 				ipf_nat_delete(softc, nat, NL_FLUSH);
   7247  1.1  christos 				removed++;
   7248  1.1  christos 			} else
   7249  1.1  christos 				natp = &nat->nat_next;
   7250  1.1  christos 		}
   7251  1.1  christos 		break;
   7252  1.1  christos 	}
   7253  1.1  christos 
   7254  1.1  christos 	if (which != 2) {
   7255  1.1  christos 		SPL_X(s);
   7256  1.1  christos 		return removed;
   7257  1.1  christos 	}
   7258  1.1  christos 
   7259  1.1  christos 	softn->ipf_nat_stats.ns_flush_queue++;
   7260  1.1  christos 
   7261  1.1  christos 	/*
   7262  1.1  christos 	 * Asked to remove inactive entries because the table is full, try
   7263  1.1  christos 	 * again, 3 times, if first attempt failed with a different criteria
   7264  1.1  christos 	 * each time.  The order tried in must be in decreasing age.
   7265  1.1  christos 	 * Another alternative is to implement random drop and drop N entries
   7266  1.1  christos 	 * at random until N have been freed up.
   7267  1.1  christos 	 */
   7268  1.1  christos 	if (softc->ipf_ticks - softn->ipf_nat_last_force_flush >
   7269  1.1  christos 	    IPF_TTLVAL(5)) {
   7270  1.1  christos 		softn->ipf_nat_last_force_flush = softc->ipf_ticks;
   7271  1.1  christos 
   7272  1.1  christos 		removed = ipf_queueflush(softc, ipf_nat_flush_entry,
   7273  1.1  christos 					 softn->ipf_nat_tcptq,
   7274  1.1  christos 					 softn->ipf_nat_utqe,
   7275  1.1  christos 					 &softn->ipf_nat_stats.ns_active,
   7276  1.1  christos 					 softn->ipf_nat_table_sz,
   7277  1.1  christos 					 softn->ipf_nat_table_wm_low);
   7278  1.1  christos 	}
   7279  1.1  christos 
   7280  1.1  christos 	SPL_X(s);
   7281  1.1  christos 	return removed;
   7282  1.1  christos }
   7283  1.1  christos 
   7284  1.1  christos 
   7285  1.1  christos /* ------------------------------------------------------------------------ */
   7286  1.1  christos /* Function:    ipf_nat_flush_entry                                         */
   7287  1.1  christos /* Returns:     0 - always succeeds                                         */
   7288  1.1  christos /* Parameters:  entry(I) - pointer to NAT entry                             */
   7289  1.1  christos /* Write Locks: ipf_nat                                                     */
   7290  1.1  christos /*                                                                          */
   7291  1.1  christos /* This function is a stepping stone between ipf_queueflush() and           */
   7292  1.1  christos /* nat_dlete().  It is used so we can provide a uniform interface via the   */
   7293  1.1  christos /* ipf_queueflush() function.  Since the nat_delete() function returns void */
   7294  1.1  christos /* we translate that to mean it always succeeds in deleting something.      */
   7295  1.1  christos /* ------------------------------------------------------------------------ */
   7296  1.1  christos static int
   7297  1.1  christos ipf_nat_flush_entry(softc, entry)
   7298  1.1  christos 	ipf_main_softc_t *softc;
   7299  1.1  christos 	void *entry;
   7300  1.1  christos {
   7301  1.1  christos 	ipf_nat_delete(softc, entry, NL_FLUSH);
   7302  1.1  christos 	return 0;
   7303  1.1  christos }
   7304  1.1  christos 
   7305  1.1  christos 
   7306  1.1  christos /* ------------------------------------------------------------------------ */
   7307  1.1  christos /* Function:    ipf_nat_iterator                                            */
   7308  1.1  christos /* Returns:     int - 0 == ok, else error                                   */
   7309  1.1  christos /* Parameters:  token(I) - pointer to ipftoken structure                    */
   7310  1.1  christos /*              itp(I) - pointer to ipfgeniter_t structure                  */
   7311  1.1  christos /*                                                                          */
   7312  1.1  christos /* This function acts as a handler for the SIOCGENITER ioctls that use a    */
   7313  1.1  christos /* generic structure to iterate through a list.  There are three different  */
   7314  1.1  christos /* linked lists of NAT related information to go through: NAT rules, active */
   7315  1.1  christos /* NAT mappings and the NAT fragment cache.                                 */
   7316  1.1  christos /* ------------------------------------------------------------------------ */
   7317  1.1  christos static int
   7318  1.1  christos ipf_nat_iterator(softc, token, itp, obj)
   7319  1.1  christos 	ipf_main_softc_t *softc;
   7320  1.1  christos 	ipftoken_t *token;
   7321  1.1  christos 	ipfgeniter_t *itp;
   7322  1.1  christos 	ipfobj_t *obj;
   7323  1.1  christos {
   7324  1.1  christos 	int error;
   7325  1.1  christos 
   7326  1.1  christos 	if (itp->igi_data == NULL) {
   7327  1.1  christos 		IPFERROR(60052);
   7328  1.1  christos 		return EFAULT;
   7329  1.1  christos 	}
   7330  1.1  christos 
   7331  1.1  christos 	switch (itp->igi_type)
   7332  1.1  christos 	{
   7333  1.1  christos 	case IPFGENITER_HOSTMAP :
   7334  1.1  christos 	case IPFGENITER_IPNAT :
   7335  1.1  christos 	case IPFGENITER_NAT :
   7336  1.1  christos 		error = ipf_nat_getnext(softc, token, itp, obj);
   7337  1.1  christos 		break;
   7338  1.1  christos 
   7339  1.1  christos 	case IPFGENITER_NATFRAG :
   7340  1.1  christos 		error = ipf_frag_nat_next(softc, token, itp);
   7341  1.1  christos 		break;
   7342  1.1  christos 	default :
   7343  1.1  christos 		IPFERROR(60053);
   7344  1.1  christos 		error = EINVAL;
   7345  1.1  christos 		break;
   7346  1.1  christos 	}
   7347  1.1  christos 
   7348  1.1  christos 	return error;
   7349  1.1  christos }
   7350  1.1  christos 
   7351  1.1  christos 
   7352  1.1  christos /* ------------------------------------------------------------------------ */
   7353  1.1  christos /* Function:    ipf_nat_setpending                                          */
   7354  1.1  christos /* Returns:     Nil                                                         */
   7355  1.1  christos /* Parameters:  nat(I) - pointer to NAT structure                           */
   7356  1.1  christos /* Locks:       ipf_nat (read or write)                                     */
   7357  1.1  christos /*                                                                          */
   7358  1.1  christos /* Put the NAT entry on to the pending queue - this queue has a very short  */
   7359  1.1  christos /* lifetime where items are put that can't be deleted straight away because */
   7360  1.1  christos /* of locking issues but we want to delete them ASAP, anyway.  In calling   */
   7361  1.1  christos /* this function, it is assumed that the owner (if there is one, as shown   */
   7362  1.1  christos /* by nat_me) is no longer interested in it.                                */
   7363  1.1  christos /* ------------------------------------------------------------------------ */
   7364  1.1  christos void
   7365  1.1  christos ipf_nat_setpending(softc, nat)
   7366  1.1  christos 	ipf_main_softc_t *softc;
   7367  1.1  christos 	nat_t *nat;
   7368  1.1  christos {
   7369  1.1  christos 	ipf_nat_softc_t *softn = softc->ipf_nat_soft;
   7370  1.1  christos 	ipftq_t *oifq;
   7371  1.1  christos 
   7372  1.1  christos 	oifq = nat->nat_tqe.tqe_ifq;
   7373  1.1  christos 	if (oifq != NULL)
   7374  1.1  christos 		ipf_movequeue(softc->ipf_ticks, &nat->nat_tqe, oifq,
   7375  1.1  christos 			      &softn->ipf_nat_pending);
   7376  1.1  christos 	else
   7377  1.1  christos 		ipf_queueappend(softc->ipf_ticks, &nat->nat_tqe,
   7378  1.1  christos 				&softn->ipf_nat_pending, nat);
   7379  1.1  christos 
   7380  1.1  christos 	if (nat->nat_me != NULL) {
   7381  1.1  christos 		*nat->nat_me = NULL;
   7382  1.1  christos 		nat->nat_me = NULL;
   7383  1.1  christos 		nat->nat_ref--;
   7384  1.1  christos 	}
   7385  1.1  christos }
   7386  1.1  christos 
   7387  1.1  christos 
   7388  1.1  christos /* ------------------------------------------------------------------------ */
   7389  1.1  christos /* Function:    nat_newrewrite                                              */
   7390  1.1  christos /* Returns:     int - -1 == error, 0 == success (no move), 1 == success and */
   7391  1.1  christos /*                    allow rule to be moved if IPN_ROUNDR is set.          */
   7392  1.1  christos /* Parameters:  fin(I) - pointer to packet information                      */
   7393  1.1  christos /*              nat(I) - pointer to NAT entry                               */
   7394  1.1  christos /*              ni(I)  - pointer to structure with misc. information needed */
   7395  1.1  christos /*                       to create new NAT entry.                           */
   7396  1.1  christos /* Write Lock:  ipf_nat                                                     */
   7397  1.1  christos /*                                                                          */
   7398  1.1  christos /* This function is responsible for setting up an active NAT session where  */
   7399  1.1  christos /* we are changing both the source and destination parameters at the same   */
   7400  1.1  christos /* time.  The loop in here works differently to elsewhere - each iteration  */
   7401  1.1  christos /* is responsible for changing a single parameter that can be incremented.  */
   7402  1.1  christos /* So one pass may increase the source IP#, next source port, next dest. IP#*/
   7403  1.1  christos /* and the last destination port for a total of 4 iterations to try each.   */
   7404  1.1  christos /* This is done to try and exhaustively use the translation space available.*/
   7405  1.1  christos /* ------------------------------------------------------------------------ */
   7406  1.1  christos static int
   7407  1.1  christos ipf_nat_newrewrite(fin, nat, nai)
   7408  1.1  christos 	fr_info_t *fin;
   7409  1.1  christos 	nat_t *nat;
   7410  1.1  christos 	natinfo_t *nai;
   7411  1.1  christos {
   7412  1.1  christos 	int src_search = 1;
   7413  1.1  christos 	int dst_search = 1;
   7414  1.1  christos 	fr_info_t frnat;
   7415  1.1  christos 	u_32_t flags;
   7416  1.1  christos 	u_short swap;
   7417  1.1  christos 	ipnat_t *np;
   7418  1.1  christos 	nat_t *natl;
   7419  1.1  christos 	int l = 0;
   7420  1.1  christos 	int changed;
   7421  1.1  christos 
   7422  1.1  christos 	natl = NULL;
   7423  1.1  christos 	changed = -1;
   7424  1.1  christos 	np = nai->nai_np;
   7425  1.1  christos 	flags = nat->nat_flags;
   7426  1.1  christos 	bcopy((char *)fin, (char *)&frnat, sizeof(*fin));
   7427  1.1  christos 
   7428  1.1  christos 	nat->nat_hm = NULL;
   7429  1.1  christos 
   7430  1.1  christos 	do {
   7431  1.1  christos 		changed = -1;
   7432  1.1  christos 		/* TRACE (l, src_search, dst_search, np) */
   7433  1.1  christos 
   7434  1.1  christos 		if ((src_search == 0) && (np->in_spnext == 0) &&
   7435  1.1  christos 		    (dst_search == 0) && (np->in_dpnext == 0)) {
   7436  1.1  christos 			if (l > 0)
   7437  1.1  christos 				return -1;
   7438  1.1  christos 		}
   7439  1.1  christos 
   7440  1.1  christos 		/*
   7441  1.1  christos 		 * Find a new source address
   7442  1.1  christos 		 */
   7443  1.1  christos 		if (ipf_nat_nextaddr(fin, &np->in_nsrc, &frnat.fin_saddr,
   7444  1.1  christos 				     &frnat.fin_saddr) == -1) {
   7445  1.1  christos 			return -1;
   7446  1.1  christos 		}
   7447  1.1  christos 
   7448  1.1  christos 		if ((np->in_nsrcaddr == 0) && (np->in_nsrcmsk == 0xffffffff)) {
   7449  1.1  christos 			src_search = 0;
   7450  1.1  christos 			if (np->in_stepnext == 0)
   7451  1.1  christos 				np->in_stepnext = 1;
   7452  1.1  christos 
   7453  1.1  christos 		} else if ((np->in_nsrcaddr == 0) && (np->in_nsrcmsk == 0)) {
   7454  1.1  christos 			src_search = 0;
   7455  1.1  christos 			if (np->in_stepnext == 0)
   7456  1.1  christos 				np->in_stepnext = 1;
   7457  1.1  christos 
   7458  1.1  christos 		} else if (np->in_nsrcmsk == 0xffffffff) {
   7459  1.1  christos 			src_search = 0;
   7460  1.1  christos 			if (np->in_stepnext == 0)
   7461  1.1  christos 				np->in_stepnext = 1;
   7462  1.1  christos 
   7463  1.1  christos 		} else if (np->in_nsrcmsk != 0xffffffff) {
   7464  1.1  christos 			if (np->in_stepnext == 0 && changed == -1) {
   7465  1.1  christos 				np->in_snip++;
   7466  1.1  christos 				np->in_stepnext++;
   7467  1.1  christos 				changed = 0;
   7468  1.1  christos 			}
   7469  1.1  christos 		}
   7470  1.1  christos 
   7471  1.1  christos 		if ((flags & IPN_TCPUDPICMP) != 0) {
   7472  1.1  christos 			if (np->in_spnext != 0)
   7473  1.1  christos 				frnat.fin_data[0] = np->in_spnext;
   7474  1.1  christos 
   7475  1.1  christos 			/*
   7476  1.1  christos 			 * Standard port translation.  Select next port.
   7477  1.1  christos 			 */
   7478  1.1  christos 			if ((flags & IPN_FIXEDSPORT) != 0) {
   7479  1.1  christos 				np->in_stepnext = 2;
   7480  1.1  christos 			} else if ((np->in_stepnext == 1) &&
   7481  1.1  christos 				   (changed == -1) && (natl != NULL)) {
   7482  1.1  christos 				np->in_spnext++;
   7483  1.1  christos 				np->in_stepnext++;
   7484  1.1  christos 				changed = 1;
   7485  1.1  christos 				if (np->in_spnext > np->in_spmax)
   7486  1.1  christos 					np->in_spnext = np->in_spmin;
   7487  1.1  christos 			}
   7488  1.1  christos 		} else {
   7489  1.1  christos 			np->in_stepnext = 2;
   7490  1.1  christos 		}
   7491  1.1  christos 		np->in_stepnext &= 0x3;
   7492  1.1  christos 
   7493  1.1  christos 		/*
   7494  1.1  christos 		 * Find a new destination address
   7495  1.1  christos 		 */
   7496  1.1  christos 		/* TRACE (fin, np, l, frnat) */
   7497  1.1  christos 
   7498  1.1  christos 		if (ipf_nat_nextaddr(fin, &np->in_ndst, &frnat.fin_daddr,
   7499  1.1  christos 				     &frnat.fin_daddr) == -1)
   7500  1.1  christos 			return -1;
   7501  1.1  christos 		if ((np->in_ndstaddr == 0) && (np->in_ndstmsk == 0xffffffff)) {
   7502  1.1  christos 			dst_search = 0;
   7503  1.1  christos 			if (np->in_stepnext == 2)
   7504  1.1  christos 				np->in_stepnext = 3;
   7505  1.1  christos 
   7506  1.1  christos 		} else if ((np->in_ndstaddr == 0) && (np->in_ndstmsk == 0)) {
   7507  1.1  christos 			dst_search = 0;
   7508  1.1  christos 			if (np->in_stepnext == 2)
   7509  1.1  christos 				np->in_stepnext = 3;
   7510  1.1  christos 
   7511  1.1  christos 		} else if (np->in_ndstmsk == 0xffffffff) {
   7512  1.1  christos 			dst_search = 0;
   7513  1.1  christos 			if (np->in_stepnext == 2)
   7514  1.1  christos 				np->in_stepnext = 3;
   7515  1.1  christos 
   7516  1.1  christos 		} else if (np->in_ndstmsk != 0xffffffff) {
   7517  1.1  christos 			if ((np->in_stepnext == 2) && (changed == -1) &&
   7518  1.1  christos 			    (natl != NULL)) {
   7519  1.1  christos 				changed = 2;
   7520  1.1  christos 				np->in_stepnext++;
   7521  1.1  christos 				np->in_dnip++;
   7522  1.1  christos 			}
   7523  1.1  christos 		}
   7524  1.1  christos 
   7525  1.1  christos 		if ((flags & IPN_TCPUDPICMP) != 0) {
   7526  1.1  christos 			if (np->in_dpnext != 0)
   7527  1.1  christos 				frnat.fin_data[1] = np->in_dpnext;
   7528  1.1  christos 
   7529  1.1  christos 			/*
   7530  1.1  christos 			 * Standard port translation.  Select next port.
   7531  1.1  christos 			 */
   7532  1.1  christos 			if ((flags & IPN_FIXEDDPORT) != 0) {
   7533  1.1  christos 				np->in_stepnext = 0;
   7534  1.1  christos 			} else if (np->in_stepnext == 3 && changed == -1) {
   7535  1.1  christos 				np->in_dpnext++;
   7536  1.1  christos 				np->in_stepnext++;
   7537  1.1  christos 				changed = 3;
   7538  1.1  christos 				if (np->in_dpnext > np->in_dpmax)
   7539  1.1  christos 					np->in_dpnext = np->in_dpmin;
   7540  1.1  christos 			}
   7541  1.1  christos 		} else {
   7542  1.1  christos 			if (np->in_stepnext == 3)
   7543  1.1  christos 				np->in_stepnext = 0;
   7544  1.1  christos 		}
   7545  1.1  christos 
   7546  1.1  christos 		/* TRACE (frnat) */
   7547  1.1  christos 
   7548  1.1  christos 		/*
   7549  1.1  christos 		 * Here we do a lookup of the connection as seen from
   7550  1.1  christos 		 * the outside.  If an IP# pair already exists, try
   7551  1.1  christos 		 * again.  So if you have A->B becomes C->B, you can
   7552  1.1  christos 		 * also have D->E become C->E but not D->B causing
   7553  1.1  christos 		 * another C->B.  Also take protocol and ports into
   7554  1.1  christos 		 * account when determining whether a pre-existing
   7555  1.1  christos 		 * NAT setup will cause an external conflict where
   7556  1.1  christos 		 * this is appropriate.
   7557  1.1  christos 		 *
   7558  1.1  christos 		 * fin_data[] is swapped around because we are doing a
   7559  1.1  christos 		 * lookup of the packet is if it were moving in the opposite
   7560  1.1  christos 		 * direction of the one we are working with now.
   7561  1.1  christos 		 */
   7562  1.1  christos 		if (flags & IPN_TCPUDP) {
   7563  1.1  christos 			swap = frnat.fin_data[0];
   7564  1.1  christos 			frnat.fin_data[0] = frnat.fin_data[1];
   7565  1.1  christos 			frnat.fin_data[1] = swap;
   7566  1.1  christos 		}
   7567  1.1  christos 		if (fin->fin_out == 1) {
   7568  1.1  christos 			natl = ipf_nat_inlookup(&frnat,
   7569  1.1  christos 						flags & ~(SI_WILDP|NAT_SEARCH),
   7570  1.1  christos 						(u_int)frnat.fin_p,
   7571  1.1  christos 						frnat.fin_dst, frnat.fin_src);
   7572  1.1  christos 
   7573  1.1  christos 		} else {
   7574  1.1  christos 			natl = ipf_nat_outlookup(&frnat,
   7575  1.1  christos 						 flags & ~(SI_WILDP|NAT_SEARCH),
   7576  1.1  christos 						 (u_int)frnat.fin_p,
   7577  1.1  christos 						 frnat.fin_dst, frnat.fin_src);
   7578  1.1  christos 		}
   7579  1.1  christos 		if (flags & IPN_TCPUDP) {
   7580  1.1  christos 			swap = frnat.fin_data[0];
   7581  1.1  christos 			frnat.fin_data[0] = frnat.fin_data[1];
   7582  1.1  christos 			frnat.fin_data[1] = swap;
   7583  1.1  christos 		}
   7584  1.1  christos 
   7585  1.1  christos 		/* TRACE natl, in_stepnext, l */
   7586  1.1  christos 
   7587  1.1  christos 		if ((natl != NULL) && (l > 8))	/* XXX 8 is arbitrary */
   7588  1.1  christos 			return -1;
   7589  1.1  christos 
   7590  1.1  christos 		np->in_stepnext &= 0x3;
   7591  1.1  christos 
   7592  1.1  christos 		l++;
   7593  1.1  christos 		changed = -1;
   7594  1.1  christos 	} while (natl != NULL);
   7595  1.1  christos 
   7596  1.1  christos 	nat->nat_osrcip = fin->fin_src;
   7597  1.1  christos 	nat->nat_odstip = fin->fin_dst;
   7598  1.1  christos 	nat->nat_nsrcip = frnat.fin_src;
   7599  1.1  christos 	nat->nat_ndstip = frnat.fin_dst;
   7600  1.1  christos 
   7601  1.1  christos 	if ((flags & IPN_TCPUDPICMP) != 0) {
   7602  1.1  christos 		nat->nat_osport = htons(fin->fin_data[0]);
   7603  1.1  christos 		nat->nat_odport = htons(fin->fin_data[1]);
   7604  1.1  christos 		nat->nat_nsport = htons(frnat.fin_data[0]);
   7605  1.1  christos 		nat->nat_ndport = htons(frnat.fin_data[1]);
   7606  1.1  christos 	}
   7607  1.1  christos 
   7608  1.1  christos 	return 0;
   7609  1.1  christos }
   7610  1.1  christos 
   7611  1.1  christos 
   7612  1.1  christos /* ------------------------------------------------------------------------ */
   7613  1.1  christos /* Function:    nat_newdivert                                               */
   7614  1.1  christos /* Returns:     int - -1 == error, 0 == success                             */
   7615  1.1  christos /* Parameters:  fin(I) - pointer to packet information                      */
   7616  1.1  christos /*              nat(I) - pointer to NAT entry                               */
   7617  1.1  christos /*              ni(I)  - pointer to structure with misc. information needed */
   7618  1.1  christos /*                       to create new NAT entry.                           */
   7619  1.1  christos /* Write Lock:  ipf_nat                                                     */
   7620  1.1  christos /*                                                                          */
   7621  1.1  christos /* Create a new NAT encap/divert session as defined by the NAT rule.  This  */
   7622  1.1  christos /* is somewhat different to other NAT session creation routines because we  */
   7623  1.1  christos /* do not iterate through either port numbers or IP addresses, searching    */
   7624  1.1  christos /* for a unique mapping, however, a complimentary duplicate check is made.  */
   7625  1.1  christos /* ------------------------------------------------------------------------ */
   7626  1.1  christos static int
   7627  1.1  christos ipf_nat_newdivert(fin, nat, nai)
   7628  1.1  christos 	fr_info_t *fin;
   7629  1.1  christos 	nat_t *nat;
   7630  1.1  christos 	natinfo_t *nai;
   7631  1.1  christos {
   7632  1.1  christos 	ipf_main_softc_t *softc = fin->fin_main_soft;
   7633  1.1  christos 	ipf_nat_softc_t *softn = softc->ipf_nat_soft;
   7634  1.1  christos 	fr_info_t frnat;
   7635  1.1  christos 	ipnat_t *np;
   7636  1.1  christos 	nat_t *natl;
   7637  1.1  christos 	int p;
   7638  1.1  christos 
   7639  1.1  christos 	np = nai->nai_np;
   7640  1.1  christos 	bcopy((char *)fin, (char *)&frnat, sizeof(*fin));
   7641  1.1  christos 
   7642  1.1  christos 	nat->nat_pr[0] = 0;
   7643  1.1  christos 	nat->nat_osrcaddr = fin->fin_saddr;
   7644  1.1  christos 	nat->nat_odstaddr = fin->fin_daddr;
   7645  1.1  christos 	nat->nat_osport = htons(fin->fin_data[0]);
   7646  1.1  christos 	nat->nat_odport = htons(fin->fin_data[1]);
   7647  1.1  christos 	frnat.fin_saddr = htonl(np->in_snip);
   7648  1.1  christos 	frnat.fin_daddr = htonl(np->in_dnip);
   7649  1.1  christos 
   7650  1.1  christos 	if (np->in_redir & NAT_DIVERTUDP) {
   7651  1.1  christos 		frnat.fin_data[0] = np->in_spnext;
   7652  1.1  christos 		frnat.fin_data[1] = np->in_dpnext;
   7653  1.1  christos 		frnat.fin_flx |= FI_TCPUDP;
   7654  1.1  christos 		p = IPPROTO_UDP;
   7655  1.1  christos 	} else {
   7656  1.1  christos 		frnat.fin_flx &= ~FI_TCPUDP;
   7657  1.1  christos 		p = IPPROTO_IPIP;
   7658  1.1  christos 	}
   7659  1.1  christos 
   7660  1.1  christos 	if (fin->fin_out == 1) {
   7661  1.1  christos 		natl = ipf_nat_inlookup(&frnat, 0, p,
   7662  1.1  christos 					frnat.fin_dst, frnat.fin_src);
   7663  1.1  christos 
   7664  1.1  christos 	} else {
   7665  1.1  christos 		natl = ipf_nat_outlookup(&frnat, 0, p,
   7666  1.1  christos 					 frnat.fin_dst, frnat.fin_src);
   7667  1.1  christos 	}
   7668  1.1  christos 
   7669  1.1  christos 	if (natl != NULL) {
   7670  1.1  christos 		NBUMPSIDED(fin->fin_out, ns_divert_exist);
   7671  1.1  christos 		return -1;
   7672  1.1  christos 	}
   7673  1.1  christos 
   7674  1.1  christos 	nat->nat_nsrcaddr = frnat.fin_saddr;
   7675  1.1  christos 	nat->nat_ndstaddr = frnat.fin_daddr;
   7676  1.1  christos 	if (np->in_redir & NAT_DIVERTUDP) {
   7677  1.1  christos 		nat->nat_nsport = htons(frnat.fin_data[0]);
   7678  1.1  christos 		nat->nat_ndport = htons(frnat.fin_data[1]);
   7679  1.1  christos 	}
   7680  1.1  christos 	nat->nat_pr[fin->fin_out] = fin->fin_p;
   7681  1.1  christos 	nat->nat_pr[1 - fin->fin_out] = p;
   7682  1.1  christos 
   7683  1.1  christos 	if (np->in_redir & NAT_ENCAP) {
   7684  1.1  christos 		if (np->in_redir & NAT_REDIRECT)
   7685  1.1  christos 			nat->nat_dir = NAT_ENCAPIN;
   7686  1.1  christos 		else
   7687  1.1  christos 			nat->nat_dir = NAT_ENCAPOUT;
   7688  1.1  christos 	} else {
   7689  1.1  christos 		if (np->in_redir & NAT_REDIRECT)
   7690  1.1  christos 			nat->nat_dir = NAT_DIVERTIN;
   7691  1.1  christos 		else
   7692  1.1  christos 			nat->nat_dir = NAT_DIVERTOUT;
   7693  1.1  christos 	}
   7694  1.1  christos 
   7695  1.1  christos 	return 0;
   7696  1.1  christos }
   7697  1.1  christos 
   7698  1.1  christos 
   7699  1.1  christos /* ------------------------------------------------------------------------ */
   7700  1.1  christos /* Function:    nat_builddivertmp                                           */
   7701  1.1  christos /* Returns:     int - -1 == error, 0 == success                             */
   7702  1.1  christos /* Parameters:  np(I) - pointer to a NAT rule                               */
   7703  1.1  christos /*                                                                          */
   7704  1.1  christos /* For encap/divert rules, a skeleton packet representing what will be      */
   7705  1.1  christos /* prepended to the real packet is created.  Even though we don't have the  */
   7706  1.1  christos /* full packet here, a checksum is calculated that we update later when we  */
   7707  1.1  christos /* fill in the final details.  At present a 0 checksum for UDP is being set */
   7708  1.1  christos /* here because it is expected that divert will be used for localhost.      */
   7709  1.1  christos /* ------------------------------------------------------------------------ */
   7710  1.1  christos static int
   7711  1.1  christos ipf_nat_builddivertmp(softn, np)
   7712  1.1  christos 	ipf_nat_softc_t *softn;
   7713  1.1  christos 	ipnat_t *np;
   7714  1.1  christos {
   7715  1.1  christos 	udphdr_t *uh;
   7716  1.1  christos 	size_t len;
   7717  1.1  christos 	ip_t *ip;
   7718  1.1  christos 
   7719  1.1  christos 	if ((np->in_redir & NAT_DIVERTUDP) != 0)
   7720  1.1  christos 		len = sizeof(ip_t) + sizeof(udphdr_t);
   7721  1.1  christos 	else
   7722  1.1  christos 		len = sizeof(ip_t);
   7723  1.1  christos 
   7724  1.1  christos 	ALLOC_MB_T(np->in_divmp, len);
   7725  1.1  christos 	if (np->in_divmp == NULL) {
   7726  1.1  christos 		NBUMPD(ipf_nat_stats, ns_divert_build);
   7727  1.1  christos 		return -1;
   7728  1.1  christos 	}
   7729  1.1  christos 
   7730  1.1  christos 	/*
   7731  1.1  christos 	 * First, the header to get the packet diverted to the new destination
   7732  1.1  christos 	 */
   7733  1.1  christos 	ip = MTOD(np->in_divmp, ip_t *);
   7734  1.1  christos 	IP_V_A(ip, 4);
   7735  1.1  christos 	IP_HL_A(ip, 5);
   7736  1.1  christos 	ip->ip_tos = 0;
   7737  1.1  christos 	if ((np->in_redir & NAT_DIVERTUDP) != 0)
   7738  1.1  christos 		ip->ip_p = IPPROTO_UDP;
   7739  1.1  christos 	else
   7740  1.1  christos 		ip->ip_p = IPPROTO_IPIP;
   7741  1.1  christos 	ip->ip_ttl = 255;
   7742  1.1  christos 	ip->ip_off = 0;
   7743  1.1  christos 	ip->ip_sum = 0;
   7744  1.1  christos 	ip->ip_len = htons(len);
   7745  1.1  christos 	ip->ip_id = 0;
   7746  1.1  christos 	ip->ip_src.s_addr = htonl(np->in_snip);
   7747  1.1  christos 	ip->ip_dst.s_addr = htonl(np->in_dnip);
   7748  1.1  christos 	ip->ip_sum = ipf_cksum((u_short *)ip, sizeof(*ip));
   7749  1.1  christos 
   7750  1.1  christos 	if (np->in_redir & NAT_DIVERTUDP) {
   7751  1.1  christos 		uh = (udphdr_t *)(ip + 1);
   7752  1.1  christos 		uh->uh_sum = 0;
   7753  1.1  christos 		uh->uh_ulen = 8;
   7754  1.1  christos 		uh->uh_sport = htons(np->in_spnext);
   7755  1.1  christos 		uh->uh_dport = htons(np->in_dpnext);
   7756  1.1  christos 	}
   7757  1.1  christos 
   7758  1.1  christos 	return 0;
   7759  1.1  christos }
   7760  1.1  christos 
   7761  1.1  christos 
   7762  1.1  christos #define	MINDECAP	(sizeof(ip_t) + sizeof(udphdr_t) + sizeof(ip_t))
   7763  1.1  christos 
   7764  1.1  christos /* ------------------------------------------------------------------------ */
   7765  1.1  christos /* Function:    nat_decap                                                   */
   7766  1.1  christos /* Returns:     int - -1 == error, 0 == success                             */
   7767  1.1  christos /* Parameters:  fin(I) - pointer to packet information                      */
   7768  1.1  christos /*              nat(I) - pointer to current NAT session                     */
   7769  1.1  christos /*                                                                          */
   7770  1.1  christos /* This function is responsible for undoing a packet's encapsulation in the */
   7771  1.1  christos /* reverse of an encap/divert rule.  After removing the outer encapsulation */
   7772  1.1  christos /* it is necessary to call ipf_makefrip() again so that the contents of 'fin'*/
   7773  1.1  christos /* match the "new" packet as it may still be used by IPFilter elsewhere.    */
   7774  1.1  christos /* We use "dir" here as the basis for some of the expectations about the    */
   7775  1.1  christos /* outer header.  If we return an error, the goal is to leave the original  */
   7776  1.1  christos /* packet information undisturbed - this falls short at the end where we'd  */
   7777  1.1  christos /* need to back a backup copy of "fin" - expensive.                         */
   7778  1.1  christos /* ------------------------------------------------------------------------ */
   7779  1.1  christos static int
   7780  1.1  christos ipf_nat_decap(fin, nat)
   7781  1.1  christos 	fr_info_t *fin;
   7782  1.1  christos 	nat_t *nat;
   7783  1.1  christos {
   7784  1.1  christos 	ipf_main_softc_t *softc = fin->fin_main_soft;
   7785  1.1  christos 	ipf_nat_softc_t *softn = softc->ipf_nat_soft;
   7786  1.1  christos 	char *hdr;
   7787  1.1  christos 	int hlen;
   7788  1.1  christos 	int skip;
   7789  1.1  christos 	mb_t *m;
   7790  1.1  christos 
   7791  1.1  christos 	if ((fin->fin_flx & FI_ICMPERR) != 0) {
   7792  1.1  christos 		/*
   7793  1.1  christos 		 * ICMP packets don't get decapsulated, instead what we need
   7794  1.1  christos 		 * to do is change the ICMP reply from including (in the data
   7795  1.1  christos 		 * portion for errors) the encapsulated packet that we sent
   7796  1.1  christos 		 * out to something that resembles the original packet prior
   7797  1.1  christos 		 * to encapsulation.  This isn't done here - all we're doing
   7798  1.1  christos 		 * here is changing the outer address to ensure that it gets
   7799  1.1  christos 		 * targetted back to the correct system.
   7800  1.1  christos 		 */
   7801  1.1  christos 
   7802  1.1  christos 		if (nat->nat_dir & NAT_OUTBOUND) {
   7803  1.1  christos 			u_32_t sum1, sum2, sumd;
   7804  1.1  christos 
   7805  1.1  christos 			sum1 = ntohl(fin->fin_daddr);
   7806  1.1  christos 			sum2 = ntohl(nat->nat_osrcaddr);
   7807  1.1  christos 			CALC_SUMD(sum1, sum2, sumd);
   7808  1.1  christos 			fin->fin_ip->ip_dst = nat->nat_osrcip;
   7809  1.1  christos 			fin->fin_daddr = nat->nat_osrcaddr;
   7810  1.1  christos #if !defined(_KERNEL) || defined(MENTAT) || defined(__sgi) || \
   7811  1.1  christos      defined(__osf__) || defined(linux)
   7812  1.1  christos 			ipf_fix_outcksum(fin, &fin->fin_ip->ip_sum, sumd);
   7813  1.1  christos #endif
   7814  1.1  christos 		}
   7815  1.1  christos 		return 0;
   7816  1.1  christos 	}
   7817  1.1  christos 
   7818  1.1  christos 	m = fin->fin_m;
   7819  1.1  christos 	skip = fin->fin_hlen;
   7820  1.1  christos 
   7821  1.1  christos 	switch (nat->nat_dir)
   7822  1.1  christos 	{
   7823  1.1  christos 	case NAT_DIVERTIN :
   7824  1.1  christos 	case NAT_DIVERTOUT :
   7825  1.1  christos 		if (fin->fin_plen < MINDECAP)
   7826  1.1  christos 			return -1;
   7827  1.1  christos 		skip += sizeof(udphdr_t);
   7828  1.1  christos 		break;
   7829  1.1  christos 
   7830  1.1  christos 	case NAT_ENCAPIN :
   7831  1.1  christos 	case NAT_ENCAPOUT :
   7832  1.1  christos 		if (fin->fin_plen < (skip + sizeof(ip_t)))
   7833  1.1  christos 			return -1;
   7834  1.1  christos 		break;
   7835  1.1  christos 	default :
   7836  1.1  christos 		return -1;
   7837  1.1  christos 		/* NOTREACHED */
   7838  1.1  christos 	}
   7839  1.1  christos 
   7840  1.1  christos 	/*
   7841  1.1  christos 	 * The aim here is to keep the original packet details in "fin" for
   7842  1.1  christos 	 * as long as possible so that returning with an error is for the
   7843  1.1  christos 	 * original packet and there is little undoing work to do.
   7844  1.1  christos 	 */
   7845  1.1  christos 	if (M_LEN(m) < skip + sizeof(ip_t)) {
   7846  1.1  christos 		if (ipf_pr_pullup(fin, skip + sizeof(ip_t)) == -1)
   7847  1.1  christos 			return -1;
   7848  1.1  christos 	}
   7849  1.1  christos 
   7850  1.1  christos 	hdr = MTOD(fin->fin_m, char *);
   7851  1.1  christos 	fin->fin_ip = (ip_t *)(hdr + skip);
   7852  1.1  christos 	hlen = IP_HL(fin->fin_ip) << 2;
   7853  1.1  christos 
   7854  1.1  christos 	if (ipf_pr_pullup(fin, skip + hlen) == -1) {
   7855  1.1  christos 		NBUMPSIDED(fin->fin_out, ns_decap_pullup);
   7856  1.1  christos 		return -1;
   7857  1.1  christos 	}
   7858  1.1  christos 
   7859  1.1  christos 	fin->fin_hlen = hlen;
   7860  1.1  christos 	fin->fin_dlen -= skip;
   7861  1.1  christos 	fin->fin_plen -= skip;
   7862  1.1  christos 	fin->fin_ipoff += skip;
   7863  1.1  christos 
   7864  1.1  christos 	if (ipf_makefrip(hlen, (ip_t *)hdr, fin) == -1) {
   7865  1.1  christos 		NBUMPSIDED(fin->fin_out, ns_decap_bad);
   7866  1.1  christos 		return -1;
   7867  1.1  christos 	}
   7868  1.1  christos 
   7869  1.1  christos 	return skip;
   7870  1.1  christos }
   7871  1.1  christos 
   7872  1.1  christos 
   7873  1.1  christos /* ------------------------------------------------------------------------ */
   7874  1.1  christos /* Function:    nat_matchencap                                              */
   7875  1.1  christos /* Returns:     int - -1 == packet error, 1 == success, 0 = no match        */
   7876  1.1  christos /* Parameters:  fin(I) - pointer to packet information                      */
   7877  1.1  christos /*              np(I) - pointer to a NAT rule                               */
   7878  1.1  christos /*                                                                          */
   7879  1.1  christos /* To properly compare a packet travelling in the reverse direction to an   */
   7880  1.1  christos /* encap rule, it needs to be pseudo-decapsulated so we can check if a      */
   7881  1.1  christos /* reply to it would be encapsulated.  In doing this, we have to be careful */
   7882  1.1  christos /* so as not to actually do any decapsulation nor affect any of the current */
   7883  1.1  christos /* stored parameters in "fin" so that we can continue processing it else-   */
   7884  1.1  christos /* where if it doesn't match.                                               */
   7885  1.1  christos /* ------------------------------------------------------------------------ */
   7886  1.1  christos static int
   7887  1.1  christos ipf_nat_matchencap(softn, fin, np)
   7888  1.1  christos 	ipf_nat_softc_t *softn;
   7889  1.1  christos 	fr_info_t *fin;
   7890  1.1  christos 	ipnat_t *np;
   7891  1.1  christos {
   7892  1.1  christos 	int hlen, match, skip;
   7893  1.1  christos 	u_short *ports;
   7894  1.1  christos 	frtuc_t *ft;
   7895  1.1  christos 	fr_ip_t fi;
   7896  1.1  christos 	char *hdr;
   7897  1.1  christos 	ip_t *ip;
   7898  1.1  christos 	mb_t *m;
   7899  1.1  christos 
   7900  1.1  christos 	/*
   7901  1.1  christos 	 * This function is only for matching packets that are appearing from
   7902  1.1  christos 	 * the reverse direction against "encap" rules.
   7903  1.1  christos 	 */
   7904  1.1  christos 	if (fin->fin_out == 1) {
   7905  1.1  christos 		if ((np->in_redir & NAT_REDIRECT) == 0)
   7906  1.1  christos 			return 0;
   7907  1.1  christos 	} else {
   7908  1.1  christos 		if ((np->in_redir & NAT_MAP) == 0)
   7909  1.1  christos 			return 0;
   7910  1.1  christos 	}
   7911  1.1  christos 	if (np->in_pr[fin->fin_out] != fin->fin_p)
   7912  1.1  christos 		return 0;
   7913  1.1  christos 
   7914  1.1  christos 	/*
   7915  1.1  christos 	 * The aim here is to keep the original packet details in "fin" for
   7916  1.1  christos 	 * as long as possible so that returning with an error is for the
   7917  1.1  christos 	 * original packet and there is little undoing work to do.
   7918  1.1  christos 	 */
   7919  1.1  christos 	m = fin->fin_m;
   7920  1.1  christos 	skip = fin->fin_hlen;
   7921  1.1  christos 	if (M_LEN(m) < skip + sizeof(ip_t)) {
   7922  1.1  christos 		if (ipf_pr_pullup(fin, sizeof(ip_t)) == -1) {
   7923  1.1  christos 			NBUMPSIDED(fin->fin_out, ns_encap_pullup);
   7924  1.1  christos 			return -1;
   7925  1.1  christos 		}
   7926  1.1  christos 	}
   7927  1.1  christos 
   7928  1.1  christos 	hdr = MTOD(fin->fin_m, char *);
   7929  1.1  christos 	ip = (ip_t *)(hdr + skip);
   7930  1.1  christos 	hlen = IP_HL(ip) << 2;
   7931  1.1  christos 
   7932  1.1  christos 	if (ipf_pr_pullup(fin, hlen) == -1) {
   7933  1.1  christos 		NBUMPSIDED(fin->fin_out, ns_encap_pullup);
   7934  1.1  christos 		return -1;
   7935  1.1  christos 	}
   7936  1.1  christos 
   7937  1.1  christos 	match = 1;
   7938  1.1  christos 
   7939  1.1  christos 	/*
   7940  1.1  christos 	 * Now we should have the entire innder header, so match up the
   7941  1.1  christos 	 * address fields - easy enough.  Reverse matching of source and
   7942  1.1  christos 	 * destination because this is purportedly a "reply" to an encap rule.
   7943  1.1  christos 	 */
   7944  1.1  christos 	switch (np->in_osrcatype)
   7945  1.1  christos 	{
   7946  1.1  christos 	case FRI_NORMAL :
   7947  1.1  christos 		match = ((ip->ip_dst.s_addr & np->in_osrcmsk)
   7948  1.1  christos 			 != np->in_osrcaddr);
   7949  1.1  christos 		break;
   7950  1.1  christos 	case FRI_LOOKUP :
   7951  1.1  christos 		match = (*np->in_nsrcfunc)(fin->fin_main_soft, np->in_osrcptr,
   7952  1.1  christos 					   np->in_v[0], &ip->ip_dst.s_addr,
   7953  1.1  christos 					   fin->fin_plen);
   7954  1.1  christos 		break;
   7955  1.1  christos 	}
   7956  1.1  christos 	if (match)
   7957  1.1  christos 		return 0;
   7958  1.1  christos 
   7959  1.1  christos 	switch (np->in_odstatype)
   7960  1.1  christos 	{
   7961  1.1  christos 	case FRI_NORMAL :
   7962  1.1  christos 		match = ((ip->ip_src.s_addr & np->in_odstmsk)
   7963  1.1  christos 			 != np->in_odstaddr);
   7964  1.1  christos 		break;
   7965  1.1  christos 	case FRI_LOOKUP :
   7966  1.1  christos 		match = (*np->in_ndstfunc)(fin->fin_main_soft, np->in_odstptr,
   7967  1.1  christos 					   np->in_v[0], &ip->ip_src.s_addr,
   7968  1.1  christos 					   fin->fin_plen);
   7969  1.1  christos 		break;
   7970  1.1  christos 	}
   7971  1.1  christos 	if (match)
   7972  1.1  christos 		return 0;
   7973  1.1  christos 
   7974  1.1  christos 	ft = &np->in_tuc;
   7975  1.1  christos 
   7976  1.1  christos 	switch (ip->ip_p)
   7977  1.1  christos 	{
   7978  1.1  christos 	case IPPROTO_TCP :
   7979  1.1  christos 	case IPPROTO_UDP :
   7980  1.1  christos 		/*
   7981  1.1  christos 		 * Only need to fetch port numbers for NAT
   7982  1.1  christos 		 */
   7983  1.1  christos 		if (ipf_pr_pullup(fin, hlen + 4) == -1) {
   7984  1.1  christos 			NBUMPSIDED(fin->fin_out, ns_encap_pullup);
   7985  1.1  christos 			return -1;
   7986  1.1  christos 		}
   7987  1.1  christos 
   7988  1.1  christos 		ports = (u_short *)((char *)ip + hlen);
   7989  1.1  christos 
   7990  1.1  christos 		fi.fi_tcpf = 0;
   7991  1.1  christos 		/*
   7992  1.1  christos 		 * And again, because we're simulating a reply, put the port
   7993  1.1  christos 		 * numbers in the revese place to where they are now.
   7994  1.1  christos 		 */
   7995  1.1  christos 		fi.fi_ports[0] = ntohs(ports[1]);
   7996  1.1  christos 		fi.fi_ports[1] = ntohs(ports[0]);
   7997  1.1  christos 		return ipf_tcpudpchk(&fi, ft);
   7998  1.1  christos 
   7999  1.1  christos 		/* NOTREACHED */
   8000  1.1  christos 
   8001  1.1  christos 	default :
   8002  1.1  christos 		if (ft->ftu_scmp || ft->ftu_dcmp)
   8003  1.1  christos 			return 0;
   8004  1.1  christos 		break;
   8005  1.1  christos 	}
   8006  1.1  christos 
   8007  1.1  christos 	return 1;
   8008  1.1  christos }
   8009  1.1  christos 
   8010  1.1  christos 
   8011  1.1  christos /* ------------------------------------------------------------------------ */
   8012  1.1  christos /* Function:    nat_nextaddr                                                */
   8013  1.1  christos /* Returns:     int - -1 == bad input (no new address),                     */
   8014  1.1  christos /*                     0 == success and dst has new address                 */
   8015  1.1  christos /* Parameters:  fin(I) - pointer to packet information                      */
   8016  1.1  christos /*              na(I)  - how to generate new address                        */
   8017  1.1  christos /*              old(I) - original address being replaced                    */
   8018  1.1  christos /*              dst(O) - where to put the new address                       */
   8019  1.1  christos /* Write Lock:  ipf_nat                                                     */
   8020  1.1  christos /*                                                                          */
   8021  1.1  christos /* This function uses the contents of the "na" structure, in combination    */
   8022  1.1  christos /* with "old" to produce a new address to store in "dst".  Not all of the   */
   8023  1.1  christos /* possible uses of "na" will result in a new address.                      */
   8024  1.1  christos /* ------------------------------------------------------------------------ */
   8025  1.1  christos static int
   8026  1.1  christos ipf_nat_nextaddr(fin, na, old, dst)
   8027  1.1  christos 	fr_info_t *fin;
   8028  1.1  christos 	nat_addr_t *na;
   8029  1.1  christos 	u_32_t *old, *dst;
   8030  1.1  christos {
   8031  1.1  christos 	ipf_main_softc_t *softc = fin->fin_main_soft;
   8032  1.1  christos 	ipf_nat_softc_t *softn = softc->ipf_nat_soft;
   8033  1.1  christos 	u_32_t amin, amax, new;
   8034  1.1  christos 	i6addr_t newip;
   8035  1.1  christos 	int error;
   8036  1.1  christos 
   8037  1.1  christos 	new = 0;
   8038  1.1  christos 	amin = na->na_addr[0].in4.s_addr;
   8039  1.1  christos 
   8040  1.1  christos 	switch (na->na_atype)
   8041  1.1  christos 	{
   8042  1.1  christos 	case FRI_RANGE :
   8043  1.1  christos 		amax = na->na_addr[1].in4.s_addr;
   8044  1.1  christos 		break;
   8045  1.1  christos 
   8046  1.1  christos 	case FRI_NETMASKED :
   8047  1.1  christos 	case FRI_DYNAMIC :
   8048  1.1  christos 	case FRI_NORMAL :
   8049  1.1  christos 		/*
   8050  1.1  christos 		 * Compute the maximum address by adding the inverse of the
   8051  1.1  christos 		 * netmask to the minimum address.
   8052  1.1  christos 		 */
   8053  1.1  christos 		amax = ~na->na_addr[1].in4.s_addr;
   8054  1.1  christos 		amax |= amin;
   8055  1.1  christos 		break;
   8056  1.1  christos 
   8057  1.1  christos 	case FRI_LOOKUP :
   8058  1.1  christos 		break;
   8059  1.1  christos 
   8060  1.1  christos 	case FRI_BROADCAST :
   8061  1.1  christos 	case FRI_PEERADDR :
   8062  1.1  christos 	case FRI_NETWORK :
   8063  1.1  christos 	default :
   8064  1.1  christos 		return -1;
   8065  1.1  christos 	}
   8066  1.1  christos 
   8067  1.1  christos 	error = -1;
   8068  1.1  christos 
   8069  1.1  christos 	if (na->na_atype == FRI_LOOKUP) {
   8070  1.1  christos 		if (na->na_type == IPLT_DSTLIST) {
   8071  1.1  christos 			error = ipf_dstlist_select_node(fin, na->na_ptr, dst,
   8072  1.1  christos 							NULL);
   8073  1.1  christos 		} else {
   8074  1.1  christos 			NBUMPSIDE(fin->fin_out, ns_badnextaddr);
   8075  1.1  christos 		}
   8076  1.1  christos 
   8077  1.1  christos 	} else if (na->na_atype == IPLT_NONE) {
   8078  1.1  christos 		/*
   8079  1.1  christos 		 * 0/0 as the new address means leave it alone.
   8080  1.1  christos 		 */
   8081  1.1  christos 		if (na->na_addr[0].in4.s_addr == 0 &&
   8082  1.1  christos 		    na->na_addr[1].in4.s_addr == 0) {
   8083  1.1  christos 			new = *old;
   8084  1.1  christos 
   8085  1.1  christos 		/*
   8086  1.1  christos 		 * 0/32 means get the interface's address
   8087  1.1  christos 		 */
   8088  1.1  christos 		} else if (na->na_addr[0].in4.s_addr == 0 &&
   8089  1.1  christos 			   na->na_addr[1].in4.s_addr == 0xffffffff) {
   8090  1.1  christos 			if (ipf_ifpaddr(softc, 4, na->na_atype,
   8091  1.1  christos 					fin->fin_ifp, &newip, NULL) == -1) {
   8092  1.1  christos 				NBUMPSIDED(fin->fin_out, ns_ifpaddrfail);
   8093  1.1  christos 				return -1;
   8094  1.1  christos 			}
   8095  1.1  christos 			new = newip.in4.s_addr;
   8096  1.1  christos 		} else {
   8097  1.1  christos 			new = htonl(na->na_nextip);
   8098  1.1  christos 		}
   8099  1.1  christos 		*dst = new;
   8100  1.1  christos 		error = 0;
   8101  1.1  christos 
   8102  1.1  christos 	} else {
   8103  1.1  christos 		NBUMPSIDE(fin->fin_out, ns_badnextaddr);
   8104  1.1  christos 	}
   8105  1.1  christos 
   8106  1.1  christos 	return error;
   8107  1.1  christos }
   8108  1.1  christos 
   8109  1.1  christos 
   8110  1.1  christos /* ------------------------------------------------------------------------ */
   8111  1.1  christos /* Function:    nat_nextaddrinit                                            */
   8112  1.1  christos /* Returns:     int - 0 == success, else error number                       */
   8113  1.1  christos /* Parameters:  na(I)      - NAT address information for generating new addr*/
   8114  1.1  christos /*              initial(I) - flag indicating if it is the first call for    */
   8115  1.1  christos /*                           this "na" structure.                           */
   8116  1.1  christos /*              ifp(I)     - network interface to derive address            */
   8117  1.1  christos /*                           information from.                              */
   8118  1.1  christos /*                                                                          */
   8119  1.1  christos /* This function is expected to be called in two scenarious: when a new NAT */
   8120  1.1  christos /* rule is loaded into the kernel and when the list of NAT rules is sync'd  */
   8121  1.1  christos /* up with the valid network interfaces (possibly due to them changing.)    */
   8122  1.1  christos /* To distinguish between these, the "initial" parameter is used.  If it is */
   8123  1.1  christos /* 1 then this indicates the rule has just been reloaded and 0 for when we  */
   8124  1.1  christos /* are updating information.  This difference is important because in       */
   8125  1.1  christos /* instances where we are not updating address information associated with  */
   8126  1.1  christos /* a network interface, we don't want to disturb what the "next" address to */
   8127  1.1  christos /* come out of ipf_nat_nextaddr() will be.                                  */
   8128  1.1  christos /* ------------------------------------------------------------------------ */
   8129  1.1  christos static int
   8130  1.1  christos ipf_nat_nextaddrinit(softc, base, na, initial, ifp)
   8131  1.1  christos 	ipf_main_softc_t *softc;
   8132  1.1  christos 	char *base;
   8133  1.1  christos 	nat_addr_t *na;
   8134  1.1  christos 	int initial;
   8135  1.1  christos 	void *ifp;
   8136  1.1  christos {
   8137  1.1  christos 
   8138  1.1  christos 	switch (na->na_atype)
   8139  1.1  christos 	{
   8140  1.1  christos 	case FRI_LOOKUP :
   8141  1.1  christos 		if (na->na_subtype == 0) {
   8142  1.1  christos 			na->na_ptr = ipf_lookup_res_num(softc, IPL_LOGNAT,
   8143  1.1  christos 							na->na_type,
   8144  1.1  christos 							na->na_num,
   8145  1.1  christos 							&na->na_func);
   8146  1.1  christos 		} else if (na->na_subtype == 1) {
   8147  1.1  christos 			na->na_ptr = ipf_lookup_res_name(softc, IPL_LOGNAT,
   8148  1.1  christos 							 na->na_type,
   8149  1.1  christos 							 base + na->na_num,
   8150  1.1  christos 							 &na->na_func);
   8151  1.1  christos 		}
   8152  1.1  christos 		if (na->na_func == NULL) {
   8153  1.1  christos 			IPFERROR(60060);
   8154  1.1  christos 			return ESRCH;
   8155  1.1  christos 		}
   8156  1.1  christos 		if (na->na_ptr == NULL) {
   8157  1.1  christos 			IPFERROR(60056);
   8158  1.1  christos 			return ESRCH;
   8159  1.1  christos 		}
   8160  1.1  christos 		break;
   8161  1.1  christos 
   8162  1.1  christos 	case FRI_DYNAMIC :
   8163  1.1  christos 	case FRI_BROADCAST :
   8164  1.1  christos 	case FRI_NETWORK :
   8165  1.1  christos 	case FRI_NETMASKED :
   8166  1.1  christos 	case FRI_PEERADDR :
   8167  1.1  christos 		if (ifp != NULL)
   8168  1.1  christos 			(void )ipf_ifpaddr(softc, 4, na->na_atype, ifp,
   8169  1.1  christos 					   &na->na_addr[0], &na->na_addr[1]);
   8170  1.1  christos 		break;
   8171  1.1  christos 
   8172  1.1  christos 	case FRI_SPLIT :
   8173  1.1  christos 	case FRI_RANGE :
   8174  1.1  christos 		if (initial)
   8175  1.1  christos 			na->na_nextip = ntohl(na->na_addr[0].in4.s_addr);
   8176  1.1  christos 		break;
   8177  1.1  christos 
   8178  1.1  christos 	case FRI_NONE :
   8179  1.1  christos 		na->na_addr[0].in4.s_addr &= na->na_addr[1].in4.s_addr;
   8180  1.1  christos 		return 0;
   8181  1.1  christos 
   8182  1.1  christos 	case FRI_NORMAL :
   8183  1.1  christos 		na->na_addr[0].in4.s_addr &= na->na_addr[1].in4.s_addr;
   8184  1.1  christos 		break;
   8185  1.1  christos 
   8186  1.1  christos 	default :
   8187  1.1  christos 		IPFERROR(60054);
   8188  1.1  christos 		return EINVAL;
   8189  1.1  christos 	}
   8190  1.1  christos 
   8191  1.1  christos 	if (initial && (na->na_atype == FRI_NORMAL)) {
   8192  1.1  christos 		if (na->na_addr[0].in4.s_addr == 0) {
   8193  1.1  christos 			if ((na->na_addr[1].in4.s_addr == 0xffffffff) ||
   8194  1.1  christos 			    (na->na_addr[1].in4.s_addr == 0)) {
   8195  1.1  christos 				return 0;
   8196  1.1  christos 			}
   8197  1.1  christos 		}
   8198  1.1  christos 
   8199  1.1  christos 		if (na->na_addr[1].in4.s_addr == 0xffffffff) {
   8200  1.1  christos 			na->na_nextip = ntohl(na->na_addr[0].in4.s_addr);
   8201  1.1  christos 		} else {
   8202  1.1  christos 			na->na_nextip = ntohl(na->na_addr[0].in4.s_addr) + 1;
   8203  1.1  christos 		}
   8204  1.1  christos 	}
   8205  1.1  christos 
   8206  1.1  christos 	return 0;
   8207  1.1  christos }
   8208  1.1  christos 
   8209  1.1  christos 
   8210  1.1  christos /* ------------------------------------------------------------------------ */
   8211  1.1  christos /* Function:    nat_encapok                                                 */
   8212  1.1  christos /* Returns:     int - -1 == MTU not big enough, 0 == ok to send packet      */
   8213  1.1  christos /* Parameters:  fin(I) - pointer to packet information                      */
   8214  1.1  christos /*              nat(I) - pointer to current NAT session                     */
   8215  1.1  christos /*                                                                          */
   8216  1.1  christos /* The purpose of this function is to determine whether or not a packet can */
   8217  1.1  christos /* be sent out of a network interface after it has been encapsulated, before*/
   8218  1.1  christos /* the actual encapsulation happens.  If it cannot - because the "Don't     */
   8219  1.1  christos /* fragment" bit has been set - then generate an ICMP error message back to */
   8220  1.1  christos /* the origin of the packet, informing it that the packet is too big and    */
   8221  1.1  christos /* what the actual MTU out for the connection is.                           */
   8222  1.1  christos /*                                                                          */
   8223  1.1  christos /* At present the only question this would leave for strange behaviour is   */
   8224  1.1  christos /* with local connections that will go out an encapsulation as sending of   */
   8225  1.1  christos /* ICMP messages to local destinations isn't considered robust.             */
   8226  1.1  christos /* ------------------------------------------------------------------------ */
   8227  1.1  christos static int
   8228  1.1  christos ipf_nat_encapok(fin, nat)
   8229  1.1  christos 	fr_info_t *fin;
   8230  1.1  christos 	nat_t *nat;
   8231  1.1  christos {
   8232  1.1  christos #ifdef INSTANCES
   8233  1.1  christos 	ipf_main_softc_t *softc = fin->fin_main_soft;	/* For GETIFMTU_4 */
   8234  1.1  christos #endif
   8235  1.1  christos 	void *sifp;
   8236  1.1  christos 	ipnat_t *n;
   8237  1.1  christos 	int extra;
   8238  1.1  christos 	int mtu;
   8239  1.1  christos 
   8240  1.1  christos 	if (!(fin->fin_ip->ip_off & htons(IP_DF)))
   8241  1.1  christos 		return 0;
   8242  1.1  christos 
   8243  1.1  christos 	n = nat->nat_ptr;
   8244  1.1  christos 
   8245  1.1  christos 	if (n->in_redir & NAT_ENCAP) {
   8246  1.1  christos 		extra = sizeof(ip_t);
   8247  1.1  christos 
   8248  1.1  christos 	} else {
   8249  1.1  christos 		return 0;
   8250  1.1  christos 	}
   8251  1.1  christos 
   8252  1.1  christos 	mtu = GETIFMTU_4(nat->nat_ifps[1]);
   8253  1.1  christos 
   8254  1.1  christos 	if (fin->fin_plen + extra < mtu)
   8255  1.1  christos 		return 0;
   8256  1.1  christos 
   8257  1.1  christos 	sifp = fin->fin_ifp;
   8258  1.1  christos 	fin->fin_ifp = NULL;
   8259  1.1  christos 	fin->fin_icode = ICMP_UNREACH_NEEDFRAG;
   8260  1.1  christos 	fin->fin_mtu = mtu - extra;
   8261  1.1  christos 
   8262  1.1  christos 	(void) ipf_send_icmp_err(ICMP_UNREACH, fin, 1);
   8263  1.1  christos 
   8264  1.1  christos 	fin->fin_mtu = 0;
   8265  1.1  christos 
   8266  1.1  christos 	return -1;
   8267  1.1  christos }
   8268  1.1  christos 
   8269  1.1  christos 
   8270  1.1  christos /* ------------------------------------------------------------------------ */
   8271  1.1  christos /* Function:    ipf_nat_rebuildencapicmp                                    */
   8272  1.1  christos /* Returns:     int - -1 == error, 0 == success                             */
   8273  1.1  christos /* Parameters:  fin(I) - pointer to packet information                      */
   8274  1.1  christos /*              nat(I) - pointer to current NAT session                     */
   8275  1.1  christos /*                                                                          */
   8276  1.1  christos /* For ICMP replies received in response to packets we've encapsulated on   */
   8277  1.1  christos /* the way out, we need to replace all of the addressing fields found in    */
   8278  1.1  christos /* the data section of the ICMP header.  The ICMP packet is going to        */
   8279  1.1  christos /* contain the the IP packet we sent out (IPENCAP) plus at least 64 bits of */
   8280  1.1  christos /* the original IP packet - not something that will be of use to the origin */
   8281  1.1  christos /* of the offending packet.                                                 */
   8282  1.1  christos /* ------------------------------------------------------------------------ */
   8283  1.1  christos static nat_t *
   8284  1.1  christos ipf_nat_rebuildencapicmp(fin, nat)
   8285  1.1  christos 	fr_info_t *fin;
   8286  1.1  christos 	nat_t *nat;
   8287  1.1  christos {
   8288  1.1  christos 	icmphdr_t *icmp;
   8289  1.1  christos 	udphdr_t *udp;
   8290  1.1  christos 	ip_t *oip;
   8291  1.1  christos 	int p;
   8292  1.1  christos 
   8293  1.1  christos 	icmp = fin->fin_dp;
   8294  1.1  christos 	oip = (ip_t *)&icmp->icmp_ip;
   8295  1.1  christos 
   8296  1.1  christos 	if (fin->fin_out == 0) {
   8297  1.1  christos 		if (nat->nat_dir == NAT_ENCAPIN) {
   8298  1.1  christos 			oip->ip_src = nat->nat_odstip;
   8299  1.1  christos 			oip->ip_dst = nat->nat_osrcip;
   8300  1.1  christos 		} else {
   8301  1.1  christos 			oip->ip_src = nat->nat_osrcip;
   8302  1.1  christos 			oip->ip_dst = nat->nat_odstip;
   8303  1.1  christos 		}
   8304  1.1  christos 	} else {
   8305  1.1  christos 		if (nat->nat_dir == NAT_ENCAPIN) {
   8306  1.1  christos 			oip->ip_src = nat->nat_osrcip;
   8307  1.1  christos 			oip->ip_dst = nat->nat_odstip;
   8308  1.1  christos 		} else {
   8309  1.1  christos 			oip->ip_src = nat->nat_odstip;
   8310  1.1  christos 			oip->ip_dst = nat->nat_osrcip;
   8311  1.1  christos 		}
   8312  1.1  christos 	}
   8313  1.1  christos 
   8314  1.1  christos 	udp = (udphdr_t *)(oip + 1);
   8315  1.1  christos 
   8316  1.1  christos 	/*
   8317  1.1  christos 	 * We use nat_p here because the original UDP header is quite likely
   8318  1.1  christos 	 * to have been lost - the error packet returned contains the outer
   8319  1.1  christos 	 * encapsulation header plus 64 bits of the inner IP header, no room
   8320  1.1  christos 	 * for a UDP or TCP header unless extra data is returned.
   8321  1.1  christos 	 *
   8322  1.1  christos 	 * XXX - If the entire original packet has been included (possible)
   8323  1.1  christos 	 *       then we should be just stripping off the outer encapsulation.
   8324  1.1  christos 	 *       This is a "todo" for the near future.
   8325  1.1  christos 	 */
   8326  1.1  christos 	p = nat->nat_pr[1 - fin->fin_out];
   8327  1.1  christos 
   8328  1.1  christos 	switch (p)
   8329  1.1  christos 	{
   8330  1.1  christos 	case IPPROTO_UDP :
   8331  1.1  christos 		udp->uh_sum = 0;
   8332  1.1  christos 		break;
   8333  1.1  christos 	case IPPROTO_TCP :
   8334  1.1  christos 		/*
   8335  1.1  christos 		 * NAT doesn't track the sequence number so we can't pretend
   8336  1.1  christos 		 * to know what value this field should carry.
   8337  1.1  christos 		 */
   8338  1.1  christos 		((tcphdr_t *)udp)->th_seq = 0;
   8339  1.1  christos 		break;
   8340  1.1  christos 	default :
   8341  1.1  christos 		break;
   8342  1.1  christos 	}
   8343  1.1  christos 
   8344  1.1  christos 	if (p == IPPROTO_TCP || p == IPPROTO_UDP) {
   8345  1.1  christos 		if (fin->fin_out == 0) {
   8346  1.1  christos 			if (nat->nat_dir == NAT_ENCAPIN) {
   8347  1.1  christos 				udp->uh_sport = nat->nat_odport;
   8348  1.1  christos 				udp->uh_dport = nat->nat_osport;
   8349  1.1  christos 			} else {
   8350  1.1  christos 				udp->uh_sport = nat->nat_osport;
   8351  1.1  christos 				udp->uh_dport = nat->nat_odport;
   8352  1.1  christos 			}
   8353  1.1  christos 		} else {
   8354  1.1  christos 			if (nat->nat_dir == NAT_ENCAPIN) {
   8355  1.1  christos 				udp->uh_sport = nat->nat_osport;
   8356  1.1  christos 				udp->uh_dport = nat->nat_odport;
   8357  1.1  christos 			} else {
   8358  1.1  christos 				udp->uh_sport = nat->nat_odport;
   8359  1.1  christos 				udp->uh_dport = nat->nat_osport;
   8360  1.1  christos 			}
   8361  1.1  christos 		}
   8362  1.1  christos 	}
   8363  1.1  christos 
   8364  1.1  christos 	/* TRACE (fin,oip,udp,icmp) */
   8365  1.1  christos 	oip->ip_p = nat->nat_pr[1 - fin->fin_out];
   8366  1.1  christos 	oip->ip_sum = 0;
   8367  1.1  christos 	oip->ip_sum = ipf_cksum((u_short *)oip, sizeof(*oip));
   8368  1.1  christos 
   8369  1.1  christos 	/*
   8370  1.1  christos 	 * Reduce the next MTU setting by the size of the encap header
   8371  1.1  christos 	 */
   8372  1.1  christos 	if (icmp->icmp_type == ICMP_UNREACH &&
   8373  1.1  christos 	    icmp->icmp_code == ICMP_UNREACH_NEEDFRAG) {
   8374  1.1  christos 		icmp->icmp_nextmtu = ntohs(icmp->icmp_nextmtu);
   8375  1.1  christos 		icmp->icmp_nextmtu -= sizeof(ip_t);
   8376  1.1  christos 		icmp->icmp_nextmtu = htons(icmp->icmp_nextmtu);
   8377  1.1  christos 	}
   8378  1.1  christos 
   8379  1.1  christos 	icmp->icmp_cksum = 0;
   8380  1.1  christos 	icmp->icmp_cksum = ipf_cksum((u_short *)icmp, fin->fin_dlen);
   8381  1.1  christos 
   8382  1.1  christos 	/* TRACE (fin,oip,udp,icmp) */
   8383  1.1  christos 
   8384  1.1  christos 	return 0;
   8385  1.1  christos }
   8386  1.1  christos 
   8387  1.1  christos 
   8388  1.1  christos /* ------------------------------------------------------------------------ */
   8389  1.1  christos /* Function:    ipf_nat_matchflush                                          */
   8390  1.1  christos /* Returns:     int - -1 == error, 0 == success                             */
   8391  1.1  christos /* Parameters:  fin(I) - pointer to packet information                      */
   8392  1.1  christos /*              nat(I) - pointer to current NAT session                     */
   8393  1.1  christos /*                                                                          */
   8394  1.1  christos /* ------------------------------------------------------------------------ */
   8395  1.1  christos static int
   8396  1.1  christos ipf_nat_matchflush(softc, softn, data)
   8397  1.1  christos 	ipf_main_softc_t *softc;
   8398  1.1  christos 	ipf_nat_softc_t *softn;
   8399  1.1  christos 	caddr_t data;
   8400  1.1  christos {
   8401  1.1  christos 	int *array, flushed, error;
   8402  1.1  christos 	nat_t *nat, *natnext;
   8403  1.1  christos 	ipfobj_t obj;
   8404  1.1  christos 
   8405  1.1  christos 	error = ipf_matcharray_load(softc, data, &obj, &array);
   8406  1.1  christos 	if (error != 0)
   8407  1.1  christos 		return error;
   8408  1.1  christos 
   8409  1.1  christos 	flushed = 0;
   8410  1.1  christos 
   8411  1.1  christos 	for (nat = softn->ipf_nat_instances; nat != NULL; nat = natnext) {
   8412  1.1  christos 		natnext = nat->nat_next;
   8413  1.1  christos 		if (ipf_nat_matcharray(nat, array, softc->ipf_ticks) == 0) {
   8414  1.1  christos 			ipf_nat_delete(softc, nat, NL_FLUSH);
   8415  1.1  christos 			flushed++;
   8416  1.1  christos 		}
   8417  1.1  christos 	}
   8418  1.1  christos 
   8419  1.1  christos 	obj.ipfo_retval = flushed;
   8420  1.1  christos 	error = BCOPYOUT(&obj, data, sizeof(obj));
   8421  1.1  christos 
   8422  1.1  christos 	KFREES(array, array[0] * sizeof(*array));
   8423  1.1  christos 
   8424  1.1  christos 	return error;
   8425  1.1  christos }
   8426  1.1  christos 
   8427  1.1  christos 
   8428  1.1  christos /* ------------------------------------------------------------------------ */
   8429  1.1  christos /* Function:    ipf_nat_matcharray                                          */
   8430  1.1  christos /* Returns:     int - -1 == error, 0 == success                             */
   8431  1.1  christos /* Parameters:  fin(I) - pointer to packet information                      */
   8432  1.1  christos /*              nat(I) - pointer to current NAT session                     */
   8433  1.1  christos /*                                                                          */
   8434  1.1  christos /* ------------------------------------------------------------------------ */
   8435  1.1  christos static int
   8436  1.1  christos ipf_nat_matcharray(nat, array, ticks)
   8437  1.1  christos 	nat_t *nat;
   8438  1.1  christos 	int *array;
   8439  1.1  christos 	u_long ticks;
   8440  1.1  christos {
   8441  1.1  christos 	int i, n, *x, e, p;
   8442  1.1  christos 
   8443  1.1  christos 	e = 0;
   8444  1.1  christos 	n = array[0];
   8445  1.1  christos 	x = array + 1;
   8446  1.1  christos 
   8447  1.1  christos 	for (; n > 0; x += 3 + x[2]) {
   8448  1.1  christos 		if (x[0] == IPF_EXP_END)
   8449  1.1  christos 			break;
   8450  1.1  christos 		e = 0;
   8451  1.1  christos 
   8452  1.1  christos 		n -= x[2] + 3;
   8453  1.1  christos 		if (n < 0)
   8454  1.1  christos 			break;
   8455  1.1  christos 
   8456  1.1  christos 		p = x[0] >> 16;
   8457  1.1  christos 		if (p != 0 && p != nat->nat_pr[1])
   8458  1.1  christos 			break;
   8459  1.1  christos 
   8460  1.1  christos 		switch (x[0])
   8461  1.1  christos 		{
   8462  1.1  christos 		case IPF_EXP_IP_PR :
   8463  1.1  christos 			for (i = 0; !e && i < x[2]; i++) {
   8464  1.1  christos 				e |= (nat->nat_pr[1] == x[i + 3]);
   8465  1.1  christos 			}
   8466  1.1  christos 			break;
   8467  1.1  christos 
   8468  1.1  christos 		case IPF_EXP_IP_SRCADDR :
   8469  1.1  christos 			if (nat->nat_v[0] == 4) {
   8470  1.1  christos 				for (i = 0; !e && i < x[2]; i++) {
   8471  1.1  christos 					e |= ((nat->nat_osrcaddr & x[i + 4]) ==
   8472  1.1  christos 					      x[i + 3]);
   8473  1.1  christos 				}
   8474  1.1  christos 			}
   8475  1.1  christos 			if (nat->nat_v[1] == 4) {
   8476  1.1  christos 				for (i = 0; !e && i < x[2]; i++) {
   8477  1.1  christos 					e |= ((nat->nat_nsrcaddr & x[i + 4]) ==
   8478  1.1  christos 					      x[i + 3]);
   8479  1.1  christos 				}
   8480  1.1  christos 			}
   8481  1.1  christos 			break;
   8482  1.1  christos 
   8483  1.1  christos 		case IPF_EXP_IP_DSTADDR :
   8484  1.1  christos 			if (nat->nat_v[0] == 4) {
   8485  1.1  christos 				for (i = 0; !e && i < x[2]; i++) {
   8486  1.1  christos 					e |= ((nat->nat_odstaddr & x[i + 4]) ==
   8487  1.1  christos 					      x[i + 3]);
   8488  1.1  christos 				}
   8489  1.1  christos 			}
   8490  1.1  christos 			if (nat->nat_v[1] == 4) {
   8491  1.1  christos 				for (i = 0; !e && i < x[2]; i++) {
   8492  1.1  christos 					e |= ((nat->nat_ndstaddr & x[i + 4]) ==
   8493  1.1  christos 					      x[i + 3]);
   8494  1.1  christos 				}
   8495  1.1  christos 			}
   8496  1.1  christos 			break;
   8497  1.1  christos 
   8498  1.1  christos 		case IPF_EXP_IP_ADDR :
   8499  1.1  christos 			for (i = 0; !e && i < x[2]; i++) {
   8500  1.1  christos 				if (nat->nat_v[0] == 4) {
   8501  1.1  christos 					e |= ((nat->nat_osrcaddr & x[i + 4]) ==
   8502  1.1  christos 					      x[i + 3]);
   8503  1.1  christos 				}
   8504  1.1  christos 				if (nat->nat_v[1] == 4) {
   8505  1.1  christos 					e |= ((nat->nat_nsrcaddr & x[i + 4]) ==
   8506  1.1  christos 					      x[i + 3]);
   8507  1.1  christos 				}
   8508  1.1  christos 				if (nat->nat_v[0] == 4) {
   8509  1.1  christos 					e |= ((nat->nat_odstaddr & x[i + 4]) ==
   8510  1.1  christos 					      x[i + 3]);
   8511  1.1  christos 				}
   8512  1.1  christos 				if (nat->nat_v[1] == 4) {
   8513  1.1  christos 					e |= ((nat->nat_ndstaddr & x[i + 4]) ==
   8514  1.1  christos 					      x[i + 3]);
   8515  1.1  christos 				}
   8516  1.1  christos 			}
   8517  1.1  christos 			break;
   8518  1.1  christos 
   8519  1.1  christos #ifdef USE_INET6
   8520  1.1  christos 		case IPF_EXP_IP6_SRCADDR :
   8521  1.1  christos 			if (nat->nat_v[0] == 6) {
   8522  1.1  christos 				for (i = 0; !e && i < x[3]; i++) {
   8523  1.1  christos 					e |= IP6_MASKEQ(&nat->nat_osrc6,
   8524  1.1  christos 							x + i + 7, x + i + 3);
   8525  1.1  christos 				}
   8526  1.1  christos 			}
   8527  1.1  christos 			if (nat->nat_v[1] == 6) {
   8528  1.1  christos 				for (i = 0; !e && i < x[3]; i++) {
   8529  1.1  christos 					e |= IP6_MASKEQ(&nat->nat_nsrc6,
   8530  1.1  christos 							x + i + 7, x + i + 3);
   8531  1.1  christos 				}
   8532  1.1  christos 			}
   8533  1.1  christos 			break;
   8534  1.1  christos 
   8535  1.1  christos 		case IPF_EXP_IP6_DSTADDR :
   8536  1.1  christos 			if (nat->nat_v[0] == 6) {
   8537  1.1  christos 				for (i = 0; !e && i < x[3]; i++) {
   8538  1.1  christos 					e |= IP6_MASKEQ(&nat->nat_odst6,
   8539  1.1  christos 							x + i + 7,
   8540  1.1  christos 							x + i + 3);
   8541  1.1  christos 				}
   8542  1.1  christos 			}
   8543  1.1  christos 			if (nat->nat_v[1] == 6) {
   8544  1.1  christos 				for (i = 0; !e && i < x[3]; i++) {
   8545  1.1  christos 					e |= IP6_MASKEQ(&nat->nat_ndst6,
   8546  1.1  christos 							x + i + 7,
   8547  1.1  christos 							x + i + 3);
   8548  1.1  christos 				}
   8549  1.1  christos 			}
   8550  1.1  christos 			break;
   8551  1.1  christos 
   8552  1.1  christos 		case IPF_EXP_IP6_ADDR :
   8553  1.1  christos 			for (i = 0; !e && i < x[3]; i++) {
   8554  1.1  christos 				if (nat->nat_v[0] == 6) {
   8555  1.1  christos 					e |= IP6_MASKEQ(&nat->nat_osrc6,
   8556  1.1  christos 							x + i + 7,
   8557  1.1  christos 							x + i + 3);
   8558  1.1  christos 				}
   8559  1.1  christos 				if (nat->nat_v[0] == 6) {
   8560  1.1  christos 					e |= IP6_MASKEQ(&nat->nat_odst6,
   8561  1.1  christos 							x + i + 7,
   8562  1.1  christos 							x + i + 3);
   8563  1.1  christos 				}
   8564  1.1  christos 				if (nat->nat_v[1] == 6) {
   8565  1.1  christos 					e |= IP6_MASKEQ(&nat->nat_nsrc6,
   8566  1.1  christos 							x + i + 7,
   8567  1.1  christos 							x + i + 3);
   8568  1.1  christos 				}
   8569  1.1  christos 				if (nat->nat_v[1] == 6) {
   8570  1.1  christos 					e |= IP6_MASKEQ(&nat->nat_ndst6,
   8571  1.1  christos 							x + i + 7,
   8572  1.1  christos 							x + i + 3);
   8573  1.1  christos 				}
   8574  1.1  christos 			}
   8575  1.1  christos 			break;
   8576  1.1  christos #endif
   8577  1.1  christos 
   8578  1.1  christos 		case IPF_EXP_UDP_PORT :
   8579  1.1  christos 		case IPF_EXP_TCP_PORT :
   8580  1.1  christos 			for (i = 0; !e && i < x[2]; i++) {
   8581  1.1  christos 				e |= (nat->nat_nsport == x[i + 3]) ||
   8582  1.1  christos 				     (nat->nat_ndport == x[i + 3]);
   8583  1.1  christos 			}
   8584  1.1  christos 			break;
   8585  1.1  christos 
   8586  1.1  christos 		case IPF_EXP_UDP_SPORT :
   8587  1.1  christos 		case IPF_EXP_TCP_SPORT :
   8588  1.1  christos 			for (i = 0; !e && i < x[2]; i++) {
   8589  1.1  christos 				e |= (nat->nat_nsport == x[i + 3]);
   8590  1.1  christos 			}
   8591  1.1  christos 			break;
   8592  1.1  christos 
   8593  1.1  christos 		case IPF_EXP_UDP_DPORT :
   8594  1.1  christos 		case IPF_EXP_TCP_DPORT :
   8595  1.1  christos 			for (i = 0; !e && i < x[2]; i++) {
   8596  1.1  christos 				e |= (nat->nat_ndport == x[i + 3]);
   8597  1.1  christos 			}
   8598  1.1  christos 			break;
   8599  1.1  christos 
   8600  1.1  christos 		case IPF_EXP_TCP_STATE :
   8601  1.1  christos 			for (i = 0; !e && i < x[2]; i++) {
   8602  1.1  christos 				e |= (nat->nat_tcpstate[0] == x[i + 3]) ||
   8603  1.1  christos 				     (nat->nat_tcpstate[1] == x[i + 3]);
   8604  1.1  christos 			}
   8605  1.1  christos 			break;
   8606  1.1  christos 
   8607  1.1  christos 		case IPF_EXP_IDLE_GT :
   8608  1.1  christos 			e |= (ticks - nat->nat_touched > x[3]);
   8609  1.1  christos 			break;
   8610  1.1  christos 		}
   8611  1.1  christos 		e ^= x[1];
   8612  1.1  christos 
   8613  1.1  christos 		if (!e)
   8614  1.1  christos 			break;
   8615  1.1  christos 	}
   8616  1.1  christos 
   8617  1.1  christos 	return e;
   8618  1.1  christos }
   8619  1.1  christos 
   8620  1.1  christos 
   8621  1.1  christos /* ------------------------------------------------------------------------ */
   8622  1.1  christos /* Function:    ipf_nat_gettable                                            */
   8623  1.1  christos /* Returns:     int     - 0 = success, else error                           */
   8624  1.1  christos /* Parameters:  data(I) - pointer to ioctl data                             */
   8625  1.1  christos /*                                                                          */
   8626  1.1  christos /* This function handles ioctl requests for tables of nat information.      */
   8627  1.1  christos /* At present the only table it deals with is the hash bucket statistics.   */
   8628  1.1  christos /* ------------------------------------------------------------------------ */
   8629  1.1  christos static int
   8630  1.1  christos ipf_nat_gettable(softc, softn, data)
   8631  1.1  christos 	ipf_main_softc_t *softc;
   8632  1.1  christos 	ipf_nat_softc_t *softn;
   8633  1.1  christos 	char *data;
   8634  1.1  christos {
   8635  1.1  christos 	ipftable_t table;
   8636  1.1  christos 	int error;
   8637  1.1  christos 
   8638  1.1  christos 	error = ipf_inobj(softc, data, NULL, &table, IPFOBJ_GTABLE);
   8639  1.1  christos 	if (error != 0)
   8640  1.1  christos 		return error;
   8641  1.1  christos 
   8642  1.1  christos 	switch (table.ita_type)
   8643  1.1  christos 	{
   8644  1.1  christos 	case IPFTABLE_BUCKETS_NATIN :
   8645  1.1  christos 		error = COPYOUT(softn->ipf_nat_stats.ns_side[0].ns_bucketlen,
   8646  1.1  christos 				table.ita_table,
   8647  1.1  christos 				softn->ipf_nat_table_sz * sizeof(u_long));
   8648  1.1  christos 		break;
   8649  1.1  christos 
   8650  1.1  christos 	case IPFTABLE_BUCKETS_NATOUT :
   8651  1.1  christos 		error = COPYOUT(softn->ipf_nat_stats.ns_side[1].ns_bucketlen,
   8652  1.1  christos 				table.ita_table,
   8653  1.1  christos 				softn->ipf_nat_table_sz * sizeof(u_long));
   8654  1.1  christos 		break;
   8655  1.1  christos 
   8656  1.1  christos 	default :
   8657  1.1  christos 		IPFERROR(60058);
   8658  1.1  christos 		return EINVAL;
   8659  1.1  christos 	}
   8660  1.1  christos 
   8661  1.1  christos 	if (error != 0) {
   8662  1.1  christos 		IPFERROR(60059);
   8663  1.1  christos 		error = EFAULT;
   8664  1.1  christos 	}
   8665  1.1  christos 	return error;
   8666  1.1  christos }
   8667  1.1  christos 
   8668  1.1  christos 
   8669  1.1  christos /* ------------------------------------------------------------------------ */
   8670  1.1  christos /* Function:    ipf_nat_settimeout                                          */
   8671  1.1  christos /* Returns:     int  - 0 = success, else failure			    */
   8672  1.1  christos /* Parameters:  t(I) - pointer to tunable                                   */
   8673  1.1  christos /*              p(I) - pointer to new tuning data                           */
   8674  1.1  christos /*                                                                          */
   8675  1.1  christos /* Apply the timeout change to the NAT timeout queues.                      */
   8676  1.1  christos /* ------------------------------------------------------------------------ */
   8677  1.1  christos int
   8678  1.1  christos ipf_nat_settimeout(softc, t, p)
   8679  1.1  christos 	struct ipf_main_softc_s *softc;
   8680  1.1  christos 	ipftuneable_t *t;
   8681  1.1  christos 	ipftuneval_t *p;
   8682  1.1  christos {
   8683  1.1  christos 	ipf_nat_softc_t *softn = softc->ipf_nat_soft;
   8684  1.1  christos 
   8685  1.1  christos 	if (!strncmp(t->ipft_name, "tcp_", 4))
   8686  1.1  christos 		return ipf_settimeout_tcp(t, p, softn->ipf_nat_tcptq);
   8687  1.1  christos 
   8688  1.1  christos 	if (!strcmp(t->ipft_name, "udp_timeout")) {
   8689  1.1  christos 		ipf_apply_timeout(&softn->ipf_nat_udptq, p->ipftu_int);
   8690  1.1  christos 	} else if (!strcmp(t->ipft_name, "udp_ack_timeout")) {
   8691  1.1  christos 		ipf_apply_timeout(&softn->ipf_nat_udpacktq, p->ipftu_int);
   8692  1.1  christos 	} else if (!strcmp(t->ipft_name, "icmp_timeout")) {
   8693  1.1  christos 		ipf_apply_timeout(&softn->ipf_nat_icmptq, p->ipftu_int);
   8694  1.1  christos 	} else if (!strcmp(t->ipft_name, "icmp_ack_timeout")) {
   8695  1.1  christos 		ipf_apply_timeout(&softn->ipf_nat_icmpacktq, p->ipftu_int);
   8696  1.1  christos 	} else if (!strcmp(t->ipft_name, "ip_timeout")) {
   8697  1.1  christos 		ipf_apply_timeout(&softn->ipf_nat_iptq, p->ipftu_int);
   8698  1.1  christos 	} else {
   8699  1.1  christos 		IPFERROR(60062);
   8700  1.1  christos 		return ESRCH;
   8701  1.1  christos 	}
   8702  1.1  christos 	return 0;
   8703  1.1  christos }
   8704  1.1  christos 
   8705  1.1  christos 
   8706  1.1  christos /* ------------------------------------------------------------------------ */
   8707  1.1  christos /* Function:    ipf_nat_rehash                                              */
   8708  1.1  christos /* Returns:     int  - 0 = success, else failure			    */
   8709  1.1  christos /* Parameters:  t(I) - pointer to tunable                                   */
   8710  1.1  christos /*              p(I) - pointer to new tuning data                           */
   8711  1.1  christos /*                                                                          */
   8712  1.1  christos /* To change the size of the basic NAT table, we need to first allocate the */
   8713  1.1  christos /* new tables (lest it fails and we've got nowhere to store all of the NAT  */
   8714  1.1  christos /* sessions currently active) and then walk through the entire list and     */
   8715  1.1  christos /* insert them into the table.  There are two tables here: an inbound one   */
   8716  1.1  christos /* and an outbound one.  Each NAT entry goes into each table once.          */
   8717  1.1  christos /* ------------------------------------------------------------------------ */
   8718  1.1  christos int
   8719  1.1  christos ipf_nat_rehash(softc, t, p)
   8720  1.1  christos 	ipf_main_softc_t *softc;
   8721  1.1  christos 	ipftuneable_t *t;
   8722  1.1  christos 	ipftuneval_t *p;
   8723  1.1  christos {
   8724  1.1  christos 	ipf_nat_softc_t *softn = softc->ipf_nat_soft;
   8725  1.1  christos 	nat_t **newtab[2], *nat, **natp;
   8726  1.1  christos 	u_int *bucketlens[2];
   8727  1.1  christos 	u_int maxbucket;
   8728  1.1  christos 	u_int newsize;
   8729  1.1  christos 	u_int hv;
   8730  1.1  christos 	int i;
   8731  1.1  christos 
   8732  1.1  christos 	newsize = p->ipftu_int;
   8733  1.1  christos 	/*
   8734  1.1  christos 	 * In case there is nothing to do...
   8735  1.1  christos 	 */
   8736  1.1  christos 	if (newsize == softn->ipf_nat_table_sz)
   8737  1.1  christos 		return 0;
   8738  1.1  christos 
   8739  1.1  christos 	/*
   8740  1.1  christos 	 * 4 tables depend on the NAT table size: the inbound looking table,
   8741  1.1  christos 	 * the outbound lookup table and the hash chain length for each.
   8742  1.1  christos 	 */
   8743  1.1  christos 	KMALLOCS(newtab[0], nat_t **, newsize * sizeof(nat_t *));
   8744  1.1  christos 	if (newtab == NULL) {
   8745  1.1  christos 		IPFERROR(60063);
   8746  1.1  christos 		return ENOMEM;
   8747  1.1  christos 	}
   8748  1.1  christos 
   8749  1.1  christos 	KMALLOCS(newtab[1], nat_t **, newsize * sizeof(nat_t *));
   8750  1.1  christos 	if (newtab == NULL) {
   8751  1.1  christos 		KFREES(newtab[0], newsize * sizeof(nat_t *));
   8752  1.1  christos 		IPFERROR(60064);
   8753  1.1  christos 		return ENOMEM;
   8754  1.1  christos 	}
   8755  1.1  christos 
   8756  1.1  christos 	KMALLOCS(bucketlens[0], u_int *, newsize * sizeof(u_int));
   8757  1.1  christos 	if (bucketlens[0] == NULL) {
   8758  1.1  christos 		KFREES(newtab[0], newsize * sizeof(nat_t *));
   8759  1.1  christos 		KFREES(newtab[1], newsize * sizeof(nat_t *));
   8760  1.1  christos 		IPFERROR(60065);
   8761  1.1  christos 		return ENOMEM;
   8762  1.1  christos 	}
   8763  1.1  christos 
   8764  1.1  christos 	KMALLOCS(bucketlens[1], u_int *, newsize * sizeof(u_int));
   8765  1.1  christos 	if (bucketlens[1] == NULL) {
   8766  1.1  christos 		KFREES(bucketlens[0], newsize * sizeof(u_int));
   8767  1.1  christos 		KFREES(newtab[0], newsize * sizeof(nat_t *));
   8768  1.1  christos 		KFREES(newtab[1], newsize * sizeof(nat_t *));
   8769  1.1  christos 		IPFERROR(60066);
   8770  1.1  christos 		return ENOMEM;
   8771  1.1  christos 	}
   8772  1.1  christos 
   8773  1.1  christos 	/*
   8774  1.1  christos 	 * Recalculate the maximum length based on the new size.
   8775  1.1  christos 	 */
   8776  1.1  christos 	for (maxbucket = 0, i = newsize; i > 0; i >>= 1)
   8777  1.1  christos 		maxbucket++;
   8778  1.1  christos 	maxbucket *= 2;
   8779  1.1  christos 
   8780  1.1  christos 	bzero((char *)newtab[0], newsize * sizeof(nat_t *));
   8781  1.1  christos 	bzero((char *)newtab[1], newsize * sizeof(nat_t *));
   8782  1.1  christos 	bzero((char *)bucketlens[0], newsize * sizeof(u_int));
   8783  1.1  christos 	bzero((char *)bucketlens[1], newsize * sizeof(u_int));
   8784  1.1  christos 
   8785  1.1  christos 	WRITE_ENTER(&softc->ipf_nat);
   8786  1.1  christos 
   8787  1.1  christos 	if (softn->ipf_nat_table[0] != NULL) {
   8788  1.1  christos 		KFREES(softn->ipf_nat_table[0],
   8789  1.1  christos 		       softn->ipf_nat_table_sz *
   8790  1.1  christos 		       sizeof(*softn->ipf_nat_table[0]));
   8791  1.1  christos 	}
   8792  1.1  christos 	softn->ipf_nat_table[0] = newtab[0];
   8793  1.1  christos 
   8794  1.1  christos 	if (softn->ipf_nat_table[1] != NULL) {
   8795  1.1  christos 		KFREES(softn->ipf_nat_table[1],
   8796  1.1  christos 		       softn->ipf_nat_table_sz *
   8797  1.1  christos 		       sizeof(*softn->ipf_nat_table[1]));
   8798  1.1  christos 	}
   8799  1.1  christos 	softn->ipf_nat_table[1] = newtab[1];
   8800  1.1  christos 
   8801  1.1  christos 	if (softn->ipf_nat_stats.ns_side[0].ns_bucketlen != NULL) {
   8802  1.1  christos 		KFREES(softn->ipf_nat_stats.ns_side[0].ns_bucketlen,
   8803  1.1  christos 		       softn->ipf_nat_table_sz * sizeof(u_int));
   8804  1.1  christos 	}
   8805  1.1  christos 	softn->ipf_nat_stats.ns_side[0].ns_bucketlen = bucketlens[0];
   8806  1.1  christos 
   8807  1.1  christos 	if (softn->ipf_nat_stats.ns_side[1].ns_bucketlen != NULL) {
   8808  1.1  christos 		KFREES(softn->ipf_nat_stats.ns_side[1].ns_bucketlen,
   8809  1.1  christos 		       softn->ipf_nat_table_sz * sizeof(u_int));
   8810  1.1  christos 	}
   8811  1.1  christos 	softn->ipf_nat_stats.ns_side[1].ns_bucketlen = bucketlens[1];
   8812  1.1  christos 
   8813  1.1  christos 	softn->ipf_nat_maxbucket = maxbucket;
   8814  1.1  christos 	softn->ipf_nat_table_sz = newsize;
   8815  1.1  christos 	/*
   8816  1.1  christos 	 * Walk through the entire list of NAT table entries and put them
   8817  1.1  christos 	 * in the new NAT table, somewhere.  Because we have a new table,
   8818  1.1  christos 	 * we need to restart the counter of how many chains are in use.
   8819  1.1  christos 	 */
   8820  1.1  christos 	softn->ipf_nat_stats.ns_side[0].ns_inuse = 0;
   8821  1.1  christos 	softn->ipf_nat_stats.ns_side[1].ns_inuse = 0;
   8822  1.1  christos 
   8823  1.1  christos 	for (nat = softn->ipf_nat_instances; nat != NULL; nat = nat->nat_next) {
   8824  1.1  christos 		nat->nat_hnext[0] = NULL;
   8825  1.1  christos 		nat->nat_phnext[0] = NULL;
   8826  1.1  christos 		hv = nat->nat_hv[0] % softn->ipf_nat_table_sz;
   8827  1.1  christos 
   8828  1.1  christos 		natp = &softn->ipf_nat_table[0][hv];
   8829  1.1  christos 		if (*natp) {
   8830  1.1  christos 			(*natp)->nat_phnext[0] = &nat->nat_hnext[0];
   8831  1.1  christos 		} else {
   8832  1.1  christos 			NBUMPSIDE(0, ns_inuse);
   8833  1.1  christos 		}
   8834  1.1  christos 		nat->nat_phnext[0] = natp;
   8835  1.1  christos 		nat->nat_hnext[0] = *natp;
   8836  1.1  christos 		*natp = nat;
   8837  1.1  christos 		NBUMPSIDE(0, ns_bucketlen[hv]);
   8838  1.1  christos 
   8839  1.1  christos 		nat->nat_hnext[1] = NULL;
   8840  1.1  christos 		nat->nat_phnext[1] = NULL;
   8841  1.1  christos 		hv = nat->nat_hv[1] % softn->ipf_nat_table_sz;
   8842  1.1  christos 
   8843  1.1  christos 		natp = &softn->ipf_nat_table[1][hv];
   8844  1.1  christos 		if (*natp) {
   8845  1.1  christos 			(*natp)->nat_phnext[1] = &nat->nat_hnext[1];
   8846  1.1  christos 		} else {
   8847  1.1  christos 			NBUMPSIDE(1, ns_inuse);
   8848  1.1  christos 		}
   8849  1.1  christos 		nat->nat_phnext[1] = natp;
   8850  1.1  christos 		nat->nat_hnext[1] = *natp;
   8851  1.1  christos 		*natp = nat;
   8852  1.1  christos 		NBUMPSIDE(1, ns_bucketlen[hv]);
   8853  1.1  christos 	}
   8854  1.1  christos 	RWLOCK_EXIT(&softc->ipf_nat);
   8855  1.1  christos 
   8856  1.1  christos 	return 0;
   8857  1.1  christos }
   8858  1.1  christos 
   8859  1.1  christos 
   8860  1.1  christos /* ------------------------------------------------------------------------ */
   8861  1.1  christos /* Function:    ipf_nat_rehash_rules                                        */
   8862  1.1  christos /* Returns:     int  - 0 = success, else failure			    */
   8863  1.1  christos /* Parameters:  t(I) - pointer to tunable                                   */
   8864  1.1  christos /*              p(I) - pointer to new tuning data                           */
   8865  1.1  christos /*                                                                          */
   8866  1.1  christos /* All of the NAT rules hang off of a hash table that is searched with a    */
   8867  1.1  christos /* hash on address after the netmask is applied.  There is a different table*/
   8868  1.1  christos /* for both inbound rules (rdr) and outbound (map.)  The resizing will only */
   8869  1.1  christos /* affect one of these two tables.                                          */
   8870  1.1  christos /* ------------------------------------------------------------------------ */
   8871  1.1  christos int
   8872  1.1  christos ipf_nat_rehash_rules(softc, t, p)
   8873  1.1  christos 	ipf_main_softc_t *softc;
   8874  1.1  christos 	ipftuneable_t *t;
   8875  1.1  christos 	ipftuneval_t *p;
   8876  1.1  christos {
   8877  1.1  christos 	ipf_nat_softc_t *softn = softc->ipf_nat_soft;
   8878  1.1  christos 	ipnat_t **newtab, *np, ***old, **npp;
   8879  1.1  christos 	u_int newsize;
   8880  1.1  christos 	u_int mask;
   8881  1.1  christos 	u_int hv;
   8882  1.1  christos 
   8883  1.1  christos 	newsize = p->ipftu_int;
   8884  1.1  christos 	/*
   8885  1.1  christos 	 * In case there is nothing to do...
   8886  1.1  christos 	 */
   8887  1.1  christos 	if (newsize == *t->ipft_pint)
   8888  1.1  christos 		return 0;
   8889  1.1  christos 
   8890  1.1  christos 	/*
   8891  1.1  christos 	 * All inbound rules have the NAT_REDIRECT bit set in in_redir and
   8892  1.1  christos 	 * all outbound rules have either NAT_MAP or MAT_MAPBLK set.
   8893  1.1  christos 	 * This if statement allows for some more generic code to be below,
   8894  1.1  christos 	 * rather than two huge gobs of code that almost do the same thing.
   8895  1.1  christos 	 */
   8896  1.1  christos 	if (t->ipft_pint == &softn->ipf_nat_rdrrules_sz) {
   8897  1.1  christos 		old = &softn->ipf_nat_rdr_rules;
   8898  1.1  christos 		mask = NAT_REDIRECT;
   8899  1.1  christos 	} else {
   8900  1.1  christos 		old = &softn->ipf_nat_map_rules;
   8901  1.1  christos 		mask = NAT_MAP|NAT_MAPBLK;
   8902  1.1  christos 	}
   8903  1.1  christos 
   8904  1.1  christos 	KMALLOCS(newtab, ipnat_t **, newsize * sizeof(ipnat_t *));
   8905  1.1  christos 	if (newtab == NULL) {
   8906  1.1  christos 		IPFERROR(60067);
   8907  1.1  christos 		return ENOMEM;
   8908  1.1  christos 	}
   8909  1.1  christos 
   8910  1.1  christos 	bzero((char *)newtab, newsize * sizeof(ipnat_t *));
   8911  1.1  christos 
   8912  1.1  christos 	WRITE_ENTER(&softc->ipf_nat);
   8913  1.1  christos 
   8914  1.1  christos 	if (*old != NULL) {
   8915  1.1  christos 		KFREES(*old, *t->ipft_pint * sizeof(ipnat_t **));
   8916  1.1  christos 	}
   8917  1.1  christos 	*old = newtab;
   8918  1.1  christos 	*t->ipft_pint = newsize;
   8919  1.1  christos 
   8920  1.1  christos 	for (np = softn->ipf_nat_list; np != NULL; np = np->in_next) {
   8921  1.1  christos 		if ((np->in_redir & mask) == 0)
   8922  1.1  christos 			continue;
   8923  1.1  christos 
   8924  1.1  christos 		if ((np->in_redir & NAT_ENCAP) == 0) {
   8925  1.1  christos 			if (np->in_redir & NAT_REDIRECT) {
   8926  1.1  christos 				np->in_rnext = NULL;
   8927  1.1  christos 				hv = np->in_hv[0] % newsize;
   8928  1.1  christos 				for (npp = newtab + hv; *npp != NULL; )
   8929  1.1  christos 					npp = &(*npp)->in_rnext;
   8930  1.1  christos 				np->in_prnext = npp;
   8931  1.1  christos 				*npp = np;
   8932  1.1  christos 			}
   8933  1.1  christos 			if (np->in_redir & NAT_MAP) {
   8934  1.1  christos 				np->in_mnext = NULL;
   8935  1.1  christos 				hv = np->in_hv[1] % newsize;
   8936  1.1  christos 				for (npp = newtab + hv; *npp != NULL; )
   8937  1.1  christos 					npp = &(*npp)->in_mnext;
   8938  1.1  christos 				np->in_pmnext = npp;
   8939  1.1  christos 				*npp = np;
   8940  1.1  christos 			}
   8941  1.1  christos 		} else {
   8942  1.1  christos 			if (np->in_redir & NAT_MAP) {
   8943  1.1  christos 				np->in_rnext = NULL;
   8944  1.1  christos 				hv = np->in_hv[0] % newsize;
   8945  1.1  christos 				for (npp = newtab + hv; *npp != NULL; )
   8946  1.1  christos 					npp = &(*npp)->in_rnext;
   8947  1.1  christos 				np->in_prnext = npp;
   8948  1.1  christos 				*npp = np;
   8949  1.1  christos 			}
   8950  1.1  christos 			if (np->in_redir & NAT_REDIRECT) {
   8951  1.1  christos 				np->in_mnext = NULL;
   8952  1.1  christos 				hv = np->in_hv[1] % newsize;
   8953  1.1  christos 				for (npp = newtab + hv; *npp != NULL; )
   8954  1.1  christos 					npp = &(*npp)->in_mnext;
   8955  1.1  christos 				np->in_pmnext = npp;
   8956  1.1  christos 				*npp = np;
   8957  1.1  christos 			}
   8958  1.1  christos 		}
   8959  1.1  christos 
   8960  1.1  christos 	}
   8961  1.1  christos 	RWLOCK_EXIT(&softc->ipf_nat);
   8962  1.1  christos 
   8963  1.1  christos 	return 0;
   8964  1.1  christos }
   8965  1.1  christos 
   8966  1.1  christos 
   8967  1.1  christos /* ------------------------------------------------------------------------ */
   8968  1.1  christos /* Function:    ipf_nat_hostmap_rehash                                      */
   8969  1.1  christos /* Returns:     int  - 0 = success, else failure			    */
   8970  1.1  christos /* Parameters:  t(I) - pointer to tunable                                   */
   8971  1.1  christos /*              p(I) - pointer to new tuning data                           */
   8972  1.1  christos /*                                                                          */
   8973  1.1  christos /* Allocate and populate a new hash table that will contain a reference to  */
   8974  1.1  christos /* all of the active IP# translations currently in place.                   */
   8975  1.1  christos /* ------------------------------------------------------------------------ */
   8976  1.1  christos int
   8977  1.1  christos ipf_nat_hostmap_rehash(softc, t, p)
   8978  1.1  christos 	ipf_main_softc_t *softc;
   8979  1.1  christos 	ipftuneable_t *t;
   8980  1.1  christos 	ipftuneval_t *p;
   8981  1.1  christos {
   8982  1.1  christos 	ipf_nat_softc_t *softn = softc->ipf_nat_soft;
   8983  1.1  christos 	hostmap_t *hm, **newtab;
   8984  1.1  christos 	u_int newsize;
   8985  1.1  christos 	u_int hv;
   8986  1.1  christos 
   8987  1.1  christos 	newsize = p->ipftu_int;
   8988  1.1  christos 	/*
   8989  1.1  christos 	 * In case there is nothing to do...
   8990  1.1  christos 	 */
   8991  1.1  christos 	if (newsize == *t->ipft_pint)
   8992  1.1  christos 		return 0;
   8993  1.1  christos 
   8994  1.1  christos 	KMALLOCS(newtab, hostmap_t **, newsize * sizeof(hostmap_t *));
   8995  1.1  christos 	if (newtab == NULL) {
   8996  1.1  christos 		IPFERROR(60068);
   8997  1.1  christos 		return ENOMEM;
   8998  1.1  christos 	}
   8999  1.1  christos 
   9000  1.1  christos 	bzero((char *)newtab, newsize * sizeof(hostmap_t *));
   9001  1.1  christos 
   9002  1.1  christos 	WRITE_ENTER(&softc->ipf_nat);
   9003  1.1  christos 	if (softn->ipf_hm_maptable != NULL) {
   9004  1.1  christos 		KFREES(softn->ipf_hm_maptable,
   9005  1.1  christos 		       softn->ipf_nat_hostmap_sz * sizeof(hostmap_t *));
   9006  1.1  christos 	}
   9007  1.1  christos 	softn->ipf_hm_maptable = newtab;
   9008  1.1  christos 	softn->ipf_nat_hostmap_sz = newsize;
   9009  1.1  christos 
   9010  1.1  christos 	for (hm = softn->ipf_hm_maplist; hm != NULL; hm = hm->hm_next) {
   9011  1.1  christos 		hv = hm->hm_hv % softn->ipf_nat_hostmap_sz;
   9012  1.1  christos 		hm->hm_hnext = softn->ipf_hm_maptable[hv];
   9013  1.1  christos 		hm->hm_phnext = softn->ipf_hm_maptable + hv;
   9014  1.1  christos 		if (softn->ipf_hm_maptable[hv] != NULL)
   9015  1.1  christos 			softn->ipf_hm_maptable[hv]->hm_phnext = &hm->hm_hnext;
   9016  1.1  christos 		softn->ipf_hm_maptable[hv] = hm;
   9017  1.1  christos 	}
   9018  1.1  christos 	RWLOCK_EXIT(&softc->ipf_nat);
   9019  1.1  christos 
   9020  1.1  christos 	return 0;
   9021  1.1  christos }
   9022  1.1  christos 
   9023  1.1  christos 
   9024  1.1  christos /* ------------------------------------------------------------------------ */
   9025  1.1  christos /* Function:    ipf_nat_add_tq                                              */
   9026  1.1  christos /* Parameters:  softc(I) - pointer to soft context main structure           */
   9027  1.1  christos /*                                                                          */
   9028  1.1  christos /* ------------------------------------------------------------------------ */
   9029  1.1  christos ipftq_t *
   9030  1.1  christos ipf_nat_add_tq(softc, ttl)
   9031  1.1  christos 	ipf_main_softc_t *softc;
   9032  1.1  christos 	int ttl;
   9033  1.1  christos {
   9034  1.1  christos 	ipf_nat_softc_t *softs = softc->ipf_nat_soft;
   9035  1.1  christos 
   9036  1.1  christos 	return ipf_addtimeoutqueue(softc, &softs->ipf_nat_utqe, ttl);
   9037  1.1  christos }
   9038  1.1  christos 
   9039  1.1  christos /* ------------------------------------------------------------------------ */
   9040  1.1  christos /* Function:    nat_uncreate                                                */
   9041  1.1  christos /* Returns:     Nil                                                         */
   9042  1.1  christos /* Parameters:  fin(I) - pointer to packet information                      */
   9043  1.1  christos /*                                                                          */
   9044  1.1  christos /* This function is used to remove a NAT entry from the NAT table when we   */
   9045  1.1  christos /* decide that the create was actually in error. It is thus assumed that    */
   9046  1.1  christos /* fin_flx will have both FI_NATED and FI_NATNEW set. Because we're dealing */
   9047  1.1  christos /* with the translated packet (not the original), we have to reverse the    */
   9048  1.1  christos /* lookup. Although doing the lookup is expensive (relatively speaking), it */
   9049  1.1  christos /* is not anticipated that this will be a frequent occurance for normal     */
   9050  1.1  christos /* traffic patterns.                                                        */
   9051  1.1  christos /* ------------------------------------------------------------------------ */
   9052  1.1  christos void
   9053  1.1  christos ipf_nat_uncreate(fin)
   9054  1.1  christos 	fr_info_t *fin;
   9055  1.1  christos {
   9056  1.1  christos 	ipf_main_softc_t *softc = fin->fin_main_soft;
   9057  1.1  christos 	ipf_nat_softc_t *softn = softc->ipf_nat_soft;
   9058  1.1  christos 	int nflags;
   9059  1.1  christos 	nat_t *nat;
   9060  1.1  christos 
   9061  1.1  christos 	switch (fin->fin_p)
   9062  1.1  christos 	{
   9063  1.1  christos 	case IPPROTO_TCP :
   9064  1.1  christos 		nflags = IPN_TCP;
   9065  1.1  christos 		break;
   9066  1.1  christos 	case IPPROTO_UDP :
   9067  1.1  christos 		nflags = IPN_UDP;
   9068  1.1  christos 		break;
   9069  1.1  christos 	default :
   9070  1.1  christos 		nflags = 0;
   9071  1.1  christos 		break;
   9072  1.1  christos 	}
   9073  1.1  christos 
   9074  1.1  christos 	WRITE_ENTER(&softc->ipf_nat);
   9075  1.1  christos 
   9076  1.1  christos 	if (fin->fin_out == 0) {
   9077  1.1  christos 		nat = ipf_nat_outlookup(fin, nflags, (u_int)fin->fin_p,
   9078  1.1  christos 					fin->fin_dst, fin->fin_src);
   9079  1.1  christos 	} else {
   9080  1.1  christos 		nat = ipf_nat_inlookup(fin, nflags, (u_int)fin->fin_p,
   9081  1.1  christos 				       fin->fin_src, fin->fin_dst);
   9082  1.1  christos 	}
   9083  1.1  christos 
   9084  1.1  christos 	if (nat != NULL) {
   9085  1.1  christos 		NBUMPSIDE(fin->fin_out, ns_uncreate[0]);
   9086  1.1  christos 		ipf_nat_delete(softc, nat, NL_DESTROY);
   9087  1.1  christos 	} else {
   9088  1.1  christos 		NBUMPSIDE(fin->fin_out, ns_uncreate[1]);
   9089  1.1  christos 	}
   9090  1.1  christos 
   9091  1.1  christos 	RWLOCK_EXIT(&softc->ipf_nat);
   9092  1.1  christos }
   9093