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