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