Home | History | Annotate | Line # | Download | only in netinet
      1  1.4  christos /*	$NetBSD: ip_rpcb_pxy.c,v 1.4 2014/03/20 20:43:12 christos Exp $	*/
      2  1.1  christos 
      3  1.1  christos /*
      4  1.3   darrenr  * Copyright (C) 2002-2012 by Ryan Beasley <ryanb (at) goddamnbastard.org>
      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 /*
      9  1.1  christos  * Overview:
     10  1.1  christos  *   This is an in-kernel application proxy for Sun's RPCBIND (nee portmap)
     11  1.1  christos  *   protocol as defined in RFC1833.  It is far from complete, mostly
     12  1.1  christos  *   lacking in less-likely corner cases, but it's definitely functional.
     13  1.1  christos  *
     14  1.1  christos  *   Invocation:
     15  1.1  christos  *     rdr <int> <e_ip>/32 port <e_p> -> <i_ip> port <i_p> udp proxy rpcbu
     16  1.1  christos  *
     17  1.1  christos  *   If the host running IP Filter is the same as the RPC server, it's
     18  1.1  christos  *   perfectly legal for both the internal and external addresses and ports
     19  1.1  christos  *   to match.
     20  1.1  christos  *
     21  1.1  christos  *   When triggered by appropriate IP NAT rules, this proxy works by
     22  1.1  christos  *   examining data contained in received packets.  Requests and replies are
     23  1.1  christos  *   modified, NAT and state table entries created, etc., as necessary.
     24  1.1  christos  */
     25  1.1  christos /*
     26  1.1  christos  * TODO / NOTES
     27  1.1  christos  *
     28  1.1  christos  *   o Must implement locking to protect proxy session data.
     29  1.1  christos  *   o Fragmentation isn't supported.
     30  1.1  christos  *   o Only supports UDP.
     31  1.1  christos  *   o Doesn't support multiple RPC records in a single request.
     32  1.1  christos  *   o Errors should be more fine-grained.  (e.g., malloc failure vs.
     33  1.1  christos  *     illegal RPCB request / reply)
     34  1.1  christos  *   o Even with the limit on the total amount of recorded transactions,
     35  1.1  christos  *     should there be a timeout on transaction removal?
     36  1.1  christos  *   o There is a potential collision between cloning, wildcard NAT and
     37  1.1  christos  *     state entries.  There should be an appr_getport routine for
     38  1.1  christos  *     to avoid this.
     39  1.1  christos  *   o The enclosed hack of STREAMS support is pretty sick and most likely
     40  1.1  christos  *     broken.
     41  1.1  christos  *
     42  1.3   darrenr  *	Id: ip_rpcb_pxy.c,v 1.1.1.2 2012/07/22 13:45:34 darrenr Exp
     43  1.1  christos  */
     44  1.2  christos 
     45  1.2  christos #include <sys/cdefs.h>
     46  1.4  christos __KERNEL_RCSID(1, "$NetBSD: ip_rpcb_pxy.c,v 1.4 2014/03/20 20:43:12 christos Exp $");
     47  1.2  christos 
     48  1.1  christos #define	IPF_RPCB_PROXY
     49  1.1  christos 
     50  1.1  christos /*
     51  1.1  christos  * Function prototypes
     52  1.1  christos  */
     53  1.2  christos void	ipf_p_rpcb_main_load(void);
     54  1.2  christos void	ipf_p_rpcb_main_unload(void);
     55  1.2  christos int	ipf_p_rpcb_new(void *, fr_info_t *, ap_session_t *, nat_t *);
     56  1.2  christos void	ipf_p_rpcb_del(ipf_main_softc_t *, ap_session_t *);
     57  1.2  christos int	ipf_p_rpcb_in(void *, fr_info_t *, ap_session_t *, nat_t *);
     58  1.2  christos int	ipf_p_rpcb_out(void *, fr_info_t *, ap_session_t *, nat_t *);
     59  1.2  christos 
     60  1.2  christos static void	ipf_p_rpcb_flush(rpcb_session_t *);
     61  1.2  christos static int	ipf_p_rpcb_decodereq(fr_info_t *, nat_t *,
     62  1.2  christos 	rpcb_session_t *, rpc_msg_t *);
     63  1.2  christos static int	ipf_p_rpcb_skipauth(rpc_msg_t *, xdr_auth_t *, u_32_t **);
     64  1.2  christos static int	ipf_p_rpcb_insert(rpcb_session_t *, rpcb_xact_t *);
     65  1.2  christos static int	ipf_p_rpcb_xdrrpcb(rpc_msg_t *, u_32_t *, rpcb_args_t *);
     66  1.2  christos static int	ipf_p_rpcb_getuaddr(rpc_msg_t *, xdr_uaddr_t *,
     67  1.2  christos 	u_32_t **);
     68  1.2  christos static u_int	ipf_p_rpcb_atoi(char *);
     69  1.2  christos static int	ipf_p_rpcb_modreq(fr_info_t *, nat_t *, rpc_msg_t *,
     70  1.2  christos 	mb_t *, u_int);
     71  1.2  christos static int	ipf_p_rpcb_decoderep(fr_info_t *, nat_t *,
     72  1.2  christos 	rpcb_session_t *, rpc_msg_t *, rpcb_xact_t **);
     73  1.2  christos static rpcb_xact_t *	ipf_p_rpcb_lookup(rpcb_session_t *, u_32_t);
     74  1.2  christos static void	ipf_p_rpcb_deref(rpcb_session_t *, rpcb_xact_t *);
     75  1.2  christos static int	ipf_p_rpcb_getproto(rpc_msg_t *, xdr_proto_t *,
     76  1.2  christos 	u_32_t **);
     77  1.2  christos static int	ipf_p_rpcb_getnat(fr_info_t *, nat_t *, u_int, u_int);
     78  1.2  christos static int	ipf_p_rpcb_modv3(fr_info_t *, nat_t *, rpc_msg_t *,
     79  1.2  christos 	mb_t *, u_int);
     80  1.2  christos static int	ipf_p_rpcb_modv4(fr_info_t *, nat_t *, rpc_msg_t *,
     81  1.2  christos 	mb_t *, u_int);
     82  1.2  christos static void     ipf_p_rpcb_fixlen(fr_info_t *, int);
     83  1.1  christos 
     84  1.1  christos /*
     85  1.1  christos  * Global variables
     86  1.1  christos  */
     87  1.1  christos static	frentry_t	rpcbfr;	/* Skeleton rule for reference by entities
     88  1.1  christos 				   this proxy creates. */
     89  1.1  christos static	int	rpcbcnt;	/* Upper bound of allocated RPCB sessions. */
     90  1.1  christos 				/* XXX rpcbcnt still requires locking. */
     91  1.1  christos 
     92  1.1  christos static	int	rpcb_proxy_init = 0;
     93  1.1  christos 
     94  1.1  christos 
     95  1.1  christos /*
     96  1.1  christos  * Since rpc_msg contains only pointers, one should use this macro as a
     97  1.1  christos  * handy way to get to the goods.  (In case you're wondering about the name,
     98  1.1  christos  * this started as BYTEREF -> BREF -> B.)
     99  1.1  christos  */
    100  1.1  christos #define	B(r)	(u_32_t)ntohl(*(r))
    101  1.1  christos 
    102  1.1  christos /*
    103  1.1  christos  * Public subroutines
    104  1.1  christos  */
    105  1.1  christos 
    106  1.1  christos /* -------------------------------------------------------------------- */
    107  1.1  christos /* Function:    ipf_p_rpcb_main_load                                    */
    108  1.1  christos /* Returns:     void                                                    */
    109  1.1  christos /* Parameters:  (void)                                                  */
    110  1.1  christos /*                                                                      */
    111  1.1  christos /* Initialize the filter rule entry and session limiter.                */
    112  1.1  christos /* -------------------------------------------------------------------- */
    113  1.1  christos void
    114  1.2  christos ipf_p_rpcb_main_load(void)
    115  1.1  christos {
    116  1.1  christos 	rpcbcnt = 0;
    117  1.1  christos 
    118  1.1  christos 	bzero((char *)&rpcbfr, sizeof(rpcbfr));
    119  1.1  christos 	rpcbfr.fr_ref = 1;
    120  1.1  christos 	rpcbfr.fr_flags = FR_PASS|FR_QUICK|FR_KEEPSTATE;
    121  1.1  christos 	MUTEX_INIT(&rpcbfr.fr_lock, "ipf Sun RPCB proxy rule lock");
    122  1.1  christos 	rpcb_proxy_init = 1;
    123  1.1  christos }
    124  1.1  christos 
    125  1.1  christos /* -------------------------------------------------------------------- */
    126  1.1  christos /* Function:    ipf_p_rpcb_main_unload                                  */
    127  1.1  christos /* Returns:     void                                                    */
    128  1.1  christos /* Parameters:  (void)                                                  */
    129  1.1  christos /*                                                                      */
    130  1.1  christos /* Destroy rpcbfr's mutex to avoid a lock leak.                         */
    131  1.1  christos /* -------------------------------------------------------------------- */
    132  1.1  christos void
    133  1.2  christos ipf_p_rpcb_main_unload(void)
    134  1.1  christos {
    135  1.1  christos 	if (rpcb_proxy_init == 1) {
    136  1.1  christos 		MUTEX_DESTROY(&rpcbfr.fr_lock);
    137  1.1  christos 		rpcb_proxy_init = 0;
    138  1.1  christos 	}
    139  1.1  christos }
    140  1.1  christos 
    141  1.1  christos /* --------------------------------------------------------------------	*/
    142  1.1  christos /* Function:	ipf_p_rpcb_new						*/
    143  1.1  christos /* Returns:	int - -1 == failure, 0 == success			*/
    144  1.1  christos /* Parameters:	fin(I)	- pointer to packet information			*/
    145  1.1  christos /*		aps(I)	- pointer to proxy session structure		*/
    146  1.1  christos /*		nat(I)	- pointer to NAT session structure		*/
    147  1.1  christos /*									*/
    148  1.1  christos /* Allocate resources for per-session proxy structures.			*/
    149  1.1  christos /* --------------------------------------------------------------------	*/
    150  1.1  christos int
    151  1.2  christos ipf_p_rpcb_new(void *arg, fr_info_t *fin, ap_session_t *aps, nat_t *nat)
    152  1.1  christos {
    153  1.1  christos 	rpcb_session_t *rs;
    154  1.1  christos 
    155  1.1  christos 	nat = nat;	/* LINT */
    156  1.1  christos 
    157  1.3   darrenr 	if (fin->fin_v != 4)
    158  1.3   darrenr 		return -1;
    159  1.3   darrenr 
    160  1.1  christos 	KMALLOC(rs, rpcb_session_t *);
    161  1.1  christos 	if (rs == NULL)
    162  1.1  christos 		return(-1);
    163  1.1  christos 
    164  1.1  christos 	bzero((char *)rs, sizeof(*rs));
    165  1.1  christos 	MUTEX_INIT(&rs->rs_rxlock, "ipf Sun RPCB proxy session lock");
    166  1.1  christos 
    167  1.1  christos 	aps->aps_data = rs;
    168  1.1  christos 
    169  1.1  christos 	return(0);
    170  1.1  christos }
    171  1.1  christos 
    172  1.1  christos /* --------------------------------------------------------------------	*/
    173  1.1  christos /* Function:	ipf_p_rpcb_del						*/
    174  1.1  christos /* Returns:	void							*/
    175  1.1  christos /* Parameters:	aps(I)	- pointer to proxy session structure		*/
    176  1.1  christos /*									*/
    177  1.1  christos /* Free up a session's list of RPCB requests.				*/
    178  1.1  christos /* --------------------------------------------------------------------	*/
    179  1.1  christos void
    180  1.2  christos ipf_p_rpcb_del(ipf_main_softc_t *softc, ap_session_t *aps)
    181  1.1  christos {
    182  1.1  christos 	rpcb_session_t *rs;
    183  1.1  christos 	rs = (rpcb_session_t *)aps->aps_data;
    184  1.1  christos 
    185  1.1  christos 	MUTEX_ENTER(&rs->rs_rxlock);
    186  1.1  christos 	ipf_p_rpcb_flush(rs);
    187  1.1  christos 	MUTEX_EXIT(&rs->rs_rxlock);
    188  1.1  christos 	MUTEX_DESTROY(&rs->rs_rxlock);
    189  1.1  christos }
    190  1.1  christos 
    191  1.1  christos /* --------------------------------------------------------------------	*/
    192  1.1  christos /* Function:	ipf_p_rpcb_in						*/
    193  1.1  christos /* Returns:	int - APR_ERR(1) == drop the packet, 			*/
    194  1.1  christos /*		      APR_ERR(2) == kill the proxy session,		*/
    195  1.1  christos /*		      else change in packet length (in bytes)		*/
    196  1.1  christos /* Parameters:	fin(I)	- pointer to packet information			*/
    197  1.1  christos /*		ip(I)	- pointer to packet header			*/
    198  1.1  christos /*		aps(I)	- pointer to proxy session structure		*/
    199  1.1  christos /*		nat(I)	- pointer to NAT session structure		*/
    200  1.1  christos /*									*/
    201  1.1  christos /* Given a presumed RPCB request, perform some minor tests and pass off */
    202  1.1  christos /* for decoding.  Also pass packet off for a rewrite if necessary.	*/
    203  1.1  christos /* --------------------------------------------------------------------	*/
    204  1.1  christos int
    205  1.2  christos ipf_p_rpcb_in(void *arg, fr_info_t *fin, ap_session_t *aps, nat_t *nat)
    206  1.1  christos {
    207  1.1  christos 	rpc_msg_t rpcmsg, *rm;
    208  1.1  christos 	rpcb_session_t *rs;
    209  1.1  christos 	u_int off, dlen;
    210  1.1  christos 	mb_t *m;
    211  1.1  christos 	int rv;
    212  1.1  christos 
    213  1.1  christos 	/* Disallow fragmented or illegally short packets. */
    214  1.1  christos 	if ((fin->fin_flx & (FI_FRAG|FI_SHORT)) != 0)
    215  1.1  christos 		return(APR_ERR(1));
    216  1.1  christos 
    217  1.1  christos 	/* Perform basic variable initialization. */
    218  1.1  christos 	rs = (rpcb_session_t *)aps->aps_data;
    219  1.1  christos 
    220  1.1  christos 	m = fin->fin_m;
    221  1.1  christos 	off = (char *)fin->fin_dp - (char *)fin->fin_ip;
    222  1.1  christos 	off += sizeof(udphdr_t) + fin->fin_ipoff;
    223  1.1  christos 	dlen = fin->fin_dlen - sizeof(udphdr_t);
    224  1.1  christos 
    225  1.1  christos 	/* Disallow packets outside legal range for supported requests. */
    226  1.1  christos 	if ((dlen < RPCB_REQMIN) || (dlen > RPCB_REQMAX))
    227  1.1  christos 		return(APR_ERR(1));
    228  1.1  christos 
    229  1.1  christos 	/* Copy packet over to convenience buffer. */
    230  1.1  christos 	rm = &rpcmsg;
    231  1.1  christos 	bzero((char *)rm, sizeof(*rm));
    232  1.2  christos 	COPYDATA(m, off, dlen, (void *)&rm->rm_msgbuf);
    233  1.1  christos 	rm->rm_buflen = dlen;
    234  1.1  christos 
    235  1.1  christos 	/* Send off to decode request. */
    236  1.1  christos 	rv = ipf_p_rpcb_decodereq(fin, nat, rs, rm);
    237  1.1  christos 
    238  1.1  christos 	switch(rv)
    239  1.1  christos 	{
    240  1.1  christos 	case -1:
    241  1.1  christos 		return(APR_ERR(1));
    242  1.1  christos 		/*NOTREACHED*/
    243  1.1  christos 		break;
    244  1.1  christos 	case 0:
    245  1.1  christos 		break;
    246  1.1  christos 	case 1:
    247  1.1  christos 		rv = ipf_p_rpcb_modreq(fin, nat, rm, m, off);
    248  1.1  christos 		break;
    249  1.1  christos 	default:
    250  1.1  christos 		/*CONSTANTCONDITION*/
    251  1.1  christos 		IPF_PANIC(1, ("illegal rv %d (ipf_p_rpcb_req)", rv));
    252  1.1  christos 	}
    253  1.1  christos 
    254  1.1  christos 	return(rv);
    255  1.1  christos }
    256  1.1  christos 
    257  1.1  christos /* --------------------------------------------------------------------	*/
    258  1.1  christos /* Function:	ipf_p_rpcb_out						*/
    259  1.1  christos /* Returns:	int - APR_ERR(1) == drop the packet, 			*/
    260  1.1  christos /*		      APR_ERR(2) == kill the proxy session,		*/
    261  1.1  christos /*		      else change in packet length (in bytes)		*/
    262  1.1  christos /* Parameters:	fin(I)	- pointer to packet information			*/
    263  1.1  christos /*		ip(I)	- pointer to packet header			*/
    264  1.1  christos /*		aps(I)	- pointer to proxy session structure		*/
    265  1.1  christos /*		nat(I)	- pointer to NAT session structure		*/
    266  1.1  christos /*									*/
    267  1.1  christos /* Given a presumed RPCB reply, perform some minor tests and pass off	*/
    268  1.1  christos /* for decoding.  If the message indicates a successful request with	*/
    269  1.1  christos /* valid addressing information, create NAT and state structures to	*/
    270  1.1  christos /* allow direct communication between RPC client and server.		*/
    271  1.1  christos /* --------------------------------------------------------------------	*/
    272  1.1  christos int
    273  1.2  christos ipf_p_rpcb_out(void *arg, fr_info_t *fin, ap_session_t *aps, nat_t *nat)
    274  1.1  christos {
    275  1.1  christos 	rpc_msg_t rpcmsg, *rm;
    276  1.1  christos 	rpcb_session_t *rs;
    277  1.1  christos 	rpcb_xact_t *rx;
    278  1.1  christos 	u_int off, dlen;
    279  1.1  christos 	int rv, diff;
    280  1.1  christos 	mb_t *m;
    281  1.1  christos 
    282  1.2  christos 	rx = NULL;	/* XXX gcc */
    283  1.2  christos 
    284  1.1  christos 	/* Disallow fragmented or illegally short packets. */
    285  1.1  christos 	if ((fin->fin_flx & (FI_FRAG|FI_SHORT)) != 0)
    286  1.1  christos 		return(APR_ERR(1));
    287  1.1  christos 
    288  1.1  christos 	/* Perform basic variable initialization. */
    289  1.1  christos 	rs = (rpcb_session_t *)aps->aps_data;
    290  1.1  christos 	rx = NULL;
    291  1.1  christos 
    292  1.1  christos 	m = fin->fin_m;
    293  1.1  christos 	off = (char *)fin->fin_dp - (char *)fin->fin_ip;
    294  1.1  christos 	off += sizeof(udphdr_t) + fin->fin_ipoff;
    295  1.1  christos 	dlen = fin->fin_dlen - sizeof(udphdr_t);
    296  1.1  christos 	diff = 0;
    297  1.1  christos 
    298  1.1  christos 	/* Disallow packets outside legal range for supported requests. */
    299  1.1  christos 	if ((dlen < RPCB_REPMIN) || (dlen > RPCB_REPMAX))
    300  1.1  christos 		return(APR_ERR(1));
    301  1.1  christos 
    302  1.1  christos 	/* Copy packet over to convenience buffer. */
    303  1.1  christos 	rm = &rpcmsg;
    304  1.1  christos 	bzero((char *)rm, sizeof(*rm));
    305  1.2  christos 	COPYDATA(m, off, dlen, (void *)&rm->rm_msgbuf);
    306  1.1  christos 	rm->rm_buflen = dlen;
    307  1.1  christos 
    308  1.1  christos 	rx = NULL;		/* XXX gcc */
    309  1.1  christos 
    310  1.1  christos 	/* Send off to decode reply. */
    311  1.1  christos 	rv = ipf_p_rpcb_decoderep(fin, nat, rs, rm, &rx);
    312  1.1  christos 
    313  1.1  christos 	switch(rv)
    314  1.1  christos 	{
    315  1.1  christos 	case -1: /* Bad packet */
    316  1.1  christos                 if (rx != NULL) {
    317  1.1  christos                         MUTEX_ENTER(&rs->rs_rxlock);
    318  1.1  christos                         ipf_p_rpcb_deref(rs, rx);
    319  1.1  christos                         MUTEX_EXIT(&rs->rs_rxlock);
    320  1.1  christos                 }
    321  1.1  christos 		return(APR_ERR(1));
    322  1.1  christos 		/*NOTREACHED*/
    323  1.1  christos 		break;
    324  1.1  christos 	case  0: /* Negative reply / request rejected */
    325  1.1  christos 		break;
    326  1.1  christos 	case  1: /* Positive reply */
    327  1.1  christos 		/*
    328  1.1  christos 		 * With the IP address embedded in a GETADDR(LIST) reply,
    329  1.1  christos 		 * we'll need to rewrite the packet in the very possible
    330  1.1  christos 		 * event that the internal & external addresses aren't the
    331  1.1  christos 		 * same.  (i.e., this box is either a router or rpcbind
    332  1.1  christos 		 * only listens on loopback.)
    333  1.1  christos 		 */
    334  1.1  christos 		if (nat->nat_odstaddr != nat->nat_ndstaddr) {
    335  1.1  christos 			if (rx->rx_type == RPCB_RES_STRING)
    336  1.1  christos 				diff = ipf_p_rpcb_modv3(fin, nat, rm, m, off);
    337  1.1  christos 			else if (rx->rx_type == RPCB_RES_LIST)
    338  1.1  christos 				diff = ipf_p_rpcb_modv4(fin, nat, rm, m, off);
    339  1.1  christos 		}
    340  1.1  christos 		break;
    341  1.1  christos 	default:
    342  1.1  christos 		/*CONSTANTCONDITION*/
    343  1.1  christos 		IPF_PANIC(1, ("illegal rv %d (ipf_p_rpcb_decoderep)", rv));
    344  1.1  christos 	}
    345  1.1  christos 
    346  1.1  christos 	if (rx != NULL) {
    347  1.1  christos                 MUTEX_ENTER(&rs->rs_rxlock);
    348  1.1  christos                 /* XXX Gross hack - I'm overloading the reference
    349  1.1  christos                  * counter to deal with both threads and retransmitted
    350  1.1  christos                  * requests.  One deref signals that this thread is
    351  1.1  christos                  * finished with rx, and the other signals that we've
    352  1.1  christos                  * processed its reply.
    353  1.1  christos                  */
    354  1.1  christos                 ipf_p_rpcb_deref(rs, rx);
    355  1.1  christos                 ipf_p_rpcb_deref(rs, rx);
    356  1.1  christos                 MUTEX_EXIT(&rs->rs_rxlock);
    357  1.1  christos 	}
    358  1.1  christos 
    359  1.1  christos 	return(diff);
    360  1.1  christos }
    361  1.1  christos 
    362  1.1  christos /*
    363  1.1  christos  * Private support subroutines
    364  1.1  christos  */
    365  1.1  christos 
    366  1.1  christos /* --------------------------------------------------------------------	*/
    367  1.1  christos /* Function:	ipf_p_rpcb_flush						*/
    368  1.1  christos /* Returns:	void							*/
    369  1.1  christos /* Parameters:	rs(I)	- pointer to RPCB session structure		*/
    370  1.1  christos /*									*/
    371  1.1  christos /* Simply flushes the list of outstanding transactions, if any.		*/
    372  1.1  christos /* --------------------------------------------------------------------	*/
    373  1.1  christos static void
    374  1.2  christos ipf_p_rpcb_flush(rpcb_session_t *rs)
    375  1.1  christos {
    376  1.1  christos 	rpcb_xact_t *r1, *r2;
    377  1.1  christos 
    378  1.1  christos 	r1 = rs->rs_rxlist;
    379  1.1  christos 	if (r1 == NULL)
    380  1.1  christos 		return;
    381  1.1  christos 
    382  1.1  christos 	while (r1 != NULL) {
    383  1.1  christos 		r2 = r1;
    384  1.1  christos 		r1 = r1->rx_next;
    385  1.1  christos 		KFREE(r2);
    386  1.1  christos 	}
    387  1.1  christos }
    388  1.1  christos 
    389  1.1  christos /* --------------------------------------------------------------------	*/
    390  1.1  christos /* Function:	ipf_p_rpcb_decodereq					*/
    391  1.1  christos /* Returns:	int - -1 == bad request or critical failure,		*/
    392  1.1  christos /*		       0 == request successfully decoded,		*/
    393  1.1  christos /*		       1 == request successfully decoded; requires	*/
    394  1.1  christos /*			    address rewrite/modification		*/
    395  1.1  christos /* Parameters:	fin(I)	- pointer to packet information			*/
    396  1.1  christos /*		nat(I)	- pointer to NAT session structure		*/
    397  1.1  christos /*		rs(I)	- pointer to RPCB session structure		*/
    398  1.1  christos /*		rm(I)	- pointer to RPC message structure		*/
    399  1.1  christos /*									*/
    400  1.1  christos /* Take a presumed RPCB request, decode it, and store the results in	*/
    401  1.1  christos /* the transaction list.  If the internal target address needs to be	*/
    402  1.1  christos /* modified, store its location in ptr.					*/
    403  1.1  christos /* WARNING:  It's the responsibility of the caller to make sure there	*/
    404  1.1  christos /* is enough room in rs_buf for the basic RPC message "preamble".	*/
    405  1.1  christos /* --------------------------------------------------------------------	*/
    406  1.1  christos static int
    407  1.2  christos ipf_p_rpcb_decodereq(fr_info_t *fin, nat_t *nat, rpcb_session_t *rs,
    408  1.2  christos     rpc_msg_t *rm)
    409  1.1  christos {
    410  1.1  christos 	rpcb_args_t *ra;
    411  1.1  christos 	u_32_t xdr, *p;
    412  1.1  christos 	rpc_call_t *rc;
    413  1.1  christos 	rpcb_xact_t rx;
    414  1.1  christos 	int mod;
    415  1.1  christos 
    416  1.1  christos 	p = (u_32_t *)rm->rm_msgbuf;
    417  1.1  christos 	mod = 0;
    418  1.1  christos 
    419  1.1  christos 	bzero((char *)&rx, sizeof(rx));
    420  1.1  christos 	rc = &rm->rm_call;
    421  1.1  christos 
    422  1.1  christos 	rm->rm_xid = p;
    423  1.1  christos 	rx.rx_xid = B(p++);	/* Record this message's XID. */
    424  1.1  christos 
    425  1.1  christos 	/* Parse out and test the RPC header. */
    426  1.1  christos 	if ((B(p++) != RPCB_CALL) ||
    427  1.1  christos 	    (B(p++) != RPCB_MSG_VERSION) ||
    428  1.1  christos 	    (B(p++) != RPCB_PROG))
    429  1.1  christos 		return(-1);
    430  1.1  christos 
    431  1.1  christos 	/* Record the RPCB version and procedure. */
    432  1.1  christos 	rc->rc_vers = p++;
    433  1.1  christos 	rc->rc_proc = p++;
    434  1.1  christos 
    435  1.1  christos 	/* Bypass RPC authentication stuff. */
    436  1.1  christos 	if (ipf_p_rpcb_skipauth(rm, &rc->rc_authcred, &p) != 0)
    437  1.1  christos 		return(-1);
    438  1.1  christos 	if (ipf_p_rpcb_skipauth(rm, &rc->rc_authverf, &p) != 0)
    439  1.1  christos 		return(-1);
    440  1.1  christos 
    441  1.1  christos 	/* Compare RPCB version and procedure numbers. */
    442  1.1  christos 	switch(B(rc->rc_vers))
    443  1.1  christos 	{
    444  1.1  christos 	case 2:
    445  1.1  christos 		/* This proxy only supports PMAP_GETPORT. */
    446  1.1  christos 		if (B(rc->rc_proc) != RPCB_GETPORT)
    447  1.1  christos 			return(-1);
    448  1.1  christos 
    449  1.1  christos 		/* Portmap requests contain four 4 byte parameters. */
    450  1.1  christos 		if (RPCB_BUF_EQ(rm, p, 16) == 0)
    451  1.1  christos 			return(-1);
    452  1.1  christos 
    453  1.1  christos 		p += 2; /* Skip requested program and version numbers. */
    454  1.1  christos 
    455  1.1  christos 		/* Sanity check the requested protocol. */
    456  1.1  christos 		xdr = B(p);
    457  1.1  christos 		if (!(xdr == IPPROTO_UDP || xdr == IPPROTO_TCP))
    458  1.1  christos 			return(-1);
    459  1.1  christos 
    460  1.1  christos 		rx.rx_type = RPCB_RES_PMAP;
    461  1.1  christos 		rx.rx_proto = xdr;
    462  1.1  christos 		break;
    463  1.1  christos 	case 3:
    464  1.1  christos 	case 4:
    465  1.1  christos 		/* GETADDRLIST is exclusive to v4; GETADDR for v3 & v4 */
    466  1.1  christos 		switch(B(rc->rc_proc))
    467  1.1  christos 		{
    468  1.1  christos 		case RPCB_GETADDR:
    469  1.1  christos 			rx.rx_type = RPCB_RES_STRING;
    470  1.1  christos 			rx.rx_proto = (u_int)fin->fin_p;
    471  1.1  christos 			break;
    472  1.1  christos 		case RPCB_GETADDRLIST:
    473  1.1  christos 			if (B(rc->rc_vers) != 4)
    474  1.1  christos 				return(-1);
    475  1.1  christos 			rx.rx_type = RPCB_RES_LIST;
    476  1.1  christos 			break;
    477  1.1  christos 		default:
    478  1.1  christos 			return(-1);
    479  1.1  christos 		}
    480  1.1  christos 
    481  1.1  christos 		ra = &rc->rc_rpcbargs;
    482  1.1  christos 
    483  1.1  christos 		/* Decode the 'struct rpcb' request. */
    484  1.1  christos 		if (ipf_p_rpcb_xdrrpcb(rm, p, ra) != 0)
    485  1.1  christos 			return(-1);
    486  1.1  christos 
    487  1.1  christos 		/* Are the target address & port valid? */
    488  1.1  christos 		if ((ra->ra_maddr.xu_ip != nat->nat_ndstaddr) ||
    489  1.1  christos 		    (ra->ra_maddr.xu_port != nat->nat_ndport))
    490  1.1  christos 		    	return(-1);
    491  1.1  christos 
    492  1.1  christos 		/* Do we need to rewrite this packet? */
    493  1.1  christos 		if ((nat->nat_ndstaddr != nat->nat_odstaddr) ||
    494  1.1  christos 		    (nat->nat_ndport != nat->nat_odport))
    495  1.1  christos 		    	mod = 1;
    496  1.1  christos 		break;
    497  1.1  christos 	default:
    498  1.1  christos 		return(-1);
    499  1.1  christos 	}
    500  1.1  christos 
    501  1.1  christos         MUTEX_ENTER(&rs->rs_rxlock);
    502  1.1  christos 	if (ipf_p_rpcb_insert(rs, &rx) != 0) {
    503  1.1  christos                 MUTEX_EXIT(&rs->rs_rxlock);
    504  1.1  christos 		return(-1);
    505  1.1  christos 	}
    506  1.1  christos         MUTEX_EXIT(&rs->rs_rxlock);
    507  1.1  christos 
    508  1.1  christos 	return(mod);
    509  1.1  christos }
    510  1.1  christos 
    511  1.1  christos /* --------------------------------------------------------------------	*/
    512  1.1  christos /* Function:	ipf_p_rpcb_skipauth					*/
    513  1.1  christos /* Returns:	int -- -1 == illegal auth parameters (lengths)		*/
    514  1.1  christos /*			0 == valid parameters, pointer advanced		*/
    515  1.1  christos /* Parameters:	rm(I)	- pointer to RPC message structure		*/
    516  1.1  christos /*		auth(I)	- pointer to RPC auth structure			*/
    517  1.1  christos /*		buf(IO)	- pointer to location within convenience buffer	*/
    518  1.1  christos /*									*/
    519  1.1  christos /* Record auth data length & location of auth data, then advance past	*/
    520  1.1  christos /* it.									*/
    521  1.1  christos /* --------------------------------------------------------------------	*/
    522  1.1  christos static int
    523  1.2  christos ipf_p_rpcb_skipauth(rpc_msg_t *rm, xdr_auth_t *auth, u_32_t **buf)
    524  1.1  christos {
    525  1.1  christos 	u_32_t *p, xdr;
    526  1.1  christos 
    527  1.1  christos 	p = *buf;
    528  1.1  christos 
    529  1.1  christos 	/* Make sure we have enough space for expected fixed auth parms. */
    530  1.1  christos 	if (RPCB_BUF_GEQ(rm, p, 8) == 0)
    531  1.1  christos 		return(-1);
    532  1.1  christos 
    533  1.1  christos 	p++; /* We don't care about auth_flavor. */
    534  1.1  christos 
    535  1.1  christos 	auth->xa_string.xs_len = p;
    536  1.1  christos 	xdr = B(p++);		/* Length of auth_data */
    537  1.1  christos 
    538  1.1  christos 	/* Test for absurdity / illegality of auth_data length. */
    539  1.1  christos 	if ((XDRALIGN(xdr) < xdr) || (RPCB_BUF_GEQ(rm, p, XDRALIGN(xdr)) == 0))
    540  1.1  christos 		return(-1);
    541  1.1  christos 
    542  1.1  christos 	auth->xa_string.xs_str = (char *)p;
    543  1.1  christos 
    544  1.1  christos 	p += XDRALIGN(xdr);	/* Advance our location. */
    545  1.1  christos 
    546  1.1  christos 	*buf = (u_32_t *)p;
    547  1.1  christos 
    548  1.1  christos 	return(0);
    549  1.1  christos }
    550  1.1  christos 
    551  1.1  christos /* --------------------------------------------------------------------	*/
    552  1.1  christos /* Function:	ipf_p_rpcb_insert					*/
    553  1.1  christos /* Returns:	int -- -1 == list insertion failed,			*/
    554  1.1  christos /*			0 == item successfully added			*/
    555  1.1  christos /* Parameters:	rs(I)	- pointer to RPCB session structure		*/
    556  1.1  christos /*		rx(I)	- pointer to RPCB transaction structure		*/
    557  1.1  christos /* --------------------------------------------------------------------	*/
    558  1.1  christos static int
    559  1.2  christos ipf_p_rpcb_insert(rpcb_session_t *rs, rpcb_xact_t *rx)
    560  1.1  christos {
    561  1.1  christos 	rpcb_xact_t *rxp;
    562  1.1  christos 
    563  1.1  christos 	rxp = ipf_p_rpcb_lookup(rs, rx->rx_xid);
    564  1.1  christos 	if (rxp != NULL) {
    565  1.1  christos                 ++rxp->rx_ref;
    566  1.1  christos 		return(0);
    567  1.1  christos         }
    568  1.1  christos 
    569  1.1  christos 	if (rpcbcnt == RPCB_MAXREQS)
    570  1.1  christos 		return(-1);
    571  1.1  christos 
    572  1.1  christos 	KMALLOC(rxp, rpcb_xact_t *);
    573  1.1  christos 	if (rxp == NULL)
    574  1.1  christos 		return(-1);
    575  1.1  christos 
    576  1.1  christos 	bcopy((char *)rx, (char *)rxp, sizeof(*rx));
    577  1.1  christos 
    578  1.1  christos 	if (rs->rs_rxlist != NULL)
    579  1.1  christos 		rs->rs_rxlist->rx_pnext = &rxp->rx_next;
    580  1.1  christos 
    581  1.1  christos 	rxp->rx_pnext = &rs->rs_rxlist;
    582  1.1  christos 	rxp->rx_next = rs->rs_rxlist;
    583  1.1  christos 	rs->rs_rxlist = rxp;
    584  1.1  christos 
    585  1.1  christos 	rxp->rx_ref = 1;
    586  1.1  christos 
    587  1.1  christos 	++rpcbcnt;
    588  1.1  christos 
    589  1.1  christos 	return(0);
    590  1.1  christos }
    591  1.1  christos 
    592  1.1  christos /* --------------------------------------------------------------------	*/
    593  1.1  christos /* Function:	ipf_p_rpcb_xdrrpcb					*/
    594  1.1  christos /* Returns:	int -- -1 == failure to properly decode the request	*/
    595  1.1  christos /*			0 == rpcb successfully decoded			*/
    596  1.1  christos /* Parameters:	rs(I)	- pointer to RPCB session structure		*/
    597  1.1  christos /*		p(I)	- pointer to location within session buffer	*/
    598  1.1  christos /*		rpcb(O)	- pointer to rpcb (xdr type) structure		*/
    599  1.1  christos /*									*/
    600  1.1  christos /* Decode a XDR encoded rpcb structure and record its contents in rpcb  */
    601  1.1  christos /* within only the context of TCP/UDP over IP networks.			*/
    602  1.1  christos /* --------------------------------------------------------------------	*/
    603  1.1  christos static int
    604  1.2  christos ipf_p_rpcb_xdrrpcb(rpc_msg_t *rm, u_32_t *p, rpcb_args_t *ra)
    605  1.1  christos {
    606  1.1  christos 	if (!RPCB_BUF_GEQ(rm, p, 20))
    607  1.1  christos 		return(-1);
    608  1.1  christos 
    609  1.1  christos 	/* Bypass target program & version. */
    610  1.1  christos 	p += 2;
    611  1.1  christos 
    612  1.1  christos 	/* Decode r_netid.  Must be "tcp" or "udp". */
    613  1.1  christos 	if (ipf_p_rpcb_getproto(rm, &ra->ra_netid, &p) != 0)
    614  1.1  christos 		return(-1);
    615  1.1  christos 
    616  1.1  christos 	/* Decode r_maddr. */
    617  1.1  christos 	if (ipf_p_rpcb_getuaddr(rm, &ra->ra_maddr, &p) != 0)
    618  1.1  christos 		return(-1);
    619  1.1  christos 
    620  1.1  christos 	/* Advance to r_owner and make sure it's empty. */
    621  1.1  christos 	if (!RPCB_BUF_EQ(rm, p, 4) || (B(p) != 0))
    622  1.1  christos 		return(-1);
    623  1.1  christos 
    624  1.1  christos 	return(0);
    625  1.1  christos }
    626  1.1  christos 
    627  1.1  christos /* --------------------------------------------------------------------	*/
    628  1.1  christos /* Function:	ipf_p_rpcb_getuaddr					*/
    629  1.1  christos /* Returns:	int -- -1 == illegal string,				*/
    630  1.1  christos /*			0 == string parsed; contents recorded		*/
    631  1.1  christos /* Parameters:	rm(I)	- pointer to RPC message structure		*/
    632  1.1  christos /*		xu(I)	- pointer to universal address structure	*/
    633  1.1  christos /*		p(IO)	- pointer to location within message buffer	*/
    634  1.1  christos /*									*/
    635  1.1  christos /* Decode the IP address / port at p and record them in xu.		*/
    636  1.1  christos /* --------------------------------------------------------------------	*/
    637  1.1  christos static int
    638  1.2  christos ipf_p_rpcb_getuaddr(rpc_msg_t *rm, xdr_uaddr_t *xu, u_32_t **p)
    639  1.1  christos {
    640  1.1  christos 	char *c, *i, *b, *pp;
    641  1.1  christos 	u_int d, dd, l, t;
    642  1.1  christos 	char uastr[24];
    643  1.1  christos 
    644  1.1  christos 	/* Test for string length. */
    645  1.1  christos 	if (!RPCB_BUF_GEQ(rm, *p, 4))
    646  1.1  christos 		return(-1);
    647  1.1  christos 
    648  1.1  christos 	xu->xu_xslen = (*p)++;
    649  1.1  christos 	xu->xu_xsstr = (char *)*p;
    650  1.1  christos 
    651  1.1  christos 	/* Length check */
    652  1.1  christos 	l = B(xu->xu_xslen);
    653  1.1  christos 	if (l < 11 || l > 23 || !RPCB_BUF_GEQ(rm, *p, XDRALIGN(l)))
    654  1.1  christos 		return(-1);
    655  1.1  christos 
    656  1.1  christos 	/* Advance p */
    657  1.1  christos 	*(char **)p += XDRALIGN(l);
    658  1.1  christos 
    659  1.1  christos 	/* Copy string to local buffer & terminate C style */
    660  1.1  christos 	bcopy(xu->xu_xsstr, uastr, l);
    661  1.1  christos 	uastr[l] = '\0';
    662  1.1  christos 
    663  1.1  christos 	i = (char *)&xu->xu_ip;
    664  1.1  christos 	pp = (char *)&xu->xu_port;
    665  1.1  christos 
    666  1.1  christos 	/*
    667  1.1  christos 	 * Expected format: a.b.c.d.e.f where [a-d] correspond to bytes of
    668  1.1  christos 	 * an IP address and [ef] are the bytes of a L4 port.
    669  1.1  christos 	 */
    670  1.1  christos 	if (!(ISDIGIT(uastr[0]) && ISDIGIT(uastr[l-1])))
    671  1.1  christos 		return(-1);
    672  1.1  christos 	b = uastr;
    673  1.1  christos 	for (c = &uastr[1], d = 0, dd = 0; c < &uastr[l-1]; c++) {
    674  1.1  christos 		if (ISDIGIT(*c)) {
    675  1.1  christos 			dd = 0;
    676  1.1  christos 			continue;
    677  1.1  christos 		}
    678  1.1  christos 		if (*c == '.') {
    679  1.1  christos 			if (dd != 0)
    680  1.1  christos 				return(-1);
    681  1.1  christos 
    682  1.1  christos 			/* Check for ASCII byte. */
    683  1.1  christos 			*c = '\0';
    684  1.1  christos 			t = ipf_p_rpcb_atoi(b);
    685  1.1  christos 			if (t > 255)
    686  1.1  christos 				return(-1);
    687  1.1  christos 
    688  1.1  christos 			/* Aim b at beginning of the next byte. */
    689  1.1  christos 			b = c + 1;
    690  1.1  christos 
    691  1.1  christos 			/* Switch off IP addr vs port parsing. */
    692  1.1  christos 			if (d < 4)
    693  1.1  christos 				i[d++] = t & 0xff;
    694  1.1  christos 			else
    695  1.1  christos 				pp[d++ - 4] = t & 0xff;
    696  1.1  christos 
    697  1.1  christos 			dd = 1;
    698  1.1  christos 			continue;
    699  1.1  christos 		}
    700  1.1  christos 		return(-1);
    701  1.1  christos 	}
    702  1.1  christos 	if (d != 5) /* String must contain exactly 5 periods. */
    703  1.1  christos 		return(-1);
    704  1.1  christos 
    705  1.1  christos 	/* Handle the last byte (port low byte) */
    706  1.1  christos 	t = ipf_p_rpcb_atoi(b);
    707  1.1  christos 	if (t > 255)
    708  1.1  christos 		return(-1);
    709  1.1  christos 	pp[d - 4] = t & 0xff;
    710  1.1  christos 
    711  1.1  christos 	return(0);
    712  1.1  christos }
    713  1.1  christos 
    714  1.1  christos /* --------------------------------------------------------------------	*/
    715  1.1  christos /* Function:	ipf_p_rpcb_atoi (XXX should be generic for all proxies)	*/
    716  1.1  christos /* Returns:	int -- integer representation of supplied string	*/
    717  1.1  christos /* Parameters:	ptr(I)	- input string					*/
    718  1.1  christos /*									*/
    719  1.1  christos /* Simple version of atoi(3) ripped from ip_rcmd_pxy.c.			*/
    720  1.1  christos /* --------------------------------------------------------------------	*/
    721  1.1  christos static u_int
    722  1.2  christos ipf_p_rpcb_atoi(char *ptr)
    723  1.1  christos {
    724  1.2  christos 	char *s = ptr, c;
    725  1.2  christos 	u_int i = 0;
    726  1.1  christos 
    727  1.1  christos 	while (((c = *s++) != '\0') && ISDIGIT(c)) {
    728  1.1  christos 		i *= 10;
    729  1.1  christos 		i += c - '0';
    730  1.1  christos 	}
    731  1.1  christos 	return i;
    732  1.1  christos }
    733  1.1  christos 
    734  1.1  christos /* --------------------------------------------------------------------	*/
    735  1.1  christos /* Function:	ipf_p_rpcb_modreq					*/
    736  1.1  christos /* Returns:	int -- change in datagram length			*/
    737  1.1  christos /*			APR_ERR(2) - critical failure			*/
    738  1.1  christos /* Parameters:	fin(I)	- pointer to packet information			*/
    739  1.1  christos /*		nat(I)	- pointer to NAT session			*/
    740  1.1  christos /*		rm(I)	- pointer to RPC message structure		*/
    741  1.1  christos /*		m(I)	- pointer to mbuf chain				*/
    742  1.1  christos /*		off(I)	- current offset within mbuf chain		*/
    743  1.1  christos /*									*/
    744  1.1  christos /* When external and internal addresses differ, we rewrite the former	*/
    745  1.1  christos /* with the latter.  (This is exclusive to protocol versions 3 & 4).	*/
    746  1.1  christos /* --------------------------------------------------------------------	*/
    747  1.1  christos static int
    748  1.2  christos ipf_p_rpcb_modreq(fr_info_t *fin, nat_t *nat, rpc_msg_t *rm, mb_t *m, u_int off)
    749  1.1  christos {
    750  1.1  christos 	u_int len, xlen, pos, bogo;
    751  1.1  christos 	rpcb_args_t *ra;
    752  1.1  christos 	char uaddr[24];
    753  1.1  christos 	udphdr_t *udp;
    754  1.1  christos 	char *i, *p;
    755  1.1  christos 	int diff;
    756  1.1  christos 
    757  1.1  christos 	ra = &rm->rm_call.rc_rpcbargs;
    758  1.1  christos 	i = (char *)&nat->nat_odstaddr;
    759  1.1  christos 	p = (char *)&nat->nat_odport;
    760  1.1  christos 
    761  1.1  christos 	/* Form new string. */
    762  1.1  christos 	bzero(uaddr, sizeof(uaddr)); /* Just in case we need padding. */
    763  1.4  christos 	snprintf(uaddr, sizeof(uaddr),
    764  1.2  christos 		       "%u.%u.%u.%u.%u.%u", i[0] & 0xff, i[1] & 0xff,
    765  1.2  christos 		       i[2] & 0xff, i[3] & 0xff, p[0] & 0xff, p[1] & 0xff);
    766  1.1  christos 	len = strlen(uaddr);
    767  1.1  christos 	xlen = XDRALIGN(len);
    768  1.1  christos 
    769  1.1  christos 	/* Determine mbuf offset to start writing to. */
    770  1.1  christos 	pos = (char *)ra->ra_maddr.xu_xslen - rm->rm_msgbuf;
    771  1.1  christos 	off += pos;
    772  1.1  christos 
    773  1.1  christos 	/* Write new string length. */
    774  1.1  christos 	bogo = htonl(len);
    775  1.2  christos 	COPYBACK(m, off, 4, (void *)&bogo);
    776  1.1  christos 	off += 4;
    777  1.1  christos 
    778  1.1  christos 	/* Write new string. */
    779  1.1  christos 	COPYBACK(m, off, xlen, uaddr);
    780  1.1  christos 	off += xlen;
    781  1.1  christos 
    782  1.1  christos 	/* Write in zero r_owner. */
    783  1.1  christos 	bogo = 0;
    784  1.2  christos 	COPYBACK(m, off, 4, (void *)&bogo);
    785  1.1  christos 
    786  1.1  christos 	/* Determine difference in data lengths. */
    787  1.1  christos 	diff = xlen - XDRALIGN(B(ra->ra_maddr.xu_xslen));
    788  1.1  christos 
    789  1.1  christos 	/*
    790  1.1  christos 	 * If our new string has a different length, make necessary
    791  1.1  christos 	 * adjustments.
    792  1.1  christos 	 */
    793  1.1  christos 	if (diff != 0) {
    794  1.1  christos 		udp = fin->fin_dp;
    795  1.1  christos 		udp->uh_ulen = htons(ntohs(udp->uh_ulen) + diff);
    796  1.1  christos 		fin->fin_plen += diff;
    797  1.1  christos 		fin->fin_ip->ip_len = htons(fin->fin_plen);
    798  1.1  christos 		fin->fin_dlen += diff;
    799  1.1  christos 		/* XXX Storage lengths. */
    800  1.1  christos 	}
    801  1.1  christos 
    802  1.1  christos 	return(diff);
    803  1.1  christos }
    804  1.1  christos 
    805  1.1  christos /* --------------------------------------------------------------------	*/
    806  1.1  christos /* Function:	ipf_p_rpcb_decoderep					*/
    807  1.1  christos /* Returns:	int - -1 == bad request or critical failure,		*/
    808  1.1  christos /*		       0 == valid, negative reply			*/
    809  1.1  christos /*		       1 == vaddlid, positive reply; needs no changes	*/
    810  1.1  christos /* Parameters:	fin(I)	- pointer to packet information			*/
    811  1.1  christos /*		nat(I)	- pointer to NAT session structure		*/
    812  1.1  christos /*		rs(I)	- pointer to RPCB session structure		*/
    813  1.1  christos /*		rm(I)	- pointer to RPC message structure		*/
    814  1.1  christos /*		rxp(O)	- pointer to RPCB transaction structure		*/
    815  1.1  christos /*									*/
    816  1.1  christos /* Take a presumed RPCB reply, extract the XID, search for the original */
    817  1.1  christos /* request information, and determine whether the request was accepted	*/
    818  1.1  christos /* or rejected.  With a valid accepted reply, go ahead and create NAT	*/
    819  1.1  christos /* and state entries, and finish up by rewriting the packet as 		*/
    820  1.1  christos /* required.								*/
    821  1.1  christos /*									*/
    822  1.1  christos /* WARNING:  It's the responsibility of the caller to make sure there	*/
    823  1.1  christos /* is enough room in rs_buf for the basic RPC message "preamble".	*/
    824  1.1  christos /* --------------------------------------------------------------------	*/
    825  1.1  christos static int
    826  1.2  christos ipf_p_rpcb_decoderep(fr_info_t *fin, nat_t *nat, rpcb_session_t *rs,
    827  1.2  christos     rpc_msg_t *rm, rpcb_xact_t **rxp)
    828  1.1  christos {
    829  1.1  christos 	rpcb_listp_t *rl;
    830  1.1  christos 	rpcb_entry_t *re;
    831  1.1  christos 	rpcb_xact_t *rx;
    832  1.1  christos 	u_32_t xdr, *p;
    833  1.1  christos 	rpc_resp_t *rr;
    834  1.1  christos 	int rv, cnt;
    835  1.1  christos 
    836  1.1  christos 	p = (u_32_t *)rm->rm_msgbuf;
    837  1.1  christos 
    838  1.1  christos 	bzero((char *)&rx, sizeof(rx));
    839  1.1  christos 	rr = &rm->rm_resp;
    840  1.1  christos 
    841  1.1  christos 	rm->rm_xid = p;
    842  1.1  christos 	xdr = B(p++);		/* Record this message's XID. */
    843  1.1  christos 
    844  1.1  christos 	/* Lookup XID */
    845  1.1  christos         MUTEX_ENTER(&rs->rs_rxlock);
    846  1.1  christos 	if ((rx = ipf_p_rpcb_lookup(rs, xdr)) == NULL) {
    847  1.1  christos                 MUTEX_EXIT(&rs->rs_rxlock);
    848  1.1  christos 		return(-1);
    849  1.1  christos         }
    850  1.1  christos         ++rx->rx_ref;        /* per thread reference */
    851  1.1  christos         MUTEX_EXIT(&rs->rs_rxlock);
    852  1.1  christos 
    853  1.1  christos 	*rxp = rx;
    854  1.1  christos 
    855  1.1  christos 	/* Test call vs reply */
    856  1.1  christos 	if (B(p++) != RPCB_REPLY)
    857  1.1  christos 		return(-1);
    858  1.1  christos 
    859  1.1  christos 	/* Test reply_stat */
    860  1.1  christos 	switch(B(p++))
    861  1.1  christos 	{
    862  1.1  christos 	case RPCB_MSG_DENIED:
    863  1.1  christos 		return(0);
    864  1.1  christos 	case RPCB_MSG_ACCEPTED:
    865  1.1  christos 		break;
    866  1.1  christos 	default:
    867  1.1  christos 		return(-1);
    868  1.1  christos 	}
    869  1.1  christos 
    870  1.1  christos 	/* Bypass RPC authentication stuff. */
    871  1.1  christos 	if (ipf_p_rpcb_skipauth(rm, &rr->rr_authverf, &p) != 0)
    872  1.1  christos 		return(-1);
    873  1.1  christos 
    874  1.1  christos 	/* Test accept status */
    875  1.1  christos 	if (!RPCB_BUF_GEQ(rm, p, 4))
    876  1.1  christos 		return(-1);
    877  1.1  christos 	if (B(p++) != 0)
    878  1.1  christos 		return(0);
    879  1.1  christos 
    880  1.1  christos 	/* Parse out the expected reply */
    881  1.1  christos 	switch(rx->rx_type)
    882  1.1  christos 	{
    883  1.1  christos 	case RPCB_RES_PMAP:
    884  1.1  christos 		/* There must be only one 4 byte argument. */
    885  1.1  christos 		if (!RPCB_BUF_EQ(rm, p, 4))
    886  1.1  christos 			return(-1);
    887  1.1  christos 
    888  1.1  christos 		rr->rr_v2 = p;
    889  1.1  christos 		xdr = B(rr->rr_v2);
    890  1.1  christos 
    891  1.1  christos 		/* Reply w/ a 0 port indicates service isn't registered */
    892  1.1  christos 		if (xdr == 0)
    893  1.1  christos 			return(0);
    894  1.1  christos 
    895  1.1  christos 		/* Is the value sane? */
    896  1.1  christos 		if (xdr > 65535)
    897  1.1  christos 			return(-1);
    898  1.1  christos 
    899  1.1  christos 		/* Create NAT & state table entries. */
    900  1.1  christos 		if (ipf_p_rpcb_getnat(fin, nat, rx->rx_proto, (u_int)xdr) != 0)
    901  1.1  christos 			return(-1);
    902  1.1  christos 		break;
    903  1.1  christos 	case RPCB_RES_STRING:
    904  1.1  christos 		/* Expecting a XDR string; need 4 bytes for length */
    905  1.1  christos 		if (!RPCB_BUF_GEQ(rm, p, 4))
    906  1.1  christos 			return(-1);
    907  1.1  christos 
    908  1.1  christos 		rr->rr_v3.xu_str.xs_len = p++;
    909  1.1  christos 		rr->rr_v3.xu_str.xs_str = (char *)p;
    910  1.1  christos 
    911  1.1  christos 		xdr = B(rr->rr_v3.xu_xslen);
    912  1.1  christos 
    913  1.1  christos 		/* A null string indicates an unregistered service */
    914  1.1  christos 		if ((xdr == 0) && RPCB_BUF_EQ(rm, p, 0))
    915  1.1  christos 			return(0);
    916  1.1  christos 
    917  1.1  christos 		/* Decode the target IP address / port. */
    918  1.1  christos 		if (ipf_p_rpcb_getuaddr(rm, &rr->rr_v3, &p) != 0)
    919  1.1  christos 			return(-1);
    920  1.1  christos 
    921  1.1  christos 		/* Validate the IP address and port contained. */
    922  1.1  christos 		if (nat->nat_odstaddr != rr->rr_v3.xu_ip)
    923  1.1  christos 			return(-1);
    924  1.1  christos 
    925  1.1  christos 		/* Create NAT & state table entries. */
    926  1.1  christos 		if (ipf_p_rpcb_getnat(fin, nat, rx->rx_proto,
    927  1.1  christos 				     (u_int)rr->rr_v3.xu_port) != 0)
    928  1.1  christos 			return(-1);
    929  1.1  christos 		break;
    930  1.1  christos 	case RPCB_RES_LIST:
    931  1.1  christos 		if (!RPCB_BUF_GEQ(rm, p, 4))
    932  1.1  christos 			return(-1);
    933  1.1  christos 		/* rpcb_entry_list_ptr */
    934  1.1  christos 		switch(B(p))
    935  1.1  christos 		{
    936  1.1  christos 		case 0:
    937  1.1  christos 			return(0);
    938  1.1  christos 			/*NOTREACHED*/
    939  1.1  christos 			break;
    940  1.1  christos 		case 1:
    941  1.1  christos 			break;
    942  1.1  christos 		default:
    943  1.1  christos 			return(-1);
    944  1.1  christos 		}
    945  1.1  christos 		rl = &rr->rr_v4;
    946  1.1  christos 		rl->rl_list = p++;
    947  1.1  christos 		cnt = 0;
    948  1.1  christos 
    949  1.1  christos 		for(;;) {
    950  1.1  christos 			re = &rl->rl_entries[rl->rl_cnt];
    951  1.1  christos 			if (ipf_p_rpcb_getuaddr(rm, &re->re_maddr, &p) != 0)
    952  1.1  christos 				return(-1);
    953  1.1  christos 			if (ipf_p_rpcb_getproto(rm, &re->re_netid, &p) != 0)
    954  1.1  christos 				return(-1);
    955  1.1  christos 			/* re_semantics & re_pfamily length */
    956  1.1  christos 			if (!RPCB_BUF_GEQ(rm, p, 12))
    957  1.1  christos 				return(-1);
    958  1.1  christos 			p++; /* Skipping re_semantics. */
    959  1.1  christos 			xdr = B(p++);
    960  1.1  christos 			if ((xdr != 4) || strncmp((char *)p, "inet", 4))
    961  1.1  christos 				return(-1);
    962  1.1  christos 			p++;
    963  1.1  christos 			if (ipf_p_rpcb_getproto(rm, &re->re_proto, &p) != 0)
    964  1.1  christos 				return(-1);
    965  1.1  christos 			if (!RPCB_BUF_GEQ(rm, p, 4))
    966  1.1  christos 				return(-1);
    967  1.1  christos 			re->re_more = p;
    968  1.1  christos 			if (B(re->re_more) > 1) /* 0,1 only legal values */
    969  1.1  christos 				return(-1);
    970  1.1  christos 			++rl->rl_cnt;
    971  1.1  christos 			++cnt;
    972  1.1  christos 			if (B(re->re_more) == 0)
    973  1.1  christos 				break;
    974  1.1  christos 			/* Replies in  max out at 2; TCP and/or UDP */
    975  1.1  christos 			if (cnt > 2)
    976  1.1  christos 				return(-1);
    977  1.1  christos 			p++;
    978  1.1  christos 		}
    979  1.1  christos 
    980  1.1  christos 		for(rl->rl_cnt = 0; rl->rl_cnt < cnt; rl->rl_cnt++) {
    981  1.1  christos 			re = &rl->rl_entries[rl->rl_cnt];
    982  1.1  christos 			rv = ipf_p_rpcb_getnat(fin, nat,
    983  1.1  christos 			                      re->re_proto.xp_proto,
    984  1.1  christos 				              (u_int)re->re_maddr.xu_port);
    985  1.1  christos 			if (rv != 0)
    986  1.1  christos 				return(-1);
    987  1.1  christos 		}
    988  1.1  christos 		break;
    989  1.1  christos 	default:
    990  1.1  christos 		/*CONSTANTCONDITION*/
    991  1.1  christos 		IPF_PANIC(1, ("illegal rx_type %d", rx->rx_type));
    992  1.1  christos 	}
    993  1.1  christos 
    994  1.1  christos 	return(1);
    995  1.1  christos }
    996  1.1  christos 
    997  1.1  christos /* --------------------------------------------------------------------	*/
    998  1.1  christos /* Function:	ipf_p_rpcb_lookup					*/
    999  1.1  christos /* Returns:	rpcb_xact_t * 	- NULL == no matching record,		*/
   1000  1.1  christos /*				  else pointer to relevant entry	*/
   1001  1.1  christos /* Parameters:	rs(I)	- pointer to RPCB session			*/
   1002  1.1  christos /*		xid(I)	- XID to look for				*/
   1003  1.1  christos /* --------------------------------------------------------------------	*/
   1004  1.1  christos static rpcb_xact_t *
   1005  1.2  christos ipf_p_rpcb_lookup(rpcb_session_t *rs, u_32_t xid)
   1006  1.1  christos {
   1007  1.1  christos 	rpcb_xact_t *rx;
   1008  1.1  christos 
   1009  1.1  christos 	if (rs->rs_rxlist == NULL)
   1010  1.1  christos 		return(NULL);
   1011  1.1  christos 
   1012  1.1  christos 	for (rx = rs->rs_rxlist; rx != NULL; rx = rx->rx_next)
   1013  1.1  christos 		if (rx->rx_xid == xid)
   1014  1.1  christos 			break;
   1015  1.1  christos 
   1016  1.1  christos 	return(rx);
   1017  1.1  christos }
   1018  1.1  christos 
   1019  1.1  christos /* --------------------------------------------------------------------	*/
   1020  1.1  christos /* Function:	ipf_p_rpcb_deref					        */
   1021  1.1  christos /* Returns:	(void)							*/
   1022  1.1  christos /* Parameters:	rs(I)	- pointer to RPCB session			*/
   1023  1.1  christos /*		rx(I)	- pointer to RPC transaction struct to remove	*/
   1024  1.1  christos /*              force(I) - indicates to delete entry regardless of      */
   1025  1.1  christos /*                         reference count                              */
   1026  1.1  christos /* Locking:	rs->rs_rxlock must be held write only			*/
   1027  1.1  christos /*									*/
   1028  1.1  christos /* Free the RPCB transaction record rx from the chain of entries.	*/
   1029  1.1  christos /* --------------------------------------------------------------------	*/
   1030  1.1  christos static void
   1031  1.2  christos ipf_p_rpcb_deref(rpcb_session_t *rs, rpcb_xact_t *rx)
   1032  1.1  christos {
   1033  1.1  christos 	rs = rs;	/* LINT */
   1034  1.1  christos 
   1035  1.1  christos 	if (rx == NULL)
   1036  1.1  christos 		return;
   1037  1.1  christos 
   1038  1.1  christos 	if (--rx->rx_ref != 0)
   1039  1.1  christos 		return;
   1040  1.1  christos 
   1041  1.1  christos 	if (rx->rx_next != NULL)
   1042  1.1  christos 		rx->rx_next->rx_pnext = rx->rx_pnext;
   1043  1.1  christos 
   1044  1.1  christos 	*rx->rx_pnext = rx->rx_next;
   1045  1.1  christos 
   1046  1.1  christos 	KFREE(rx);
   1047  1.1  christos 
   1048  1.1  christos 	--rpcbcnt;
   1049  1.1  christos }
   1050  1.1  christos 
   1051  1.1  christos /* --------------------------------------------------------------------	*/
   1052  1.1  christos /* Function:	ipf_p_rpcb_getproto					*/
   1053  1.1  christos /* Returns:	int - -1 == illegal protocol/netid,			*/
   1054  1.1  christos /*		       0 == legal protocol/netid			*/
   1055  1.1  christos /* Parameters:	rm(I)	- pointer to RPC message structure		*/
   1056  1.1  christos /*		xp(I)	- pointer to netid structure			*/
   1057  1.1  christos /*		p(IO)	- pointer to location within packet buffer	*/
   1058  1.1  christos /* 									*/
   1059  1.1  christos /* Decode netid/proto stored at p and record its numeric value.	 	*/
   1060  1.1  christos /* --------------------------------------------------------------------	*/
   1061  1.1  christos static int
   1062  1.2  christos ipf_p_rpcb_getproto(rpc_msg_t *rm, xdr_proto_t *xp, u_32_t **p)
   1063  1.1  christos {
   1064  1.1  christos 	u_int len;
   1065  1.1  christos 
   1066  1.1  christos 	/* Must have 4 bytes for length & 4 bytes for "tcp" or "udp". */
   1067  1.1  christos 	if (!RPCB_BUF_GEQ(rm, p, 8))
   1068  1.1  christos 		return(-1);
   1069  1.1  christos 
   1070  1.1  christos 	xp->xp_xslen = (*p)++;
   1071  1.1  christos 	xp->xp_xsstr = (char *)*p;
   1072  1.1  christos 
   1073  1.1  christos 	/* Test the string length. */
   1074  1.1  christos 	len = B(xp->xp_xslen);
   1075  1.1  christos 	if (len != 3)
   1076  1.1  christos 	 	return(-1);
   1077  1.1  christos 
   1078  1.1  christos 	/* Test the actual string & record the protocol accordingly. */
   1079  1.1  christos 	if (!strncmp((char *)xp->xp_xsstr, "tcp\0", 4))
   1080  1.1  christos 		xp->xp_proto = IPPROTO_TCP;
   1081  1.1  christos 	else if (!strncmp((char *)xp->xp_xsstr, "udp\0", 4))
   1082  1.1  christos 		xp->xp_proto = IPPROTO_UDP;
   1083  1.1  christos 	else {
   1084  1.1  christos 		return(-1);
   1085  1.1  christos 	}
   1086  1.1  christos 
   1087  1.1  christos 	/* Advance past the string. */
   1088  1.1  christos 	(*p)++;
   1089  1.1  christos 
   1090  1.1  christos 	return(0);
   1091  1.1  christos }
   1092  1.1  christos 
   1093  1.1  christos /* --------------------------------------------------------------------	*/
   1094  1.1  christos /* Function:	ipf_p_rpcb_getnat					*/
   1095  1.1  christos /* Returns:	int -- -1 == failed to create table entries,		*/
   1096  1.1  christos /*			0 == success					*/
   1097  1.1  christos /* Parameters:	fin(I)	- pointer to packet information			*/
   1098  1.1  christos /*		nat(I)	- pointer to NAT table entry			*/
   1099  1.1  christos /*		proto(I) - transport protocol for new entries		*/
   1100  1.1  christos /*		port(I)	- new port to use w/ wildcard table entries	*/
   1101  1.1  christos /*									*/
   1102  1.1  christos /* Create state and NAT entries to handle an anticipated connection	*/
   1103  1.1  christos /* attempt between RPC client and server.				*/
   1104  1.1  christos /* --------------------------------------------------------------------	*/
   1105  1.1  christos static int
   1106  1.2  christos ipf_p_rpcb_getnat(fr_info_t *fin, nat_t *nat, u_int proto, u_int port)
   1107  1.1  christos {
   1108  1.1  christos 	ipf_main_softc_t *softc = fin->fin_main_soft;
   1109  1.1  christos 	ipnat_t *ipn, ipnat;
   1110  1.1  christos 	tcphdr_t tcp;
   1111  1.1  christos 	ipstate_t *is;
   1112  1.1  christos 	fr_info_t fi;
   1113  1.1  christos 	nat_t *natl;
   1114  1.1  christos 	int nflags;
   1115  1.1  christos 
   1116  1.1  christos 	ipn = nat->nat_ptr;
   1117  1.1  christos 
   1118  1.1  christos 	/* Generate dummy fr_info */
   1119  1.1  christos 	bcopy((char *)fin, (char *)&fi, sizeof(fi));
   1120  1.1  christos 	fi.fin_out = 0;
   1121  1.1  christos 	fi.fin_p = proto;
   1122  1.1  christos 	fi.fin_sport = 0;
   1123  1.1  christos 	fi.fin_dport = port & 0xffff;
   1124  1.1  christos 	fi.fin_flx |= FI_IGNORE;
   1125  1.1  christos 	fi.fin_saddr = nat->nat_osrcaddr;
   1126  1.1  christos 	fi.fin_daddr = nat->nat_odstaddr;
   1127  1.1  christos 
   1128  1.1  christos 	bzero((char *)&tcp, sizeof(tcp));
   1129  1.1  christos 	tcp.th_dport = htons(port);
   1130  1.1  christos 
   1131  1.1  christos 	if (proto == IPPROTO_TCP) {
   1132  1.1  christos 		tcp.th_win = htons(8192);
   1133  1.1  christos 		TCP_OFF_A(&tcp, sizeof(tcphdr_t) >> 2);
   1134  1.1  christos 		fi.fin_dlen = sizeof(tcphdr_t);
   1135  1.1  christos 		tcp.th_flags = TH_SYN;
   1136  1.1  christos 		nflags = NAT_TCP;
   1137  1.1  christos 	} else {
   1138  1.1  christos 		fi.fin_dlen = sizeof(udphdr_t);
   1139  1.1  christos 		nflags = NAT_UDP;
   1140  1.1  christos 	}
   1141  1.1  christos 
   1142  1.1  christos 	nflags |= SI_W_SPORT|NAT_SEARCH;
   1143  1.1  christos 	fi.fin_dp = &tcp;
   1144  1.1  christos 	fi.fin_plen = fi.fin_hlen + fi.fin_dlen;
   1145  1.1  christos 
   1146  1.1  christos 	/*
   1147  1.1  christos 	 * Search for existing NAT & state entries.  Pay close attention to
   1148  1.1  christos 	 * mutexes / locks grabbed from lookup routines, as not doing so could
   1149  1.1  christos 	 * lead to bad things.
   1150  1.1  christos 	 *
   1151  1.1  christos 	 * If successful, fr_stlookup returns with ipf_state locked.  We have
   1152  1.1  christos 	 * no use for this lock, so simply unlock it if necessary.
   1153  1.1  christos 	 */
   1154  1.1  christos 	is = ipf_state_lookup(&fi, &tcp, NULL);
   1155  1.1  christos 	if (is != NULL) {
   1156  1.1  christos 		RWLOCK_EXIT(&softc->ipf_state);
   1157  1.1  christos 	}
   1158  1.1  christos 
   1159  1.1  christos 	RWLOCK_EXIT(&softc->ipf_nat);
   1160  1.1  christos 
   1161  1.1  christos 	WRITE_ENTER(&softc->ipf_nat);
   1162  1.1  christos 	natl = ipf_nat_inlookup(&fi, nflags, proto, fi.fin_src, fi.fin_dst);
   1163  1.1  christos 
   1164  1.1  christos 	if ((natl != NULL) && (is != NULL)) {
   1165  1.1  christos 		MUTEX_DOWNGRADE(&softc->ipf_nat);
   1166  1.1  christos 		return(0);
   1167  1.1  christos 	}
   1168  1.1  christos 
   1169  1.1  christos 	/* Slightly modify the following structures for actual use in creating
   1170  1.1  christos 	 * NAT and/or state entries.  We're primarily concerned with stripping
   1171  1.1  christos 	 * flags that may be detrimental to the creation process or simply
   1172  1.1  christos 	 * shouldn't be associated with a table entry.
   1173  1.1  christos 	 */
   1174  1.1  christos 	fi.fin_fr = &rpcbfr;
   1175  1.1  christos 	fi.fin_flx &= ~FI_IGNORE;
   1176  1.1  christos 	nflags &= ~NAT_SEARCH;
   1177  1.1  christos 
   1178  1.1  christos 	if (natl == NULL) {
   1179  1.1  christos #ifdef USE_MUTEXES
   1180  1.1  christos 		ipf_nat_softc_t *softn = softc->ipf_nat_soft;
   1181  1.1  christos #endif
   1182  1.1  christos 
   1183  1.1  christos 		/* XXX Since we're just copying the original ipn contents
   1184  1.1  christos 		 * back, would we be better off just sending a pointer to
   1185  1.1  christos 		 * the 'temp' copy off to nat_new instead?
   1186  1.1  christos 		 */
   1187  1.1  christos 		/* Generate template/bogus NAT rule. */
   1188  1.1  christos 		bcopy((char *)ipn, (char *)&ipnat, sizeof(ipnat));
   1189  1.1  christos 		ipn->in_flags = nflags & IPN_TCPUDP;
   1190  1.1  christos 		ipn->in_apr = NULL;
   1191  1.1  christos 		ipn->in_pr[0] = proto;
   1192  1.1  christos 		ipn->in_pr[1] = proto;
   1193  1.1  christos 		ipn->in_dpmin = fi.fin_dport;
   1194  1.1  christos 		ipn->in_dpmax = fi.fin_dport;
   1195  1.1  christos 		ipn->in_dpnext = fi.fin_dport;
   1196  1.1  christos 		ipn->in_space = 1;
   1197  1.1  christos 		ipn->in_ippip = 1;
   1198  1.1  christos 		if (ipn->in_flags & IPN_FILTER) {
   1199  1.1  christos 			ipn->in_scmp = 0;
   1200  1.1  christos 			ipn->in_dcmp = 0;
   1201  1.1  christos 		}
   1202  1.1  christos 		ipn->in_plabel = -1;
   1203  1.1  christos 
   1204  1.1  christos 		/* Create NAT entry.  return NULL if this fails. */
   1205  1.1  christos 		MUTEX_ENTER(&softn->ipf_nat_new);
   1206  1.1  christos 		natl = ipf_nat_add(&fi, ipn, NULL, nflags|SI_CLONE|NAT_SLAVE,
   1207  1.1  christos 			       NAT_INBOUND);
   1208  1.1  christos 		MUTEX_EXIT(&softn->ipf_nat_new);
   1209  1.1  christos 
   1210  1.1  christos 		bcopy((char *)&ipnat, (char *)ipn, sizeof(ipnat));
   1211  1.1  christos 
   1212  1.1  christos 		if (natl == NULL) {
   1213  1.1  christos 			MUTEX_DOWNGRADE(&softc->ipf_nat);
   1214  1.1  christos 			return(-1);
   1215  1.1  christos 		}
   1216  1.1  christos 
   1217  1.3   darrenr 		natl->nat_ptr = ipn;
   1218  1.1  christos 		fi.fin_saddr = natl->nat_nsrcaddr;
   1219  1.1  christos 		fi.fin_daddr = natl->nat_ndstaddr;
   1220  1.1  christos 		ipn->in_use++;
   1221  1.1  christos 		(void) ipf_nat_proto(&fi, natl, nflags);
   1222  1.1  christos 		MUTEX_ENTER(&natl->nat_lock);
   1223  1.1  christos 		ipf_nat_update(&fi, natl);
   1224  1.1  christos 		MUTEX_EXIT(&natl->nat_lock);
   1225  1.1  christos 	}
   1226  1.1  christos 	MUTEX_DOWNGRADE(&softc->ipf_nat);
   1227  1.1  christos 
   1228  1.1  christos 	if (is == NULL) {
   1229  1.1  christos 		/* Create state entry.  Return NULL if this fails. */
   1230  1.1  christos 		fi.fin_flx |= FI_NATED;
   1231  1.1  christos 		fi.fin_flx &= ~FI_STATE;
   1232  1.1  christos 		nflags &= NAT_TCPUDP;
   1233  1.1  christos 		nflags |= SI_W_SPORT|SI_CLONE;
   1234  1.1  christos 
   1235  1.1  christos 		if (ipf_state_add(softc, &fi, NULL, nflags) != 0) {
   1236  1.1  christos 			/*
   1237  1.1  christos 			 * XXX nat_delete is private to ip_nat.c.  Should
   1238  1.1  christos 			 * check w/ Darren about this one.
   1239  1.1  christos 			 *
   1240  1.1  christos 			 * nat_delete(natl, NL_EXPIRE);
   1241  1.1  christos 			 */
   1242  1.1  christos 			return(-1);
   1243  1.1  christos 		}
   1244  1.1  christos 	}
   1245  1.1  christos 
   1246  1.1  christos 	return(0);
   1247  1.1  christos }
   1248  1.1  christos 
   1249  1.1  christos /* --------------------------------------------------------------------	*/
   1250  1.1  christos /* Function:	ipf_p_rpcb_modv3						*/
   1251  1.1  christos /* Returns:	int -- change in packet length				*/
   1252  1.1  christos /* Parameters:	fin(I)	- pointer to packet information			*/
   1253  1.1  christos /*		nat(I)	- pointer to NAT session			*/
   1254  1.1  christos /*		rm(I)	- pointer to RPC message structure		*/
   1255  1.1  christos /*		m(I)	- pointer to mbuf chain				*/
   1256  1.1  christos /*		off(I)	- offset within mbuf chain			*/
   1257  1.1  christos /*									*/
   1258  1.1  christos /* Write a new universal address string to this packet, adjusting	*/
   1259  1.1  christos /* lengths as necessary.						*/
   1260  1.1  christos /* --------------------------------------------------------------------	*/
   1261  1.1  christos static int
   1262  1.2  christos ipf_p_rpcb_modv3(fr_info_t *fin, nat_t *nat, rpc_msg_t *rm, mb_t *m, u_int off)
   1263  1.1  christos {
   1264  1.1  christos 	u_int len, xlen, pos, bogo;
   1265  1.1  christos 	rpc_resp_t *rr;
   1266  1.1  christos 	char uaddr[24];
   1267  1.1  christos 	char *i, *p;
   1268  1.1  christos 	int diff;
   1269  1.1  christos 
   1270  1.1  christos 	rr = &rm->rm_resp;
   1271  1.1  christos 	i = (char *)&nat->nat_ndstaddr;
   1272  1.1  christos 	p = (char *)&rr->rr_v3.xu_port;
   1273  1.1  christos 
   1274  1.1  christos 	/* Form new string. */
   1275  1.1  christos 	bzero(uaddr, sizeof(uaddr)); /* Just in case we need padding. */
   1276  1.4  christos 	snprintf(uaddr, sizeof(uaddr),
   1277  1.1  christos 		       "%u.%u.%u.%u.%u.%u", i[0] & 0xff, i[1] & 0xff,
   1278  1.1  christos 		       i[2] & 0xff, i[3] & 0xff, p[0] & 0xff, p[1] & 0xff);
   1279  1.1  christos 	len = strlen(uaddr);
   1280  1.1  christos 	xlen = XDRALIGN(len);
   1281  1.1  christos 
   1282  1.1  christos 	/* Determine mbuf offset to write to. */
   1283  1.1  christos 	pos = (char *)rr->rr_v3.xu_xslen - rm->rm_msgbuf;
   1284  1.1  christos 	off += pos;
   1285  1.1  christos 
   1286  1.1  christos 	/* Write new string length. */
   1287  1.1  christos 	bogo = htonl(len);
   1288  1.2  christos 	COPYBACK(m, off, 4, (void *)&bogo);
   1289  1.1  christos 	off += 4;
   1290  1.1  christos 
   1291  1.1  christos 	/* Write new string. */
   1292  1.1  christos 	COPYBACK(m, off, xlen, uaddr);
   1293  1.1  christos 
   1294  1.1  christos 	/* Determine difference in data lengths. */
   1295  1.1  christos 	diff = xlen - XDRALIGN(B(rr->rr_v3.xu_xslen));
   1296  1.1  christos 
   1297  1.1  christos 	/*
   1298  1.1  christos 	 * If our new string has a different length, make necessary
   1299  1.1  christos 	 * adjustments.
   1300  1.1  christos 	 */
   1301  1.1  christos 	if (diff != 0)
   1302  1.1  christos 		ipf_p_rpcb_fixlen(fin, diff);
   1303  1.1  christos 
   1304  1.1  christos 	return(diff);
   1305  1.1  christos }
   1306  1.1  christos 
   1307  1.1  christos /* --------------------------------------------------------------------	*/
   1308  1.1  christos /* Function:	ipf_p_rpcb_modv4						*/
   1309  1.1  christos /* Returns:	int -- change in packet length				*/
   1310  1.1  christos /* Parameters:	fin(I)	- pointer to packet information			*/
   1311  1.1  christos /*		nat(I)	- pointer to NAT session			*/
   1312  1.1  christos /*		rm(I)	- pointer to RPC message structure		*/
   1313  1.1  christos /*		m(I)	- pointer to mbuf chain				*/
   1314  1.1  christos /*		off(I)	- offset within mbuf chain			*/
   1315  1.1  christos /*									*/
   1316  1.1  christos /* Write new rpcb_entry list, adjusting	lengths as necessary.		*/
   1317  1.1  christos /* --------------------------------------------------------------------	*/
   1318  1.1  christos static int
   1319  1.2  christos ipf_p_rpcb_modv4(fr_info_t *fin, nat_t *nat, rpc_msg_t *rm, mb_t *m, u_int off)
   1320  1.1  christos {
   1321  1.1  christos 	u_int len, xlen, pos, bogo;
   1322  1.1  christos 	rpcb_listp_t *rl;
   1323  1.1  christos 	rpcb_entry_t *re;
   1324  1.1  christos 	rpc_resp_t *rr;
   1325  1.1  christos 	char uaddr[24];
   1326  1.1  christos 	int diff, cnt;
   1327  1.1  christos 	char *i, *p;
   1328  1.1  christos 
   1329  1.1  christos 	diff = 0;
   1330  1.1  christos 	rr = &rm->rm_resp;
   1331  1.1  christos 	rl = &rr->rr_v4;
   1332  1.1  christos 
   1333  1.1  christos 	i = (char *)&nat->nat_ndstaddr;
   1334  1.1  christos 
   1335  1.1  christos 	/* Determine mbuf offset to write to. */
   1336  1.1  christos 	re = &rl->rl_entries[0];
   1337  1.1  christos 	pos = (char *)re->re_maddr.xu_xslen - rm->rm_msgbuf;
   1338  1.1  christos 	off += pos;
   1339  1.1  christos 
   1340  1.1  christos 	for (cnt = 0; cnt < rl->rl_cnt; cnt++) {
   1341  1.1  christos 		re = &rl->rl_entries[cnt];
   1342  1.1  christos 		p = (char *)&re->re_maddr.xu_port;
   1343  1.1  christos 
   1344  1.1  christos 		/* Form new string. */
   1345  1.1  christos 		bzero(uaddr, sizeof(uaddr)); /* Just in case we need
   1346  1.1  christos 						padding. */
   1347  1.4  christos 		snprintf(uaddr, sizeof(uaddr),
   1348  1.2  christos 			       "%u.%u.%u.%u.%u.%u", i[0] & 0xff,
   1349  1.2  christos 			       i[1] & 0xff, i[2] & 0xff, i[3] & 0xff,
   1350  1.2  christos 			       p[0] & 0xff, p[1] & 0xff);
   1351  1.1  christos 		len = strlen(uaddr);
   1352  1.1  christos 		xlen = XDRALIGN(len);
   1353  1.1  christos 
   1354  1.1  christos 		/* Write new string length. */
   1355  1.1  christos 		bogo = htonl(len);
   1356  1.2  christos 		COPYBACK(m, off, 4, (void *)&bogo);
   1357  1.1  christos 		off += 4;
   1358  1.1  christos 
   1359  1.1  christos 		/* Write new string. */
   1360  1.1  christos 		COPYBACK(m, off, xlen, uaddr);
   1361  1.1  christos 		off += xlen;
   1362  1.1  christos 
   1363  1.1  christos 		/* Record any change in length. */
   1364  1.1  christos 		diff += xlen - XDRALIGN(B(re->re_maddr.xu_xslen));
   1365  1.1  christos 
   1366  1.1  christos 		/* If the length changed, copy back the rest of this entry. */
   1367  1.1  christos 		len = ((char *)re->re_more + 4) -
   1368  1.1  christos 		       (char *)re->re_netid.xp_xslen;
   1369  1.1  christos 		if (diff != 0) {
   1370  1.2  christos 			COPYBACK(m, off, len, (void *)re->re_netid.xp_xslen);
   1371  1.1  christos 		}
   1372  1.1  christos 		off += len;
   1373  1.1  christos 	}
   1374  1.1  christos 
   1375  1.1  christos 	/*
   1376  1.1  christos 	 * If our new string has a different length, make necessary
   1377  1.1  christos 	 * adjustments.
   1378  1.1  christos 	 */
   1379  1.1  christos 	if (diff != 0)
   1380  1.1  christos 		ipf_p_rpcb_fixlen(fin, diff);
   1381  1.1  christos 
   1382  1.1  christos 	return(diff);
   1383  1.1  christos }
   1384  1.1  christos 
   1385  1.1  christos 
   1386  1.1  christos /* --------------------------------------------------------------------	*/
   1387  1.1  christos /* Function:    ipf_p_rpcb_fixlen                                        */
   1388  1.1  christos /* Returns:     (void)                                                  */
   1389  1.1  christos /* Parameters:  fin(I)  - pointer to packet information                 */
   1390  1.1  christos /*              len(I)  - change in packet length                       */
   1391  1.1  christos /*                                                                      */
   1392  1.1  christos /* Adjust various packet related lengths held in structure and packet   */
   1393  1.1  christos /* header fields.                                                       */
   1394  1.1  christos /* --------------------------------------------------------------------	*/
   1395  1.1  christos static void
   1396  1.2  christos ipf_p_rpcb_fixlen(fr_info_t *fin, int len)
   1397  1.1  christos {
   1398  1.1  christos         udphdr_t *udp;
   1399  1.1  christos 
   1400  1.1  christos         udp = fin->fin_dp;
   1401  1.1  christos         udp->uh_ulen = htons(ntohs(udp->uh_ulen) + len);
   1402  1.1  christos         fin->fin_plen += len;
   1403  1.1  christos         fin->fin_ip->ip_len = htons(fin->fin_plen);
   1404  1.1  christos         fin->fin_dlen += len;
   1405  1.1  christos }
   1406  1.1  christos 
   1407  1.1  christos #undef B
   1408