Home | History | Annotate | Line # | Download | only in netinet
ip_nat6.c revision 1.4.2.1
      1  1.4.2.1     rmind /*	$NetBSD: ip_nat6.c,v 1.4.2.1 2014/05/18 17:46:03 rmind Exp $	*/
      2      1.1  christos 
      3      1.1  christos /*
      4      1.3   darrenr  * 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_nat6_KERNEL
     11      1.1  christos # define        KERNEL	1
     12      1.1  christos # define        ipf_nat6_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) && defined(__NetBSD_Version__) && \
     20      1.1  christos     (__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_nat6_KERNEL
     28      1.1  christos # ifdef ipf_nat6__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_nat6_KERNEL
     33      1.1  christos #endif
     34      1.1  christos #if defined(_KERNEL) && (__FreeBSD_version >= 220000)
     35      1.1  christos # include <sys/filio.h>
     36      1.1  christos # include <sys/fcntl.h>
     37      1.1  christos #else
     38      1.1  christos # include <sys/ioctl.h>
     39      1.1  christos #endif
     40      1.1  christos #if !defined(AIX)
     41      1.1  christos # include <sys/fcntl.h>
     42      1.1  christos #endif
     43      1.1  christos #if !defined(linux)
     44      1.1  christos # include <sys/protosw.h>
     45      1.1  christos #endif
     46      1.1  christos #include <sys/socket.h>
     47      1.1  christos #if defined(_KERNEL)
     48      1.1  christos # include <sys/systm.h>
     49      1.1  christos # if !defined(__SVR4) && !defined(__svr4__)
     50      1.1  christos #  include <sys/mbuf.h>
     51      1.1  christos # endif
     52      1.1  christos #endif
     53      1.1  christos #if defined(__SVR4) || defined(__svr4__)
     54      1.1  christos # include <sys/filio.h>
     55      1.1  christos # include <sys/byteorder.h>
     56      1.1  christos # ifdef ipf_nat6_KERNEL
     57      1.1  christos #  include <sys/dditypes.h>
     58      1.1  christos # endif
     59      1.1  christos # include <sys/stream.h>
     60      1.1  christos # include <sys/kmem.h>
     61      1.1  christos #endif
     62      1.1  christos #if ipf_nat6__FreeBSD_version >= 300000
     63      1.1  christos # include <sys/queue.h>
     64      1.1  christos #endif
     65      1.1  christos #include <net/if.h>
     66      1.1  christos #if ipf_nat6__FreeBSD_version >= 300000
     67      1.1  christos # include <net/if_var.h>
     68      1.1  christos #endif
     69      1.1  christos #ifdef sun
     70      1.1  christos # include <net/af.h>
     71      1.1  christos #endif
     72      1.1  christos #include <net/route.h>
     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/ip_fil.h"
     92      1.1  christos #include "netinet/ip_nat.h"
     93      1.1  christos #include "netinet/ip_frag.h"
     94      1.1  christos #include "netinet/ip_state.h"
     95      1.1  christos #include "netinet/ip_proxy.h"
     96      1.1  christos #include "netinet/ip_lookup.h"
     97      1.1  christos #include "netinet/ip_dstlist.h"
     98      1.1  christos #include "netinet/ip_sync.h"
     99      1.1  christos #if (__FreeBSD_version >= 300000)
    100      1.1  christos # include <sys/malloc.h>
    101      1.1  christos #endif
    102      1.1  christos #ifdef HAS_SYS_MD5_H
    103      1.1  christos # include <sys/md5.h>
    104      1.1  christos #else
    105      1.1  christos # include "md5.h"
    106      1.1  christos #endif
    107      1.1  christos /* END OF INCLUDES */
    108      1.1  christos 
    109      1.1  christos #undef	SOCKADDR_IN
    110      1.1  christos #define	SOCKADDR_IN	struct sockaddr_in
    111      1.1  christos 
    112  1.4.2.1     rmind __KERNEL_RCSID(0, "Id: ip_nat6.c,v 1.1.1.2 2012/07/22 13:45:29 darrenr Exp");
    113      1.1  christos 
    114      1.1  christos #ifdef USE_INET6
    115      1.2  christos static struct hostmap *ipf_nat6_hostmap(ipf_nat_softc_t *, ipnat_t *,
    116      1.1  christos 					     i6addr_t *, i6addr_t *,
    117      1.2  christos 					     i6addr_t *, u_32_t);
    118      1.2  christos static int ipf_nat6_match(fr_info_t *, ipnat_t *);
    119      1.2  christos static void ipf_nat6_tabmove(ipf_nat_softc_t *, nat_t *);
    120      1.2  christos static int ipf_nat6_decap(fr_info_t *, nat_t *);
    121      1.2  christos static int ipf_nat6_nextaddr(fr_info_t *, nat_addr_t *, i6addr_t *,
    122      1.2  christos 				  i6addr_t *);
    123      1.2  christos static int ipf_nat6_icmpquerytype(int);
    124      1.2  christos static int ipf_nat6_out(fr_info_t *, nat_t *, int, u_32_t);
    125      1.2  christos static int ipf_nat6_in(fr_info_t *, nat_t *, int, u_32_t);
    126      1.2  christos static int ipf_nat6_builddivertmp(ipf_nat_softc_t *, ipnat_t *);
    127      1.2  christos static int ipf_nat6_nextaddrinit(ipf_main_softc_t *, char *,
    128      1.2  christos 				      nat_addr_t *, int, void *);
    129      1.2  christos static int ipf_nat6_insert(ipf_main_softc_t *, ipf_nat_softc_t *,
    130      1.2  christos 				nat_t *);
    131      1.1  christos 
    132      1.1  christos 
    133      1.1  christos #define	NINCLSIDE6(y,x)	ATOMIC_INCL(softn->ipf_nat_stats.ns_side6[y].x)
    134      1.3   darrenr #define	NBUMPSIDE(y,x)	softn->ipf_nat_stats.ns_side[y].x++
    135      1.1  christos #define	NBUMPSIDE6(y,x)	softn->ipf_nat_stats.ns_side6[y].x++
    136      1.1  christos #define	NBUMPSIDE6D(y,x) \
    137      1.1  christos 			do { \
    138      1.1  christos 				softn->ipf_nat_stats.ns_side6[y].x++; \
    139      1.1  christos 				DT(x); \
    140      1.1  christos 			} while (0)
    141      1.1  christos #define	NBUMPSIDE6DX(y,x,z) \
    142      1.1  christos 			do { \
    143      1.1  christos 				softn->ipf_nat_stats.ns_side6[y].x++; \
    144      1.1  christos 				DT(z); \
    145      1.1  christos 			} while (0)
    146      1.1  christos 
    147      1.1  christos 
    148      1.1  christos /* ------------------------------------------------------------------------ */
    149      1.1  christos /* Function:    ipf_nat6_ruleaddrinit                                       */
    150      1.1  christos /* Returns:     int   - 0 == success, else failure                          */
    151      1.1  christos /* Parameters:  in(I) - NAT rule that requires address fields to be init'd  */
    152      1.1  christos /*                                                                          */
    153      1.1  christos /* For each of the source/destination address fields in a NAT rule, call    */
    154      1.1  christos /* ipf_nat6_nextaddrinit() to prepare the structure for active duty.  Other */
    155      1.1  christos /* IPv6 specific actions can also be taken care of here.                    */
    156      1.1  christos /* ------------------------------------------------------------------------ */
    157      1.1  christos int
    158      1.2  christos ipf_nat6_ruleaddrinit(ipf_main_softc_t *softc, ipf_nat_softc_t *softn,
    159      1.2  christos     ipnat_t *n)
    160      1.1  christos {
    161      1.1  christos 	int idx, error;
    162      1.1  christos 
    163      1.1  christos 	if (n->in_redir == NAT_BIMAP) {
    164      1.1  christos 		n->in_ndstip6 = n->in_osrcip6;
    165      1.1  christos 		n->in_ndstmsk6 = n->in_osrcmsk6;
    166      1.1  christos 		n->in_odstip6 = n->in_nsrcip6;
    167      1.1  christos 		n->in_odstmsk6 = n->in_nsrcmsk6;
    168      1.1  christos 
    169      1.1  christos 	}
    170      1.1  christos 
    171      1.1  christos 	if (n->in_redir & NAT_REDIRECT)
    172      1.1  christos 		idx = 1;
    173      1.1  christos 	else
    174      1.1  christos 		idx = 0;
    175      1.1  christos 	/*
    176      1.1  christos 	 * Initialise all of the address fields.
    177      1.1  christos 	 */
    178      1.1  christos 	error = ipf_nat6_nextaddrinit(softc, n->in_names, &n->in_osrc, 1,
    179      1.1  christos 				      n->in_ifps[idx]);
    180      1.1  christos 	if (error != 0)
    181      1.1  christos 		return error;
    182      1.1  christos 
    183      1.1  christos 	error = ipf_nat6_nextaddrinit(softc, n->in_names, &n->in_odst, 1,
    184      1.1  christos 				      n->in_ifps[idx]);
    185      1.1  christos 	if (error != 0)
    186      1.1  christos 		return error;
    187      1.1  christos 
    188      1.1  christos 	error = ipf_nat6_nextaddrinit(softc, n->in_names, &n->in_nsrc, 1,
    189      1.1  christos 				      n->in_ifps[idx]);
    190      1.1  christos 	if (error != 0)
    191      1.1  christos 		return error;
    192      1.1  christos 
    193      1.1  christos 	error = ipf_nat6_nextaddrinit(softc, n->in_names, &n->in_ndst, 1,
    194      1.1  christos 				      n->in_ifps[idx]);
    195      1.1  christos 	if (error != 0)
    196      1.1  christos 		return error;
    197      1.1  christos 
    198      1.3   darrenr 	if (n->in_redir & NAT_DIVERTUDP)
    199      1.1  christos 		ipf_nat6_builddivertmp(softn, n);
    200      1.1  christos 	return 0;
    201      1.1  christos }
    202      1.1  christos 
    203      1.1  christos 
    204      1.1  christos /* ------------------------------------------------------------------------ */
    205      1.1  christos /* Function:    ipf_nat6_addrdr                                             */
    206      1.1  christos /* Returns:     Nil                                                         */
    207      1.1  christos /* Parameters:  n(I) - pointer to NAT rule to add                           */
    208      1.1  christos /*                                                                          */
    209      1.1  christos /* Adds a redirect rule to the hash table of redirect rules and the list of */
    210      1.1  christos /* loaded NAT rules.  Updates the bitmask indicating which netmasks are in  */
    211      1.1  christos /* use by redirect rules.                                                   */
    212      1.1  christos /* ------------------------------------------------------------------------ */
    213      1.1  christos void
    214      1.2  christos ipf_nat6_addrdr(ipf_nat_softc_t *softn, ipnat_t *n)
    215      1.1  christos {
    216      1.3   darrenr 	i6addr_t *mask;
    217      1.1  christos 	ipnat_t **np;
    218      1.1  christos 	i6addr_t j;
    219      1.1  christos 	u_int hv;
    220      1.1  christos 	int k;
    221      1.1  christos 
    222      1.3   darrenr 	if ((n->in_redir & NAT_BIMAP) == NAT_BIMAP) {
    223      1.1  christos 		k = count6bits(n->in_nsrcmsk6.i6);
    224      1.3   darrenr 		mask = &n->in_nsrcmsk6;
    225      1.1  christos 		IP6_AND(&n->in_odstip6, &n->in_odstmsk6, &j);
    226      1.1  christos 		hv = NAT_HASH_FN6(&j, 0, softn->ipf_nat_rdrrules_sz);
    227      1.1  christos 
    228      1.1  christos 	} else if (n->in_odstatype == FRI_NORMAL) {
    229      1.1  christos 		k = count6bits(n->in_odstmsk6.i6);
    230      1.3   darrenr 		mask = &n->in_odstmsk6;
    231      1.1  christos 		IP6_AND(&n->in_odstip6, &n->in_odstmsk6, &j);
    232      1.1  christos 		hv = NAT_HASH_FN6(&j, 0, softn->ipf_nat_rdrrules_sz);
    233      1.1  christos 	} else {
    234      1.3   darrenr 		k = 0;
    235      1.1  christos 		hv = 0;
    236      1.3   darrenr 		mask = NULL;
    237      1.1  christos 	}
    238      1.3   darrenr 	ipf_inet6_mask_add(k, mask, &softn->ipf_nat6_rdr_mask);
    239      1.3   darrenr 
    240      1.1  christos 	np = softn->ipf_nat_rdr_rules + hv;
    241      1.1  christos 	while (*np != NULL)
    242      1.1  christos 		np = &(*np)->in_rnext;
    243      1.1  christos 	n->in_rnext = NULL;
    244      1.1  christos 	n->in_prnext = np;
    245      1.1  christos 	n->in_hv[0] = hv;
    246      1.3   darrenr 	n->in_use++;
    247      1.1  christos 	*np = n;
    248      1.1  christos }
    249      1.1  christos 
    250      1.1  christos 
    251      1.1  christos /* ------------------------------------------------------------------------ */
    252      1.1  christos /* Function:    ipf_nat6_addmap                                             */
    253      1.1  christos /* Returns:     Nil                                                         */
    254      1.1  christos /* Parameters:  n(I) - pointer to NAT rule to add                           */
    255      1.1  christos /*                                                                          */
    256      1.1  christos /* Adds a NAT map rule to the hash table of rules and the list of  loaded   */
    257      1.1  christos /* NAT rules.  Updates the bitmask indicating which netmasks are in use by  */
    258      1.1  christos /* redirect rules.                                                          */
    259      1.1  christos /* ------------------------------------------------------------------------ */
    260      1.1  christos void
    261      1.2  christos ipf_nat6_addmap(ipf_nat_softc_t *softn, ipnat_t *n)
    262      1.1  christos {
    263      1.3   darrenr 	i6addr_t *mask;
    264      1.1  christos 	ipnat_t **np;
    265      1.1  christos 	i6addr_t j;
    266      1.1  christos 	u_int hv;
    267      1.1  christos 	int k;
    268      1.1  christos 
    269      1.1  christos 	if (n->in_osrcatype == FRI_NORMAL) {
    270      1.1  christos 		k = count6bits(n->in_osrcmsk6.i6);
    271      1.3   darrenr 		mask = &n->in_osrcmsk6;
    272      1.1  christos 		IP6_AND(&n->in_osrcip6, &n->in_osrcmsk6, &j);
    273      1.1  christos 		hv = NAT_HASH_FN6(&j, 0, softn->ipf_nat_maprules_sz);
    274      1.1  christos 	} else {
    275      1.3   darrenr 		k = 0;
    276      1.1  christos 		hv = 0;
    277      1.3   darrenr 		mask = NULL;
    278      1.1  christos 	}
    279      1.3   darrenr 	ipf_inet6_mask_add(k, mask, &softn->ipf_nat6_map_mask);
    280      1.3   darrenr 
    281      1.1  christos 	np = softn->ipf_nat_map_rules + hv;
    282      1.1  christos 	while (*np != NULL)
    283      1.1  christos 		np = &(*np)->in_mnext;
    284      1.1  christos 	n->in_mnext = NULL;
    285      1.1  christos 	n->in_pmnext = np;
    286      1.1  christos 	n->in_hv[1] = hv;
    287      1.3   darrenr 	n->in_use++;
    288      1.1  christos 	*np = n;
    289      1.1  christos }
    290      1.1  christos 
    291      1.1  christos 
    292      1.1  christos /* ------------------------------------------------------------------------ */
    293      1.1  christos /* Function:    ipf_nat6_del_rdr                                             */
    294      1.1  christos /* Returns:     Nil                                                         */
    295      1.1  christos /* Parameters:  n(I) - pointer to NAT rule to delete                        */
    296      1.1  christos /*                                                                          */
    297      1.1  christos /* Removes a NAT rdr rule from the hash table of NAT rdr rules.             */
    298      1.1  christos /* ------------------------------------------------------------------------ */
    299      1.3   darrenr void
    300      1.2  christos ipf_nat6_delrdr(ipf_nat_softc_t *softn, ipnat_t *n)
    301      1.1  christos {
    302      1.3   darrenr 	i6addr_t *mask;
    303      1.1  christos 	int k;
    304      1.1  christos 
    305      1.3   darrenr 	if ((n->in_redir & NAT_BIMAP) == NAT_BIMAP) {
    306      1.3   darrenr 		k = count6bits(n->in_nsrcmsk6.i6);
    307      1.3   darrenr 		mask = &n->in_nsrcmsk6;
    308      1.3   darrenr 	} else if (n->in_odstatype == FRI_NORMAL) {
    309      1.3   darrenr 		k = count6bits(n->in_odstmsk6.i6);
    310      1.3   darrenr 		mask = &n->in_odstmsk6;
    311      1.1  christos 	} else {
    312      1.1  christos 		k = 0;
    313      1.3   darrenr 		mask = NULL;
    314      1.1  christos 	}
    315      1.3   darrenr 	ipf_inet6_mask_del(k, mask, &softn->ipf_nat6_rdr_mask);
    316      1.1  christos 
    317      1.3   darrenr 	if (n->in_rnext != NULL)
    318      1.3   darrenr 		n->in_rnext->in_prnext = n->in_prnext;
    319      1.3   darrenr 	*n->in_prnext = n->in_rnext;
    320      1.3   darrenr 	n->in_use--;
    321      1.1  christos }
    322      1.1  christos 
    323      1.1  christos 
    324      1.1  christos /* ------------------------------------------------------------------------ */
    325      1.1  christos /* Function:    ipf_nat6_delmap                                             */
    326      1.1  christos /* Returns:     Nil                                                         */
    327      1.1  christos /* Parameters:  n(I) - pointer to NAT rule to delete                        */
    328      1.1  christos /*                                                                          */
    329      1.1  christos /* Removes a NAT map rule from the hash table of NAT map rules.             */
    330      1.1  christos /* ------------------------------------------------------------------------ */
    331      1.3   darrenr void
    332      1.2  christos ipf_nat6_delmap(ipf_nat_softc_t *softn, ipnat_t *n)
    333      1.1  christos {
    334      1.3   darrenr 	i6addr_t *mask;
    335      1.1  christos 	int k;
    336      1.1  christos 
    337      1.1  christos 	if (n->in_osrcatype == FRI_NORMAL) {
    338      1.3   darrenr 		k = count6bits(n->in_osrcmsk6.i6);
    339      1.3   darrenr 		mask = &n->in_osrcmsk6;
    340      1.1  christos 	} else {
    341      1.1  christos 		k = 0;
    342      1.3   darrenr 		mask = NULL;
    343      1.1  christos 	}
    344      1.3   darrenr 	ipf_inet6_mask_del(k, mask, &softn->ipf_nat6_map_mask);
    345      1.1  christos 
    346      1.1  christos 	if (n->in_mnext != NULL)
    347      1.1  christos 		n->in_mnext->in_pmnext = n->in_pmnext;
    348      1.1  christos 	*n->in_pmnext = n->in_mnext;
    349      1.3   darrenr 	n->in_use--;
    350      1.1  christos }
    351      1.1  christos 
    352      1.1  christos 
    353      1.1  christos /* ------------------------------------------------------------------------ */
    354      1.1  christos /* Function:    ipf_nat6_hostmap                                            */
    355      1.1  christos /* Returns:     struct hostmap* - NULL if no hostmap could be created,      */
    356      1.1  christos /*                                else a pointer to the hostmapping to use  */
    357      1.1  christos /* Parameters:  np(I)   - pointer to NAT rule                               */
    358      1.1  christos /*              real(I) - real IP address                                   */
    359      1.1  christos /*              map(I)  - mapped IP address                                 */
    360      1.1  christos /*              port(I) - destination port number                           */
    361      1.1  christos /* Write Locks: ipf_nat                                                     */
    362      1.1  christos /*                                                                          */
    363      1.1  christos /* Check if an ip address has already been allocated for a given mapping    */
    364      1.1  christos /* that is not doing port based translation.  If is not yet allocated, then */
    365      1.1  christos /* create a new entry if a non-NULL NAT rule pointer has been supplied.     */
    366      1.1  christos /* ------------------------------------------------------------------------ */
    367      1.1  christos static struct hostmap *
    368      1.2  christos ipf_nat6_hostmap(ipf_nat_softc_t *softn, ipnat_t *np, i6addr_t *src,
    369      1.2  christos     i6addr_t *dst, i6addr_t *map, u_32_t port)
    370      1.1  christos {
    371      1.1  christos 	hostmap_t *hm;
    372      1.1  christos 	u_int hv;
    373      1.1  christos 
    374      1.1  christos 	hv = (src->i6[3] ^ dst->i6[3]);
    375      1.1  christos 	hv += (src->i6[2] ^ dst->i6[2]);
    376      1.1  christos 	hv += (src->i6[1] ^ dst->i6[1]);
    377      1.1  christos 	hv += (src->i6[0] ^ dst->i6[0]);
    378      1.1  christos 	hv += src->i6[3];
    379      1.1  christos 	hv += src->i6[2];
    380      1.1  christos 	hv += src->i6[1];
    381      1.1  christos 	hv += src->i6[0];
    382      1.1  christos 	hv += dst->i6[3];
    383      1.1  christos 	hv += dst->i6[2];
    384      1.1  christos 	hv += dst->i6[1];
    385      1.1  christos 	hv += dst->i6[0];
    386      1.1  christos 	hv %= HOSTMAP_SIZE;
    387      1.1  christos 	for (hm = softn->ipf_hm_maptable[hv]; hm; hm = hm->hm_next)
    388      1.1  christos 		if (IP6_EQ(&hm->hm_osrc6, src) &&
    389      1.1  christos 		    IP6_EQ(&hm->hm_odst6, dst) &&
    390      1.1  christos 		    ((np == NULL) || (np == hm->hm_ipnat)) &&
    391      1.1  christos 		    ((port == 0) || (port == hm->hm_port))) {
    392      1.1  christos 			softn->ipf_nat_stats.ns_hm_addref++;
    393      1.1  christos 			hm->hm_ref++;
    394      1.1  christos 			return hm;
    395      1.1  christos 		}
    396      1.1  christos 
    397      1.1  christos 	if (np == NULL) {
    398      1.1  christos 		softn->ipf_nat_stats.ns_hm_nullnp++;
    399      1.1  christos 		return NULL;
    400      1.1  christos 	}
    401      1.1  christos 
    402      1.1  christos 	KMALLOC(hm, hostmap_t *);
    403      1.1  christos 	if (hm) {
    404      1.1  christos 		hm->hm_next = softn->ipf_hm_maplist;
    405      1.1  christos 		hm->hm_pnext = &softn->ipf_hm_maplist;
    406      1.1  christos 		if (softn->ipf_hm_maplist != NULL)
    407      1.1  christos 			softn->ipf_hm_maplist->hm_pnext = &hm->hm_next;
    408      1.1  christos 		softn->ipf_hm_maplist = hm;
    409      1.1  christos 		hm->hm_hnext = softn->ipf_hm_maptable[hv];
    410      1.1  christos 		hm->hm_phnext = softn->ipf_hm_maptable + hv;
    411      1.1  christos 		if (softn->ipf_hm_maptable[hv] != NULL)
    412      1.1  christos 			softn->ipf_hm_maptable[hv]->hm_phnext = &hm->hm_hnext;
    413      1.1  christos 		softn->ipf_hm_maptable[hv] = hm;
    414      1.1  christos 		hm->hm_ipnat = np;
    415      1.3   darrenr 		np->in_use++;
    416      1.1  christos 		hm->hm_osrcip6 = *src;
    417      1.1  christos 		hm->hm_odstip6 = *dst;
    418      1.1  christos 		hm->hm_nsrcip6 = *map;
    419      1.1  christos 		hm->hm_ndstip6.i6[0] = 0;
    420      1.1  christos 		hm->hm_ndstip6.i6[1] = 0;
    421      1.1  christos 		hm->hm_ndstip6.i6[2] = 0;
    422      1.1  christos 		hm->hm_ndstip6.i6[3] = 0;
    423      1.1  christos 		hm->hm_ref = 1;
    424      1.1  christos 		hm->hm_port = port;
    425      1.1  christos 		hm->hm_hv = hv;
    426      1.1  christos 		hm->hm_v = 6;
    427      1.1  christos 		softn->ipf_nat_stats.ns_hm_new++;
    428      1.1  christos 	} else {
    429      1.1  christos 		softn->ipf_nat_stats.ns_hm_newfail++;
    430      1.1  christos 	}
    431      1.1  christos 	return hm;
    432      1.1  christos }
    433      1.1  christos 
    434      1.1  christos 
    435      1.1  christos /* ------------------------------------------------------------------------ */
    436      1.1  christos /* Function:    ipf_nat6_newmap                                             */
    437      1.1  christos /* Returns:     int - -1 == error, 0 == success                             */
    438      1.1  christos /* Parameters:  fin(I) - pointer to packet information                      */
    439      1.1  christos /*              nat(I) - pointer to NAT entry                               */
    440      1.1  christos /*              ni(I)  - pointer to structure with misc. information needed */
    441      1.1  christos /*                       to create new NAT entry.                           */
    442      1.1  christos /*                                                                          */
    443      1.1  christos /* Given an empty NAT structure, populate it with new information about a   */
    444      1.1  christos /* new NAT session, as defined by the matching NAT rule.                    */
    445      1.1  christos /* ni.nai_ip is passed in uninitialised and must be set, in host byte order,*/
    446      1.1  christos /* to the new IP address for the translation.                               */
    447      1.1  christos /* ------------------------------------------------------------------------ */
    448      1.1  christos int
    449      1.2  christos ipf_nat6_newmap(fr_info_t *fin, nat_t *nat, natinfo_t *ni)
    450      1.1  christos {
    451      1.1  christos 	ipf_main_softc_t *softc = fin->fin_main_soft;
    452      1.1  christos 	ipf_nat_softc_t *softn = softc->ipf_nat_soft;
    453      1.1  christos 	u_short st_port, dport, sport, port, sp, dp;
    454      1.1  christos 	i6addr_t in, st_ip;
    455      1.1  christos 	hostmap_t *hm;
    456      1.1  christos 	u_32_t flags;
    457      1.1  christos 	ipnat_t *np;
    458      1.1  christos 	nat_t *natl;
    459      1.1  christos 	int l;
    460      1.1  christos 
    461      1.1  christos 	/*
    462      1.1  christos 	 * If it's an outbound packet which doesn't match any existing
    463      1.1  christos 	 * record, then create a new port
    464      1.1  christos 	 */
    465      1.1  christos 	l = 0;
    466      1.1  christos 	hm = NULL;
    467      1.1  christos 	np = ni->nai_np;
    468      1.1  christos 	st_ip = np->in_snip6;
    469      1.1  christos 	st_port = np->in_spnext;
    470      1.1  christos 	flags = nat->nat_flags;
    471      1.1  christos 
    472      1.1  christos 	if (flags & IPN_ICMPQUERY) {
    473      1.1  christos 		sport = fin->fin_data[1];
    474      1.1  christos 		dport = 0;
    475      1.1  christos 	} else {
    476      1.1  christos 		sport = htons(fin->fin_data[0]);
    477      1.1  christos 		dport = htons(fin->fin_data[1]);
    478      1.1  christos 	}
    479      1.1  christos 
    480      1.1  christos 	/*
    481      1.1  christos 	 * Do a loop until we either run out of entries to try or we find
    482      1.1  christos 	 * a NAT mapping that isn't currently being used.  This is done
    483      1.1  christos 	 * because the change to the source is not (usually) being fixed.
    484      1.1  christos 	 */
    485      1.1  christos 	do {
    486      1.1  christos 		port = 0;
    487      1.1  christos 		in = np->in_nsrc.na_nextaddr;
    488      1.1  christos 		if (l == 0) {
    489      1.1  christos 			/*
    490      1.1  christos 			 * Check to see if there is an existing NAT
    491      1.1  christos 			 * setup for this IP address pair.
    492      1.1  christos 			 */
    493      1.1  christos 			hm = ipf_nat6_hostmap(softn, np, &fin->fin_src6,
    494      1.1  christos 					      &fin->fin_dst6, &in, 0);
    495      1.1  christos 			if (hm != NULL)
    496      1.1  christos 				in = hm->hm_nsrcip6;
    497      1.1  christos 		} else if ((l == 1) && (hm != NULL)) {
    498      1.3   darrenr 			ipf_nat_hostmapdel(softc, &hm);
    499      1.1  christos 		}
    500      1.1  christos 
    501      1.1  christos 		nat->nat_hm = hm;
    502      1.1  christos 
    503      1.1  christos 		if (IP6_ISONES(&np->in_nsrcmsk6) && (np->in_spnext == 0)) {
    504      1.1  christos 			if (l > 0) {
    505      1.1  christos 				NBUMPSIDE6DX(1, ns_exhausted, ns_exhausted_1);
    506      1.1  christos 				return -1;
    507      1.1  christos 			}
    508      1.1  christos 		}
    509      1.1  christos 
    510      1.1  christos 		if ((np->in_redir == NAT_BIMAP) &&
    511      1.1  christos 		    IP6_EQ(&np->in_osrcmsk6, &np->in_nsrcmsk6)) {
    512      1.1  christos 			i6addr_t temp;
    513      1.1  christos 			/*
    514      1.1  christos 			 * map the address block in a 1:1 fashion
    515      1.1  christos 			 */
    516      1.1  christos 			temp.i6[0] = fin->fin_src6.i6[0] &
    517      1.1  christos 				     ~np->in_osrcmsk6.i6[0];
    518      1.1  christos 			temp.i6[1] = fin->fin_src6.i6[1] &
    519      1.1  christos 				     ~np->in_osrcmsk6.i6[1];
    520      1.1  christos 			temp.i6[2] = fin->fin_src6.i6[2] &
    521      1.1  christos 				     ~np->in_osrcmsk6.i6[0];
    522      1.1  christos 			temp.i6[3] = fin->fin_src6.i6[3] &
    523      1.1  christos 				     ~np->in_osrcmsk6.i6[3];
    524      1.1  christos 			in = np->in_nsrcip6;
    525      1.1  christos 			IP6_MERGE(&in, &temp, &np->in_osrc);
    526      1.1  christos 
    527      1.1  christos #ifdef NEED_128BIT_MATH
    528      1.1  christos 		} else if (np->in_redir & NAT_MAPBLK) {
    529      1.1  christos 			if ((l >= np->in_ppip) || ((l > 0) &&
    530      1.1  christos 			     !(flags & IPN_TCPUDP))) {
    531      1.1  christos 				NBUMPSIDE6DX(1, ns_exhausted, ns_exhausted_2);
    532      1.1  christos 				return -1;
    533      1.1  christos 			}
    534      1.1  christos 			/*
    535      1.1  christos 			 * map-block - Calculate destination address.
    536      1.1  christos 			 */
    537      1.1  christos 			IP6_MASK(&in, &fin->fin_src6, &np->in_osrcmsk6);
    538      1.1  christos 			in = ntohl(in);
    539      1.1  christos 			inb = in;
    540      1.1  christos 			in.s_addr /= np->in_ippip;
    541      1.1  christos 			in.s_addr &= ntohl(~np->in_nsrcmsk6);
    542      1.1  christos 			in.s_addr += ntohl(np->in_nsrcaddr6);
    543      1.1  christos 			/*
    544      1.1  christos 			 * Calculate destination port.
    545      1.1  christos 			 */
    546      1.1  christos 			if ((flags & IPN_TCPUDP) &&
    547      1.1  christos 			    (np->in_ppip != 0)) {
    548      1.1  christos 				port = ntohs(sport) + l;
    549      1.1  christos 				port %= np->in_ppip;
    550      1.1  christos 				port += np->in_ppip *
    551      1.1  christos 					(inb.s_addr % np->in_ippip);
    552      1.1  christos 				port += MAPBLK_MINPORT;
    553      1.1  christos 				port = htons(port);
    554      1.1  christos 			}
    555      1.1  christos #endif
    556      1.1  christos 
    557      1.1  christos 		} else if (IP6_ISZERO(&np->in_nsrcaddr) &&
    558      1.1  christos 			   IP6_ISONES(&np->in_nsrcmsk)) {
    559      1.1  christos 			/*
    560      1.1  christos 			 * 0/32 - use the interface's IP address.
    561      1.1  christos 			 */
    562      1.1  christos 			if ((l > 0) ||
    563      1.1  christos 			    ipf_ifpaddr(softc, 6, FRI_NORMAL, fin->fin_ifp,
    564      1.1  christos 				       &in, NULL) == -1) {
    565      1.1  christos 				NBUMPSIDE6DX(1, ns_new_ifpaddr,
    566      1.1  christos 					     ns_new_ifpaddr_1);
    567      1.1  christos 				return -1;
    568      1.1  christos 			}
    569      1.1  christos 
    570      1.1  christos 		} else if (IP6_ISZERO(&np->in_nsrcip6) &&
    571      1.1  christos 			   IP6_ISZERO(&np->in_nsrcmsk6)) {
    572      1.1  christos 			/*
    573      1.1  christos 			 * 0/0 - use the original source address/port.
    574      1.1  christos 			 */
    575      1.1  christos 			if (l > 0) {
    576      1.1  christos 				NBUMPSIDE6DX(1, ns_exhausted, ns_exhausted_3);
    577      1.1  christos 				return -1;
    578      1.1  christos 			}
    579      1.1  christos 			in = fin->fin_src6;
    580      1.1  christos 
    581      1.1  christos 		} else if (!IP6_ISONES(&np->in_nsrcmsk6) &&
    582      1.1  christos 			   (np->in_spnext == 0) && ((l > 0) || (hm == NULL))) {
    583      1.1  christos 			IP6_INC(&np->in_snip6);
    584      1.1  christos 		}
    585      1.1  christos 
    586      1.1  christos 		natl = NULL;
    587      1.1  christos 
    588      1.1  christos 		if ((flags & IPN_TCPUDP) &&
    589      1.1  christos 		    ((np->in_redir & NAT_MAPBLK) == 0) &&
    590      1.1  christos 		    (np->in_flags & IPN_AUTOPORTMAP)) {
    591      1.1  christos #ifdef NEED_128BIT_MATH
    592      1.1  christos 			/*
    593      1.1  christos 			 * "ports auto" (without map-block)
    594      1.1  christos 			 */
    595      1.1  christos 			if ((l > 0) && (l % np->in_ppip == 0)) {
    596      1.1  christos 				if ((l > np->in_ppip) &&
    597      1.1  christos 				    !IP6_ISONES(&np->in_nsrcmsk)) {
    598      1.1  christos 					IP6_INC(&np->in_snip6)
    599      1.1  christos 				}
    600      1.1  christos 			}
    601      1.1  christos 			if (np->in_ppip != 0) {
    602      1.1  christos 				port = ntohs(sport);
    603      1.1  christos 				port += (l % np->in_ppip);
    604      1.1  christos 				port %= np->in_ppip;
    605      1.1  christos 				port += np->in_ppip *
    606      1.1  christos 					(ntohl(fin->fin_src6) %
    607      1.1  christos 					 np->in_ippip);
    608      1.1  christos 				port += MAPBLK_MINPORT;
    609      1.1  christos 				port = htons(port);
    610      1.1  christos 			}
    611      1.1  christos #endif
    612      1.1  christos 
    613      1.1  christos 		} else if (((np->in_redir & NAT_MAPBLK) == 0) &&
    614      1.1  christos 			   (flags & IPN_TCPUDPICMP) && (np->in_spnext != 0)) {
    615      1.1  christos                         /*
    616      1.1  christos                          * Standard port translation.  Select next port.
    617      1.1  christos                          */
    618      1.1  christos                         if (np->in_flags & IPN_SEQUENTIAL) {
    619      1.1  christos                                 port = np->in_spnext;
    620      1.1  christos                         } else {
    621      1.1  christos 				port = ipf_random() % (np->in_spmax -
    622      1.1  christos 						       np->in_spmin + 1);
    623      1.1  christos                                 port += np->in_spmin;
    624      1.1  christos                         }
    625      1.1  christos                         port = htons(port);
    626      1.1  christos                         np->in_spnext++;
    627      1.1  christos 
    628      1.1  christos 			if (np->in_spnext > np->in_spmax) {
    629      1.1  christos 				np->in_spnext = np->in_spmin;
    630      1.1  christos 				if (!IP6_ISONES(&np->in_nsrcmsk6)) {
    631      1.1  christos 					IP6_INC(&np->in_snip6);
    632      1.1  christos 				}
    633      1.1  christos 			}
    634      1.1  christos 		}
    635      1.1  christos 
    636      1.1  christos 		if (np->in_flags & IPN_SIPRANGE) {
    637      1.1  christos 			if (IP6_GT(&np->in_snip, &np->in_nsrcmsk))
    638      1.1  christos 				np->in_snip6 = np->in_nsrcip6;
    639      1.1  christos 		} else {
    640      1.1  christos 			i6addr_t a1, a2;
    641      1.1  christos 
    642      1.1  christos 			a1 = np->in_snip6;
    643      1.1  christos 			IP6_INC(&a1);
    644      1.1  christos 			IP6_AND(&a1, &np->in_nsrcmsk6, &a2);
    645      1.1  christos 
    646      1.1  christos 			if (!IP6_ISONES(&np->in_nsrcmsk6) &&
    647      1.1  christos 			    IP6_GT(&a2, &np->in_nsrcip6)) {
    648      1.1  christos 				IP6_ADD(&np->in_nsrcip6, 1, &np->in_snip6);
    649      1.1  christos 			}
    650      1.1  christos 		}
    651      1.1  christos 
    652      1.1  christos 		if ((port == 0) && (flags & (IPN_TCPUDPICMP|IPN_ICMPQUERY)))
    653      1.1  christos 			port = sport;
    654      1.1  christos 
    655      1.1  christos 		/*
    656      1.1  christos 		 * Here we do a lookup of the connection as seen from
    657      1.1  christos 		 * the outside.  If an IP# pair already exists, try
    658      1.1  christos 		 * again.  So if you have A->B becomes C->B, you can
    659      1.1  christos 		 * also have D->E become C->E but not D->B causing
    660      1.1  christos 		 * another C->B.  Also take protocol and ports into
    661      1.1  christos 		 * account when determining whether a pre-existing
    662      1.1  christos 		 * NAT setup will cause an external conflict where
    663      1.1  christos 		 * this is appropriate.
    664      1.1  christos 		 */
    665      1.1  christos 		sp = fin->fin_data[0];
    666      1.1  christos 		dp = fin->fin_data[1];
    667      1.1  christos 		fin->fin_data[0] = fin->fin_data[1];
    668      1.1  christos 		fin->fin_data[1] = ntohs(port);
    669      1.1  christos 		natl = ipf_nat6_inlookup(fin, flags & ~(SI_WILDP|NAT_SEARCH),
    670      1.1  christos 					 (u_int)fin->fin_p, &fin->fin_dst6.in6,
    671      1.1  christos 					 &in.in6);
    672      1.1  christos 		fin->fin_data[0] = sp;
    673      1.1  christos 		fin->fin_data[1] = dp;
    674      1.1  christos 
    675      1.1  christos 		/*
    676      1.1  christos 		 * Has the search wrapped around and come back to the
    677      1.1  christos 		 * start ?
    678      1.1  christos 		 */
    679      1.1  christos 		if ((natl != NULL) &&
    680      1.1  christos 		    (np->in_spnext != 0) && (st_port == np->in_spnext) &&
    681      1.1  christos 		    (!IP6_ISZERO(&np->in_snip6) &&
    682      1.1  christos 		     IP6_EQ(&st_ip, &np->in_snip6))) {
    683      1.1  christos 			NBUMPSIDE6D(1, ns_wrap);
    684      1.1  christos 			return -1;
    685      1.1  christos 		}
    686      1.1  christos 		l++;
    687      1.1  christos 	} while (natl != NULL);
    688      1.1  christos 
    689      1.1  christos 	/* Setup the NAT table */
    690      1.1  christos 	nat->nat_osrc6 = fin->fin_src6;
    691      1.1  christos 	nat->nat_nsrc6 = in;
    692      1.1  christos 	nat->nat_odst6 = fin->fin_dst6;
    693      1.1  christos 	nat->nat_ndst6 = fin->fin_dst6;
    694      1.1  christos 	if (nat->nat_hm == NULL)
    695      1.1  christos 		nat->nat_hm = ipf_nat6_hostmap(softn, np, &fin->fin_src6,
    696      1.1  christos 					       &fin->fin_dst6,
    697      1.1  christos 					       &nat->nat_nsrc6, 0);
    698      1.1  christos 
    699      1.1  christos 	if (flags & IPN_TCPUDP) {
    700      1.1  christos 		nat->nat_osport = sport;
    701      1.1  christos 		nat->nat_nsport = port;	/* sport */
    702      1.1  christos 		nat->nat_odport = dport;
    703      1.1  christos 		nat->nat_ndport = dport;
    704      1.1  christos 		((tcphdr_t *)fin->fin_dp)->th_sport = port;
    705      1.1  christos 	} else if (flags & IPN_ICMPQUERY) {
    706      1.1  christos 		nat->nat_oicmpid = fin->fin_data[1];
    707      1.1  christos 		((struct icmp6_hdr *)fin->fin_dp)->icmp6_id = port;
    708      1.1  christos 		nat->nat_nicmpid = port;
    709      1.1  christos 	}
    710      1.1  christos 	return 0;
    711      1.1  christos }
    712      1.1  christos 
    713      1.1  christos 
    714      1.1  christos /* ------------------------------------------------------------------------ */
    715      1.1  christos /* Function:    ipf_nat6_newrdr                                             */
    716      1.1  christos /* Returns:     int - -1 == error, 0 == success (no move), 1 == success and */
    717      1.1  christos /*                    allow rule to be moved if IPN_ROUNDR is set.          */
    718      1.1  christos /* Parameters:  fin(I) - pointer to packet information                      */
    719      1.1  christos /*              nat(I) - pointer to NAT entry                               */
    720      1.1  christos /*              ni(I)  - pointer to structure with misc. information needed */
    721      1.1  christos /*                       to create new NAT entry.                           */
    722      1.1  christos /*                                                                          */
    723      1.1  christos /* ni.nai_ip is passed in uninitialised and must be set, in host byte order,*/
    724      1.1  christos /* to the new IP address for the translation.                               */
    725      1.1  christos /* ------------------------------------------------------------------------ */
    726      1.1  christos int
    727      1.2  christos ipf_nat6_newrdr(fr_info_t *fin, nat_t *nat, natinfo_t *ni)
    728      1.1  christos {
    729      1.1  christos 	ipf_main_softc_t *softc = fin->fin_main_soft;
    730      1.1  christos 	ipf_nat_softc_t *softn = softc->ipf_nat_soft;
    731      1.1  christos 	u_short nport, dport, sport;
    732      1.1  christos 	u_short sp, dp;
    733      1.1  christos 	hostmap_t *hm;
    734      1.1  christos 	u_32_t flags;
    735      1.1  christos 	i6addr_t in;
    736      1.1  christos 	ipnat_t *np;
    737      1.1  christos 	nat_t *natl;
    738      1.1  christos 	int move;
    739      1.1  christos 
    740      1.1  christos 	move = 1;
    741      1.1  christos 	hm = NULL;
    742      1.1  christos 	in.i6[0] = 0;
    743      1.1  christos 	in.i6[1] = 0;
    744      1.1  christos 	in.i6[2] = 0;
    745      1.1  christos 	in.i6[3] = 0;
    746      1.1  christos 	np = ni->nai_np;
    747      1.1  christos 	flags = nat->nat_flags;
    748      1.1  christos 
    749      1.1  christos 	if (flags & IPN_ICMPQUERY) {
    750      1.1  christos 		dport = fin->fin_data[1];
    751      1.1  christos 		sport = 0;
    752      1.1  christos 	} else {
    753      1.1  christos 		sport = htons(fin->fin_data[0]);
    754      1.1  christos 		dport = htons(fin->fin_data[1]);
    755      1.1  christos 	}
    756      1.1  christos 
    757      1.1  christos 	/* TRACE sport, dport */
    758      1.1  christos 
    759      1.1  christos 
    760      1.1  christos 	/*
    761      1.1  christos 	 * If the matching rule has IPN_STICKY set, then we want to have the
    762      1.1  christos 	 * same rule kick in as before.  Why would this happen?  If you have
    763      1.1  christos 	 * a collection of rdr rules with "round-robin sticky", the current
    764      1.1  christos 	 * packet might match a different one to the previous connection but
    765      1.1  christos 	 * we want the same destination to be used.
    766      1.1  christos 	 */
    767      1.1  christos 	if (((np->in_flags & (IPN_ROUNDR|IPN_SPLIT)) != 0) &&
    768      1.1  christos 	    ((np->in_flags & IPN_STICKY) != 0)) {
    769      1.1  christos 		hm = ipf_nat6_hostmap(softn, NULL, &fin->fin_src6,
    770      1.1  christos 				      &fin->fin_dst6, &in, (u_32_t)dport);
    771      1.1  christos 		if (hm != NULL) {
    772      1.1  christos 			in = hm->hm_ndstip6;
    773      1.1  christos 			np = hm->hm_ipnat;
    774      1.1  christos 			ni->nai_np = np;
    775      1.1  christos 			move = 0;
    776      1.1  christos 		}
    777      1.1  christos 	}
    778      1.1  christos 
    779      1.1  christos 	/*
    780      1.1  christos 	 * Otherwise, it's an inbound packet. Most likely, we don't
    781      1.1  christos 	 * want to rewrite source ports and source addresses. Instead,
    782      1.1  christos 	 * we want to rewrite to a fixed internal address and fixed
    783      1.1  christos 	 * internal port.
    784      1.1  christos 	 */
    785      1.1  christos 	if (np->in_flags & IPN_SPLIT) {
    786      1.1  christos 		in = np->in_dnip6;
    787      1.1  christos 
    788      1.1  christos 		if ((np->in_flags & (IPN_ROUNDR|IPN_STICKY)) == IPN_STICKY) {
    789      1.1  christos 			hm = ipf_nat6_hostmap(softn, NULL, &fin->fin_src6,
    790      1.1  christos 					      &fin->fin_dst6, &in,
    791      1.1  christos 					      (u_32_t)dport);
    792      1.1  christos 			if (hm != NULL) {
    793      1.1  christos 				in = hm->hm_ndstip6;
    794      1.1  christos 				move = 0;
    795      1.1  christos 			}
    796      1.1  christos 		}
    797      1.1  christos 
    798      1.1  christos 		if (hm == NULL || hm->hm_ref == 1) {
    799      1.1  christos 			if (IP6_EQ(&np->in_ndstip6, &in)) {
    800      1.1  christos 				np->in_dnip6 = np->in_ndstmsk6;
    801      1.1  christos 				move = 0;
    802      1.1  christos 			} else {
    803      1.1  christos 				np->in_dnip6 = np->in_ndstip6;
    804      1.1  christos 			}
    805      1.1  christos 		}
    806      1.1  christos 
    807      1.1  christos 	} else if (IP6_ISZERO(&np->in_ndstaddr) &&
    808      1.1  christos 		   IP6_ISONES(&np->in_ndstmsk)) {
    809      1.1  christos 		/*
    810      1.1  christos 		 * 0/32 - use the interface's IP address.
    811      1.1  christos 		 */
    812      1.1  christos 		if (ipf_ifpaddr(softc, 6, FRI_NORMAL, fin->fin_ifp,
    813      1.1  christos 			       &in, NULL) == -1) {
    814      1.1  christos 			NBUMPSIDE6DX(0, ns_new_ifpaddr, ns_new_ifpaddr_2);
    815      1.1  christos 			return -1;
    816      1.1  christos 		}
    817      1.1  christos 
    818      1.1  christos 	} else if (IP6_ISZERO(&np->in_ndstip6) &&
    819      1.1  christos 		   IP6_ISZERO(&np->in_ndstmsk6)) {
    820      1.1  christos 		/*
    821      1.1  christos 		 * 0/0 - use the original destination address/port.
    822      1.1  christos 		 */
    823      1.1  christos 		in = fin->fin_dst6;
    824      1.1  christos 
    825      1.1  christos 	} else if (np->in_redir == NAT_BIMAP &&
    826      1.1  christos 		   IP6_EQ(&np->in_ndstmsk6, &np->in_odstmsk6)) {
    827      1.1  christos 		i6addr_t temp;
    828      1.1  christos 		/*
    829      1.1  christos 		 * map the address block in a 1:1 fashion
    830      1.1  christos 		 */
    831      1.1  christos 		temp.i6[0] = fin->fin_dst6.i6[0] & ~np->in_osrcmsk6.i6[0];
    832      1.1  christos 		temp.i6[1] = fin->fin_dst6.i6[1] & ~np->in_osrcmsk6.i6[1];
    833      1.1  christos 		temp.i6[2] = fin->fin_dst6.i6[2] & ~np->in_osrcmsk6.i6[0];
    834      1.1  christos 		temp.i6[3] = fin->fin_dst6.i6[3] & ~np->in_osrcmsk6.i6[3];
    835      1.1  christos 		in = np->in_ndstip6;
    836      1.1  christos 		IP6_MERGE(&in, &temp, &np->in_ndstmsk6);
    837      1.1  christos 	} else {
    838      1.1  christos 		in = np->in_ndstip6;
    839      1.1  christos 	}
    840      1.1  christos 
    841      1.1  christos 	if ((np->in_dpnext == 0) || ((flags & NAT_NOTRULEPORT) != 0))
    842      1.1  christos 		nport = dport;
    843      1.1  christos 	else {
    844      1.1  christos 		/*
    845      1.1  christos 		 * Whilst not optimized for the case where
    846      1.1  christos 		 * pmin == pmax, the gain is not significant.
    847      1.1  christos 		 */
    848      1.1  christos 		if (((np->in_flags & IPN_FIXEDDPORT) == 0) &&
    849      1.1  christos 		    (np->in_odport != np->in_dtop)) {
    850      1.1  christos 			nport = ntohs(dport) - np->in_odport + np->in_dpmax;
    851      1.1  christos 			nport = htons(nport);
    852      1.1  christos 		} else {
    853      1.1  christos 			nport = htons(np->in_dpnext);
    854      1.1  christos 			np->in_dpnext++;
    855      1.1  christos 			if (np->in_dpnext > np->in_dpmax)
    856      1.1  christos 				np->in_dpnext = np->in_dpmin;
    857      1.1  christos 		}
    858      1.1  christos 	}
    859      1.1  christos 
    860      1.1  christos 	/*
    861      1.1  christos 	 * When the redirect-to address is set to 0.0.0.0, just
    862      1.1  christos 	 * assume a blank `forwarding' of the packet.  We don't
    863      1.1  christos 	 * setup any translation for this either.
    864      1.1  christos 	 */
    865      1.1  christos 	if (IP6_ISZERO(&in)) {
    866      1.1  christos 		if (nport == dport) {
    867      1.1  christos 			NBUMPSIDE6D(0, ns_xlate_null);
    868      1.1  christos 			return -1;
    869      1.1  christos 		}
    870      1.1  christos 		in = fin->fin_dst6;
    871      1.1  christos 	}
    872      1.1  christos 
    873      1.1  christos 	/*
    874      1.1  christos 	 * Check to see if this redirect mapping already exists and if
    875      1.1  christos 	 * it does, return "failure" (allowing it to be created will just
    876      1.1  christos 	 * cause one or both of these "connections" to stop working.)
    877      1.1  christos 	 */
    878      1.1  christos 	sp = fin->fin_data[0];
    879      1.1  christos 	dp = fin->fin_data[1];
    880      1.1  christos 	fin->fin_data[1] = fin->fin_data[0];
    881      1.1  christos 	fin->fin_data[0] = ntohs(nport);
    882      1.1  christos 	natl = ipf_nat6_outlookup(fin, flags & ~(SI_WILDP|NAT_SEARCH),
    883      1.1  christos 				  (u_int)fin->fin_p, &in.in6,
    884      1.1  christos 				  &fin->fin_src6.in6);
    885      1.1  christos 	fin->fin_data[0] = sp;
    886      1.1  christos 	fin->fin_data[1] = dp;
    887      1.1  christos 	if (natl != NULL) {
    888      1.1  christos 		NBUMPSIDE6D(0, ns_xlate_exists);
    889      1.1  christos 		return -1;
    890      1.1  christos 	}
    891      1.1  christos 
    892      1.1  christos 	nat->nat_ndst6 = in;
    893      1.1  christos 	nat->nat_odst6 = fin->fin_dst6;
    894      1.1  christos 	nat->nat_nsrc6 = fin->fin_src6;
    895      1.1  christos 	nat->nat_osrc6 = fin->fin_src6;
    896      1.1  christos 	if ((nat->nat_hm == NULL) && ((np->in_flags & IPN_STICKY) != 0))
    897      1.1  christos 		nat->nat_hm = ipf_nat6_hostmap(softn, np, &fin->fin_src6,
    898      1.1  christos 					       &fin->fin_dst6, &in,
    899      1.1  christos 					       (u_32_t)dport);
    900      1.1  christos 
    901      1.1  christos 	if (flags & IPN_TCPUDP) {
    902      1.1  christos 		nat->nat_odport = dport;
    903      1.1  christos 		nat->nat_ndport = nport;
    904      1.1  christos 		nat->nat_osport = sport;
    905      1.1  christos 		nat->nat_nsport = sport;
    906      1.1  christos 		((tcphdr_t *)fin->fin_dp)->th_dport = nport;
    907      1.1  christos 	} else if (flags & IPN_ICMPQUERY) {
    908      1.1  christos 		nat->nat_oicmpid = fin->fin_data[1];
    909      1.1  christos 		((struct icmp6_hdr *)fin->fin_dp)->icmp6_id = nport;
    910      1.1  christos 		nat->nat_nicmpid = nport;
    911      1.1  christos 	}
    912      1.1  christos 
    913      1.1  christos 	return move;
    914      1.1  christos }
    915      1.1  christos 
    916      1.1  christos /* ------------------------------------------------------------------------ */
    917      1.1  christos /* Function:    ipf_nat6_add                                                */
    918      1.1  christos /* Returns:     nat6_t*      - NULL == failure to create new NAT structure, */
    919      1.1  christos /*                             else pointer to new NAT structure            */
    920      1.1  christos /* Parameters:  fin(I)       - pointer to packet information                */
    921      1.1  christos /*              np(I)        - pointer to NAT rule                          */
    922      1.1  christos /*              natsave(I)   - pointer to where to store NAT struct pointer */
    923      1.1  christos /*              flags(I)     - flags describing the current packet          */
    924      1.1  christos /*              direction(I) - direction of packet (in/out)                 */
    925      1.1  christos /* Write Lock:  ipf_nat                                                     */
    926      1.1  christos /*                                                                          */
    927      1.1  christos /* Attempts to create a new NAT entry.  Does not actually change the packet */
    928      1.1  christos /* in any way.                                                              */
    929      1.1  christos /*                                                                          */
    930      1.1  christos /* This fucntion is in three main parts: (1) deal with creating a new NAT   */
    931      1.1  christos /* structure for a "MAP" rule (outgoing NAT translation); (2) deal with     */
    932      1.1  christos /* creating a new NAT structure for a "RDR" rule (incoming NAT translation) */
    933      1.1  christos /* and (3) building that structure and putting it into the NAT table(s).    */
    934      1.1  christos /*                                                                          */
    935      1.1  christos /* NOTE: natsave should NOT be used top point back to an ipstate_t struct   */
    936      1.1  christos /*       as it can result in memory being corrupted.                        */
    937      1.1  christos /* ------------------------------------------------------------------------ */
    938      1.1  christos nat_t *
    939      1.2  christos ipf_nat6_add(fr_info_t *fin, ipnat_t *np, nat_t **natsave, u_int flags,
    940      1.2  christos     int direction)
    941      1.1  christos {
    942      1.1  christos 	ipf_main_softc_t *softc = fin->fin_main_soft;
    943      1.1  christos 	ipf_nat_softc_t *softn = softc->ipf_nat_soft;
    944      1.1  christos 	hostmap_t *hm = NULL;
    945      1.1  christos 	nat_t *nat, *natl;
    946      1.3   darrenr 	natstat_t *nsp;
    947      1.1  christos 	u_int nflags;
    948      1.1  christos 	natinfo_t ni;
    949      1.1  christos 	int move;
    950      1.1  christos #if SOLARIS && defined(_KERNEL) && (SOLARIS2 >= 6) && defined(ICK_M_CTL_MAGIC)
    951      1.1  christos 	qpktinfo_t *qpi = fin->fin_qpi;
    952      1.1  christos #endif
    953      1.1  christos 
    954      1.3   darrenr 	nsp = &softn->ipf_nat_stats;
    955      1.3   darrenr 
    956      1.3   darrenr 	if ((nsp->ns_active * 100 / softn->ipf_nat_table_max) >
    957      1.1  christos 	    softn->ipf_nat_table_wm_high) {
    958      1.1  christos 		softn->ipf_nat_doflush = 1;
    959      1.1  christos 	}
    960      1.1  christos 
    961      1.3   darrenr 	if (nsp->ns_active >= softn->ipf_nat_table_max) {
    962      1.1  christos 		NBUMPSIDE6(fin->fin_out, ns_table_max);
    963      1.1  christos 		return NULL;
    964      1.1  christos 	}
    965      1.1  christos 
    966      1.1  christos 	move = 1;
    967      1.1  christos 	nflags = np->in_flags & flags;
    968      1.1  christos 	nflags &= NAT_FROMRULE;
    969      1.1  christos 
    970      1.1  christos 	ni.nai_np = np;
    971      1.1  christos 	ni.nai_dport = 0;
    972      1.1  christos 	ni.nai_sport = 0;
    973      1.1  christos 
    974      1.1  christos 	/* Give me a new nat */
    975      1.1  christos 	KMALLOC(nat, nat_t *);
    976      1.1  christos 	if (nat == NULL) {
    977      1.1  christos 		NBUMPSIDE6(fin->fin_out, ns_memfail);
    978      1.1  christos 		/*
    979      1.1  christos 		 * Try to automatically tune the max # of entries in the
    980      1.1  christos 		 * table allowed to be less than what will cause kmem_alloc()
    981      1.1  christos 		 * to fail and try to eliminate panics due to out of memory
    982      1.1  christos 		 * conditions arising.
    983      1.1  christos 		 */
    984      1.1  christos 		if ((softn->ipf_nat_table_max > softn->ipf_nat_table_sz) &&
    985      1.3   darrenr 		    (nsp->ns_active > 100)) {
    986      1.3   darrenr 			softn->ipf_nat_table_max = nsp->ns_active - 100;
    987      1.1  christos 			printf("table_max reduced to %d\n",
    988      1.1  christos 				softn->ipf_nat_table_max);
    989      1.1  christos 		}
    990      1.1  christos 		return NULL;
    991      1.1  christos 	}
    992      1.1  christos 
    993      1.1  christos 	if (flags & IPN_ICMPQUERY) {
    994      1.1  christos 		/*
    995      1.1  christos 		 * In the ICMP query NAT code, we translate the ICMP id fields
    996      1.1  christos 		 * to make them unique. This is indepedent of the ICMP type
    997      1.1  christos 		 * (e.g. in the unlikely event that a host sends an echo and
    998      1.1  christos 		 * an tstamp request with the same id, both packets will have
    999      1.1  christos 		 * their ip address/id field changed in the same way).
   1000      1.1  christos 		 */
   1001      1.1  christos 		/* The icmp6_id field is used by the sender to identify the
   1002      1.1  christos 		 * process making the icmp request. (the receiver justs
   1003      1.1  christos 		 * copies it back in its response). So, it closely matches
   1004      1.1  christos 		 * the concept of source port. We overlay sport, so we can
   1005      1.1  christos 		 * maximally reuse the existing code.
   1006      1.1  christos 		 */
   1007      1.1  christos 		ni.nai_sport = fin->fin_data[1];
   1008      1.1  christos 		ni.nai_dport = 0;
   1009      1.1  christos 	}
   1010      1.1  christos 
   1011      1.1  christos 	bzero((char *)nat, sizeof(*nat));
   1012      1.1  christos 	nat->nat_flags = flags;
   1013      1.1  christos 	nat->nat_redir = np->in_redir;
   1014      1.1  christos 	nat->nat_dir = direction;
   1015      1.1  christos 	nat->nat_pr[0] = fin->fin_p;
   1016      1.1  christos 	nat->nat_pr[1] = fin->fin_p;
   1017      1.1  christos 
   1018      1.1  christos 	/*
   1019      1.1  christos 	 * Search the current table for a match and create a new mapping
   1020      1.1  christos 	 * if there is none found.
   1021      1.1  christos 	 */
   1022      1.3   darrenr 	if (np->in_redir & NAT_DIVERTUDP) {
   1023      1.1  christos 		move = ipf_nat6_newdivert(fin, nat, &ni);
   1024      1.1  christos 
   1025      1.1  christos 	} else if (np->in_redir & NAT_REWRITE) {
   1026      1.1  christos 		move = ipf_nat6_newrewrite(fin, nat, &ni);
   1027      1.1  christos 
   1028      1.1  christos 	} else if (direction == NAT_OUTBOUND) {
   1029      1.1  christos 		/*
   1030      1.1  christos 		 * We can now arrange to call this for the same connection
   1031      1.1  christos 		 * because ipf_nat6_new doesn't protect the code path into
   1032      1.1  christos 		 * this function.
   1033      1.1  christos 		 */
   1034      1.1  christos 		natl = ipf_nat6_outlookup(fin, nflags, (u_int)fin->fin_p,
   1035      1.1  christos 					  &fin->fin_src6.in6,
   1036      1.1  christos 					  &fin->fin_dst6.in6);
   1037      1.1  christos 		if (natl != NULL) {
   1038      1.1  christos 			KFREE(nat);
   1039      1.1  christos 			nat = natl;
   1040      1.1  christos 			goto done;
   1041      1.1  christos 		}
   1042      1.1  christos 
   1043      1.1  christos 		move = ipf_nat6_newmap(fin, nat, &ni);
   1044      1.1  christos 	} else {
   1045      1.1  christos 		/*
   1046      1.1  christos 		 * NAT_INBOUND is used for redirects rules
   1047      1.1  christos 		 */
   1048      1.1  christos 		natl = ipf_nat6_inlookup(fin, nflags, (u_int)fin->fin_p,
   1049      1.1  christos 					 &fin->fin_src6.in6,
   1050      1.1  christos 					 &fin->fin_dst6.in6);
   1051      1.1  christos 		if (natl != NULL) {
   1052      1.1  christos 			KFREE(nat);
   1053      1.1  christos 			nat = natl;
   1054      1.1  christos 			goto done;
   1055      1.1  christos 		}
   1056      1.1  christos 
   1057      1.1  christos 		move = ipf_nat6_newrdr(fin, nat, &ni);
   1058      1.1  christos 	}
   1059      1.1  christos 	if (move == -1)
   1060      1.1  christos 		goto badnat;
   1061      1.1  christos 
   1062      1.1  christos 	np = ni.nai_np;
   1063      1.1  christos 
   1064      1.1  christos 	nat->nat_mssclamp = np->in_mssclamp;
   1065      1.1  christos 	nat->nat_me = natsave;
   1066      1.1  christos 	nat->nat_fr = fin->fin_fr;
   1067      1.1  christos 	nat->nat_rev = fin->fin_rev;
   1068      1.1  christos 	nat->nat_ptr = np;
   1069      1.3   darrenr 	nat->nat_dlocal = np->in_dlocal;
   1070      1.1  christos 
   1071      1.3   darrenr 	if ((np->in_apr != NULL) && ((nat->nat_flags & NAT_SLAVE) == 0)) {
   1072      1.3   darrenr 		if (ipf_proxy_new(fin, nat) == -1) {
   1073      1.3   darrenr 			NBUMPSIDE6D(fin->fin_out, ns_appr_fail);
   1074      1.1  christos 			goto badnat;
   1075      1.3   darrenr 		}
   1076      1.3   darrenr 	}
   1077      1.1  christos 
   1078      1.1  christos 	nat->nat_ifps[0] = np->in_ifps[0];
   1079      1.1  christos 	if (np->in_ifps[0] != NULL) {
   1080      1.1  christos 		COPYIFNAME(np->in_v[0], np->in_ifps[0], nat->nat_ifnames[0]);
   1081      1.1  christos 	}
   1082      1.1  christos 
   1083      1.1  christos 	nat->nat_ifps[1] = np->in_ifps[1];
   1084      1.1  christos 	if (np->in_ifps[1] != NULL) {
   1085      1.1  christos 		COPYIFNAME(np->in_v[1], np->in_ifps[1], nat->nat_ifnames[1]);
   1086      1.1  christos 	}
   1087      1.1  christos 
   1088      1.1  christos 	if (ipf_nat6_finalise(fin, nat) == -1) {
   1089      1.1  christos 		goto badnat;
   1090      1.1  christos 	}
   1091      1.1  christos 
   1092      1.1  christos 	np->in_use++;
   1093      1.1  christos 
   1094      1.1  christos 	if ((move == 1) && (np->in_flags & IPN_ROUNDR)) {
   1095      1.1  christos 		if ((np->in_redir & (NAT_REDIRECT|NAT_MAP)) == NAT_REDIRECT) {
   1096      1.1  christos 			ipf_nat6_delrdr(softn, np);
   1097      1.1  christos 			ipf_nat6_addrdr(softn, np);
   1098      1.1  christos 		} else if ((np->in_redir & (NAT_REDIRECT|NAT_MAP)) == NAT_MAP) {
   1099      1.1  christos 			ipf_nat6_delmap(softn, np);
   1100      1.1  christos 			ipf_nat6_addmap(softn, np);
   1101      1.1  christos 		}
   1102      1.1  christos 	}
   1103      1.1  christos 
   1104      1.1  christos 	if (flags & SI_WILDP)
   1105      1.3   darrenr 		nsp->ns_wilds++;
   1106      1.1  christos 	softn->ipf_nat_stats.ns_proto[nat->nat_pr[0]]++;
   1107      1.1  christos 
   1108      1.1  christos 	goto done;
   1109      1.1  christos badnat:
   1110      1.1  christos 	NBUMPSIDE6(fin->fin_out, ns_badnatnew);
   1111      1.1  christos 	if ((hm = nat->nat_hm) != NULL)
   1112      1.3   darrenr 		ipf_nat_hostmapdel(softc, &hm);
   1113      1.1  christos 	KFREE(nat);
   1114      1.1  christos 	nat = NULL;
   1115      1.1  christos done:
   1116      1.3   darrenr 	if (nat != NULL && np != NULL)
   1117      1.3   darrenr 		np->in_hits++;
   1118      1.3   darrenr 	if (natsave != NULL)
   1119      1.3   darrenr 		*natsave = nat;
   1120      1.1  christos 	return nat;
   1121      1.1  christos }
   1122      1.1  christos 
   1123      1.1  christos 
   1124      1.1  christos /* ------------------------------------------------------------------------ */
   1125      1.1  christos /* Function:    ipf_nat6_finalise                                           */
   1126      1.1  christos /* Returns:     int - 0 == sucess, -1 == failure                            */
   1127      1.1  christos /* Parameters:  fin(I) - pointer to packet information                      */
   1128      1.1  christos /*              nat(I) - pointer to NAT entry                               */
   1129      1.1  christos /* Write Lock:  ipf_nat                                                     */
   1130      1.1  christos /*                                                                          */
   1131      1.1  christos /* This is the tail end of constructing a new NAT entry and is the same     */
   1132      1.1  christos /* for both IPv4 and IPv6.                                                  */
   1133      1.1  christos /* ------------------------------------------------------------------------ */
   1134      1.1  christos /*ARGSUSED*/
   1135      1.1  christos int
   1136      1.2  christos ipf_nat6_finalise(fr_info_t *fin, nat_t *nat)
   1137      1.1  christos {
   1138      1.1  christos 	ipf_main_softc_t *softc = fin->fin_main_soft;
   1139      1.1  christos 	ipf_nat_softc_t *softn = softc->ipf_nat_soft;
   1140      1.1  christos 	u_32_t sum1, sum2, sumd;
   1141      1.1  christos 	frentry_t *fr;
   1142      1.1  christos 
   1143      1.1  christos 	switch (fin->fin_p)
   1144      1.1  christos 	{
   1145      1.1  christos 	case IPPROTO_ICMPV6 :
   1146      1.3   darrenr 		sum1 = LONG_SUM6(&nat->nat_osrc6);
   1147      1.3   darrenr 		sum1 += ntohs(nat->nat_oicmpid);
   1148      1.3   darrenr 		sum2 = LONG_SUM6(&nat->nat_nsrc6);
   1149      1.3   darrenr 		sum2 += ntohs(nat->nat_nicmpid);
   1150      1.1  christos 		CALC_SUMD(sum1, sum2, sumd);
   1151      1.1  christos 		nat->nat_sumd[0] = (sumd & 0xffff) + (sumd >> 16);
   1152      1.1  christos 
   1153      1.3   darrenr 		sum1 = LONG_SUM6(&nat->nat_odst6);
   1154      1.3   darrenr 		sum2 = LONG_SUM6(&nat->nat_ndst6);
   1155      1.3   darrenr 		CALC_SUMD(sum1, sum2, sumd);
   1156      1.3   darrenr 		nat->nat_sumd[0] += (sumd & 0xffff) + (sumd >> 16);
   1157      1.1  christos 		break;
   1158      1.1  christos 
   1159      1.3   darrenr 	case IPPROTO_TCP :
   1160      1.3   darrenr 	case IPPROTO_UDP :
   1161      1.1  christos 		sum1 = LONG_SUM6(&nat->nat_osrc6);
   1162      1.1  christos 		sum1 += ntohs(nat->nat_osport);
   1163      1.1  christos 		sum2 = LONG_SUM6(&nat->nat_nsrc6);
   1164      1.1  christos 		sum2 += ntohs(nat->nat_nsport);
   1165      1.1  christos 		CALC_SUMD(sum1, sum2, sumd);
   1166      1.1  christos 		nat->nat_sumd[0] = (sumd & 0xffff) + (sumd >> 16);
   1167      1.1  christos 
   1168      1.1  christos 		sum1 = LONG_SUM6(&nat->nat_odst6);
   1169      1.1  christos 		sum1 += ntohs(nat->nat_odport);
   1170      1.1  christos 		sum2 = LONG_SUM6(&nat->nat_ndst6);
   1171      1.1  christos 		sum2 += ntohs(nat->nat_ndport);
   1172      1.1  christos 		CALC_SUMD(sum1, sum2, sumd);
   1173      1.1  christos 		nat->nat_sumd[0] += (sumd & 0xffff) + (sumd >> 16);
   1174      1.1  christos 		break;
   1175      1.3   darrenr 
   1176      1.3   darrenr 	default :
   1177      1.3   darrenr 		sum1 = LONG_SUM6(&nat->nat_osrc6);
   1178      1.3   darrenr 		sum2 = LONG_SUM6(&nat->nat_nsrc6);
   1179      1.3   darrenr 		CALC_SUMD(sum1, sum2, sumd);
   1180      1.3   darrenr 		nat->nat_sumd[0] = (sumd & 0xffff) + (sumd >> 16);
   1181      1.3   darrenr 
   1182      1.3   darrenr 		sum1 = LONG_SUM6(&nat->nat_odst6);
   1183      1.3   darrenr 		sum2 = LONG_SUM6(&nat->nat_ndst6);
   1184      1.3   darrenr 		CALC_SUMD(sum1, sum2, sumd);
   1185      1.3   darrenr 		nat->nat_sumd[0] += (sumd & 0xffff) + (sumd >> 16);
   1186      1.3   darrenr 		break;
   1187      1.1  christos 	}
   1188      1.1  christos 
   1189      1.3   darrenr 	/*
   1190      1.3   darrenr 	 * Compute the partial checksum, just in case.
   1191      1.3   darrenr 	 * This is only ever placed into outbound packets so care needs
   1192      1.3   darrenr 	 * to be taken over which pair of addresses are used.
   1193      1.3   darrenr 	 */
   1194      1.3   darrenr 	if (nat->nat_dir == NAT_OUTBOUND) {
   1195      1.3   darrenr 		sum1 = LONG_SUM6(&nat->nat_nsrc6);
   1196      1.3   darrenr 		sum1 += LONG_SUM6(&nat->nat_ndst6);
   1197      1.3   darrenr 	} else {
   1198      1.3   darrenr 		sum1 = LONG_SUM6(&nat->nat_osrc6);
   1199      1.3   darrenr 		sum1 += LONG_SUM6(&nat->nat_odst6);
   1200      1.3   darrenr 	}
   1201      1.3   darrenr 	sum1 += nat->nat_pr[1];
   1202      1.3   darrenr 	nat->nat_sumd[1] = (sum1 & 0xffff) + (sum1 >> 16);
   1203      1.1  christos 
   1204      1.1  christos 	if ((nat->nat_flags & SI_CLONE) == 0)
   1205      1.1  christos 		nat->nat_sync = ipf_sync_new(softc, SMC_NAT, fin, nat);
   1206      1.1  christos 
   1207      1.1  christos 	if ((nat->nat_ifps[0] != NULL) && (nat->nat_ifps[0] != (void *)-1)) {
   1208      1.1  christos 		nat->nat_mtu[0] = GETIFMTU_6(nat->nat_ifps[0]);
   1209      1.1  christos 	}
   1210      1.1  christos 
   1211      1.1  christos 	if ((nat->nat_ifps[1] != NULL) && (nat->nat_ifps[1] != (void *)-1)) {
   1212      1.1  christos 		nat->nat_mtu[1] = GETIFMTU_6(nat->nat_ifps[1]);
   1213      1.1  christos 	}
   1214      1.1  christos 
   1215      1.1  christos 	nat->nat_v[0] = 6;
   1216      1.1  christos 	nat->nat_v[1] = 6;
   1217      1.1  christos 
   1218      1.1  christos 	if (ipf_nat6_insert(softc, softn, nat) == 0) {
   1219      1.1  christos 		if (softn->ipf_nat_logging)
   1220      1.1  christos 			ipf_nat_log(softc, softn, nat, NL_NEW);
   1221      1.1  christos 		fr = nat->nat_fr;
   1222      1.1  christos 		if (fr != NULL) {
   1223      1.1  christos 			MUTEX_ENTER(&fr->fr_lock);
   1224      1.1  christos 			fr->fr_ref++;
   1225      1.1  christos 			MUTEX_EXIT(&fr->fr_lock);
   1226      1.1  christos 		}
   1227      1.1  christos 		return 0;
   1228      1.1  christos 	}
   1229      1.1  christos 
   1230      1.1  christos 	NBUMPSIDE6D(fin->fin_out, ns_unfinalised);
   1231      1.1  christos 	/*
   1232      1.1  christos 	 * nat6_insert failed, so cleanup time...
   1233      1.1  christos 	 */
   1234      1.3   darrenr 	if (nat->nat_sync != NULL)
   1235      1.3   darrenr 		ipf_sync_del_nat(softc->ipf_sync_soft, nat->nat_sync);
   1236      1.1  christos 	return -1;
   1237      1.1  christos }
   1238      1.1  christos 
   1239      1.1  christos 
   1240      1.1  christos /* ------------------------------------------------------------------------ */
   1241      1.3   darrenr /* Function:    ipf_nat6_insert                                             */
   1242      1.3   darrenr /* Returns:     int - 0 == sucess, -1 == failure                            */
   1243      1.3   darrenr /* Parameters:  softc(I) - pointer to soft context main structure           */
   1244      1.3   darrenr /*              softn(I) - pointer to NAT context structure                 */
   1245      1.3   darrenr /*              nat(I) - pointer to NAT structure                           */
   1246      1.3   darrenr /* Write Lock:  ipf_nat                                                     */
   1247      1.1  christos /*                                                                          */
   1248      1.1  christos /* Insert a NAT entry into the hash tables for searching and add it to the  */
   1249      1.1  christos /* list of active NAT entries.  Adjust global counters when complete.       */
   1250      1.1  christos /* ------------------------------------------------------------------------ */
   1251      1.1  christos static int
   1252      1.2  christos ipf_nat6_insert(ipf_main_softc_t *softc, ipf_nat_softc_t *softn, nat_t *nat)
   1253      1.1  christos {
   1254      1.4  christos 	u_int hv0, hv1;
   1255      1.3   darrenr 	u_32_t sp, dp;
   1256      1.1  christos 	ipnat_t *in;
   1257      1.1  christos 
   1258      1.1  christos 	/*
   1259      1.1  christos 	 * Try and return an error as early as possible, so calculate the hash
   1260      1.1  christos 	 * entry numbers first and then proceed.
   1261      1.1  christos 	 */
   1262      1.1  christos 	if ((nat->nat_flags & (SI_W_SPORT|SI_W_DPORT)) == 0) {
   1263      1.3   darrenr 		if ((nat->nat_flags & IPN_TCPUDP) != 0) {
   1264      1.3   darrenr 			sp = nat->nat_osport;
   1265      1.3   darrenr 			dp = nat->nat_odport;
   1266      1.3   darrenr 		} else if ((nat->nat_flags & IPN_ICMPQUERY) != 0) {
   1267      1.3   darrenr 			sp = 0;
   1268      1.3   darrenr 			dp = nat->nat_oicmpid;
   1269      1.3   darrenr 		} else {
   1270      1.3   darrenr 			sp = 0;
   1271      1.3   darrenr 			dp = 0;
   1272      1.3   darrenr 		}
   1273      1.4  christos 		hv0 = NAT_HASH_FN6(&nat->nat_osrc6, sp, 0xffffffff);
   1274      1.4  christos 		hv0 = NAT_HASH_FN6(&nat->nat_odst6, hv0 + dp,
   1275      1.1  christos 				   softn->ipf_nat_table_sz);
   1276      1.1  christos 
   1277      1.1  christos 		/*
   1278      1.1  christos 		 * TRACE nat6_osrc6, nat6_osport, nat6_odst6,
   1279      1.4  christos 		 * nat6_odport, hv0
   1280      1.1  christos 		 */
   1281      1.1  christos 
   1282      1.3   darrenr 		if ((nat->nat_flags & IPN_TCPUDP) != 0) {
   1283      1.3   darrenr 			sp = nat->nat_nsport;
   1284      1.3   darrenr 			dp = nat->nat_ndport;
   1285      1.3   darrenr 		} else if ((nat->nat_flags & IPN_ICMPQUERY) != 0) {
   1286      1.3   darrenr 			sp = 0;
   1287      1.3   darrenr 			dp = nat->nat_nicmpid;
   1288      1.3   darrenr 		} else {
   1289      1.3   darrenr 			sp = 0;
   1290      1.3   darrenr 			dp = 0;
   1291      1.3   darrenr 		}
   1292      1.4  christos 		hv1 = NAT_HASH_FN6(&nat->nat_nsrc6, sp, 0xffffffff);
   1293      1.4  christos 		hv1 = NAT_HASH_FN6(&nat->nat_ndst6, hv1 + dp,
   1294      1.1  christos 				   softn->ipf_nat_table_sz);
   1295      1.1  christos 		/*
   1296      1.1  christos 		 * TRACE nat6_nsrcaddr, nat6_nsport, nat6_ndstaddr,
   1297      1.4  christos 		 * nat6_ndport, hv0
   1298      1.1  christos 		 */
   1299      1.1  christos 	} else {
   1300      1.4  christos 		hv0 = NAT_HASH_FN6(&nat->nat_osrc6, 0, 0xffffffff);
   1301      1.4  christos 		hv0 = NAT_HASH_FN6(&nat->nat_odst6, hv0,
   1302      1.1  christos 				   softn->ipf_nat_table_sz);
   1303      1.4  christos 		/* TRACE nat6_osrcip6, nat6_odstip6, hv0 */
   1304      1.1  christos 
   1305      1.4  christos 		hv1 = NAT_HASH_FN6(&nat->nat_nsrc6, 0, 0xffffffff);
   1306      1.4  christos 		hv1 = NAT_HASH_FN6(&nat->nat_ndst6, hv1,
   1307      1.1  christos 				   softn->ipf_nat_table_sz);
   1308      1.4  christos 		/* TRACE nat6_nsrcip6, nat6_ndstip6, hv1 */
   1309      1.1  christos 	}
   1310      1.1  christos 
   1311      1.4  christos 	if ((nat->nat_dir & NAT_OUTBOUND) == NAT_OUTBOUND) {
   1312      1.4  christos 		nat->nat_hv[0] = hv0;
   1313      1.4  christos 		nat->nat_hv[1] = hv1;
   1314      1.4  christos 	} else {
   1315      1.4  christos 		nat->nat_hv[0] = hv1;
   1316      1.4  christos 		nat->nat_hv[1] = hv0;
   1317      1.4  christos 	}
   1318      1.1  christos 
   1319      1.1  christos 	MUTEX_INIT(&nat->nat_lock, "nat entry lock");
   1320      1.1  christos 
   1321      1.1  christos 	in = nat->nat_ptr;
   1322      1.3   darrenr 	nat->nat_ref = nat->nat_me ? 2 : 1;
   1323      1.1  christos 
   1324      1.1  christos 	nat->nat_ifnames[0][LIFNAMSIZ - 1] = '\0';
   1325      1.1  christos 	nat->nat_ifps[0] = ipf_resolvenic(softc, nat->nat_ifnames[0],
   1326      1.1  christos 					  nat->nat_v[0]);
   1327      1.1  christos 
   1328      1.1  christos 	if (nat->nat_ifnames[1][0] != '\0') {
   1329      1.1  christos 		nat->nat_ifnames[1][LIFNAMSIZ - 1] = '\0';
   1330      1.1  christos 		nat->nat_ifps[1] = ipf_resolvenic(softc, nat->nat_ifnames[1],
   1331      1.1  christos 						  nat->nat_v[1]);
   1332      1.1  christos 	} else if (in->in_ifnames[1] != -1) {
   1333      1.1  christos 		char *name;
   1334      1.1  christos 
   1335      1.1  christos 		name = in->in_names + in->in_ifnames[1];
   1336      1.1  christos 		if (name[1] != '\0' && name[0] != '-' && name[0] != '*') {
   1337      1.1  christos 			(void) strncpy(nat->nat_ifnames[1],
   1338      1.1  christos 				       nat->nat_ifnames[0], LIFNAMSIZ);
   1339      1.1  christos 			nat->nat_ifnames[1][LIFNAMSIZ - 1] = '\0';
   1340      1.1  christos 			nat->nat_ifps[1] = nat->nat_ifps[0];
   1341      1.1  christos 		}
   1342      1.1  christos 	}
   1343      1.1  christos 	if ((nat->nat_ifps[0] != NULL) && (nat->nat_ifps[0] != (void *)-1)) {
   1344      1.1  christos 		nat->nat_mtu[0] = GETIFMTU_6(nat->nat_ifps[0]);
   1345      1.1  christos 	}
   1346      1.1  christos 	if ((nat->nat_ifps[1] != NULL) && (nat->nat_ifps[1] != (void *)-1)) {
   1347      1.1  christos 		nat->nat_mtu[1] = GETIFMTU_6(nat->nat_ifps[1]);
   1348      1.1  christos 	}
   1349      1.1  christos 
   1350      1.3   darrenr 	return ipf_nat_hashtab_add(softc, softn, nat);
   1351      1.1  christos }
   1352      1.1  christos 
   1353      1.1  christos 
   1354      1.1  christos /* ------------------------------------------------------------------------ */
   1355      1.1  christos /* Function:    ipf_nat6_icmperrorlookup                                    */
   1356      1.1  christos /* Returns:     nat6_t* - point to matching NAT structure                    */
   1357      1.1  christos /* Parameters:  fin(I) - pointer to packet information                      */
   1358      1.1  christos /*              dir(I) - direction of packet (in/out)                       */
   1359      1.1  christos /*                                                                          */
   1360      1.1  christos /* Check if the ICMP error message is related to an existing TCP, UDP or    */
   1361      1.1  christos /* ICMP query nat entry.  It is assumed that the packet is already of the   */
   1362      1.1  christos /* the required length.                                                     */
   1363      1.1  christos /* ------------------------------------------------------------------------ */
   1364      1.1  christos nat_t *
   1365      1.2  christos ipf_nat6_icmperrorlookup(fr_info_t *fin, int dir)
   1366      1.1  christos {
   1367      1.1  christos 	ipf_main_softc_t *softc = fin->fin_main_soft;
   1368      1.1  christos 	ipf_nat_softc_t *softn = softc->ipf_nat_soft;
   1369  1.4.2.1     rmind 	struct icmp6_hdr *orgicmp;
   1370  1.4.2.1     rmind 	int flags = 0, minlen;
   1371      1.1  christos 	nat_stat_side_t *nside;
   1372      1.1  christos 	tcphdr_t *tcp = NULL;
   1373      1.1  christos 	u_short data[2];
   1374      1.1  christos 	ip6_t *oip6;
   1375      1.1  christos 	nat_t *nat;
   1376      1.1  christos 	u_int p;
   1377      1.1  christos 
   1378      1.1  christos 	minlen = 40;
   1379      1.1  christos 	nside = &softn->ipf_nat_stats.ns_side6[fin->fin_out];
   1380      1.1  christos 	/*
   1381      1.1  christos 	 * Does it at least have the return (basic) IP header ?
   1382      1.1  christos 	 * Only a basic IP header (no options) should be with an ICMP error
   1383      1.1  christos 	 * header.  Also, if it's not an error type, then return.
   1384      1.1  christos 	 */
   1385      1.1  christos 	if (!(fin->fin_flx & FI_ICMPERR)) {
   1386      1.1  christos 		ATOMIC_INCL(nside->ns_icmp_basic);
   1387      1.1  christos 		return NULL;
   1388      1.1  christos 	}
   1389      1.1  christos 
   1390      1.1  christos 	/*
   1391      1.1  christos 	 * Check packet size
   1392      1.1  christos 	 */
   1393      1.1  christos 	if (fin->fin_plen < ICMP6ERR_IPICMPHLEN) {
   1394      1.1  christos 		ATOMIC_INCL(nside->ns_icmp_size);
   1395      1.1  christos 		return NULL;
   1396      1.1  christos 	}
   1397      1.1  christos 	oip6 = (ip6_t *)((char *)fin->fin_dp + 8);
   1398      1.1  christos 
   1399      1.1  christos 	/*
   1400      1.1  christos 	 * Is the buffer big enough for all of it ?  It's the size of the IP
   1401      1.1  christos 	 * header claimed in the encapsulated part which is of concern.  It
   1402      1.1  christos 	 * may be too big to be in this buffer but not so big that it's
   1403      1.1  christos 	 * outside the ICMP packet, leading to TCP deref's causing problems.
   1404      1.1  christos 	 * This is possible because we don't know how big oip_hl is when we
   1405      1.1  christos 	 * do the pullup early in ipf_check() and thus can't gaurantee it is
   1406      1.1  christos 	 * all here now.
   1407      1.1  christos 	 */
   1408      1.1  christos #ifdef  ipf_nat6_KERNEL
   1409      1.1  christos 	{
   1410      1.1  christos 	mb_t *m;
   1411      1.1  christos 
   1412      1.1  christos 	m = fin->fin_m;
   1413      1.1  christos # if defined(MENTAT)
   1414      1.1  christos 	if ((char *)oip6 + fin->fin_dlen - ICMPERR_ICMPHLEN >
   1415      1.1  christos 	    (char *)m->b_wptr) {
   1416      1.1  christos 		ATOMIC_INCL(nside->ns_icmp_mbuf);
   1417      1.1  christos 		return NULL;
   1418      1.1  christos 	}
   1419      1.1  christos # else
   1420      1.1  christos 	if ((char *)oip6 + fin->fin_dlen - ICMPERR_ICMPHLEN >
   1421      1.1  christos 	    (char *)fin->fin_ip + M_LEN(m)) {
   1422      1.1  christos 		ATOMIC_INCL(nside->ns_icmp_mbuf);
   1423      1.1  christos 		return NULL;
   1424      1.1  christos 	}
   1425      1.1  christos # endif
   1426      1.1  christos 	}
   1427      1.1  christos #endif
   1428      1.1  christos 
   1429      1.1  christos 	if (IP6_NEQ(&fin->fin_dst6, &oip6->ip6_src)) {
   1430      1.1  christos 		ATOMIC_INCL(nside->ns_icmp_address);
   1431      1.1  christos 		return NULL;
   1432      1.1  christos 	}
   1433      1.1  christos 
   1434      1.1  christos 	p = oip6->ip6_nxt;
   1435      1.1  christos 	if (p == IPPROTO_TCP)
   1436      1.1  christos 		flags = IPN_TCP;
   1437      1.1  christos 	else if (p == IPPROTO_UDP)
   1438      1.1  christos 		flags = IPN_UDP;
   1439      1.1  christos 	else if (p == IPPROTO_ICMPV6) {
   1440      1.1  christos 		orgicmp = (struct icmp6_hdr *)(oip6 + 1);
   1441      1.1  christos 
   1442      1.1  christos 		/* see if this is related to an ICMP query */
   1443      1.1  christos 		if (ipf_nat6_icmpquerytype(orgicmp->icmp6_type)) {
   1444      1.1  christos 			data[0] = fin->fin_data[0];
   1445      1.1  christos 			data[1] = fin->fin_data[1];
   1446      1.1  christos 			fin->fin_data[0] = 0;
   1447      1.1  christos 			fin->fin_data[1] = orgicmp->icmp6_id;
   1448      1.1  christos 
   1449      1.1  christos 			flags = IPN_ICMPERR|IPN_ICMPQUERY;
   1450      1.1  christos 			/*
   1451      1.1  christos 			 * NOTE : dir refers to the direction of the original
   1452      1.1  christos 			 *        ip packet. By definition the icmp error
   1453      1.1  christos 			 *        message flows in the opposite direction.
   1454      1.1  christos 			 */
   1455      1.1  christos 			if (dir == NAT_INBOUND)
   1456      1.1  christos 				nat = ipf_nat6_inlookup(fin, flags, p,
   1457      1.1  christos 						        &oip6->ip6_dst,
   1458      1.1  christos 						        &oip6->ip6_src);
   1459      1.1  christos 			else
   1460      1.1  christos 				nat = ipf_nat6_outlookup(fin, flags, p,
   1461      1.1  christos 							 &oip6->ip6_dst,
   1462      1.1  christos 							 &oip6->ip6_src);
   1463      1.1  christos 			fin->fin_data[0] = data[0];
   1464      1.1  christos 			fin->fin_data[1] = data[1];
   1465      1.1  christos 			return nat;
   1466      1.1  christos 		}
   1467      1.1  christos 	}
   1468      1.1  christos 
   1469      1.1  christos 	if (flags & IPN_TCPUDP) {
   1470      1.1  christos 		minlen += 8;		/* + 64bits of data to get ports */
   1471      1.1  christos 		/* TRACE (fin,minlen) */
   1472      1.1  christos 		if (fin->fin_plen < ICMPERR_IPICMPHLEN + minlen) {
   1473      1.1  christos 			ATOMIC_INCL(nside->ns_icmp_short);
   1474      1.1  christos 			return NULL;
   1475      1.1  christos 		}
   1476      1.1  christos 
   1477      1.1  christos 		data[0] = fin->fin_data[0];
   1478      1.1  christos 		data[1] = fin->fin_data[1];
   1479      1.1  christos 		tcp = (tcphdr_t *)(oip6 + 1);
   1480      1.1  christos 		fin->fin_data[0] = ntohs(tcp->th_dport);
   1481      1.1  christos 		fin->fin_data[1] = ntohs(tcp->th_sport);
   1482      1.1  christos 
   1483      1.1  christos 		if (dir == NAT_INBOUND) {
   1484      1.1  christos 			nat = ipf_nat6_inlookup(fin, flags, p, &oip6->ip6_dst,
   1485      1.1  christos 						&oip6->ip6_src);
   1486      1.1  christos 		} else {
   1487      1.1  christos 			nat = ipf_nat6_outlookup(fin, flags, p, &oip6->ip6_dst,
   1488      1.1  christos 						 &oip6->ip6_src);
   1489      1.1  christos 		}
   1490      1.1  christos 		fin->fin_data[0] = data[0];
   1491      1.1  christos 		fin->fin_data[1] = data[1];
   1492      1.1  christos 		return nat;
   1493      1.1  christos 	}
   1494      1.1  christos 	if (dir == NAT_INBOUND)
   1495      1.1  christos 		nat = ipf_nat6_inlookup(fin, 0, p, &oip6->ip6_dst,
   1496      1.1  christos 					&oip6->ip6_src);
   1497      1.1  christos 	else
   1498      1.1  christos 		nat = ipf_nat6_outlookup(fin, 0, p, &oip6->ip6_dst,
   1499      1.1  christos 					 &oip6->ip6_src);
   1500      1.1  christos 
   1501      1.1  christos 	return nat;
   1502      1.1  christos }
   1503      1.1  christos 
   1504      1.1  christos 
   1505      1.1  christos /* result = ip1 - ip2 */
   1506      1.1  christos u_32_t
   1507      1.2  christos ipf_nat6_ip6subtract(i6addr_t *ip1, i6addr_t *ip2)
   1508      1.1  christos {
   1509      1.1  christos 	i6addr_t l1, l2, d;
   1510      1.1  christos 	u_short *s1, *s2, *ds;
   1511      1.1  christos 	u_32_t r;
   1512  1.4.2.1     rmind 	int i;
   1513      1.1  christos 
   1514      1.1  christos 	l1 = *ip1;
   1515      1.1  christos 	l2 = *ip2;
   1516      1.1  christos 	s1 = (u_short *)&l1;
   1517      1.1  christos 	s2 = (u_short *)&l2;
   1518      1.1  christos 	ds = (u_short *)&d;
   1519      1.1  christos 
   1520      1.1  christos 	for (i = 7; i > 0; i--) {
   1521      1.1  christos 		if (s1[i] > s2[i]) {
   1522      1.1  christos 			ds[i] = s2[i] + 0x10000 - s1[i];
   1523      1.1  christos 			s2[i - 1] += 0x10000;
   1524      1.1  christos 		} else {
   1525      1.1  christos 			ds[i] = s2[i] - s1[i];
   1526      1.1  christos 		}
   1527      1.1  christos 	}
   1528      1.1  christos 	if (s2[0] > s1[0]) {
   1529      1.1  christos 		ds[0] = s2[0] + 0x10000 - s1[0];
   1530      1.1  christos 	} else {
   1531      1.1  christos 		ds[0] = s2[0] - s1[0];
   1532      1.1  christos 	}
   1533      1.1  christos 
   1534      1.1  christos 	for (i = 0, r = 0; i < 8; i++) {
   1535      1.1  christos 		r += ds[i];
   1536      1.1  christos 	}
   1537      1.1  christos 
   1538      1.1  christos 	return r;
   1539      1.1  christos }
   1540      1.1  christos 
   1541      1.1  christos 
   1542      1.1  christos /* ------------------------------------------------------------------------ */
   1543      1.1  christos /* Function:    ipf_nat6_icmperror                                          */
   1544      1.1  christos /* Returns:     nat6_t* - point to matching NAT structure                    */
   1545      1.1  christos /* Parameters:  fin(I)    - pointer to packet information                   */
   1546      1.1  christos /*              nflags(I) - NAT flags for this packet                       */
   1547      1.1  christos /*              dir(I)    - direction of packet (in/out)                    */
   1548      1.1  christos /*                                                                          */
   1549      1.1  christos /* Fix up an ICMP packet which is an error message for an existing NAT      */
   1550      1.1  christos /* session.  This will correct both packet header data and checksums.       */
   1551      1.1  christos /*                                                                          */
   1552      1.1  christos /* This should *ONLY* be used for incoming ICMP error packets to make sure  */
   1553      1.1  christos /* a NAT'd ICMP packet gets correctly recognised.                           */
   1554      1.1  christos /* ------------------------------------------------------------------------ */
   1555      1.1  christos nat_t *
   1556      1.2  christos ipf_nat6_icmperror(fr_info_t *fin, u_int *nflags, int dir)
   1557      1.1  christos {
   1558      1.1  christos 	ipf_main_softc_t *softc = fin->fin_main_soft;
   1559      1.1  christos 	ipf_nat_softc_t *softn = softc->ipf_nat_soft;
   1560      1.1  christos 	u_32_t sum1, sum2, sumd, sumd2;
   1561      1.1  christos 	i6addr_t a1, a2, a3, a4;
   1562      1.1  christos 	struct icmp6_hdr *icmp6;
   1563      1.1  christos 	int flags, dlen, odst;
   1564      1.1  christos 	u_short *csump;
   1565      1.1  christos 	tcphdr_t *tcp;
   1566      1.1  christos 	ip6_t *oip6;
   1567      1.1  christos 	nat_t *nat;
   1568      1.1  christos 	void *dp;
   1569      1.1  christos 
   1570      1.1  christos 	if ((fin->fin_flx & (FI_SHORT|FI_FRAGBODY))) {
   1571      1.1  christos 		NBUMPSIDE6D(fin->fin_out, ns_icmp_short);
   1572      1.1  christos 		return NULL;
   1573      1.1  christos 	}
   1574      1.1  christos 
   1575      1.1  christos 	/*
   1576      1.1  christos 	 * ipf_nat6_icmperrorlookup() will return NULL for `defective' packets.
   1577      1.1  christos 	 */
   1578      1.1  christos 	if ((fin->fin_v != 6) || !(nat = ipf_nat6_icmperrorlookup(fin, dir))) {
   1579      1.1  christos 		NBUMPSIDE6D(fin->fin_out, ns_icmp_notfound);
   1580      1.1  christos 		return NULL;
   1581      1.1  christos 	}
   1582      1.1  christos 
   1583      1.1  christos 	tcp = NULL;
   1584      1.1  christos 	csump = NULL;
   1585      1.1  christos 	flags = 0;
   1586      1.1  christos 	sumd2 = 0;
   1587      1.1  christos 	*nflags = IPN_ICMPERR;
   1588      1.1  christos 	icmp6 = fin->fin_dp;
   1589      1.1  christos 	oip6 = (ip6_t *)((u_char *)icmp6 + sizeof(*icmp6));
   1590      1.1  christos 	dp = (u_char *)oip6 + sizeof(*oip6);
   1591      1.1  christos 	if (oip6->ip6_nxt == IPPROTO_TCP) {
   1592      1.1  christos 		tcp = (tcphdr_t *)dp;
   1593      1.1  christos 		csump = (u_short *)&tcp->th_sum;
   1594      1.1  christos 		flags = IPN_TCP;
   1595      1.1  christos 	} else if (oip6->ip6_nxt == IPPROTO_UDP) {
   1596      1.1  christos 		udphdr_t *udp;
   1597      1.1  christos 
   1598      1.1  christos 		udp = (udphdr_t *)dp;
   1599      1.1  christos 		tcp = (tcphdr_t *)dp;
   1600      1.1  christos 		csump = (u_short *)&udp->uh_sum;
   1601      1.1  christos 		flags = IPN_UDP;
   1602      1.1  christos 	} else if (oip6->ip6_nxt == IPPROTO_ICMPV6)
   1603      1.1  christos 		flags = IPN_ICMPQUERY;
   1604      1.1  christos 	dlen = fin->fin_plen - ((char *)dp - (char *)fin->fin_ip);
   1605      1.1  christos 
   1606      1.1  christos 	/*
   1607      1.1  christos 	 * Need to adjust ICMP header to include the real IP#'s and
   1608      1.1  christos 	 * port #'s.  Only apply a checksum change relative to the
   1609      1.1  christos 	 * IP address change as it will be modified again in ipf_nat6_checkout
   1610      1.1  christos 	 * for both address and port.  Two checksum changes are
   1611      1.1  christos 	 * necessary for the two header address changes.  Be careful
   1612      1.1  christos 	 * to only modify the checksum once for the port # and twice
   1613      1.1  christos 	 * for the IP#.
   1614      1.1  christos 	 */
   1615      1.1  christos 
   1616      1.1  christos 	/*
   1617      1.1  christos 	 * Step 1
   1618      1.1  christos 	 * Fix the IP addresses in the offending IP packet. You also need
   1619      1.1  christos 	 * to adjust the IP header checksum of that offending IP packet.
   1620      1.1  christos 	 *
   1621      1.1  christos 	 * Normally, you would expect that the ICMP checksum of the
   1622      1.1  christos 	 * ICMP error message needs to be adjusted as well for the
   1623      1.1  christos 	 * IP address change in oip.
   1624      1.1  christos 	 * However, this is a NOP, because the ICMP checksum is
   1625      1.1  christos 	 * calculated over the complete ICMP packet, which includes the
   1626      1.1  christos 	 * changed oip IP addresses and oip6->ip6_sum. However, these
   1627      1.1  christos 	 * two changes cancel each other out (if the delta for
   1628      1.1  christos 	 * the IP address is x, then the delta for ip_sum is minus x),
   1629      1.1  christos 	 * so no change in the icmp_cksum is necessary.
   1630      1.1  christos 	 *
   1631      1.1  christos 	 * Inbound ICMP
   1632      1.1  christos 	 * ------------
   1633      1.1  christos 	 * MAP rule, SRC=a,DST=b -> SRC=c,DST=b
   1634      1.1  christos 	 * - response to outgoing packet (a,b)=>(c,b) (OIP_SRC=c,OIP_DST=b)
   1635      1.1  christos 	 * - OIP_SRC(c)=nat6_newsrcip,          OIP_DST(b)=nat6_newdstip
   1636      1.1  christos 	 *=> OIP_SRC(c)=nat6_oldsrcip,          OIP_DST(b)=nat6_olddstip
   1637      1.1  christos 	 *
   1638      1.1  christos 	 * RDR rule, SRC=a,DST=b -> SRC=a,DST=c
   1639      1.1  christos 	 * - response to outgoing packet (c,a)=>(b,a) (OIP_SRC=b,OIP_DST=a)
   1640      1.1  christos 	 * - OIP_SRC(b)=nat6_olddstip,          OIP_DST(a)=nat6_oldsrcip
   1641      1.1  christos 	 *=> OIP_SRC(b)=nat6_newdstip,          OIP_DST(a)=nat6_newsrcip
   1642      1.1  christos 	 *
   1643      1.1  christos 	 * REWRITE out rule, SRC=a,DST=b -> SRC=c,DST=d
   1644      1.1  christos 	 * - response to outgoing packet (a,b)=>(c,d) (OIP_SRC=c,OIP_DST=d)
   1645      1.1  christos 	 * - OIP_SRC(c)=nat6_newsrcip,          OIP_DST(d)=nat6_newdstip
   1646      1.1  christos 	 *=> OIP_SRC(c)=nat6_oldsrcip,          OIP_DST(d)=nat6_olddstip
   1647      1.1  christos 	 *
   1648      1.1  christos 	 * REWRITE in rule, SRC=a,DST=b -> SRC=c,DST=d
   1649      1.1  christos 	 * - response to outgoing packet (d,c)=>(b,a) (OIP_SRC=b,OIP_DST=a)
   1650      1.1  christos 	 * - OIP_SRC(b)=nat6_olddstip,          OIP_DST(a)=nat6_oldsrcip
   1651      1.1  christos 	 *=> OIP_SRC(b)=nat6_newdstip,          OIP_DST(a)=nat6_newsrcip
   1652      1.1  christos 	 *
   1653      1.1  christos 	 * Outbound ICMP
   1654      1.1  christos 	 * -------------
   1655      1.1  christos 	 * MAP rule, SRC=a,DST=b -> SRC=c,DST=b
   1656      1.1  christos 	 * - response to incoming packet (b,c)=>(b,a) (OIP_SRC=b,OIP_DST=a)
   1657      1.1  christos 	 * - OIP_SRC(b)=nat6_olddstip,          OIP_DST(a)=nat6_oldsrcip
   1658      1.1  christos 	 *=> OIP_SRC(b)=nat6_newdstip,          OIP_DST(a)=nat6_newsrcip
   1659      1.1  christos 	 *
   1660      1.1  christos 	 * RDR rule, SRC=a,DST=b -> SRC=a,DST=c
   1661      1.1  christos 	 * - response to incoming packet (a,b)=>(a,c) (OIP_SRC=a,OIP_DST=c)
   1662      1.1  christos 	 * - OIP_SRC(a)=nat6_newsrcip,          OIP_DST(c)=nat6_newdstip
   1663      1.1  christos 	 *=> OIP_SRC(a)=nat6_oldsrcip,          OIP_DST(c)=nat6_olddstip
   1664      1.1  christos 	 *
   1665      1.1  christos 	 * REWRITE out rule, SRC=a,DST=b -> SRC=c,DST=d
   1666      1.1  christos 	 * - response to incoming packet (d,c)=>(b,a) (OIP_SRC=c,OIP_DST=d)
   1667      1.1  christos 	 * - OIP_SRC(c)=nat6_olddstip,          OIP_DST(d)=nat6_oldsrcip
   1668      1.1  christos 	 *=> OIP_SRC(b)=nat6_newdstip,          OIP_DST(a)=nat6_newsrcip
   1669      1.1  christos 	 *
   1670      1.1  christos 	 * REWRITE in rule, SRC=a,DST=b -> SRC=c,DST=d
   1671      1.1  christos 	 * - response to incoming packet (a,b)=>(c,d) (OIP_SRC=b,OIP_DST=a)
   1672      1.1  christos 	 * - OIP_SRC(b)=nat6_newsrcip,          OIP_DST(a)=nat6_newdstip
   1673      1.1  christos 	 *=> OIP_SRC(a)=nat6_oldsrcip,          OIP_DST(c)=nat6_olddstip
   1674      1.1  christos 	 */
   1675      1.1  christos 
   1676      1.1  christos 	if (((fin->fin_out == 0) && ((nat->nat_redir & NAT_MAP) != 0)) ||
   1677      1.1  christos 	    ((fin->fin_out == 1) && ((nat->nat_redir & NAT_REDIRECT) != 0))) {
   1678      1.1  christos 		a1 = nat->nat_osrc6;
   1679      1.1  christos 		a4.in6 = oip6->ip6_src;
   1680      1.1  christos 		a3 = nat->nat_odst6;
   1681      1.1  christos 		a2.in6 = oip6->ip6_dst;
   1682      1.1  christos 		oip6->ip6_src = a1.in6;
   1683      1.1  christos 		oip6->ip6_dst = a3.in6;
   1684      1.1  christos 		odst = 1;
   1685      1.1  christos 	} else {
   1686      1.1  christos 		a1 = nat->nat_ndst6;
   1687      1.1  christos 		a2.in6 = oip6->ip6_dst;
   1688      1.1  christos 		a3 = nat->nat_nsrc6;
   1689      1.1  christos 		a4.in6 = oip6->ip6_src;
   1690      1.1  christos 		oip6->ip6_dst = a3.in6;
   1691      1.1  christos 		oip6->ip6_src = a1.in6;
   1692      1.1  christos 		odst = 0;
   1693      1.1  christos 	}
   1694      1.1  christos 
   1695      1.1  christos 	sumd = 0;
   1696      1.1  christos 	if (IP6_NEQ(&a3, &a2) || IP6_NEQ(&a1, &a4)) {
   1697      1.1  christos 		if (IP6_GT(&a3, &a2)) {
   1698      1.1  christos 			sumd = ipf_nat6_ip6subtract(&a2, &a3);
   1699      1.1  christos 			sumd--;
   1700      1.1  christos 		} else {
   1701      1.1  christos 			sumd = ipf_nat6_ip6subtract(&a2, &a3);
   1702      1.1  christos 		}
   1703      1.1  christos 		if (IP6_GT(&a1, &a4)) {
   1704      1.1  christos 			sumd += ipf_nat6_ip6subtract(&a4, &a1);
   1705      1.1  christos 			sumd--;
   1706      1.1  christos 		} else {
   1707      1.1  christos 			sumd += ipf_nat6_ip6subtract(&a4, &a1);
   1708      1.1  christos 		}
   1709      1.1  christos 		sumd = ~sumd;
   1710      1.1  christos 	}
   1711      1.1  christos 
   1712      1.1  christos 	sumd2 = sumd;
   1713      1.1  christos 	sum1 = 0;
   1714      1.1  christos 	sum2 = 0;
   1715      1.1  christos 
   1716      1.1  christos 	/*
   1717      1.1  christos 	 * Fix UDP pseudo header checksum to compensate for the
   1718      1.1  christos 	 * IP address change.
   1719      1.1  christos 	 */
   1720      1.1  christos 	if (((flags & IPN_TCPUDP) != 0) && (dlen >= 4)) {
   1721      1.1  christos 		u_32_t sum3, sum4;
   1722      1.1  christos 		/*
   1723      1.1  christos 		 * Step 2 :
   1724      1.1  christos 		 * For offending TCP/UDP IP packets, translate the ports as
   1725      1.1  christos 		 * well, based on the NAT specification. Of course such
   1726      1.1  christos 		 * a change may be reflected in the ICMP checksum as well.
   1727      1.1  christos 		 *
   1728      1.1  christos 		 * Since the port fields are part of the TCP/UDP checksum
   1729      1.1  christos 		 * of the offending IP packet, you need to adjust that checksum
   1730      1.1  christos 		 * as well... except that the change in the port numbers should
   1731      1.1  christos 		 * be offset by the checksum change.  However, the TCP/UDP
   1732      1.1  christos 		 * checksum will also need to change if there has been an
   1733      1.1  christos 		 * IP address change.
   1734      1.1  christos 		 */
   1735      1.1  christos 		if (odst == 1) {
   1736      1.1  christos 			sum1 = ntohs(nat->nat_osport);
   1737      1.1  christos 			sum4 = ntohs(tcp->th_sport);
   1738      1.1  christos 			sum3 = ntohs(nat->nat_odport);
   1739      1.1  christos 			sum2 = ntohs(tcp->th_dport);
   1740      1.1  christos 
   1741      1.1  christos 			tcp->th_sport = htons(sum1);
   1742      1.1  christos 			tcp->th_dport = htons(sum3);
   1743      1.1  christos 		} else {
   1744      1.1  christos 			sum1 = ntohs(nat->nat_ndport);
   1745      1.1  christos 			sum2 = ntohs(tcp->th_dport);
   1746      1.1  christos 			sum3 = ntohs(nat->nat_nsport);
   1747      1.1  christos 			sum4 = ntohs(tcp->th_sport);
   1748      1.1  christos 
   1749      1.1  christos 			tcp->th_dport = htons(sum3);
   1750      1.1  christos 			tcp->th_sport = htons(sum1);
   1751      1.1  christos 		}
   1752      1.1  christos 		sumd += sum1 - sum4;
   1753      1.1  christos 		sumd += sum3 - sum2;
   1754      1.1  christos 
   1755      1.1  christos 		if (sumd != 0 || sumd2 != 0) {
   1756      1.1  christos 			/*
   1757      1.1  christos 			 * At this point, sumd is the delta to apply to the
   1758      1.1  christos 			 * TCP/UDP header, given the changes in both the IP
   1759      1.1  christos 			 * address and the ports and sumd2 is the delta to
   1760      1.1  christos 			 * apply to the ICMP header, given the IP address
   1761      1.1  christos 			 * change delta that may need to be applied to the
   1762      1.1  christos 			 * TCP/UDP checksum instead.
   1763      1.1  christos 			 *
   1764      1.1  christos 			 * If we will both the IP and TCP/UDP checksums
   1765      1.1  christos 			 * then the ICMP checksum changes by the address
   1766      1.1  christos 			 * delta applied to the TCP/UDP checksum.  If we
   1767      1.1  christos 			 * do not change the TCP/UDP checksum them we
   1768      1.1  christos 			 * apply the delta in ports to the ICMP checksum.
   1769      1.1  christos 			 */
   1770      1.1  christos 			if (oip6->ip6_nxt == IPPROTO_UDP) {
   1771      1.1  christos 				if ((dlen >= 8) && (*csump != 0)) {
   1772      1.1  christos 					ipf_fix_datacksum(csump, sumd);
   1773      1.1  christos 				} else {
   1774      1.1  christos 					sumd2 = sum4 - sum1;
   1775      1.1  christos 					if (sum1 > sum4)
   1776      1.1  christos 						sumd2--;
   1777      1.1  christos 					sumd2 += sum2 - sum3;
   1778      1.1  christos 					if (sum3 > sum2)
   1779      1.1  christos 						sumd2--;
   1780      1.1  christos 				}
   1781      1.1  christos 			} else if (oip6->ip6_nxt == IPPROTO_TCP) {
   1782      1.1  christos 				if (dlen >= 18) {
   1783      1.1  christos 					ipf_fix_datacksum(csump, sumd);
   1784      1.1  christos 				} else {
   1785      1.1  christos 					sumd2 = sum4 - sum1;
   1786      1.1  christos 					if (sum1 > sum4)
   1787      1.1  christos 						sumd2--;
   1788      1.1  christos 					sumd2 += sum2 - sum3;
   1789      1.1  christos 					if (sum3 > sum2)
   1790      1.1  christos 						sumd2--;
   1791      1.1  christos 				}
   1792      1.1  christos 			}
   1793      1.1  christos 			if (sumd2 != 0) {
   1794      1.1  christos 				sumd2 = (sumd2 & 0xffff) + (sumd2 >> 16);
   1795      1.1  christos 				sumd2 = (sumd2 & 0xffff) + (sumd2 >> 16);
   1796      1.1  christos 				sumd2 = (sumd2 & 0xffff) + (sumd2 >> 16);
   1797      1.3   darrenr 				ipf_fix_incksum(0, &icmp6->icmp6_cksum,
   1798      1.3   darrenr 						sumd2, 0);
   1799      1.1  christos 			}
   1800      1.1  christos 		}
   1801      1.1  christos 	} else if (((flags & IPN_ICMPQUERY) != 0) && (dlen >= 8)) {
   1802      1.1  christos 		struct icmp6_hdr *orgicmp;
   1803      1.1  christos 
   1804      1.1  christos 		/*
   1805      1.1  christos 		 * XXX - what if this is bogus hl and we go off the end ?
   1806      1.1  christos 		 * In this case, ipf_nat6_icmperrorlookup() will have
   1807      1.1  christos 		 * returned NULL.
   1808      1.1  christos 		 */
   1809      1.1  christos 		orgicmp = (struct icmp6_hdr *)dp;
   1810      1.1  christos 
   1811      1.1  christos 		if (odst == 1) {
   1812      1.1  christos 			if (orgicmp->icmp6_id != nat->nat_osport) {
   1813      1.1  christos 
   1814      1.1  christos 				/*
   1815      1.1  christos 				 * Fix ICMP checksum (of the offening ICMP
   1816      1.1  christos 				 * query packet) to compensate the change
   1817      1.1  christos 				 * in the ICMP id of the offending ICMP
   1818      1.1  christos 				 * packet.
   1819      1.1  christos 				 *
   1820      1.1  christos 				 * Since you modify orgicmp->icmp6_id with
   1821      1.1  christos 				 * a delta (say x) and you compensate that
   1822      1.1  christos 				 * in origicmp->icmp6_cksum with a delta
   1823      1.1  christos 				 * minus x, you don't have to adjust the
   1824      1.1  christos 				 * overall icmp->icmp6_cksum
   1825      1.1  christos 				 */
   1826      1.1  christos 				sum1 = ntohs(orgicmp->icmp6_id);
   1827      1.1  christos 				sum2 = ntohs(nat->nat_osport);
   1828      1.1  christos 				CALC_SUMD(sum1, sum2, sumd);
   1829      1.1  christos 				orgicmp->icmp6_id = nat->nat_oicmpid;
   1830      1.1  christos 				ipf_fix_datacksum(&orgicmp->icmp6_cksum, sumd);
   1831      1.1  christos 			}
   1832      1.1  christos 		} /* nat6_dir == NAT_INBOUND is impossible for icmp queries */
   1833      1.1  christos 	}
   1834      1.1  christos 	return nat;
   1835      1.1  christos }
   1836      1.1  christos 
   1837      1.1  christos 
   1838      1.1  christos /*
   1839      1.1  christos  *       MAP-IN    MAP-OUT   RDR-IN   RDR-OUT
   1840      1.1  christos  * osrc    X       == src    == src      X
   1841      1.1  christos  * odst    X       == dst    == dst      X
   1842      1.1  christos  * nsrc  == dst      X         X      == dst
   1843      1.1  christos  * ndst  == src      X         X      == src
   1844      1.1  christos  * MAP = NAT_OUTBOUND, RDR = NAT_INBOUND
   1845      1.1  christos  */
   1846      1.1  christos /*
   1847      1.1  christos  * NB: these lookups don't lock access to the list, it assumed that it has
   1848      1.1  christos  * already been done!
   1849      1.1  christos  */
   1850      1.1  christos /* ------------------------------------------------------------------------ */
   1851      1.1  christos /* Function:    ipf_nat6_inlookup                                           */
   1852      1.1  christos /* Returns:     nat6_t*   - NULL == no match,                               */
   1853      1.1  christos /*                          else pointer to matching NAT entry              */
   1854      1.1  christos /* Parameters:  fin(I)    - pointer to packet information                   */
   1855      1.1  christos /*              flags(I)  - NAT flags for this packet                       */
   1856      1.1  christos /*              p(I)      - protocol for this packet                        */
   1857      1.1  christos /*              src(I)    - source IP address                               */
   1858      1.1  christos /*              mapdst(I) - destination IP address                          */
   1859      1.1  christos /*                                                                          */
   1860      1.1  christos /* Lookup a nat entry based on the mapped destination ip address/port and   */
   1861      1.1  christos /* real source address/port.  We use this lookup when receiving a packet,   */
   1862      1.1  christos /* we're looking for a table entry, based on the destination address.       */
   1863      1.1  christos /*                                                                          */
   1864      1.1  christos /* NOTE: THE PACKET BEING CHECKED (IF FOUND) HAS A MAPPING ALREADY.         */
   1865      1.1  christos /*                                                                          */
   1866      1.1  christos /* NOTE: IT IS ASSUMED THAT  IS ONLY HELD WITH A READ LOCK WHEN             */
   1867      1.1  christos /*       THIS FUNCTION IS CALLED WITH NAT_SEARCH SET IN nflags.             */
   1868      1.1  christos /*                                                                          */
   1869      1.1  christos /* flags   -> relevant are IPN_UDP/IPN_TCP/IPN_ICMPQUERY that indicate if   */
   1870      1.1  christos /*            the packet is of said protocol                                */
   1871      1.1  christos /* ------------------------------------------------------------------------ */
   1872      1.1  christos nat_t *
   1873      1.2  christos ipf_nat6_inlookup(fr_info_t *fin, u_int flags, u_int p, struct in6_addr *src,
   1874      1.2  christos     struct in6_addr *mapdst)
   1875      1.1  christos {
   1876      1.1  christos 	ipf_main_softc_t *softc = fin->fin_main_soft;
   1877      1.1  christos 	ipf_nat_softc_t *softn = softc->ipf_nat_soft;
   1878      1.1  christos 	u_short sport, dport;
   1879      1.1  christos 	nat_t *nat;
   1880      1.1  christos 	int nflags;
   1881      1.1  christos 	i6addr_t dst;
   1882      1.1  christos 	void *ifp;
   1883      1.1  christos 	u_int hv;
   1884      1.1  christos 
   1885      1.1  christos 	ifp = fin->fin_ifp;
   1886      1.1  christos 	sport = 0;
   1887      1.1  christos 	dport = 0;
   1888      1.1  christos 	dst.in6 = *mapdst;
   1889      1.1  christos 
   1890      1.1  christos 	switch (p)
   1891      1.1  christos 	{
   1892      1.1  christos 	case IPPROTO_TCP :
   1893      1.1  christos 	case IPPROTO_UDP :
   1894      1.1  christos 		sport = htons(fin->fin_data[0]);
   1895      1.1  christos 		dport = htons(fin->fin_data[1]);
   1896      1.1  christos 		break;
   1897      1.1  christos 	case IPPROTO_ICMPV6 :
   1898      1.1  christos 		if (flags & IPN_ICMPERR)
   1899      1.1  christos 			sport = fin->fin_data[1];
   1900      1.1  christos 		else
   1901      1.1  christos 			dport = fin->fin_data[1];
   1902      1.1  christos 		break;
   1903      1.1  christos 	default :
   1904      1.1  christos 		break;
   1905      1.1  christos 	}
   1906      1.1  christos 
   1907      1.1  christos 
   1908      1.1  christos 	if ((flags & SI_WILDP) != 0)
   1909      1.1  christos 		goto find_in_wild_ports;
   1910      1.1  christos 
   1911      1.1  christos 	hv = NAT_HASH_FN6(&dst, dport, 0xffffffff);
   1912      1.1  christos 	hv = NAT_HASH_FN6(src, hv + sport, softn->ipf_nat_table_sz);
   1913      1.1  christos 	nat = softn->ipf_nat_table[1][hv];
   1914      1.1  christos 	/* TRACE dst, dport, src, sport, hv, nat */
   1915      1.1  christos 
   1916      1.1  christos 	for (; nat; nat = nat->nat_hnext[1]) {
   1917      1.1  christos 		if (nat->nat_ifps[0] != NULL) {
   1918      1.1  christos 			if ((ifp != NULL) && (ifp != nat->nat_ifps[0]))
   1919      1.1  christos 				continue;
   1920      1.1  christos 		}
   1921      1.1  christos 
   1922      1.1  christos 		if (nat->nat_pr[0] != p)
   1923      1.1  christos 			continue;
   1924      1.1  christos 
   1925      1.1  christos 		switch (nat->nat_dir)
   1926      1.1  christos 		{
   1927      1.1  christos 		case NAT_INBOUND :
   1928      1.1  christos 			if (nat->nat_v[0] != 6)
   1929      1.1  christos 				continue;
   1930      1.1  christos 			if (IP6_NEQ(&nat->nat_osrc6, src) ||
   1931      1.1  christos 			    IP6_NEQ(&nat->nat_odst6, &dst))
   1932      1.1  christos 				continue;
   1933      1.1  christos 			if ((nat->nat_flags & IPN_TCPUDP) != 0) {
   1934      1.1  christos 				if (nat->nat_osport != sport)
   1935      1.1  christos 					continue;
   1936      1.1  christos 				if (nat->nat_odport != dport)
   1937      1.1  christos 					continue;
   1938      1.1  christos 
   1939      1.1  christos 			} else if (p == IPPROTO_ICMPV6) {
   1940      1.1  christos 				if (nat->nat_osport != dport) {
   1941      1.1  christos 					continue;
   1942      1.1  christos 				}
   1943      1.1  christos 			}
   1944      1.1  christos 			break;
   1945      1.1  christos 		case NAT_OUTBOUND :
   1946      1.1  christos 			if (nat->nat_v[1] != 6)
   1947      1.1  christos 				continue;
   1948      1.1  christos 			if (IP6_NEQ(&nat->nat_ndst6, src) ||
   1949      1.1  christos 			    IP6_NEQ(&nat->nat_nsrc6, &dst))
   1950      1.1  christos 				continue;
   1951      1.1  christos 			if ((nat->nat_flags & IPN_TCPUDP) != 0) {
   1952      1.1  christos 				if (nat->nat_ndport != sport)
   1953      1.1  christos 					continue;
   1954      1.1  christos 				if (nat->nat_nsport != dport)
   1955      1.1  christos 					continue;
   1956      1.1  christos 
   1957      1.1  christos 			} else if (p == IPPROTO_ICMPV6) {
   1958      1.1  christos 				if (nat->nat_osport != dport) {
   1959      1.1  christos 					continue;
   1960      1.1  christos 				}
   1961      1.1  christos 			}
   1962      1.1  christos 			break;
   1963      1.1  christos 		}
   1964      1.1  christos 
   1965      1.1  christos 
   1966      1.1  christos 		if ((nat->nat_flags & IPN_TCPUDP) != 0) {
   1967      1.1  christos #ifdef IPF_V6_PROXIES
   1968  1.4.2.1     rmind 			if ((nat->nat_ptr != NULL) && (nat->nat_aps != NULL))
   1969      1.1  christos 				if (appr_match(fin, nat) != 0)
   1970      1.1  christos 					continue;
   1971      1.1  christos #endif
   1972      1.1  christos 		}
   1973      1.1  christos 		if ((nat->nat_ifps[0] == NULL) && (ifp != NULL)) {
   1974      1.1  christos 			nat->nat_ifps[0] = ifp;
   1975      1.1  christos 			nat->nat_mtu[0] = GETIFMTU_6(ifp);
   1976      1.1  christos 		}
   1977      1.1  christos 		return nat;
   1978      1.1  christos 	}
   1979      1.1  christos 
   1980      1.1  christos 	/*
   1981      1.1  christos 	 * So if we didn't find it but there are wildcard members in the hash
   1982      1.1  christos 	 * table, go back and look for them.  We do this search and update here
   1983      1.1  christos 	 * because it is modifying the NAT table and we want to do this only
   1984      1.1  christos 	 * for the first packet that matches.  The exception, of course, is
   1985      1.1  christos 	 * for "dummy" (FI_IGNORE) lookups.
   1986      1.1  christos 	 */
   1987      1.1  christos find_in_wild_ports:
   1988      1.1  christos 	if (!(flags & NAT_TCPUDP) || !(flags & NAT_SEARCH)) {
   1989      1.1  christos 		NBUMPSIDE6DX(0, ns_lookup_miss, ns_lookup_miss_1);
   1990      1.1  christos 		return NULL;
   1991      1.1  christos 	}
   1992      1.3   darrenr 	if (softn->ipf_nat_stats.ns_wilds == 0 || (fin->fin_flx & FI_NOWILD)) {
   1993      1.1  christos 		NBUMPSIDE6D(0, ns_lookup_nowild);
   1994      1.1  christos 		return NULL;
   1995      1.1  christos 	}
   1996      1.1  christos 
   1997      1.1  christos 	RWLOCK_EXIT(&softc->ipf_nat);
   1998      1.1  christos 
   1999      1.1  christos 	hv = NAT_HASH_FN6(&dst, 0, 0xffffffff);
   2000      1.1  christos 	hv = NAT_HASH_FN6(src, hv, softn->ipf_nat_table_sz);
   2001      1.1  christos 	WRITE_ENTER(&softc->ipf_nat);
   2002      1.1  christos 
   2003      1.1  christos 	nat = softn->ipf_nat_table[1][hv];
   2004      1.1  christos 	/* TRACE dst, src, hv, nat */
   2005      1.1  christos 	for (; nat; nat = nat->nat_hnext[1]) {
   2006      1.1  christos 		if (nat->nat_ifps[0] != NULL) {
   2007      1.1  christos 			if ((ifp != NULL) && (ifp != nat->nat_ifps[0]))
   2008      1.1  christos 				continue;
   2009      1.1  christos 		}
   2010      1.1  christos 
   2011      1.1  christos 		if (nat->nat_pr[0] != fin->fin_p)
   2012      1.1  christos 			continue;
   2013      1.1  christos 
   2014      1.1  christos 		switch (nat->nat_dir)
   2015      1.1  christos 		{
   2016      1.1  christos 		case NAT_INBOUND :
   2017      1.1  christos 			if (nat->nat_v[0] != 6)
   2018      1.1  christos 				continue;
   2019      1.1  christos 			if (IP6_NEQ(&nat->nat_osrc6, src) ||
   2020      1.1  christos 			    IP6_NEQ(&nat->nat_odst6, &dst))
   2021      1.1  christos 				continue;
   2022      1.1  christos 			break;
   2023      1.1  christos 		case NAT_OUTBOUND :
   2024      1.1  christos 			if (nat->nat_v[1] != 6)
   2025      1.1  christos 				continue;
   2026      1.1  christos 			if (IP6_NEQ(&nat->nat_ndst6, src) ||
   2027      1.1  christos 			    IP6_NEQ(&nat->nat_nsrc6, &dst))
   2028      1.1  christos 				continue;
   2029      1.1  christos 			break;
   2030      1.1  christos 		}
   2031      1.1  christos 
   2032      1.1  christos 		nflags = nat->nat_flags;
   2033      1.1  christos 		if (!(nflags & (NAT_TCPUDP|SI_WILDP)))
   2034      1.1  christos 			continue;
   2035      1.1  christos 
   2036      1.1  christos 		if (ipf_nat_wildok(nat, (int)sport, (int)dport, nflags,
   2037      1.1  christos 				   NAT_INBOUND) == 1) {
   2038      1.1  christos 			if ((fin->fin_flx & FI_IGNORE) != 0)
   2039      1.1  christos 				break;
   2040      1.1  christos 			if ((nflags & SI_CLONE) != 0) {
   2041      1.1  christos 				nat = ipf_nat_clone(fin, nat);
   2042      1.1  christos 				if (nat == NULL)
   2043      1.1  christos 					break;
   2044      1.1  christos 			} else {
   2045      1.1  christos 				MUTEX_ENTER(&softn->ipf_nat_new);
   2046      1.1  christos 				softn->ipf_nat_stats.ns_wilds--;
   2047      1.1  christos 				MUTEX_EXIT(&softn->ipf_nat_new);
   2048      1.1  christos 			}
   2049      1.1  christos 
   2050      1.1  christos 			if (nat->nat_dir == NAT_INBOUND) {
   2051      1.1  christos 				if (nat->nat_osport == 0) {
   2052      1.1  christos 					nat->nat_osport = sport;
   2053      1.1  christos 					nat->nat_nsport = sport;
   2054      1.1  christos 				}
   2055      1.1  christos 				if (nat->nat_odport == 0) {
   2056      1.1  christos 					nat->nat_odport = dport;
   2057      1.1  christos 					nat->nat_ndport = dport;
   2058      1.1  christos 				}
   2059      1.1  christos 			} else {
   2060      1.1  christos 				if (nat->nat_osport == 0) {
   2061      1.1  christos 					nat->nat_osport = dport;
   2062      1.1  christos 					nat->nat_nsport = dport;
   2063      1.1  christos 				}
   2064      1.1  christos 				if (nat->nat_odport == 0) {
   2065      1.1  christos 					nat->nat_odport = sport;
   2066      1.1  christos 					nat->nat_ndport = sport;
   2067      1.1  christos 				}
   2068      1.1  christos 			}
   2069      1.1  christos 			if ((nat->nat_ifps[0] == NULL) && (ifp != NULL)) {
   2070      1.1  christos 				nat->nat_ifps[0] = ifp;
   2071      1.1  christos 				nat->nat_mtu[0] = GETIFMTU_6(ifp);
   2072      1.1  christos 			}
   2073      1.1  christos 			nat->nat_flags &= ~(SI_W_DPORT|SI_W_SPORT);
   2074      1.1  christos 			ipf_nat6_tabmove(softn, nat);
   2075      1.1  christos 			break;
   2076      1.1  christos 		}
   2077      1.1  christos 	}
   2078      1.1  christos 
   2079      1.1  christos 	MUTEX_DOWNGRADE(&softc->ipf_nat);
   2080      1.1  christos 
   2081      1.1  christos 	if (nat == NULL) {
   2082      1.1  christos 		NBUMPSIDE6DX(0, ns_lookup_miss, ns_lookup_miss_2);
   2083      1.1  christos 	}
   2084      1.1  christos 	return nat;
   2085      1.1  christos }
   2086      1.1  christos 
   2087      1.1  christos 
   2088      1.1  christos /* ------------------------------------------------------------------------ */
   2089      1.1  christos /* Function:    ipf_nat6_tabmove                                            */
   2090      1.1  christos /* Returns:     Nil                                                         */
   2091      1.1  christos /* Parameters:  nat(I) - pointer to NAT structure                           */
   2092      1.1  christos /* Write Lock:  ipf_nat                                                     */
   2093      1.1  christos /*                                                                          */
   2094      1.1  christos /* This function is only called for TCP/UDP NAT table entries where the     */
   2095      1.1  christos /* original was placed in the table without hashing on the ports and we now */
   2096      1.1  christos /* want to include hashing on port numbers.                                 */
   2097      1.1  christos /* ------------------------------------------------------------------------ */
   2098      1.1  christos static void
   2099      1.2  christos ipf_nat6_tabmove(ipf_nat_softc_t *softn, nat_t *nat)
   2100      1.1  christos {
   2101      1.4  christos 	u_int rhv0, rhv1, hv0, hv1;
   2102      1.1  christos 	nat_t **natp;
   2103      1.1  christos 
   2104      1.1  christos 	if (nat->nat_flags & SI_CLONE)
   2105      1.1  christos 		return;
   2106      1.1  christos 
   2107      1.1  christos 	/*
   2108      1.1  christos 	 * Remove the NAT entry from the old location
   2109      1.1  christos 	 */
   2110      1.1  christos 	if (nat->nat_hnext[0])
   2111      1.1  christos 		nat->nat_hnext[0]->nat_phnext[0] = nat->nat_phnext[0];
   2112      1.1  christos 	*nat->nat_phnext[0] = nat->nat_hnext[0];
   2113      1.1  christos 	softn->ipf_nat_stats.ns_side[0].ns_bucketlen[nat->nat_hv[0]]--;
   2114      1.1  christos 
   2115      1.1  christos 	if (nat->nat_hnext[1])
   2116      1.1  christos 		nat->nat_hnext[1]->nat_phnext[1] = nat->nat_phnext[1];
   2117      1.1  christos 	*nat->nat_phnext[1] = nat->nat_hnext[1];
   2118      1.1  christos 	softn->ipf_nat_stats.ns_side[1].ns_bucketlen[nat->nat_hv[1]]--;
   2119      1.1  christos 
   2120      1.1  christos 	/*
   2121      1.1  christos 	 * Add into the NAT table in the new position
   2122      1.1  christos 	 */
   2123      1.4  christos 	rhv0 = NAT_HASH_FN6(&nat->nat_osrc6, nat->nat_osport, 0xffffffff);
   2124      1.4  christos 	rhv0 = NAT_HASH_FN6(&nat->nat_odst6, rhv0 + nat->nat_odport,
   2125      1.4  christos 			    softn->ipf_nat_table_sz);
   2126      1.4  christos 	rhv1 = NAT_HASH_FN6(&nat->nat_nsrc6, nat->nat_nsport, 0xffffffff);
   2127      1.4  christos 	rhv1 = NAT_HASH_FN6(&nat->nat_ndst6, rhv1 + nat->nat_ndport,
   2128      1.4  christos 			    softn->ipf_nat_table_sz);
   2129      1.4  christos 
   2130      1.4  christos 	if ((nat->nat_dir & NAT_OUTBOUND) == NAT_OUTBOUND) {
   2131      1.4  christos 		nat->nat_hv[0] = rhv0;
   2132      1.4  christos 		nat->nat_hv[1] = rhv1;
   2133      1.4  christos 	} else {
   2134      1.4  christos 		nat->nat_hv[0] = rhv1;
   2135      1.4  christos 		nat->nat_hv[1] = rhv0;
   2136      1.1  christos 	}
   2137      1.1  christos 
   2138      1.4  christos 	hv0 = nat->nat_hv[0] % softn->ipf_nat_table_sz;
   2139      1.4  christos         hv1 = nat->nat_hv[1] % softn->ipf_nat_table_sz;
   2140      1.4  christos 
   2141      1.1  christos 	/* TRACE nat_osrc6, nat_osport, nat_odst6, nat_odport, hv0 */
   2142      1.1  christos 	/* TRACE nat_nsrc6, nat_nsport, nat_ndst6, nat_ndport, hv1 */
   2143      1.1  christos 
   2144      1.1  christos 	natp = &softn->ipf_nat_table[0][hv0];
   2145      1.1  christos 	if (*natp)
   2146      1.1  christos 		(*natp)->nat_phnext[0] = &nat->nat_hnext[0];
   2147      1.1  christos 	nat->nat_phnext[0] = natp;
   2148      1.1  christos 	nat->nat_hnext[0] = *natp;
   2149      1.1  christos 	*natp = nat;
   2150      1.1  christos 	softn->ipf_nat_stats.ns_side[0].ns_bucketlen[hv0]++;
   2151      1.1  christos 
   2152      1.1  christos 	natp = &softn->ipf_nat_table[1][hv1];
   2153      1.1  christos 	if (*natp)
   2154      1.1  christos 		(*natp)->nat_phnext[1] = &nat->nat_hnext[1];
   2155      1.1  christos 	nat->nat_phnext[1] = natp;
   2156      1.1  christos 	nat->nat_hnext[1] = *natp;
   2157      1.1  christos 	*natp = nat;
   2158      1.1  christos 	softn->ipf_nat_stats.ns_side[1].ns_bucketlen[hv1]++;
   2159      1.1  christos }
   2160      1.1  christos 
   2161      1.1  christos 
   2162      1.1  christos /* ------------------------------------------------------------------------ */
   2163      1.1  christos /* Function:    ipf_nat6_outlookup                                          */
   2164      1.1  christos /* Returns:     nat6_t*  - NULL == no match,                                */
   2165      1.1  christos /*                         else pointer to matching NAT entry               */
   2166      1.1  christos /* Parameters:  fin(I)   - pointer to packet information                    */
   2167      1.1  christos /*              flags(I) - NAT flags for this packet                        */
   2168      1.1  christos /*              p(I)     - protocol for this packet                         */
   2169      1.1  christos /*              src(I)   - source IP address                                */
   2170      1.1  christos /*              dst(I)   - destination IP address                           */
   2171      1.1  christos /*              rw(I)    - 1 == write lock on  held, 0 == read lock.        */
   2172      1.1  christos /*                                                                          */
   2173      1.1  christos /* Lookup a nat entry based on the source 'real' ip address/port and        */
   2174      1.1  christos /* destination address/port.  We use this lookup when sending a packet out, */
   2175      1.1  christos /* we're looking for a table entry, based on the source address.            */
   2176      1.1  christos /*                                                                          */
   2177      1.1  christos /* NOTE: THE PACKET BEING CHECKED (IF FOUND) HAS A MAPPING ALREADY.         */
   2178      1.1  christos /*                                                                          */
   2179      1.1  christos /* NOTE: IT IS ASSUMED THAT  IS ONLY HELD WITH A READ LOCK WHEN             */
   2180      1.1  christos /*       THIS FUNCTION IS CALLED WITH NAT_SEARCH SET IN nflags.             */
   2181      1.1  christos /*                                                                          */
   2182      1.1  christos /* flags   -> relevant are IPN_UDP/IPN_TCP/IPN_ICMPQUERY that indicate if   */
   2183      1.1  christos /*            the packet is of said protocol                                */
   2184      1.1  christos /* ------------------------------------------------------------------------ */
   2185      1.1  christos nat_t *
   2186      1.2  christos ipf_nat6_outlookup(fr_info_t *fin, u_int flags, u_int p, struct in6_addr *src,
   2187      1.2  christos     struct in6_addr *dst)
   2188      1.1  christos {
   2189      1.1  christos 	ipf_main_softc_t *softc = fin->fin_main_soft;
   2190      1.1  christos 	ipf_nat_softc_t *softn = softc->ipf_nat_soft;
   2191      1.1  christos 	u_short sport, dport;
   2192      1.1  christos 	nat_t *nat;
   2193      1.1  christos 	void *ifp;
   2194      1.1  christos 	u_int hv;
   2195      1.1  christos 
   2196      1.1  christos 	ifp = fin->fin_ifp;
   2197      1.1  christos 	sport = 0;
   2198      1.1  christos 	dport = 0;
   2199      1.1  christos 
   2200      1.1  christos 	switch (p)
   2201      1.1  christos 	{
   2202      1.1  christos 	case IPPROTO_TCP :
   2203      1.1  christos 	case IPPROTO_UDP :
   2204      1.1  christos 		sport = htons(fin->fin_data[0]);
   2205      1.1  christos 		dport = htons(fin->fin_data[1]);
   2206      1.1  christos 		break;
   2207      1.1  christos 	case IPPROTO_ICMPV6 :
   2208      1.1  christos 		if (flags & IPN_ICMPERR)
   2209      1.1  christos 			sport = fin->fin_data[1];
   2210      1.1  christos 		else
   2211      1.1  christos 			dport = fin->fin_data[1];
   2212      1.1  christos 		break;
   2213      1.1  christos 	default :
   2214      1.1  christos 		break;
   2215      1.1  christos 	}
   2216      1.1  christos 
   2217      1.1  christos 	if ((flags & SI_WILDP) != 0)
   2218      1.1  christos 		goto find_out_wild_ports;
   2219      1.1  christos 
   2220      1.1  christos 	hv = NAT_HASH_FN6(src, sport, 0xffffffff);
   2221      1.1  christos 	hv = NAT_HASH_FN6(dst, hv + dport, softn->ipf_nat_table_sz);
   2222      1.1  christos 	nat = softn->ipf_nat_table[0][hv];
   2223      1.1  christos 
   2224      1.1  christos 	/* TRACE src, sport, dst, dport, hv, nat */
   2225      1.1  christos 
   2226      1.1  christos 	for (; nat; nat = nat->nat_hnext[0]) {
   2227      1.1  christos 		if (nat->nat_ifps[1] != NULL) {
   2228      1.1  christos 			if ((ifp != NULL) && (ifp != nat->nat_ifps[1]))
   2229      1.1  christos 				continue;
   2230      1.1  christos 		}
   2231      1.1  christos 
   2232      1.1  christos 		if (nat->nat_pr[1] != p)
   2233      1.1  christos 			continue;
   2234      1.1  christos 
   2235      1.1  christos 		switch (nat->nat_dir)
   2236      1.1  christos 		{
   2237      1.1  christos 		case NAT_INBOUND :
   2238      1.1  christos 			if (nat->nat_v[1] != 6)
   2239      1.1  christos 				continue;
   2240      1.1  christos 			if (IP6_NEQ(&nat->nat_ndst6, src) ||
   2241      1.1  christos 			    IP6_NEQ(&nat->nat_nsrc6, dst))
   2242      1.1  christos 				continue;
   2243      1.1  christos 
   2244      1.1  christos 			if ((nat->nat_flags & IPN_TCPUDP) != 0) {
   2245      1.1  christos 				if (nat->nat_ndport != sport)
   2246      1.1  christos 					continue;
   2247      1.1  christos 				if (nat->nat_nsport != dport)
   2248      1.1  christos 					continue;
   2249      1.1  christos 
   2250      1.1  christos 			} else if (p == IPPROTO_ICMPV6) {
   2251      1.1  christos 				if (nat->nat_osport != dport) {
   2252      1.1  christos 					continue;
   2253      1.1  christos 				}
   2254      1.1  christos 			}
   2255      1.1  christos 			break;
   2256      1.1  christos 		case NAT_OUTBOUND :
   2257      1.1  christos 			if (nat->nat_v[0] != 6)
   2258      1.1  christos 				continue;
   2259      1.1  christos 			if (IP6_NEQ(&nat->nat_osrc6, src) ||
   2260      1.1  christos 			    IP6_NEQ(&nat->nat_odst6, dst))
   2261      1.1  christos 				continue;
   2262      1.1  christos 
   2263      1.1  christos 			if ((nat->nat_flags & IPN_TCPUDP) != 0) {
   2264      1.1  christos 				if (nat->nat_odport != dport)
   2265      1.1  christos 					continue;
   2266      1.1  christos 				if (nat->nat_osport != sport)
   2267      1.1  christos 					continue;
   2268      1.1  christos 
   2269      1.1  christos 			} else if (p == IPPROTO_ICMPV6) {
   2270      1.1  christos 				if (nat->nat_osport != dport) {
   2271      1.1  christos 					continue;
   2272      1.1  christos 				}
   2273      1.1  christos 			}
   2274      1.1  christos 			break;
   2275      1.1  christos 		}
   2276      1.1  christos 
   2277      1.1  christos #ifdef IPF_V6_PROXIES
   2278  1.4.2.1     rmind 		if ((nat->nat_ptr != NULL) && (nat->nat_aps != NULL))
   2279      1.1  christos 			if (appr_match(fin, nat) != 0)
   2280      1.1  christos 				continue;
   2281      1.1  christos #endif
   2282      1.1  christos 
   2283      1.1  christos 		if ((nat->nat_ifps[1] == NULL) && (ifp != NULL)) {
   2284      1.1  christos 			nat->nat_ifps[1] = ifp;
   2285      1.1  christos 			nat->nat_mtu[1] = GETIFMTU_6(ifp);
   2286      1.1  christos 		}
   2287      1.1  christos 		return nat;
   2288      1.1  christos 	}
   2289      1.1  christos 
   2290      1.1  christos 	/*
   2291      1.1  christos 	 * So if we didn't find it but there are wildcard members in the hash
   2292      1.1  christos 	 * table, go back and look for them.  We do this search and update here
   2293      1.1  christos 	 * because it is modifying the NAT table and we want to do this only
   2294      1.1  christos 	 * for the first packet that matches.  The exception, of course, is
   2295      1.1  christos 	 * for "dummy" (FI_IGNORE) lookups.
   2296      1.1  christos 	 */
   2297      1.1  christos find_out_wild_ports:
   2298      1.1  christos 	if (!(flags & NAT_TCPUDP) || !(flags & NAT_SEARCH)) {
   2299      1.1  christos 		NBUMPSIDE6DX(1, ns_lookup_miss, ns_lookup_miss_3);
   2300      1.1  christos 		return NULL;
   2301      1.1  christos 	}
   2302      1.3   darrenr 	if (softn->ipf_nat_stats.ns_wilds == 0 || (fin->fin_flx & FI_NOWILD)) {
   2303      1.1  christos 		NBUMPSIDE6D(1, ns_lookup_nowild);
   2304      1.1  christos 		return NULL;
   2305      1.1  christos 	}
   2306      1.1  christos 
   2307      1.1  christos 	RWLOCK_EXIT(&softc->ipf_nat);
   2308      1.1  christos 
   2309      1.1  christos 	hv = NAT_HASH_FN6(src, 0, 0xffffffff);
   2310      1.1  christos 	hv = NAT_HASH_FN6(dst, hv, softn->ipf_nat_table_sz);
   2311      1.1  christos 
   2312      1.1  christos 	WRITE_ENTER(&softc->ipf_nat);
   2313      1.1  christos 
   2314      1.1  christos 	nat = softn->ipf_nat_table[0][hv];
   2315      1.1  christos 	for (; nat; nat = nat->nat_hnext[0]) {
   2316      1.1  christos 		if (nat->nat_ifps[1] != NULL) {
   2317      1.1  christos 			if ((ifp != NULL) && (ifp != nat->nat_ifps[1]))
   2318      1.1  christos 				continue;
   2319      1.1  christos 		}
   2320      1.1  christos 
   2321      1.1  christos 		if (nat->nat_pr[1] != fin->fin_p)
   2322      1.1  christos 			continue;
   2323      1.1  christos 
   2324      1.1  christos 		switch (nat->nat_dir)
   2325      1.1  christos 		{
   2326      1.1  christos 		case NAT_INBOUND :
   2327      1.1  christos 			if (nat->nat_v[1] != 6)
   2328      1.1  christos 				continue;
   2329      1.1  christos 			if (IP6_NEQ(&nat->nat_ndst6, src) ||
   2330      1.1  christos 			    IP6_NEQ(&nat->nat_nsrc6, dst))
   2331      1.1  christos 				continue;
   2332      1.1  christos 			break;
   2333      1.1  christos 		case NAT_OUTBOUND :
   2334      1.1  christos 			if (nat->nat_v[0] != 6)
   2335      1.1  christos 			continue;
   2336      1.1  christos 			if (IP6_NEQ(&nat->nat_osrc6, src) ||
   2337      1.1  christos 			    IP6_NEQ(&nat->nat_odst6, dst))
   2338      1.1  christos 				continue;
   2339      1.1  christos 			break;
   2340      1.1  christos 		}
   2341      1.1  christos 
   2342      1.1  christos 		if (!(nat->nat_flags & (NAT_TCPUDP|SI_WILDP)))
   2343      1.1  christos 			continue;
   2344      1.1  christos 
   2345      1.1  christos 		if (ipf_nat_wildok(nat, (int)sport, (int)dport, nat->nat_flags,
   2346      1.1  christos 				   NAT_OUTBOUND) == 1) {
   2347      1.1  christos 			if ((fin->fin_flx & FI_IGNORE) != 0)
   2348      1.1  christos 				break;
   2349      1.1  christos 			if ((nat->nat_flags & SI_CLONE) != 0) {
   2350      1.1  christos 				nat = ipf_nat_clone(fin, nat);
   2351      1.1  christos 				if (nat == NULL)
   2352      1.1  christos 					break;
   2353      1.1  christos 			} else {
   2354      1.1  christos 				MUTEX_ENTER(&softn->ipf_nat_new);
   2355      1.1  christos 				softn->ipf_nat_stats.ns_wilds--;
   2356      1.1  christos 				MUTEX_EXIT(&softn->ipf_nat_new);
   2357      1.1  christos 			}
   2358      1.1  christos 
   2359      1.1  christos 			if (nat->nat_dir == NAT_OUTBOUND) {
   2360      1.1  christos 				if (nat->nat_osport == 0) {
   2361      1.1  christos 					nat->nat_osport = sport;
   2362      1.1  christos 					nat->nat_nsport = sport;
   2363      1.1  christos 				}
   2364      1.1  christos 				if (nat->nat_odport == 0) {
   2365      1.1  christos 					nat->nat_odport = dport;
   2366      1.1  christos 					nat->nat_ndport = dport;
   2367      1.1  christos 				}
   2368      1.1  christos 			} else {
   2369      1.1  christos 				if (nat->nat_osport == 0) {
   2370      1.1  christos 					nat->nat_osport = dport;
   2371      1.1  christos 					nat->nat_nsport = dport;
   2372      1.1  christos 				}
   2373      1.1  christos 				if (nat->nat_odport == 0) {
   2374      1.1  christos 					nat->nat_odport = sport;
   2375      1.1  christos 					nat->nat_ndport = sport;
   2376      1.1  christos 				}
   2377      1.1  christos 			}
   2378      1.1  christos 			if ((nat->nat_ifps[1] == NULL) && (ifp != NULL)) {
   2379      1.1  christos 				nat->nat_ifps[1] = ifp;
   2380      1.1  christos 				nat->nat_mtu[1] = GETIFMTU_6(ifp);
   2381      1.1  christos 			}
   2382      1.1  christos 			nat->nat_flags &= ~(SI_W_DPORT|SI_W_SPORT);
   2383      1.1  christos 			ipf_nat6_tabmove(softn, nat);
   2384      1.1  christos 			break;
   2385      1.1  christos 		}
   2386      1.1  christos 	}
   2387      1.1  christos 
   2388      1.1  christos 	MUTEX_DOWNGRADE(&softc->ipf_nat);
   2389      1.1  christos 
   2390      1.1  christos 	if (nat == NULL) {
   2391      1.1  christos 		NBUMPSIDE6DX(1, ns_lookup_miss, ns_lookup_miss_4);
   2392      1.1  christos 	}
   2393      1.1  christos 	return nat;
   2394      1.1  christos }
   2395      1.1  christos 
   2396      1.1  christos 
   2397      1.1  christos /* ------------------------------------------------------------------------ */
   2398      1.1  christos /* Function:    ipf_nat6_lookupredir                                        */
   2399      1.1  christos /* Returns:     nat6_t* - NULL == no match,                                 */
   2400      1.1  christos /*                       else pointer to matching NAT entry                 */
   2401      1.1  christos /* Parameters:  np(I) - pointer to description of packet to find NAT table  */
   2402      1.1  christos /*                      entry for.                                          */
   2403      1.1  christos /*                                                                          */
   2404      1.1  christos /* Lookup the NAT tables to search for a matching redirect                  */
   2405      1.1  christos /* The contents of natlookup_t should imitate those found in a packet that  */
   2406      1.1  christos /* would be translated - ie a packet coming in for RDR or going out for MAP.*/
   2407      1.1  christos /* We can do the lookup in one of two ways, imitating an inbound or         */
   2408      1.1  christos /* outbound  packet.  By default we assume outbound, unless IPN_IN is set.  */
   2409      1.1  christos /* For IN, the fields are set as follows:                                   */
   2410      1.1  christos /*     nl_real* = source information                                        */
   2411      1.1  christos /*     nl_out* = destination information (translated)                       */
   2412      1.1  christos /* For an out packet, the fields are set like this:                         */
   2413      1.1  christos /*     nl_in* = source information (untranslated)                           */
   2414      1.1  christos /*     nl_out* = destination information (translated)                       */
   2415      1.1  christos /* ------------------------------------------------------------------------ */
   2416      1.1  christos nat_t *
   2417      1.2  christos ipf_nat6_lookupredir(natlookup_t *np)
   2418      1.1  christos {
   2419      1.1  christos 	fr_info_t fi;
   2420      1.1  christos 	nat_t *nat;
   2421      1.1  christos 
   2422      1.1  christos 	bzero((char *)&fi, sizeof(fi));
   2423      1.1  christos 	if (np->nl_flags & IPN_IN) {
   2424      1.1  christos 		fi.fin_data[0] = ntohs(np->nl_realport);
   2425      1.1  christos 		fi.fin_data[1] = ntohs(np->nl_outport);
   2426      1.1  christos 	} else {
   2427      1.1  christos 		fi.fin_data[0] = ntohs(np->nl_inport);
   2428      1.1  christos 		fi.fin_data[1] = ntohs(np->nl_outport);
   2429      1.1  christos 	}
   2430      1.1  christos 	if (np->nl_flags & IPN_TCP)
   2431      1.1  christos 		fi.fin_p = IPPROTO_TCP;
   2432      1.1  christos 	else if (np->nl_flags & IPN_UDP)
   2433      1.1  christos 		fi.fin_p = IPPROTO_UDP;
   2434      1.1  christos 	else if (np->nl_flags & (IPN_ICMPERR|IPN_ICMPQUERY))
   2435      1.1  christos 		fi.fin_p = IPPROTO_ICMPV6;
   2436      1.1  christos 
   2437      1.1  christos 	/*
   2438      1.1  christos 	 * We can do two sorts of lookups:
   2439      1.1  christos 	 * - IPN_IN: we have the `real' and `out' address, look for `in'.
   2440      1.1  christos 	 * - default: we have the `in' and `out' address, look for `real'.
   2441      1.1  christos 	 */
   2442      1.1  christos 	if (np->nl_flags & IPN_IN) {
   2443      1.1  christos 		if ((nat = ipf_nat6_inlookup(&fi, np->nl_flags, fi.fin_p,
   2444      1.1  christos 					     &np->nl_realip6,
   2445      1.1  christos 					     &np->nl_outip6))) {
   2446      1.1  christos 			np->nl_inip6 = nat->nat_odst6.in6;
   2447      1.1  christos 			np->nl_inport = nat->nat_odport;
   2448      1.1  christos 		}
   2449      1.1  christos 	} else {
   2450      1.1  christos 		/*
   2451      1.1  christos 		 * If nl_inip is non null, this is a lookup based on the real
   2452      1.1  christos 		 * ip address. Else, we use the fake.
   2453      1.1  christos 		 */
   2454      1.1  christos 		if ((nat = ipf_nat6_outlookup(&fi, np->nl_flags, fi.fin_p,
   2455      1.1  christos 					      &np->nl_inip6, &np->nl_outip6))) {
   2456      1.1  christos 
   2457      1.1  christos 			if ((np->nl_flags & IPN_FINDFORWARD) != 0) {
   2458      1.1  christos 				fr_info_t fin;
   2459      1.1  christos 				bzero((char *)&fin, sizeof(fin));
   2460      1.1  christos 				fin.fin_p = nat->nat_pr[0];
   2461      1.1  christos 				fin.fin_data[0] = ntohs(nat->nat_ndport);
   2462      1.1  christos 				fin.fin_data[1] = ntohs(nat->nat_nsport);
   2463      1.1  christos 				if (ipf_nat6_inlookup(&fin, np->nl_flags,
   2464      1.1  christos 						     fin.fin_p,
   2465      1.1  christos 						     &nat->nat_ndst6.in6,
   2466      1.1  christos 						     &nat->nat_nsrc6.in6) !=
   2467      1.1  christos 				    NULL) {
   2468      1.1  christos 					np->nl_flags &= ~IPN_FINDFORWARD;
   2469      1.1  christos 				}
   2470      1.1  christos 			}
   2471      1.1  christos 
   2472      1.1  christos 			np->nl_realip6 = nat->nat_ndst6.in6;
   2473      1.1  christos 			np->nl_realport = nat->nat_ndport;
   2474      1.1  christos 		}
   2475      1.1  christos  	}
   2476      1.1  christos 
   2477      1.1  christos 	return nat;
   2478      1.1  christos }
   2479      1.1  christos 
   2480      1.1  christos 
   2481      1.1  christos /* ------------------------------------------------------------------------ */
   2482      1.1  christos /* Function:    ipf_nat6_match                                              */
   2483      1.1  christos /* Returns:     int - 0 == no match, 1 == match                             */
   2484      1.1  christos /* Parameters:  fin(I)   - pointer to packet information                    */
   2485      1.1  christos /*              np(I)    - pointer to NAT rule                              */
   2486      1.1  christos /*                                                                          */
   2487      1.1  christos /* Pull the matching of a packet against a NAT rule out of that complex     */
   2488      1.1  christos /* loop inside ipf_nat6_checkin() and lay it out properly in its own        */
   2489      1.1  christos /* function.                                                                */
   2490      1.1  christos /* ------------------------------------------------------------------------ */
   2491      1.1  christos static int
   2492      1.2  christos ipf_nat6_match(fr_info_t *fin, ipnat_t *np)
   2493      1.1  christos {
   2494      1.1  christos 	frtuc_t *ft;
   2495      1.1  christos 	int match;
   2496      1.1  christos 
   2497      1.1  christos 	match = 0;
   2498      1.1  christos 	switch (np->in_osrcatype)
   2499      1.1  christos 	{
   2500      1.1  christos 	case FRI_NORMAL :
   2501      1.1  christos 		match = IP6_MASKNEQ(&fin->fin_src6, &np->in_osrcmsk6,
   2502      1.1  christos 				    &np->in_osrcip6);
   2503      1.1  christos 		break;
   2504      1.1  christos 	case FRI_LOOKUP :
   2505      1.1  christos 		match = (*np->in_osrcfunc)(fin->fin_main_soft, np->in_osrcptr,
   2506      1.1  christos 					   6, &fin->fin_src6, fin->fin_plen);
   2507      1.1  christos 		break;
   2508      1.1  christos 	}
   2509      1.1  christos 	match ^= ((np->in_flags & IPN_NOTSRC) != 0);
   2510      1.1  christos 	if (match)
   2511      1.1  christos 		return 0;
   2512      1.1  christos 
   2513      1.1  christos 	match = 0;
   2514      1.1  christos 	switch (np->in_odstatype)
   2515      1.1  christos 	{
   2516      1.1  christos 	case FRI_NORMAL :
   2517      1.1  christos 		match = IP6_MASKNEQ(&fin->fin_dst6, &np->in_odstmsk6,
   2518      1.1  christos 				    &np->in_odstip6);
   2519      1.1  christos 		break;
   2520      1.1  christos 	case FRI_LOOKUP :
   2521      1.1  christos 		match = (*np->in_odstfunc)(fin->fin_main_soft, np->in_odstptr,
   2522      1.1  christos 					   6, &fin->fin_dst6, fin->fin_plen);
   2523      1.1  christos 		break;
   2524      1.1  christos 	}
   2525      1.1  christos 
   2526      1.1  christos 	match ^= ((np->in_flags & IPN_NOTDST) != 0);
   2527      1.1  christos 	if (match)
   2528      1.1  christos 		return 0;
   2529      1.1  christos 
   2530      1.1  christos 	ft = &np->in_tuc;
   2531      1.1  christos 	if (!(fin->fin_flx & FI_TCPUDP) ||
   2532      1.1  christos 	    (fin->fin_flx & (FI_SHORT|FI_FRAGBODY))) {
   2533      1.1  christos 		if (ft->ftu_scmp || ft->ftu_dcmp)
   2534      1.1  christos 			return 0;
   2535      1.1  christos 		return 1;
   2536      1.1  christos 	}
   2537      1.1  christos 
   2538      1.1  christos 	return ipf_tcpudpchk(&fin->fin_fi, ft);
   2539      1.1  christos }
   2540      1.1  christos 
   2541      1.1  christos 
   2542      1.1  christos /* ------------------------------------------------------------------------ */
   2543      1.1  christos /* Function:    ipf_nat6_checkout                                           */
   2544      1.1  christos /* Returns:     int - -1 == packet failed NAT checks so block it,           */
   2545      1.1  christos /*                     0 == no packet translation occurred,                 */
   2546      1.1  christos /*                     1 == packet was successfully translated.             */
   2547      1.1  christos /* Parameters:  fin(I)   - pointer to packet information                    */
   2548      1.1  christos /*              passp(I) - pointer to filtering result flags                */
   2549      1.1  christos /*                                                                          */
   2550      1.1  christos /* Check to see if an outcoming packet should be changed.  ICMP packets are */
   2551      1.1  christos /* first checked to see if they match an existing entry (if an error),      */
   2552      1.1  christos /* otherwise a search of the current NAT table is made.  If neither results */
   2553      1.1  christos /* in a match then a search for a matching NAT rule is made.  Create a new  */
   2554      1.1  christos /* NAT entry if a we matched a NAT rule.  Lastly, actually change the       */
   2555      1.1  christos /* packet header(s) as required.                                            */
   2556      1.1  christos /* ------------------------------------------------------------------------ */
   2557      1.1  christos int
   2558      1.2  christos ipf_nat6_checkout(fr_info_t *fin, u_32_t *passp)
   2559      1.1  christos {
   2560      1.1  christos 	ipf_main_softc_t *softc = fin->fin_main_soft;
   2561      1.1  christos 	ipf_nat_softc_t *softn = softc->ipf_nat_soft;
   2562      1.1  christos 	struct icmp6_hdr *icmp6 = NULL;
   2563      1.1  christos 	struct ifnet *ifp, *sifp;
   2564  1.4.2.1     rmind #ifdef IPF_V6_PROXIES
   2565      1.1  christos 	tcphdr_t *tcp = NULL;
   2566  1.4.2.1     rmind #endif
   2567      1.1  christos 	int rval, natfailed;
   2568      1.1  christos 	ipnat_t *np = NULL;
   2569      1.1  christos 	u_int nflags = 0;
   2570      1.1  christos 	i6addr_t ipa, iph;
   2571      1.1  christos 	int natadd = 1;
   2572      1.1  christos 	frentry_t *fr;
   2573      1.1  christos 	nat_t *nat;
   2574      1.1  christos 
   2575      1.1  christos 	if (softn->ipf_nat_stats.ns_rules == 0 || softn->ipf_nat_lock != 0)
   2576      1.1  christos 		return 0;
   2577      1.1  christos 
   2578      1.3   darrenr 	icmp6 = NULL;
   2579      1.1  christos 	natfailed = 0;
   2580      1.1  christos 	fr = fin->fin_fr;
   2581      1.1  christos 	sifp = fin->fin_ifp;
   2582      1.1  christos 	if (fr != NULL) {
   2583      1.1  christos 		ifp = fr->fr_tifs[fin->fin_rev].fd_ptr;
   2584      1.1  christos 		if ((ifp != NULL) && (ifp != (void *)-1))
   2585      1.1  christos 			fin->fin_ifp = ifp;
   2586      1.1  christos 	}
   2587      1.1  christos 	ifp = fin->fin_ifp;
   2588      1.1  christos 
   2589      1.1  christos 	if (!(fin->fin_flx & FI_SHORT) && (fin->fin_off == 0)) {
   2590      1.1  christos 		switch (fin->fin_p)
   2591      1.1  christos 		{
   2592      1.1  christos 		case IPPROTO_TCP :
   2593      1.1  christos 			nflags = IPN_TCP;
   2594      1.1  christos 			break;
   2595      1.1  christos 		case IPPROTO_UDP :
   2596      1.1  christos 			nflags = IPN_UDP;
   2597      1.1  christos 			break;
   2598      1.1  christos 		case IPPROTO_ICMPV6 :
   2599      1.1  christos 			icmp6 = fin->fin_dp;
   2600      1.1  christos 
   2601      1.1  christos 			/*
   2602      1.3   darrenr 			 * Apart from ECHO request and reply, all other
   2603      1.3   darrenr 			 * informational messages should not be translated
   2604      1.3   darrenr 			 * so as to keep IPv6 working.
   2605      1.3   darrenr 			 */
   2606      1.3   darrenr 			if (icmp6->icmp6_type > ICMP6_ECHO_REPLY)
   2607      1.3   darrenr 				return 0;
   2608      1.3   darrenr 
   2609      1.3   darrenr 			/*
   2610      1.1  christos 			 * This is an incoming packet, so the destination is
   2611      1.1  christos 			 * the icmp6_id and the source port equals 0
   2612      1.1  christos 			 */
   2613      1.1  christos 			if ((fin->fin_flx & FI_ICMPQUERY) != 0)
   2614      1.1  christos 				nflags = IPN_ICMPQUERY;
   2615      1.1  christos 			break;
   2616      1.1  christos 		default :
   2617      1.1  christos 			break;
   2618      1.1  christos 		}
   2619      1.1  christos 
   2620  1.4.2.1     rmind #ifdef IPF_V6_PROXIES
   2621      1.1  christos 		if ((nflags & IPN_TCPUDP))
   2622      1.1  christos 			tcp = fin->fin_dp;
   2623  1.4.2.1     rmind #endif
   2624      1.1  christos 	}
   2625      1.1  christos 
   2626      1.1  christos 	ipa = fin->fin_src6;
   2627      1.1  christos 
   2628      1.1  christos 	READ_ENTER(&softc->ipf_nat);
   2629      1.1  christos 
   2630      1.1  christos 	if ((fin->fin_p == IPPROTO_ICMPV6) && !(nflags & IPN_ICMPQUERY) &&
   2631      1.1  christos 	    (nat = ipf_nat6_icmperror(fin, &nflags, NAT_OUTBOUND)))
   2632      1.1  christos 		/*EMPTY*/;
   2633      1.1  christos 	else if ((fin->fin_flx & FI_FRAG) && (nat = ipf_frag_natknown(fin)))
   2634      1.1  christos 		natadd = 0;
   2635      1.1  christos 	else if ((nat = ipf_nat6_outlookup(fin, nflags|NAT_SEARCH,
   2636      1.1  christos 					   (u_int)fin->fin_p,
   2637      1.1  christos 					   &fin->fin_src6.in6,
   2638      1.1  christos 					   &fin->fin_dst6.in6))) {
   2639      1.1  christos 		nflags = nat->nat_flags;
   2640      1.1  christos 	} else if (fin->fin_off == 0) {
   2641      1.1  christos 		u_32_t hv, nmsk = 0;
   2642      1.1  christos 		i6addr_t *msk;
   2643      1.1  christos 
   2644      1.1  christos 		/*
   2645      1.1  christos 		 * If there is no current entry in the nat table for this IP#,
   2646      1.1  christos 		 * create one for it (if there is a matching rule).
   2647      1.1  christos 		 */
   2648      1.1  christos maskloop:
   2649      1.1  christos 		msk = &softn->ipf_nat6_map_active_masks[nmsk];
   2650      1.1  christos 		IP6_AND(&ipa, msk, &iph);
   2651      1.1  christos 		hv = NAT_HASH_FN6(&iph, 0, softn->ipf_nat_maprules_sz);
   2652      1.1  christos 		for (np = softn->ipf_nat_map_rules[hv]; np; np = np->in_mnext) {
   2653      1.1  christos 			if ((np->in_ifps[1] && (np->in_ifps[1] != ifp)))
   2654      1.1  christos 				continue;
   2655      1.1  christos 			if (np->in_v[0] != 6)
   2656      1.1  christos 				continue;
   2657      1.1  christos 			if (np->in_pr[1] && (np->in_pr[1] != fin->fin_p))
   2658      1.1  christos 				continue;
   2659      1.1  christos 			if ((np->in_flags & IPN_RF) &&
   2660      1.1  christos 			    !(np->in_flags & nflags))
   2661      1.1  christos 				continue;
   2662      1.1  christos 			if (np->in_flags & IPN_FILTER) {
   2663      1.1  christos 				switch (ipf_nat6_match(fin, np))
   2664      1.1  christos 				{
   2665      1.1  christos 				case 0 :
   2666      1.1  christos 					continue;
   2667      1.1  christos 				case -1 :
   2668      1.1  christos 					rval = -1;
   2669      1.1  christos 					goto outmatchfail;
   2670      1.1  christos 				case 1 :
   2671      1.1  christos 				default :
   2672      1.1  christos 					break;
   2673      1.1  christos 				}
   2674      1.1  christos 			} else if (!IP6_MASKEQ(&ipa, &np->in_osrcmsk,
   2675      1.1  christos 					       &np->in_osrcip6))
   2676      1.1  christos 				continue;
   2677      1.1  christos 
   2678      1.1  christos 			if ((fr != NULL) &&
   2679      1.1  christos 			    !ipf_matchtag(&np->in_tag, &fr->fr_nattag))
   2680      1.1  christos 				continue;
   2681      1.1  christos 
   2682      1.1  christos #ifdef IPF_V6_PROXIES
   2683      1.1  christos 			if (np->in_plabel != -1) {
   2684      1.1  christos 				if (((np->in_flags & IPN_FILTER) == 0) &&
   2685      1.1  christos 				    (np->in_odport != fin->fin_data[1]))
   2686      1.1  christos 					continue;
   2687      1.1  christos 				if (appr_ok(fin, tcp, np) == 0)
   2688      1.1  christos 					continue;
   2689      1.1  christos 			}
   2690      1.1  christos #endif
   2691      1.1  christos 
   2692      1.1  christos 			if (np->in_flags & IPN_NO) {
   2693      1.1  christos 				np->in_hits++;
   2694      1.1  christos 				break;
   2695      1.1  christos 			}
   2696      1.1  christos 
   2697      1.1  christos 			MUTEX_ENTER(&softn->ipf_nat_new);
   2698      1.1  christos 			nat = ipf_nat6_add(fin, np, NULL, nflags, NAT_OUTBOUND);
   2699      1.1  christos 			MUTEX_EXIT(&softn->ipf_nat_new);
   2700      1.1  christos 			if (nat != NULL) {
   2701      1.1  christos 				np->in_hits++;
   2702      1.1  christos 				break;
   2703      1.1  christos 			}
   2704      1.1  christos 			natfailed = -1;
   2705      1.1  christos 		}
   2706      1.1  christos 		if ((np == NULL) && (nmsk < softn->ipf_nat6_map_max)) {
   2707      1.1  christos 			nmsk++;
   2708      1.1  christos 			goto maskloop;
   2709      1.1  christos 		}
   2710      1.1  christos 	}
   2711      1.1  christos 
   2712      1.1  christos 	if (nat != NULL) {
   2713      1.1  christos 		rval = ipf_nat6_out(fin, nat, natadd, nflags);
   2714      1.1  christos 		if (rval == 1) {
   2715      1.1  christos 			MUTEX_ENTER(&nat->nat_lock);
   2716      1.1  christos 			ipf_nat_update(fin, nat);
   2717      1.1  christos 			nat->nat_bytes[1] += fin->fin_plen;
   2718      1.1  christos 			nat->nat_pkts[1]++;
   2719      1.1  christos 			MUTEX_EXIT(&nat->nat_lock);
   2720      1.1  christos 		}
   2721      1.1  christos 	} else
   2722      1.1  christos 		rval = natfailed;
   2723      1.1  christos outmatchfail:
   2724      1.1  christos 	RWLOCK_EXIT(&softc->ipf_nat);
   2725      1.1  christos 
   2726      1.1  christos 	switch (rval)
   2727      1.1  christos 	{
   2728      1.1  christos 	case -1 :
   2729      1.1  christos 		if (passp != NULL) {
   2730      1.1  christos 			NBUMPSIDE6D(1, ns_drop);
   2731      1.1  christos 			*passp = FR_BLOCK;
   2732      1.3   darrenr 			fin->fin_reason = FRB_NATV6;
   2733      1.1  christos 		}
   2734      1.1  christos 		fin->fin_flx |= FI_BADNAT;
   2735      1.1  christos 		NBUMPSIDE6D(1, ns_badnat);
   2736      1.1  christos 		break;
   2737      1.1  christos 	case 0 :
   2738      1.1  christos 		NBUMPSIDE6D(1, ns_ignored);
   2739      1.1  christos 		break;
   2740      1.1  christos 	case 1 :
   2741      1.1  christos 		NBUMPSIDE6D(1, ns_translated);
   2742      1.1  christos 		break;
   2743      1.1  christos 	}
   2744      1.1  christos 	fin->fin_ifp = sifp;
   2745      1.1  christos 	return rval;
   2746      1.1  christos }
   2747      1.1  christos 
   2748      1.1  christos /* ------------------------------------------------------------------------ */
   2749      1.1  christos /* Function:    ipf_nat6_out                                                */
   2750      1.1  christos /* Returns:     int - -1 == packet failed NAT checks so block it,           */
   2751      1.1  christos /*                     1 == packet was successfully translated.             */
   2752      1.1  christos /* Parameters:  fin(I)    - pointer to packet information                   */
   2753      1.1  christos /*              nat(I)    - pointer to NAT structure                        */
   2754      1.1  christos /*              natadd(I) - flag indicating if it is safe to add frag cache */
   2755      1.1  christos /*              nflags(I) - NAT flags set for this packet                   */
   2756      1.1  christos /*                                                                          */
   2757      1.1  christos /* Translate a packet coming "out" on an interface.                         */
   2758      1.1  christos /* ------------------------------------------------------------------------ */
   2759      1.1  christos static int
   2760      1.2  christos ipf_nat6_out(fr_info_t *fin, nat_t *nat, int natadd, u_32_t nflags)
   2761      1.1  christos {
   2762      1.1  christos 	ipf_main_softc_t *softc = fin->fin_main_soft;
   2763      1.1  christos 	ipf_nat_softc_t *softn = softc->ipf_nat_soft;
   2764      1.1  christos 	struct icmp6_hdr *icmp6;
   2765      1.1  christos 	tcphdr_t *tcp;
   2766      1.1  christos 	ipnat_t *np;
   2767      1.1  christos 	int skip;
   2768      1.1  christos 	int i;
   2769      1.1  christos 
   2770      1.1  christos 	tcp = NULL;
   2771      1.1  christos 	icmp6 = NULL;
   2772      1.1  christos 	np = nat->nat_ptr;
   2773      1.1  christos 
   2774      1.1  christos 	if ((natadd != 0) && (fin->fin_flx & FI_FRAG) && (np != NULL))
   2775      1.1  christos 		(void) ipf_frag_natnew(softc, fin, 0, nat);
   2776      1.1  christos 
   2777      1.1  christos 	/*
   2778      1.1  christos 	 * Address assignment is after the checksum modification because
   2779      1.1  christos 	 * we are using the address in the packet for determining the
   2780      1.1  christos 	 * correct checksum offset (the ICMP error could be coming from
   2781      1.1  christos 	 * anyone...)
   2782      1.1  christos 	 */
   2783      1.1  christos 	switch (nat->nat_dir)
   2784      1.1  christos 	{
   2785      1.1  christos 	case NAT_OUTBOUND :
   2786      1.1  christos 		fin->fin_ip6->ip6_src = nat->nat_nsrc6.in6;
   2787      1.1  christos 		fin->fin_src6 = nat->nat_nsrc6;
   2788      1.1  christos 		fin->fin_ip6->ip6_dst = nat->nat_ndst6.in6;
   2789      1.1  christos 		fin->fin_dst6 = nat->nat_ndst6;
   2790      1.1  christos 		break;
   2791      1.1  christos 
   2792      1.1  christos 	case NAT_INBOUND :
   2793      1.1  christos 		fin->fin_ip6->ip6_src = nat->nat_odst6.in6;
   2794      1.1  christos 		fin->fin_src6 = nat->nat_ndst6;
   2795      1.1  christos 		fin->fin_ip6->ip6_dst = nat->nat_osrc6.in6;
   2796      1.1  christos 		fin->fin_dst6 = nat->nat_nsrc6;
   2797      1.1  christos 		break;
   2798      1.1  christos 
   2799      1.1  christos 	case NAT_DIVERTIN :
   2800      1.1  christos 	    {
   2801      1.1  christos 		mb_t *m;
   2802      1.1  christos 
   2803      1.1  christos 		skip = ipf_nat6_decap(fin, nat);
   2804      1.1  christos 		if (skip <= 0) {
   2805      1.1  christos 			NBUMPSIDE6D(1, ns_decap_fail);
   2806      1.1  christos 			return -1;
   2807      1.1  christos 		}
   2808      1.1  christos 
   2809      1.1  christos 		m = fin->fin_m;
   2810      1.1  christos 
   2811      1.1  christos #if defined(MENTAT) && defined(_KERNEL)
   2812      1.1  christos 		m->b_rptr += skip;
   2813      1.1  christos #else
   2814      1.1  christos 		m->m_data += skip;
   2815      1.1  christos 		m->m_len -= skip;
   2816      1.1  christos 
   2817      1.1  christos # ifdef M_PKTHDR
   2818      1.1  christos 		if (m->m_flags & M_PKTHDR)
   2819      1.1  christos 			m->m_pkthdr.len -= skip;
   2820      1.1  christos # endif
   2821      1.1  christos #endif
   2822      1.1  christos 
   2823      1.1  christos 		MUTEX_ENTER(&nat->nat_lock);
   2824      1.1  christos 		ipf_nat_update(fin, nat);
   2825      1.1  christos 		MUTEX_EXIT(&nat->nat_lock);
   2826      1.1  christos 		fin->fin_flx |= FI_NATED;
   2827      1.1  christos 		if (np != NULL && np->in_tag.ipt_num[0] != 0)
   2828      1.1  christos 			fin->fin_nattag = &np->in_tag;
   2829      1.1  christos 		return 1;
   2830      1.1  christos 		/* NOTREACHED */
   2831      1.1  christos 	    }
   2832      1.1  christos 
   2833      1.1  christos 	case NAT_DIVERTOUT :
   2834      1.1  christos 	    {
   2835      1.1  christos 		udphdr_t *uh;
   2836      1.1  christos 		ip6_t *ip6;
   2837      1.1  christos 		mb_t *m;
   2838      1.1  christos 
   2839      1.1  christos 		m = M_DUP(np->in_divmp);
   2840      1.1  christos 		if (m == NULL) {
   2841      1.1  christos 			NBUMPSIDE6D(1, ns_divert_dup);
   2842      1.1  christos 			return -1;
   2843      1.1  christos 		}
   2844      1.1  christos 
   2845      1.1  christos 		ip6 = MTOD(m, ip6_t *);
   2846      1.1  christos 
   2847      1.1  christos 		ip6->ip6_plen = htons(fin->fin_plen + 8);
   2848      1.1  christos 
   2849      1.1  christos 		uh = (udphdr_t *)(ip6 + 1);
   2850      1.1  christos 		uh->uh_ulen = htons(fin->fin_plen);
   2851      1.1  christos 
   2852      1.1  christos 		PREP_MB_T(fin, m);
   2853      1.1  christos 
   2854      1.1  christos 		fin->fin_ip6 = ip6;
   2855      1.1  christos 		fin->fin_plen += sizeof(ip6_t) + 8;	/* UDP + new IPv4 hdr */
   2856      1.1  christos 		fin->fin_dlen += sizeof(ip6_t) + 8;	/* UDP + old IPv4 hdr */
   2857      1.1  christos 
   2858      1.1  christos 		nflags &= ~IPN_TCPUDPICMP;
   2859      1.1  christos 
   2860      1.1  christos 		break;
   2861      1.1  christos 	    }
   2862      1.1  christos 
   2863      1.1  christos 	default :
   2864      1.1  christos 		break;
   2865      1.1  christos 	}
   2866      1.1  christos 
   2867      1.1  christos 	if (!(fin->fin_flx & FI_SHORT) && (fin->fin_off == 0)) {
   2868      1.3   darrenr 		u_short *csump;
   2869      1.3   darrenr 
   2870      1.1  christos 		if ((nat->nat_nsport != 0) && (nflags & IPN_TCPUDP)) {
   2871      1.1  christos 			tcp = fin->fin_dp;
   2872      1.1  christos 
   2873      1.1  christos 			switch (nat->nat_dir)
   2874      1.1  christos 			{
   2875      1.1  christos 			case NAT_OUTBOUND :
   2876      1.1  christos 				tcp->th_sport = nat->nat_nsport;
   2877      1.1  christos 				fin->fin_data[0] = ntohs(nat->nat_nsport);
   2878      1.1  christos 				tcp->th_dport = nat->nat_ndport;
   2879      1.3   darrenr 				fin->fin_data[1] = ntohs(nat->nat_ndport);
   2880      1.1  christos 				break;
   2881      1.1  christos 
   2882      1.1  christos 			case NAT_INBOUND :
   2883      1.1  christos 				tcp->th_sport = nat->nat_odport;
   2884      1.1  christos 				fin->fin_data[0] = ntohs(nat->nat_odport);
   2885      1.1  christos 				tcp->th_dport = nat->nat_osport;
   2886      1.3   darrenr 				fin->fin_data[1] = ntohs(nat->nat_osport);
   2887      1.1  christos 				break;
   2888      1.1  christos 			}
   2889      1.1  christos 		}
   2890      1.1  christos 
   2891      1.1  christos 		if ((nat->nat_nsport != 0) && (nflags & IPN_ICMPQUERY)) {
   2892      1.1  christos 			icmp6 = fin->fin_dp;
   2893      1.1  christos 			icmp6->icmp6_id = nat->nat_nicmpid;
   2894      1.1  christos 		}
   2895      1.1  christos 
   2896      1.1  christos 		csump = ipf_nat_proto(fin, nat, nflags);
   2897      1.3   darrenr 
   2898      1.3   darrenr 		/*
   2899      1.3   darrenr 		 * The above comments do not hold for layer 4 (or higher)
   2900      1.3   darrenr 		 * checksums...
   2901      1.3   darrenr 		 */
   2902      1.3   darrenr 		if (csump != NULL) {
   2903      1.3   darrenr 			if (nat->nat_dir == NAT_OUTBOUND)
   2904      1.3   darrenr 				ipf_fix_outcksum(fin->fin_cksum, csump,
   2905      1.3   darrenr 						 nat->nat_sumd[0],
   2906      1.3   darrenr 						 nat->nat_sumd[1] +
   2907      1.3   darrenr 						 fin->fin_dlen);
   2908      1.3   darrenr 			else
   2909      1.3   darrenr 				ipf_fix_incksum(fin->fin_cksum, csump,
   2910      1.3   darrenr 						nat->nat_sumd[0],
   2911      1.3   darrenr 						nat->nat_sumd[1] +
   2912      1.3   darrenr 						fin->fin_dlen);
   2913      1.3   darrenr 		}
   2914      1.1  christos 	}
   2915      1.1  christos 
   2916      1.1  christos 	ipf_sync_update(softc, SMC_NAT, fin, nat->nat_sync);
   2917      1.1  christos 	/* ------------------------------------------------------------- */
   2918      1.3   darrenr 	/* A few quick notes:                                            */
   2919      1.3   darrenr 	/*      Following are test conditions prior to calling the       */
   2920      1.3   darrenr 	/*      ipf_proxy_check routine.                                 */
   2921      1.3   darrenr 	/*                                                               */
   2922      1.3   darrenr 	/*      A NULL tcp indicates a non TCP/UDP packet.  When dealing */
   2923      1.3   darrenr 	/*      with a redirect rule, we attempt to match the packet's   */
   2924      1.3   darrenr 	/*      source port against in_dport, otherwise we'd compare the */
   2925      1.3   darrenr 	/*      packet's destination.                                    */
   2926      1.1  christos 	/* ------------------------------------------------------------- */
   2927      1.1  christos 	if ((np != NULL) && (np->in_apr != NULL)) {
   2928      1.3   darrenr 		i = ipf_proxy_check(fin, nat);
   2929      1.3   darrenr 		if (i == 0) {
   2930      1.1  christos 			i = 1;
   2931      1.3   darrenr 		} else if (i == -1) {
   2932      1.3   darrenr 			NBUMPSIDE6D(1, ns_ipf_proxy_fail);
   2933      1.1  christos 		}
   2934      1.1  christos 	} else {
   2935      1.1  christos 		i = 1;
   2936      1.1  christos 	}
   2937      1.1  christos 	fin->fin_flx |= FI_NATED;
   2938      1.1  christos 	return i;
   2939      1.1  christos }
   2940      1.1  christos 
   2941      1.1  christos 
   2942      1.1  christos /* ------------------------------------------------------------------------ */
   2943      1.1  christos /* Function:    ipf_nat6_checkin                                            */
   2944      1.1  christos /* Returns:     int - -1 == packet failed NAT checks so block it,           */
   2945      1.1  christos /*                     0 == no packet translation occurred,                 */
   2946      1.1  christos /*                     1 == packet was successfully translated.             */
   2947      1.1  christos /* Parameters:  fin(I)   - pointer to packet information                    */
   2948      1.1  christos /*              passp(I) - pointer to filtering result flags                */
   2949      1.1  christos /*                                                                          */
   2950      1.1  christos /* Check to see if an incoming packet should be changed.  ICMP packets are  */
   2951      1.1  christos /* first checked to see if they match an existing entry (if an error),      */
   2952      1.1  christos /* otherwise a search of the current NAT table is made.  If neither results */
   2953      1.1  christos /* in a match then a search for a matching NAT rule is made.  Create a new  */
   2954      1.1  christos /* NAT entry if a we matched a NAT rule.  Lastly, actually change the       */
   2955      1.1  christos /* packet header(s) as required.                                            */
   2956      1.1  christos /* ------------------------------------------------------------------------ */
   2957      1.1  christos int
   2958      1.2  christos ipf_nat6_checkin(fr_info_t *fin, u_32_t *passp)
   2959      1.1  christos {
   2960      1.1  christos 	ipf_main_softc_t *softc = fin->fin_main_soft;
   2961      1.1  christos 	ipf_nat_softc_t *softn = softc->ipf_nat_soft;
   2962      1.1  christos 	struct icmp6_hdr *icmp6;
   2963      1.1  christos 	u_int nflags, natadd;
   2964      1.1  christos 	int rval, natfailed;
   2965      1.1  christos 	struct ifnet *ifp;
   2966      1.1  christos 	i6addr_t ipa, iph;
   2967  1.4.2.1     rmind #ifdef IPF_V6_PROXIES
   2968      1.1  christos 	tcphdr_t *tcp;
   2969  1.4.2.1     rmind #endif
   2970      1.1  christos 	u_short dport;
   2971      1.1  christos 	ipnat_t *np;
   2972      1.1  christos 	nat_t *nat;
   2973      1.1  christos 
   2974      1.1  christos 	if (softn->ipf_nat_stats.ns_rules == 0 || softn->ipf_nat_lock != 0)
   2975      1.1  christos 		return 0;
   2976      1.1  christos 
   2977  1.4.2.1     rmind #ifdef IPF_V6_PROXIES
   2978      1.1  christos 	tcp = NULL;
   2979  1.4.2.1     rmind #endif
   2980      1.1  christos 	icmp6 = NULL;
   2981      1.1  christos 	dport = 0;
   2982      1.1  christos 	natadd = 1;
   2983      1.1  christos 	nflags = 0;
   2984      1.1  christos 	natfailed = 0;
   2985      1.1  christos 	ifp = fin->fin_ifp;
   2986      1.1  christos 
   2987      1.1  christos 	if (!(fin->fin_flx & FI_SHORT) && (fin->fin_off == 0)) {
   2988      1.1  christos 		switch (fin->fin_p)
   2989      1.1  christos 		{
   2990      1.1  christos 		case IPPROTO_TCP :
   2991      1.1  christos 			nflags = IPN_TCP;
   2992      1.1  christos 			break;
   2993      1.1  christos 		case IPPROTO_UDP :
   2994      1.1  christos 			nflags = IPN_UDP;
   2995      1.1  christos 			break;
   2996      1.1  christos 		case IPPROTO_ICMPV6 :
   2997      1.1  christos 			icmp6 = fin->fin_dp;
   2998      1.1  christos 
   2999      1.1  christos 			/*
   3000      1.3   darrenr 			 * Apart from ECHO request and reply, all other
   3001      1.3   darrenr 			 * informational messages should not be translated
   3002      1.3   darrenr 			 * so as to keep IPv6 working.
   3003      1.3   darrenr 			 */
   3004      1.3   darrenr 			if (icmp6->icmp6_type > ICMP6_ECHO_REPLY)
   3005      1.3   darrenr 				return 0;
   3006      1.3   darrenr 
   3007      1.3   darrenr 			/*
   3008      1.1  christos 			 * This is an incoming packet, so the destination is
   3009      1.1  christos 			 * the icmp6_id and the source port equals 0
   3010      1.1  christos 			 */
   3011      1.1  christos 			if ((fin->fin_flx & FI_ICMPQUERY) != 0) {
   3012      1.1  christos 				nflags = IPN_ICMPQUERY;
   3013      1.1  christos 				dport = icmp6->icmp6_id;
   3014      1.1  christos 			} break;
   3015      1.1  christos 		default :
   3016      1.1  christos 			break;
   3017      1.1  christos 		}
   3018      1.1  christos 
   3019      1.1  christos 		if ((nflags & IPN_TCPUDP)) {
   3020  1.4.2.1     rmind #ifdef IPF_V6_PROXIES
   3021      1.1  christos 			tcp = fin->fin_dp;
   3022  1.4.2.1     rmind #endif
   3023      1.1  christos 			dport = fin->fin_data[1];
   3024      1.1  christos 		}
   3025      1.1  christos 	}
   3026      1.1  christos 
   3027      1.1  christos 	ipa = fin->fin_dst6;
   3028      1.1  christos 
   3029      1.1  christos 	READ_ENTER(&softc->ipf_nat);
   3030      1.1  christos 
   3031      1.1  christos 	if ((fin->fin_p == IPPROTO_ICMPV6) && !(nflags & IPN_ICMPQUERY) &&
   3032      1.1  christos 	    (nat = ipf_nat6_icmperror(fin, &nflags, NAT_INBOUND)))
   3033      1.1  christos 		/*EMPTY*/;
   3034      1.1  christos 	else if ((fin->fin_flx & FI_FRAG) && (nat = ipf_frag_natknown(fin)))
   3035      1.1  christos 		natadd = 0;
   3036      1.1  christos 	else if ((nat = ipf_nat6_inlookup(fin, nflags|NAT_SEARCH,
   3037      1.1  christos 					  (u_int)fin->fin_p,
   3038      1.1  christos 					  &fin->fin_src6.in6, &ipa.in6))) {
   3039      1.1  christos 		nflags = nat->nat_flags;
   3040      1.1  christos 	} else if (fin->fin_off == 0) {
   3041      1.1  christos 		u_32_t hv, rmsk = 0;
   3042      1.1  christos 		i6addr_t *msk;
   3043      1.1  christos 
   3044      1.1  christos 		/*
   3045      1.1  christos 		 * If there is no current entry in the nat table for this IP#,
   3046      1.1  christos 		 * create one for it (if there is a matching rule).
   3047      1.1  christos 		 */
   3048      1.1  christos maskloop:
   3049      1.1  christos 		msk = &softn->ipf_nat6_rdr_active_masks[rmsk];
   3050      1.1  christos 		IP6_AND(&ipa, msk, &iph);
   3051      1.1  christos 		hv = NAT_HASH_FN6(&iph, 0, softn->ipf_nat_rdrrules_sz);
   3052      1.1  christos 		for (np = softn->ipf_nat_rdr_rules[hv]; np; np = np->in_rnext) {
   3053      1.1  christos 			if (np->in_ifps[0] && (np->in_ifps[0] != ifp))
   3054      1.1  christos 				continue;
   3055      1.1  christos 			if (np->in_v[0] != 6)
   3056      1.1  christos 				continue;
   3057      1.1  christos 			if (np->in_pr[0] && (np->in_pr[0] != fin->fin_p))
   3058      1.1  christos 				continue;
   3059      1.1  christos 			if ((np->in_flags & IPN_RF) && !(np->in_flags & nflags))
   3060      1.1  christos 				continue;
   3061      1.1  christos 			if (np->in_flags & IPN_FILTER) {
   3062      1.1  christos 				switch (ipf_nat6_match(fin, np))
   3063      1.1  christos 				{
   3064      1.1  christos 				case 0 :
   3065      1.1  christos 					continue;
   3066      1.1  christos 				case -1 :
   3067      1.1  christos 					rval = -1;
   3068      1.1  christos 					goto inmatchfail;
   3069      1.1  christos 				case 1 :
   3070      1.1  christos 				default :
   3071      1.1  christos 					break;
   3072      1.1  christos 				}
   3073      1.1  christos 			} else {
   3074      1.1  christos 				if (!IP6_MASKEQ(&ipa, &np->in_odstmsk6,
   3075      1.1  christos 						&np->in_odstip6)) {
   3076      1.1  christos 					continue;
   3077      1.1  christos 				}
   3078      1.1  christos 				if (np->in_odport &&
   3079      1.1  christos 				    ((np->in_dtop < dport) ||
   3080      1.1  christos 				     (dport < np->in_odport)))
   3081      1.1  christos 					continue;
   3082      1.1  christos 			}
   3083      1.1  christos 
   3084      1.1  christos #ifdef IPF_V6_PROXIES
   3085      1.1  christos 			if (np->in_plabel != -1) {
   3086      1.1  christos 				if (!appr_ok(fin, tcp, np)) {
   3087      1.1  christos 					continue;
   3088      1.1  christos 				}
   3089      1.1  christos 			}
   3090      1.1  christos #endif
   3091      1.1  christos 
   3092      1.1  christos 			if (np->in_flags & IPN_NO) {
   3093      1.1  christos 				np->in_hits++;
   3094      1.1  christos 				break;
   3095      1.1  christos 			}
   3096      1.1  christos 
   3097      1.1  christos 			MUTEX_ENTER(&softn->ipf_nat_new);
   3098      1.1  christos 			nat = ipf_nat6_add(fin, np, NULL, nflags, NAT_INBOUND);
   3099      1.1  christos 			MUTEX_EXIT(&softn->ipf_nat_new);
   3100      1.1  christos 			if (nat != NULL) {
   3101      1.1  christos 				np->in_hits++;
   3102      1.1  christos 				break;
   3103      1.1  christos 			}
   3104      1.1  christos 			natfailed = -1;
   3105      1.1  christos 		}
   3106      1.1  christos 
   3107      1.1  christos 		if ((np == NULL) && (rmsk < softn->ipf_nat6_rdr_max)) {
   3108      1.1  christos 			rmsk++;
   3109      1.1  christos 			goto maskloop;
   3110      1.1  christos 		}
   3111      1.1  christos 	}
   3112      1.1  christos 	if (nat != NULL) {
   3113      1.1  christos 		rval = ipf_nat6_in(fin, nat, natadd, nflags);
   3114      1.1  christos 		if (rval == 1) {
   3115      1.1  christos 			MUTEX_ENTER(&nat->nat_lock);
   3116      1.1  christos 			ipf_nat_update(fin, nat);
   3117      1.1  christos 			nat->nat_bytes[0] += fin->fin_plen;
   3118      1.1  christos 			nat->nat_pkts[0]++;
   3119      1.1  christos 			MUTEX_EXIT(&nat->nat_lock);
   3120      1.1  christos 		}
   3121      1.1  christos 	} else
   3122      1.1  christos 		rval = natfailed;
   3123      1.1  christos inmatchfail:
   3124      1.1  christos 	RWLOCK_EXIT(&softc->ipf_nat);
   3125      1.1  christos 
   3126      1.1  christos 	switch (rval)
   3127      1.1  christos 	{
   3128      1.1  christos 	case -1 :
   3129      1.1  christos 		if (passp != NULL) {
   3130      1.1  christos 			NBUMPSIDE6D(0, ns_drop);
   3131      1.1  christos 			*passp = FR_BLOCK;
   3132      1.3   darrenr 			fin->fin_reason = FRB_NATV6;
   3133      1.1  christos 		}
   3134      1.1  christos 		fin->fin_flx |= FI_BADNAT;
   3135      1.1  christos 		NBUMPSIDE6D(0, ns_badnat);
   3136      1.1  christos 		break;
   3137      1.1  christos 	case 0 :
   3138      1.1  christos 		NBUMPSIDE6D(0, ns_ignored);
   3139      1.1  christos 		break;
   3140      1.1  christos 	case 1 :
   3141      1.1  christos 		NBUMPSIDE6D(0, ns_translated);
   3142      1.1  christos 		break;
   3143      1.1  christos 	}
   3144      1.1  christos 	return rval;
   3145      1.1  christos }
   3146      1.1  christos 
   3147      1.1  christos 
   3148      1.1  christos /* ------------------------------------------------------------------------ */
   3149      1.1  christos /* Function:    ipf_nat6_in                                                 */
   3150      1.1  christos /* Returns:     int - -1 == packet failed NAT checks so block it,           */
   3151      1.1  christos /*                     1 == packet was successfully translated.             */
   3152      1.1  christos /* Parameters:  fin(I)    - pointer to packet information                   */
   3153      1.1  christos /*              nat(I)    - pointer to NAT structure                        */
   3154      1.1  christos /*              natadd(I) - flag indicating if it is safe to add frag cache */
   3155      1.1  christos /*              nflags(I) - NAT flags set for this packet                   */
   3156      1.1  christos /* Locks Held:   (READ)                                              */
   3157      1.1  christos /*                                                                          */
   3158      1.1  christos /* Translate a packet coming "in" on an interface.                          */
   3159      1.1  christos /* ------------------------------------------------------------------------ */
   3160      1.1  christos static int
   3161      1.2  christos ipf_nat6_in(fr_info_t *fin, nat_t *nat, int natadd, u_32_t nflags)
   3162      1.1  christos {
   3163      1.1  christos 	ipf_main_softc_t *softc = fin->fin_main_soft;
   3164      1.1  christos 	ipf_nat_softc_t *softn = softc->ipf_nat_soft;
   3165      1.1  christos 	struct icmp6_hdr *icmp6;
   3166      1.1  christos 	u_short *csump;
   3167      1.1  christos 	tcphdr_t *tcp;
   3168      1.1  christos 	ipnat_t *np;
   3169      1.1  christos 	int skip;
   3170      1.3   darrenr 	int i;
   3171      1.1  christos 
   3172      1.1  christos 	tcp = NULL;
   3173      1.1  christos 	csump = NULL;
   3174      1.1  christos 	np = nat->nat_ptr;
   3175      1.1  christos 	fin->fin_fr = nat->nat_fr;
   3176      1.1  christos 
   3177      1.1  christos 	if (np != NULL) {
   3178      1.1  christos 		if ((natadd != 0) && (fin->fin_flx & FI_FRAG))
   3179      1.1  christos 			(void) ipf_frag_natnew(softc, fin, 0, nat);
   3180      1.1  christos 
   3181      1.1  christos 	/* ------------------------------------------------------------- */
   3182      1.3   darrenr 	/* A few quick notes:                                            */
   3183      1.3   darrenr 	/*      Following are test conditions prior to calling the       */
   3184      1.3   darrenr 	/*      ipf_proxy_check routine.                                 */
   3185      1.3   darrenr 	/*                                                               */
   3186      1.3   darrenr 	/*      A NULL tcp indicates a non TCP/UDP packet.  When dealing */
   3187      1.3   darrenr 	/*      with a map rule, we attempt to match the packet's        */
   3188      1.3   darrenr 	/*      source port against in_dport, otherwise we'd compare the */
   3189      1.3   darrenr 	/*      packet's destination.                                    */
   3190      1.1  christos 	/* ------------------------------------------------------------- */
   3191      1.1  christos 		if (np->in_apr != NULL) {
   3192      1.3   darrenr 			i = ipf_proxy_check(fin, nat);
   3193      1.1  christos 			if (i == -1) {
   3194      1.3   darrenr 				NBUMPSIDE6D(0, ns_ipf_proxy_fail);
   3195      1.1  christos 				return -1;
   3196      1.1  christos 			}
   3197      1.1  christos 		}
   3198      1.1  christos 	}
   3199      1.1  christos 
   3200      1.1  christos 	ipf_sync_update(softc, SMC_NAT, fin, nat->nat_sync);
   3201      1.1  christos 
   3202      1.1  christos 	/*
   3203      1.1  christos 	 * Fix up checksums, not by recalculating them, but
   3204      1.1  christos 	 * simply computing adjustments.
   3205      1.1  christos 	 * Why only do this for some platforms on inbound packets ?
   3206      1.1  christos 	 * Because for those that it is done, IP processing is yet to happen
   3207      1.1  christos 	 * and so the IPv4 header checksum has not yet been evaluated.
   3208      1.1  christos 	 * Perhaps it should always be done for the benefit of things like
   3209      1.1  christos 	 * fast forwarding (so that it doesn't need to be recomputed) but with
   3210      1.1  christos 	 * header checksum offloading, perhaps it is a moot point.
   3211      1.1  christos 	 */
   3212      1.1  christos 
   3213      1.1  christos 	switch (nat->nat_dir)
   3214      1.1  christos 	{
   3215      1.1  christos 	case NAT_INBOUND :
   3216      1.1  christos 		if ((fin->fin_flx & FI_ICMPERR) == 0) {
   3217      1.1  christos 			fin->fin_ip6->ip6_src = nat->nat_nsrc6.in6;
   3218      1.1  christos 			fin->fin_src6 = nat->nat_nsrc6;
   3219      1.1  christos 		}
   3220      1.1  christos 		fin->fin_ip6->ip6_dst = nat->nat_ndst6.in6;
   3221      1.1  christos 		fin->fin_dst6 = nat->nat_ndst6;
   3222      1.1  christos 		break;
   3223      1.1  christos 
   3224      1.1  christos 	case NAT_OUTBOUND :
   3225      1.1  christos 		if ((fin->fin_flx & FI_ICMPERR) == 0) {
   3226      1.1  christos 			fin->fin_ip6->ip6_src = nat->nat_odst6.in6;
   3227      1.1  christos 			fin->fin_src6 = nat->nat_odst6;
   3228      1.1  christos 		}
   3229      1.1  christos 		fin->fin_ip6->ip6_dst = nat->nat_osrc6.in6;
   3230      1.1  christos 		fin->fin_dst6 = nat->nat_osrc6;
   3231      1.1  christos 		break;
   3232      1.1  christos 
   3233      1.1  christos 	case NAT_DIVERTIN :
   3234      1.1  christos 	    {
   3235      1.1  christos 		udphdr_t *uh;
   3236      1.1  christos 		ip6_t *ip6;
   3237      1.1  christos 		mb_t *m;
   3238      1.1  christos 
   3239      1.1  christos 		m = M_DUP(np->in_divmp);
   3240      1.1  christos 		if (m == NULL) {
   3241      1.1  christos 			NBUMPSIDE6D(0, ns_divert_dup);
   3242      1.1  christos 			return -1;
   3243      1.1  christos 		}
   3244      1.1  christos 
   3245      1.1  christos 		ip6 = MTOD(m, ip6_t *);
   3246      1.1  christos 		ip6->ip6_plen = htons(fin->fin_plen + sizeof(udphdr_t));
   3247      1.1  christos 
   3248      1.1  christos 		uh = (udphdr_t *)(ip6 + 1);
   3249      1.1  christos 		uh->uh_ulen = ntohs(fin->fin_plen);
   3250      1.1  christos 
   3251      1.1  christos 		PREP_MB_T(fin, m);
   3252      1.1  christos 
   3253      1.1  christos 		fin->fin_ip6 = ip6;
   3254      1.1  christos 		fin->fin_plen += sizeof(ip6_t) + 8;	/* UDP + new IPv6 hdr */
   3255      1.1  christos 		fin->fin_dlen += sizeof(ip6_t) + 8;	/* UDP + old IPv6 hdr */
   3256      1.1  christos 
   3257      1.1  christos 		nflags &= ~IPN_TCPUDPICMP;
   3258      1.1  christos 
   3259      1.1  christos 		break;
   3260      1.1  christos 	    }
   3261      1.1  christos 
   3262      1.1  christos 	case NAT_DIVERTOUT :
   3263      1.1  christos 	    {
   3264      1.1  christos 		mb_t *m;
   3265      1.1  christos 
   3266      1.1  christos 		skip = ipf_nat6_decap(fin, nat);
   3267      1.1  christos 		if (skip <= 0) {
   3268      1.1  christos 			NBUMPSIDE6D(0, ns_decap_fail);
   3269      1.1  christos 			return -1;
   3270      1.1  christos 		}
   3271      1.1  christos 
   3272      1.1  christos 		m = fin->fin_m;
   3273      1.1  christos 
   3274      1.1  christos #if defined(MENTAT) && defined(_KERNEL)
   3275      1.1  christos 		m->b_rptr += skip;
   3276      1.1  christos #else
   3277      1.1  christos 		m->m_data += skip;
   3278      1.1  christos 		m->m_len -= skip;
   3279      1.1  christos 
   3280      1.1  christos # ifdef M_PKTHDR
   3281      1.1  christos 		if (m->m_flags & M_PKTHDR)
   3282      1.1  christos 			m->m_pkthdr.len -= skip;
   3283      1.1  christos # endif
   3284      1.1  christos #endif
   3285      1.1  christos 
   3286      1.1  christos 		ipf_nat_update(fin, nat);
   3287      1.1  christos 		fin->fin_flx |= FI_NATED;
   3288      1.1  christos 		if (np != NULL && np->in_tag.ipt_num[0] != 0)
   3289      1.1  christos 			fin->fin_nattag = &np->in_tag;
   3290      1.1  christos 		return 1;
   3291      1.1  christos 		/* NOTREACHED */
   3292      1.1  christos 	    }
   3293      1.1  christos 	}
   3294      1.1  christos 	if (nflags & IPN_TCPUDP)
   3295      1.1  christos 		tcp = fin->fin_dp;
   3296      1.1  christos 
   3297      1.1  christos 	if (!(fin->fin_flx & FI_SHORT) && (fin->fin_off == 0)) {
   3298      1.1  christos 		if ((nat->nat_odport != 0) && (nflags & IPN_TCPUDP)) {
   3299      1.1  christos 			switch (nat->nat_dir)
   3300      1.1  christos 			{
   3301      1.1  christos 			case NAT_INBOUND :
   3302      1.1  christos 				tcp->th_sport = nat->nat_nsport;
   3303      1.1  christos 				fin->fin_data[0] = ntohs(nat->nat_nsport);
   3304      1.1  christos 				tcp->th_dport = nat->nat_ndport;
   3305      1.1  christos 				fin->fin_data[1] = ntohs(nat->nat_ndport);
   3306      1.1  christos 				break;
   3307      1.1  christos 
   3308      1.1  christos 			case NAT_OUTBOUND :
   3309      1.1  christos 				tcp->th_sport = nat->nat_odport;
   3310      1.1  christos 				fin->fin_data[0] = ntohs(nat->nat_odport);
   3311      1.1  christos 				tcp->th_dport = nat->nat_osport;
   3312      1.1  christos 				fin->fin_data[1] = ntohs(nat->nat_osport);
   3313      1.1  christos 				break;
   3314      1.1  christos 			}
   3315      1.1  christos 		}
   3316      1.1  christos 
   3317      1.1  christos 
   3318      1.1  christos 		if ((nat->nat_odport != 0) && (nflags & IPN_ICMPQUERY)) {
   3319      1.1  christos 			icmp6 = fin->fin_dp;
   3320      1.1  christos 
   3321      1.1  christos 			icmp6->icmp6_id = nat->nat_nicmpid;
   3322      1.1  christos 		}
   3323      1.1  christos 
   3324      1.1  christos 		csump = ipf_nat_proto(fin, nat, nflags);
   3325      1.1  christos 	}
   3326      1.1  christos 
   3327      1.1  christos 	/*
   3328      1.1  christos 	 * The above comments do not hold for layer 4 (or higher) checksums...
   3329      1.1  christos 	 */
   3330      1.1  christos 	if (csump != NULL) {
   3331      1.1  christos 		if (nat->nat_dir == NAT_OUTBOUND)
   3332      1.3   darrenr 			ipf_fix_incksum(0, csump, nat->nat_sumd[0], 0);
   3333      1.1  christos 		else
   3334      1.3   darrenr 			ipf_fix_outcksum(0, csump, nat->nat_sumd[0], 0);
   3335      1.1  christos 	}
   3336      1.1  christos 	fin->fin_flx |= FI_NATED;
   3337      1.1  christos 	if (np != NULL && np->in_tag.ipt_num[0] != 0)
   3338      1.1  christos 		fin->fin_nattag = &np->in_tag;
   3339      1.1  christos 	return 1;
   3340      1.1  christos }
   3341      1.1  christos 
   3342      1.1  christos 
   3343      1.1  christos /* ------------------------------------------------------------------------ */
   3344      1.1  christos /* Function:    ipf_nat6_newrewrite                                         */
   3345      1.1  christos /* Returns:     int - -1 == error, 0 == success (no move), 1 == success and */
   3346      1.1  christos /*                    allow rule to be moved if IPN_ROUNDR is set.          */
   3347      1.1  christos /* Parameters:  fin(I) - pointer to packet information                      */
   3348      1.1  christos /*              nat(I) - pointer to NAT entry                               */
   3349      1.1  christos /*              ni(I)  - pointer to structure with misc. information needed */
   3350      1.1  christos /*                       to create new NAT entry.                           */
   3351      1.1  christos /* Write Lock:  ipf_nat                                                     */
   3352      1.1  christos /*                                                                          */
   3353      1.1  christos /* This function is responsible for setting up an active NAT session where  */
   3354      1.1  christos /* we are changing both the source and destination parameters at the same   */
   3355      1.1  christos /* time.  The loop in here works differently to elsewhere - each iteration  */
   3356      1.1  christos /* is responsible for changing a single parameter that can be incremented.  */
   3357      1.1  christos /* So one pass may increase the source IP#, next source port, next dest. IP#*/
   3358      1.1  christos /* and the last destination port for a total of 4 iterations to try each.   */
   3359      1.1  christos /* This is done to try and exhaustively use the translation space available.*/
   3360      1.1  christos /* ------------------------------------------------------------------------ */
   3361      1.1  christos int
   3362      1.2  christos ipf_nat6_newrewrite(fr_info_t *fin, nat_t *nat, natinfo_t *nai)
   3363      1.1  christos {
   3364      1.1  christos 	int src_search = 1;
   3365      1.1  christos 	int dst_search = 1;
   3366      1.1  christos 	fr_info_t frnat;
   3367      1.1  christos 	u_32_t flags;
   3368      1.1  christos 	u_short swap;
   3369      1.1  christos 	ipnat_t *np;
   3370      1.1  christos 	nat_t *natl;
   3371      1.1  christos 	int l = 0;
   3372      1.1  christos 	int changed;
   3373      1.1  christos 
   3374      1.1  christos 	natl = NULL;
   3375      1.1  christos 	changed = -1;
   3376      1.1  christos 	np = nai->nai_np;
   3377      1.1  christos 	flags = nat->nat_flags;
   3378      1.1  christos 	bcopy((char *)fin, (char *)&frnat, sizeof(*fin));
   3379      1.1  christos 
   3380      1.1  christos 	nat->nat_hm = NULL;
   3381      1.1  christos 
   3382      1.1  christos 	do {
   3383      1.1  christos 		changed = -1;
   3384      1.1  christos 		/* TRACE (l, src_search, dst_search, np) */
   3385      1.1  christos 
   3386      1.1  christos 		if ((src_search == 0) && (np->in_spnext == 0) &&
   3387      1.1  christos 		    (dst_search == 0) && (np->in_dpnext == 0)) {
   3388      1.1  christos 			if (l > 0)
   3389      1.1  christos 				return -1;
   3390      1.1  christos 		}
   3391      1.1  christos 
   3392      1.1  christos 		/*
   3393      1.1  christos 		 * Find a new source address
   3394      1.1  christos 		 */
   3395      1.1  christos 		if (ipf_nat6_nextaddr(fin, &np->in_nsrc, &frnat.fin_src6,
   3396      1.1  christos 				 &frnat.fin_src6) == -1) {
   3397      1.1  christos 			return -1;
   3398      1.1  christos 		}
   3399      1.1  christos 
   3400      1.1  christos 		if (IP6_ISZERO(&np->in_nsrcip6) &&
   3401      1.1  christos 		    IP6_ISONES(&np->in_nsrcmsk6)) {
   3402      1.1  christos 			src_search = 0;
   3403      1.1  christos 			if (np->in_stepnext == 0)
   3404      1.1  christos 				np->in_stepnext = 1;
   3405      1.1  christos 
   3406      1.1  christos 		} else if (IP6_ISZERO(&np->in_nsrcip6) &&
   3407      1.1  christos 			   IP6_ISZERO(&np->in_nsrcmsk6)) {
   3408      1.1  christos 			src_search = 0;
   3409      1.1  christos 			if (np->in_stepnext == 0)
   3410      1.1  christos 				np->in_stepnext = 1;
   3411      1.1  christos 
   3412      1.1  christos 		} else if (IP6_ISONES(&np->in_nsrcmsk)) {
   3413      1.1  christos 			src_search = 0;
   3414      1.1  christos 			if (np->in_stepnext == 0)
   3415      1.1  christos 				np->in_stepnext = 1;
   3416      1.1  christos 
   3417      1.1  christos 		} else if (!IP6_ISONES(&np->in_nsrcmsk6)) {
   3418      1.1  christos 			if (np->in_stepnext == 0 && changed == -1) {
   3419      1.1  christos 				IP6_INC(&np->in_snip);
   3420      1.1  christos 				np->in_stepnext++;
   3421      1.1  christos 				changed = 0;
   3422      1.1  christos 			}
   3423      1.1  christos 		}
   3424      1.1  christos 
   3425      1.1  christos 		if ((flags & IPN_TCPUDPICMP) != 0) {
   3426      1.1  christos 			if (np->in_spnext != 0)
   3427      1.1  christos 				frnat.fin_data[0] = np->in_spnext;
   3428      1.1  christos 
   3429      1.1  christos 			/*
   3430      1.1  christos 			 * Standard port translation.  Select next port.
   3431      1.1  christos 			 */
   3432      1.1  christos 			if ((flags & IPN_FIXEDSPORT) != 0) {
   3433      1.1  christos 				np->in_stepnext = 2;
   3434      1.1  christos 			} else if ((np->in_stepnext == 1) &&
   3435      1.1  christos 				   (changed == -1) && (natl != NULL)) {
   3436      1.1  christos 				np->in_spnext++;
   3437      1.1  christos 				np->in_stepnext++;
   3438      1.1  christos 				changed = 1;
   3439      1.1  christos 				if (np->in_spnext > np->in_spmax)
   3440      1.1  christos 					np->in_spnext = np->in_spmin;
   3441      1.1  christos 			}
   3442      1.1  christos 		} else {
   3443      1.1  christos 			np->in_stepnext = 2;
   3444      1.1  christos 		}
   3445      1.1  christos 		np->in_stepnext &= 0x3;
   3446      1.1  christos 
   3447      1.1  christos 		/*
   3448      1.1  christos 		 * Find a new destination address
   3449      1.1  christos 		 */
   3450      1.1  christos 		/* TRACE (fin, np, l, frnat) */
   3451      1.1  christos 
   3452      1.1  christos 		if (ipf_nat6_nextaddr(fin, &np->in_ndst, &frnat.fin_dst6,
   3453      1.1  christos 				      &frnat.fin_dst6) == -1)
   3454      1.1  christos 			return -1;
   3455      1.1  christos 
   3456      1.1  christos 		if (IP6_ISZERO(&np->in_ndstip6) &&
   3457      1.1  christos 		    IP6_ISONES(&np->in_ndstmsk6)) {
   3458      1.1  christos 			dst_search = 0;
   3459      1.1  christos 			if (np->in_stepnext == 2)
   3460      1.1  christos 				np->in_stepnext = 3;
   3461      1.1  christos 
   3462      1.1  christos 		} else if (IP6_ISZERO(&np->in_ndstip6) &&
   3463      1.1  christos 			   IP6_ISZERO(&np->in_ndstmsk6)) {
   3464      1.1  christos 			dst_search = 0;
   3465      1.1  christos 			if (np->in_stepnext == 2)
   3466      1.1  christos 				np->in_stepnext = 3;
   3467      1.1  christos 
   3468      1.1  christos 		} else if (IP6_ISONES(&np->in_ndstmsk6)) {
   3469      1.1  christos 			dst_search = 0;
   3470      1.1  christos 			if (np->in_stepnext == 2)
   3471      1.1  christos 				np->in_stepnext = 3;
   3472      1.1  christos 
   3473      1.1  christos 		} else if (!IP6_ISONES(&np->in_ndstmsk6)) {
   3474      1.1  christos 			if ((np->in_stepnext == 2) && (changed == -1) &&
   3475      1.1  christos 			    (natl != NULL)) {
   3476      1.1  christos 				changed = 2;
   3477      1.1  christos 				np->in_stepnext++;
   3478      1.1  christos 				IP6_INC(&np->in_dnip6);
   3479      1.1  christos 			}
   3480      1.1  christos 		}
   3481      1.1  christos 
   3482      1.1  christos 		if ((flags & IPN_TCPUDPICMP) != 0) {
   3483      1.1  christos 			if (np->in_dpnext != 0)
   3484      1.1  christos 				frnat.fin_data[1] = np->in_dpnext;
   3485      1.1  christos 
   3486      1.1  christos 			/*
   3487      1.1  christos 			 * Standard port translation.  Select next port.
   3488      1.1  christos 			 */
   3489      1.1  christos 			if ((flags & IPN_FIXEDDPORT) != 0) {
   3490      1.1  christos 				np->in_stepnext = 0;
   3491      1.1  christos 			} else if (np->in_stepnext == 3 && changed == -1) {
   3492      1.1  christos 				np->in_dpnext++;
   3493      1.1  christos 				np->in_stepnext++;
   3494      1.1  christos 				changed = 3;
   3495      1.1  christos 				if (np->in_dpnext > np->in_dpmax)
   3496      1.1  christos 					np->in_dpnext = np->in_dpmin;
   3497      1.1  christos 			}
   3498      1.1  christos 		} else {
   3499      1.1  christos 			if (np->in_stepnext == 3)
   3500      1.1  christos 				np->in_stepnext = 0;
   3501      1.1  christos 		}
   3502      1.1  christos 
   3503      1.1  christos 		/* TRACE (frnat) */
   3504      1.1  christos 
   3505      1.1  christos 		/*
   3506      1.1  christos 		 * Here we do a lookup of the connection as seen from
   3507      1.1  christos 		 * the outside.  If an IP# pair already exists, try
   3508      1.1  christos 		 * again.  So if you have A->B becomes C->B, you can
   3509      1.1  christos 		 * also have D->E become C->E but not D->B causing
   3510      1.1  christos 		 * another C->B.  Also take protocol and ports into
   3511      1.1  christos 		 * account when determining whether a pre-existing
   3512      1.1  christos 		 * NAT setup will cause an external conflict where
   3513      1.1  christos 		 * this is appropriate.
   3514      1.1  christos 		 *
   3515      1.1  christos 		 * fin_data[] is swapped around because we are doing a
   3516      1.1  christos 		 * lookup of the packet is if it were moving in the opposite
   3517      1.1  christos 		 * direction of the one we are working with now.
   3518      1.1  christos 		 */
   3519      1.1  christos 		if (flags & IPN_TCPUDP) {
   3520      1.1  christos 			swap = frnat.fin_data[0];
   3521      1.1  christos 			frnat.fin_data[0] = frnat.fin_data[1];
   3522      1.1  christos 			frnat.fin_data[1] = swap;
   3523      1.1  christos 		}
   3524      1.1  christos 		if (fin->fin_out == 1) {
   3525      1.1  christos 			natl = ipf_nat6_inlookup(&frnat,
   3526      1.1  christos 					    flags & ~(SI_WILDP|NAT_SEARCH),
   3527      1.1  christos 					    (u_int)frnat.fin_p,
   3528      1.1  christos 					    &frnat.fin_dst6.in6,
   3529      1.1  christos 					    &frnat.fin_src6.in6);
   3530      1.1  christos 
   3531      1.1  christos 		} else {
   3532      1.1  christos 			natl = ipf_nat6_outlookup(&frnat,
   3533      1.1  christos 					     flags & ~(SI_WILDP|NAT_SEARCH),
   3534      1.1  christos 					     (u_int)frnat.fin_p,
   3535      1.1  christos 					     &frnat.fin_dst6.in6,
   3536      1.1  christos 					     &frnat.fin_src6.in6);
   3537      1.1  christos 		}
   3538      1.1  christos 		if (flags & IPN_TCPUDP) {
   3539      1.1  christos 			swap = frnat.fin_data[0];
   3540      1.1  christos 			frnat.fin_data[0] = frnat.fin_data[1];
   3541      1.1  christos 			frnat.fin_data[1] = swap;
   3542      1.1  christos 		}
   3543      1.1  christos 
   3544      1.1  christos 		/* TRACE natl, in_stepnext, l */
   3545      1.1  christos 
   3546      1.1  christos 		if ((natl != NULL) && (l > 8))	/* XXX 8 is arbitrary */
   3547      1.1  christos 			return -1;
   3548      1.1  christos 
   3549      1.1  christos 		np->in_stepnext &= 0x3;
   3550      1.1  christos 
   3551      1.1  christos 		l++;
   3552      1.1  christos 		changed = -1;
   3553      1.1  christos 	} while (natl != NULL);
   3554      1.1  christos 	nat->nat_osrc6 = fin->fin_src6;
   3555      1.1  christos 	nat->nat_odst6 = fin->fin_dst6;
   3556      1.1  christos 	nat->nat_nsrc6 = frnat.fin_src6;
   3557      1.1  christos 	nat->nat_ndst6 = frnat.fin_dst6;
   3558      1.1  christos 
   3559      1.3   darrenr 	if ((flags & IPN_TCPUDP) != 0) {
   3560      1.1  christos 		nat->nat_osport = htons(fin->fin_data[0]);
   3561      1.1  christos 		nat->nat_odport = htons(fin->fin_data[1]);
   3562      1.1  christos 		nat->nat_nsport = htons(frnat.fin_data[0]);
   3563      1.1  christos 		nat->nat_ndport = htons(frnat.fin_data[1]);
   3564      1.3   darrenr 	} else if ((flags & IPN_ICMPQUERY) != 0) {
   3565      1.3   darrenr 		nat->nat_oicmpid = fin->fin_data[1];
   3566      1.3   darrenr 		nat->nat_nicmpid = frnat.fin_data[1];
   3567      1.1  christos 	}
   3568      1.1  christos 
   3569      1.1  christos 	return 0;
   3570      1.1  christos }
   3571      1.1  christos 
   3572      1.1  christos 
   3573      1.1  christos /* ------------------------------------------------------------------------ */
   3574      1.1  christos /* Function:    ipf_nat6_newdivert                                          */
   3575      1.1  christos /* Returns:     int - -1 == error, 0 == success                             */
   3576      1.1  christos /* Parameters:  fin(I) - pointer to packet information                      */
   3577      1.1  christos /*              nat(I) - pointer to NAT entry                               */
   3578      1.1  christos /*              ni(I)  - pointer to structure with misc. information needed */
   3579      1.1  christos /*                       to create new NAT entry.                           */
   3580      1.1  christos /* Write Lock:  ipf_nat                                                     */
   3581      1.1  christos /*                                                                          */
   3582      1.3   darrenr /* Create a new NAT divert session as defined by the NAT rule.  This is     */
   3583      1.3   darrenr /* somewhat different to other NAT session creation routines because we     */
   3584      1.1  christos /* do not iterate through either port numbers or IP addresses, searching    */
   3585      1.1  christos /* for a unique mapping, however, a complimentary duplicate check is made.  */
   3586      1.1  christos /* ------------------------------------------------------------------------ */
   3587      1.1  christos int
   3588      1.2  christos ipf_nat6_newdivert(fr_info_t *fin, nat_t *nat, natinfo_t *nai)
   3589      1.1  christos {
   3590      1.1  christos 	ipf_main_softc_t *softc = fin->fin_main_soft;
   3591      1.1  christos 	ipf_nat_softc_t *softn = softc->ipf_nat_soft;
   3592      1.1  christos 	fr_info_t frnat;
   3593      1.1  christos 	ipnat_t *np;
   3594      1.1  christos 	nat_t *natl;
   3595      1.1  christos 	int p;
   3596      1.1  christos 
   3597      1.1  christos 	np = nai->nai_np;
   3598      1.1  christos 	bcopy((char *)fin, (char *)&frnat, sizeof(*fin));
   3599      1.1  christos 
   3600      1.1  christos 	nat->nat_pr[0] = 0;
   3601      1.1  christos 	nat->nat_osrc6 = fin->fin_src6;
   3602      1.1  christos 	nat->nat_odst6 = fin->fin_dst6;
   3603      1.1  christos 	nat->nat_osport = htons(fin->fin_data[0]);
   3604      1.1  christos 	nat->nat_odport = htons(fin->fin_data[1]);
   3605      1.1  christos 	frnat.fin_src6 = np->in_snip6;
   3606      1.1  christos 	frnat.fin_dst6 = np->in_dnip6;
   3607      1.1  christos 
   3608      1.1  christos 	if (np->in_redir & NAT_DIVERTUDP) {
   3609      1.1  christos 		frnat.fin_data[0] = np->in_spnext;
   3610      1.1  christos 		frnat.fin_data[1] = np->in_dpnext;
   3611      1.1  christos 		frnat.fin_flx |= FI_TCPUDP;
   3612      1.1  christos 		p = IPPROTO_UDP;
   3613      1.1  christos 	} else {
   3614      1.1  christos 		frnat.fin_flx &= ~FI_TCPUDP;
   3615      1.1  christos 		p = IPPROTO_IPIP;
   3616      1.1  christos 	}
   3617      1.1  christos 
   3618      1.1  christos 	if (fin->fin_out == 1) {
   3619      1.1  christos 		natl = ipf_nat6_inlookup(&frnat, 0, p, &frnat.fin_dst6.in6,
   3620      1.1  christos 					 &frnat.fin_src6.in6);
   3621      1.1  christos 
   3622      1.1  christos 	} else {
   3623      1.1  christos 		natl = ipf_nat6_outlookup(&frnat, 0, p, &frnat.fin_dst6.in6,
   3624      1.1  christos 					  &frnat.fin_src6.in6);
   3625      1.1  christos 	}
   3626      1.1  christos 
   3627      1.1  christos 	if (natl != NULL) {
   3628      1.1  christos 		NBUMPSIDE6D(fin->fin_out, ns_divert_exist);
   3629      1.1  christos 		return -1;
   3630      1.1  christos 	}
   3631      1.1  christos 
   3632      1.1  christos 	nat->nat_nsrc6 = frnat.fin_src6;
   3633      1.1  christos 	nat->nat_ndst6 = frnat.fin_dst6;
   3634      1.1  christos 	if (np->in_redir & NAT_DIVERTUDP) {
   3635      1.1  christos 		nat->nat_nsport = htons(frnat.fin_data[0]);
   3636      1.1  christos 		nat->nat_ndport = htons(frnat.fin_data[1]);
   3637      1.1  christos 	}
   3638      1.1  christos 	nat->nat_pr[fin->fin_out] = fin->fin_p;
   3639      1.1  christos 	nat->nat_pr[1 - fin->fin_out] = p;
   3640      1.1  christos 
   3641      1.3   darrenr 	if (np->in_redir & NAT_REDIRECT)
   3642      1.3   darrenr 		nat->nat_dir = NAT_DIVERTIN;
   3643      1.3   darrenr 	else
   3644      1.3   darrenr 		nat->nat_dir = NAT_DIVERTOUT;
   3645      1.1  christos 
   3646      1.1  christos 	return 0;
   3647      1.1  christos }
   3648      1.1  christos 
   3649      1.1  christos 
   3650      1.1  christos /* ------------------------------------------------------------------------ */
   3651      1.1  christos /* Function:    nat6_builddivertmp                                          */
   3652      1.1  christos /* Returns:     int - -1 == error, 0 == success                             */
   3653      1.1  christos /* Parameters:  np(I) - pointer to a NAT rule                               */
   3654      1.1  christos /*                                                                          */
   3655      1.3   darrenr /* For divert rules, a skeleton packet representing what will be prepended  */
   3656      1.3   darrenr /* to the real packet is created.  Even though we don't have the full       */
   3657      1.3   darrenr /* packet here, a checksum is calculated that we update later when we       */
   3658      1.1  christos /* fill in the final details.  At present a 0 checksum for UDP is being set */
   3659      1.1  christos /* here because it is expected that divert will be used for localhost.      */
   3660      1.1  christos /* ------------------------------------------------------------------------ */
   3661      1.1  christos static int
   3662      1.2  christos ipf_nat6_builddivertmp(ipf_nat_softc_t *softn, ipnat_t *np)
   3663      1.1  christos {
   3664      1.1  christos 	udphdr_t *uh;
   3665      1.1  christos 	size_t len;
   3666      1.1  christos 	ip6_t *ip6;
   3667      1.1  christos 
   3668      1.1  christos 	if ((np->in_redir & NAT_DIVERTUDP) != 0)
   3669      1.1  christos 		len = sizeof(ip6_t) + sizeof(udphdr_t);
   3670      1.1  christos 	else
   3671      1.1  christos 		len = sizeof(ip6_t);
   3672      1.1  christos 
   3673      1.1  christos 	ALLOC_MB_T(np->in_divmp, len);
   3674      1.1  christos 	if (np->in_divmp == NULL) {
   3675      1.1  christos 		ATOMIC_INCL(softn->ipf_nat_stats.ns_divert_build);
   3676      1.1  christos 		return -1;
   3677      1.1  christos 	}
   3678      1.1  christos 
   3679      1.1  christos 	/*
   3680      1.1  christos 	 * First, the header to get the packet diverted to the new destination
   3681      1.1  christos 	 */
   3682      1.1  christos 	ip6 = MTOD(np->in_divmp, ip6_t *);
   3683      1.1  christos 	ip6->ip6_vfc = 0x60;
   3684      1.1  christos 	if ((np->in_redir & NAT_DIVERTUDP) != 0)
   3685      1.1  christos 		ip6->ip6_nxt = IPPROTO_UDP;
   3686      1.1  christos 	else
   3687      1.1  christos 		ip6->ip6_nxt = IPPROTO_IPIP;
   3688      1.1  christos 	ip6->ip6_hlim = 255;
   3689      1.1  christos 	ip6->ip6_plen = 0;
   3690      1.1  christos 	ip6->ip6_src = np->in_snip6.in6;
   3691      1.1  christos 	ip6->ip6_dst = np->in_dnip6.in6;
   3692      1.1  christos 
   3693      1.1  christos 	if (np->in_redir & NAT_DIVERTUDP) {
   3694      1.1  christos 		uh = (udphdr_t *)((u_char *)ip6 + sizeof(*ip6));
   3695      1.1  christos 		uh->uh_sum = 0;
   3696      1.1  christos 		uh->uh_ulen = 8;
   3697      1.1  christos 		uh->uh_sport = htons(np->in_spnext);
   3698      1.1  christos 		uh->uh_dport = htons(np->in_dpnext);
   3699      1.1  christos 	}
   3700      1.1  christos 
   3701      1.1  christos 	return 0;
   3702      1.1  christos }
   3703      1.1  christos 
   3704      1.1  christos 
   3705      1.1  christos #define	MINDECAP	(sizeof(ip6_t) + sizeof(udphdr_t) + sizeof(ip6_t))
   3706      1.1  christos 
   3707      1.1  christos /* ------------------------------------------------------------------------ */
   3708      1.1  christos /* Function:    nat6_decap                                                  */
   3709      1.1  christos /* Returns:     int - -1 == error, 0 == success                             */
   3710      1.1  christos /* Parameters:  fin(I) - pointer to packet information                      */
   3711      1.1  christos /*              nat(I) - pointer to current NAT session                     */
   3712      1.1  christos /*                                                                          */
   3713      1.1  christos /* This function is responsible for undoing a packet's encapsulation in the */
   3714      1.1  christos /* reverse of an encap/divert rule.  After removing the outer encapsulation */
   3715      1.1  christos /* it is necessary to call ipf_makefrip() again so that the contents of 'fin'*/
   3716      1.1  christos /* match the "new" packet as it may still be used by IPFilter elsewhere.    */
   3717      1.1  christos /* We use "dir" here as the basis for some of the expectations about the    */
   3718      1.1  christos /* outer header.  If we return an error, the goal is to leave the original  */
   3719      1.1  christos /* packet information undisturbed - this falls short at the end where we'd  */
   3720      1.1  christos /* need to back a backup copy of "fin" - expensive.                         */
   3721      1.1  christos /* ------------------------------------------------------------------------ */
   3722      1.1  christos static int
   3723      1.2  christos ipf_nat6_decap(fr_info_t *fin, nat_t *nat)
   3724      1.1  christos {
   3725      1.1  christos 	ipf_main_softc_t *softc = fin->fin_main_soft;
   3726      1.1  christos 	ipf_nat_softc_t *softn = softc->ipf_nat_soft;
   3727      1.1  christos 	char *hdr;
   3728      1.1  christos 	int skip;
   3729      1.1  christos 	mb_t *m;
   3730      1.1  christos 
   3731      1.1  christos 	if ((fin->fin_flx & FI_ICMPERR) != 0) {
   3732      1.1  christos 		return 0;
   3733      1.1  christos 	}
   3734      1.1  christos 
   3735      1.1  christos 	m = fin->fin_m;
   3736      1.1  christos 	skip = fin->fin_hlen;
   3737      1.1  christos 
   3738      1.1  christos 	switch (nat->nat_dir)
   3739      1.1  christos 	{
   3740      1.1  christos 	case NAT_DIVERTIN :
   3741      1.1  christos 	case NAT_DIVERTOUT :
   3742      1.1  christos 		if (fin->fin_plen < MINDECAP)
   3743      1.1  christos 			return -1;
   3744      1.1  christos 		skip += sizeof(udphdr_t);
   3745      1.1  christos 		break;
   3746      1.1  christos 
   3747      1.1  christos 	case NAT_ENCAPIN :
   3748      1.1  christos 	case NAT_ENCAPOUT :
   3749      1.1  christos 		if (fin->fin_plen < (skip + sizeof(ip6_t)))
   3750      1.1  christos 			return -1;
   3751      1.1  christos 		break;
   3752      1.1  christos 	default :
   3753      1.1  christos 		return -1;
   3754      1.1  christos 		/* NOTREACHED */
   3755      1.1  christos 	}
   3756      1.1  christos 
   3757      1.1  christos 	/*
   3758      1.1  christos 	 * The aim here is to keep the original packet details in "fin" for
   3759      1.1  christos 	 * as long as possible so that returning with an error is for the
   3760      1.1  christos 	 * original packet and there is little undoing work to do.
   3761      1.1  christos 	 */
   3762      1.1  christos 	if (M_LEN(m) < skip + sizeof(ip6_t)) {
   3763      1.1  christos 		if (ipf_pr_pullup(fin, skip + sizeof(ip6_t)) == -1)
   3764      1.1  christos 			return -1;
   3765      1.1  christos 	}
   3766      1.1  christos 
   3767      1.1  christos 	hdr = MTOD(fin->fin_m, char *);
   3768      1.1  christos 	fin->fin_ip6 = (ip6_t *)(hdr + skip);
   3769      1.1  christos 
   3770      1.1  christos 	if (ipf_pr_pullup(fin, skip + sizeof(ip6_t)) == -1) {
   3771      1.1  christos 		NBUMPSIDE6D(fin->fin_out, ns_decap_pullup);
   3772      1.1  christos 		return -1;
   3773      1.1  christos 	}
   3774      1.1  christos 
   3775      1.1  christos 	fin->fin_hlen = sizeof(ip6_t);
   3776      1.1  christos 	fin->fin_dlen -= skip;
   3777      1.1  christos 	fin->fin_plen -= skip;
   3778      1.1  christos 	fin->fin_ipoff += skip;
   3779      1.1  christos 
   3780      1.1  christos 	if (ipf_makefrip(sizeof(ip6_t), (ip_t *)hdr, fin) == -1) {
   3781      1.1  christos 		NBUMPSIDE6D(fin->fin_out, ns_decap_bad);
   3782      1.1  christos 		return -1;
   3783      1.1  christos 	}
   3784      1.1  christos 
   3785      1.1  christos 	return skip;
   3786      1.1  christos }
   3787      1.1  christos 
   3788      1.1  christos 
   3789      1.1  christos /* ------------------------------------------------------------------------ */
   3790      1.1  christos /* Function:    nat6_nextaddr                                               */
   3791      1.1  christos /* Returns:     int - -1 == bad input (no new address),                     */
   3792      1.1  christos /*                     0 == success and dst has new address                 */
   3793      1.1  christos /* Parameters:  fin(I) - pointer to packet information                      */
   3794      1.1  christos /*              na(I)  - how to generate new address                        */
   3795      1.1  christos /*              old(I) - original address being replaced                    */
   3796      1.1  christos /*              dst(O) - where to put the new address                       */
   3797      1.1  christos /* Write Lock:  ipf_nat                                                     */
   3798      1.1  christos /*                                                                          */
   3799      1.1  christos /* This function uses the contents of the "na" structure, in combination    */
   3800      1.1  christos /* with "old" to produce a new address to store in "dst".  Not all of the   */
   3801      1.1  christos /* possible uses of "na" will result in a new address.                      */
   3802      1.1  christos /* ------------------------------------------------------------------------ */
   3803      1.1  christos static int
   3804      1.2  christos ipf_nat6_nextaddr(fr_info_t *fin, nat_addr_t *na, i6addr_t *old, i6addr_t *dst)
   3805      1.1  christos {
   3806      1.1  christos 	ipf_main_softc_t *softc = fin->fin_main_soft;
   3807      1.1  christos 	ipf_nat_softc_t *softn = softc->ipf_nat_soft;
   3808      1.1  christos 	i6addr_t newip, new;
   3809      1.1  christos 	u_32_t amin, amax;
   3810      1.1  christos 	int error;
   3811      1.1  christos 
   3812      1.1  christos 	new.i6[0] = 0;
   3813      1.1  christos 	new.i6[1] = 0;
   3814      1.1  christos 	new.i6[2] = 0;
   3815      1.1  christos 	new.i6[3] = 0;
   3816      1.1  christos 	amin = na->na_addr[0].in4.s_addr;
   3817      1.1  christos 
   3818      1.1  christos 	switch (na->na_atype)
   3819      1.1  christos 	{
   3820      1.1  christos 	case FRI_RANGE :
   3821      1.1  christos 		amax = na->na_addr[1].in4.s_addr;
   3822      1.1  christos 		break;
   3823      1.1  christos 
   3824      1.1  christos 	case FRI_NETMASKED :
   3825      1.1  christos 	case FRI_DYNAMIC :
   3826      1.1  christos 	case FRI_NORMAL :
   3827      1.1  christos 		/*
   3828      1.1  christos 		 * Compute the maximum address by adding the inverse of the
   3829      1.1  christos 		 * netmask to the minimum address.
   3830      1.1  christos 		 */
   3831      1.1  christos 		amax = ~na->na_addr[1].in4.s_addr;
   3832      1.1  christos 		amax |= amin;
   3833      1.1  christos 		break;
   3834      1.1  christos 
   3835      1.1  christos 	case FRI_LOOKUP :
   3836      1.1  christos 		break;
   3837      1.1  christos 
   3838      1.1  christos 	case FRI_BROADCAST :
   3839      1.1  christos 	case FRI_PEERADDR :
   3840      1.1  christos 	case FRI_NETWORK :
   3841      1.1  christos 	default :
   3842      1.1  christos 		return -1;
   3843      1.1  christos 	}
   3844      1.1  christos 
   3845      1.1  christos 	error = -1;
   3846      1.1  christos 	switch (na->na_function)
   3847      1.1  christos 	{
   3848      1.1  christos 	case IPLT_DSTLIST :
   3849      1.1  christos 		error = ipf_dstlist_select_node(fin, na->na_ptr, dst->i6,
   3850      1.1  christos 						NULL);
   3851      1.1  christos 		break;
   3852      1.1  christos 
   3853      1.1  christos 	case IPLT_NONE :
   3854      1.1  christos 		/*
   3855      1.1  christos 		 * 0/0 as the new address means leave it alone.
   3856      1.1  christos 		 */
   3857      1.1  christos 		if (na->na_addr[0].in4.s_addr == 0 &&
   3858      1.1  christos 		    na->na_addr[1].in4.s_addr == 0) {
   3859      1.1  christos 			new = *old;
   3860      1.1  christos 
   3861      1.1  christos 		/*
   3862      1.1  christos 		 * 0/32 means get the interface's address
   3863      1.1  christos 		 */
   3864      1.1  christos 		} else if (IP6_ISZERO(&na->na_addr[0].in6) &&
   3865      1.1  christos 			   IP6_ISONES(&na->na_addr[1].in6)) {
   3866      1.1  christos 			if (ipf_ifpaddr(softc, 6, na->na_atype,
   3867      1.1  christos 				       fin->fin_ifp, &newip, NULL) == -1) {
   3868      1.1  christos 				NBUMPSIDE6(fin->fin_out, ns_ifpaddrfail);
   3869      1.1  christos 				return -1;
   3870      1.1  christos 			}
   3871      1.1  christos 			new = newip;
   3872      1.1  christos 		} else {
   3873      1.1  christos 			new.in6 = na->na_nextip6;
   3874      1.1  christos 		}
   3875      1.1  christos 		*dst = new;
   3876      1.3   darrenr 		error = 0;
   3877      1.1  christos 		break;
   3878      1.1  christos 
   3879      1.1  christos 	default :
   3880      1.1  christos 		NBUMPSIDE6(fin->fin_out, ns_badnextaddr);
   3881      1.1  christos 		break;
   3882      1.1  christos 	}
   3883      1.1  christos 
   3884      1.1  christos 	return error;
   3885      1.1  christos }
   3886      1.1  christos 
   3887      1.1  christos 
   3888      1.1  christos /* ------------------------------------------------------------------------ */
   3889      1.1  christos /* Function:    ipf_nat6_nextaddrinit                                       */
   3890      1.1  christos /* Returns:     int - 0 == success, else error number                       */
   3891      1.1  christos /* Parameters:  na(I)      - NAT address information for generating new addr*/
   3892      1.1  christos /*              base(I)    - start of where to find strings                 */
   3893      1.1  christos /*              initial(I) - flag indicating if it is the first call for    */
   3894      1.1  christos /*                           this "na" structure.                           */
   3895      1.1  christos /*              ifp(I)     - network interface to derive address            */
   3896      1.1  christos /*                           information from.                              */
   3897      1.1  christos /*                                                                          */
   3898      1.1  christos /* This function is expected to be called in two scenarious: when a new NAT */
   3899      1.1  christos /* rule is loaded into the kernel and when the list of NAT rules is sync'd  */
   3900      1.1  christos /* up with the valid network interfaces (possibly due to them changing.)    */
   3901      1.1  christos /* To distinguish between these, the "initial" parameter is used.  If it is */
   3902      1.1  christos /* 1 then this indicates the rule has just been reloaded and 0 for when we  */
   3903      1.1  christos /* are updating information.  This difference is important because in       */
   3904      1.1  christos /* instances where we are not updating address information associated with  */
   3905      1.1  christos /* a network interface, we don't want to disturb what the "next" address to */
   3906      1.1  christos /* come out of ipf_nat6_nextaddr() will be.                                 */
   3907      1.1  christos /* ------------------------------------------------------------------------ */
   3908      1.1  christos static int
   3909      1.2  christos ipf_nat6_nextaddrinit(ipf_main_softc_t *softc, char *base, nat_addr_t *na,
   3910      1.2  christos     int initial, void *ifp)
   3911      1.1  christos {
   3912      1.1  christos 	switch (na->na_atype)
   3913      1.1  christos 	{
   3914      1.1  christos 	case FRI_LOOKUP :
   3915      1.1  christos 		if (na->na_subtype == 0) {
   3916      1.1  christos 			na->na_ptr = ipf_lookup_res_num(softc, IPL_LOGNAT,
   3917      1.1  christos 							na->na_type,
   3918      1.1  christos 							na->na_num,
   3919      1.1  christos 							&na->na_func);
   3920      1.1  christos 		} else if (na->na_subtype == 1) {
   3921      1.1  christos 			na->na_ptr = ipf_lookup_res_name(softc, IPL_LOGNAT,
   3922      1.1  christos 							 na->na_type,
   3923      1.1  christos 							 base + na->na_num,
   3924      1.1  christos 							 &na->na_func);
   3925      1.1  christos 		}
   3926      1.1  christos 		if (na->na_func == NULL) {
   3927      1.1  christos 			IPFERROR(60072);
   3928      1.1  christos 			return ESRCH;
   3929      1.1  christos 		}
   3930      1.1  christos 		if (na->na_ptr == NULL) {
   3931      1.1  christos 			IPFERROR(60073);
   3932      1.1  christos 			return ESRCH;
   3933      1.1  christos 		}
   3934      1.1  christos 		break;
   3935      1.1  christos 	case FRI_DYNAMIC :
   3936      1.1  christos 	case FRI_BROADCAST :
   3937      1.1  christos 	case FRI_NETWORK :
   3938      1.1  christos 	case FRI_NETMASKED :
   3939      1.1  christos 	case FRI_PEERADDR :
   3940      1.1  christos 		if (ifp != NULL)
   3941      1.1  christos 			(void )ipf_ifpaddr(softc, 6, na->na_atype, ifp,
   3942      1.1  christos 					   &na->na_addr[0],
   3943      1.1  christos 					   &na->na_addr[1]);
   3944      1.1  christos 		break;
   3945      1.1  christos 
   3946      1.1  christos 	case FRI_SPLIT :
   3947      1.1  christos 	case FRI_RANGE :
   3948      1.1  christos 		if (initial)
   3949      1.1  christos 			na->na_nextip6 = na->na_addr[0].in6;
   3950      1.1  christos 		break;
   3951      1.1  christos 
   3952      1.1  christos 	case FRI_NONE :
   3953      1.1  christos 		IP6_ANDASSIGN(&na->na_addr[0].in6, &na->na_addr[1].in6);
   3954      1.1  christos 		return 0;
   3955      1.1  christos 
   3956      1.1  christos 	case FRI_NORMAL :
   3957      1.1  christos 		IP6_ANDASSIGN(&na->na_addr[0].in6, &na->na_addr[1].in6);
   3958      1.1  christos 		break;
   3959      1.1  christos 
   3960      1.1  christos 	default :
   3961      1.1  christos 		IPFERROR(60074);
   3962      1.1  christos 		return EINVAL;
   3963      1.1  christos 	}
   3964      1.1  christos 
   3965      1.1  christos 	if (initial && (na->na_atype == FRI_NORMAL)) {
   3966      1.1  christos 		if (IP6_ISZERO(&na->na_addr[0].in6)) {
   3967      1.1  christos 			if (IP6_ISONES(&na->na_addr[1].in6) ||
   3968      1.1  christos 			    IP6_ISZERO(&na->na_addr[1].in6)) {
   3969      1.1  christos 				return 0;
   3970      1.1  christos 			}
   3971      1.1  christos 		}
   3972      1.1  christos 
   3973      1.1  christos 		na->na_nextip6 = na->na_addr[0].in6;
   3974      1.1  christos 		if (!IP6_ISONES(&na->na_addr[1].in6)) {
   3975      1.1  christos 			IP6_INC(&na->na_nextip6);
   3976      1.1  christos 		}
   3977      1.1  christos 	}
   3978      1.1  christos 
   3979      1.1  christos 	return 0;
   3980      1.1  christos }
   3981      1.1  christos 
   3982      1.1  christos 
   3983      1.1  christos /* ------------------------------------------------------------------------ */
   3984      1.1  christos /* Function:    ipf_nat6_icmpquerytype                                      */
   3985      1.1  christos /* Returns:     int - 1 == success, 0 == failure                            */
   3986      1.1  christos /* Parameters:  icmptype(I) - ICMP type number                              */
   3987      1.1  christos /*                                                                          */
   3988      1.1  christos /* Tests to see if the ICMP type number passed is a query/response type or  */
   3989      1.1  christos /* not.                                                                     */
   3990      1.1  christos /* ------------------------------------------------------------------------ */
   3991      1.1  christos static int
   3992      1.2  christos ipf_nat6_icmpquerytype(int icmptype)
   3993      1.1  christos {
   3994      1.1  christos 
   3995      1.1  christos 	/*
   3996      1.1  christos 	 * For the ICMP query NAT code, it is essential that both the query
   3997      1.1  christos 	 * and the reply match on the NAT rule. Because the NAT structure
   3998      1.1  christos 	 * does not keep track of the icmptype, and a single NAT structure
   3999      1.1  christos 	 * is used for all icmp types with the same src, dest and id, we
   4000      1.1  christos 	 * simply define the replies as queries as well. The funny thing is,
   4001      1.1  christos 	 * altough it seems silly to call a reply a query, this is exactly
   4002      1.1  christos 	 * as it is defined in the IPv4 specification
   4003      1.1  christos 	 */
   4004      1.1  christos 
   4005      1.1  christos 	switch (icmptype)
   4006      1.1  christos 	{
   4007      1.1  christos 
   4008      1.1  christos 	case ICMP6_ECHO_REPLY:
   4009      1.1  christos 	case ICMP6_ECHO_REQUEST:
   4010      1.1  christos 	/* route aedvertisement/solliciation is currently unsupported: */
   4011      1.1  christos 	/* it would require rewriting the ICMP data section            */
   4012      1.1  christos 	case ICMP6_MEMBERSHIP_QUERY:
   4013      1.1  christos 	case ICMP6_MEMBERSHIP_REPORT:
   4014      1.1  christos 	case ICMP6_MEMBERSHIP_REDUCTION:
   4015      1.1  christos 	case ICMP6_WRUREQUEST:
   4016      1.1  christos 	case ICMP6_WRUREPLY:
   4017      1.1  christos 	case MLD6_MTRACE_RESP:
   4018      1.1  christos 	case MLD6_MTRACE:
   4019      1.1  christos 		return 1;
   4020      1.1  christos 	default:
   4021      1.1  christos 		return 0;
   4022      1.1  christos 	}
   4023      1.1  christos }
   4024      1.1  christos #endif /* USE_INET6 */
   4025