Home | History | Annotate | Line # | Download | only in libsa
rpc.c revision 1.6
      1 /*	$NetBSD: rpc.c,v 1.6 1995/07/03 02:59:16 gwr Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1992 Regents of the University of California.
      5  * All rights reserved.
      6  *
      7  * This software was developed by the Computer Systems Engineering group
      8  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
      9  * contributed to Berkeley.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the University of
     22  *	California, Lawrence Berkeley Laboratory and its contributors.
     23  * 4. Neither the name of the University nor the names of its contributors
     24  *    may be used to endorse or promote products derived from this software
     25  *    without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     37  * SUCH DAMAGE.
     38  *
     39  * @(#) Header: rpc.c,v 1.12 93/09/28 08:31:56 leres Exp  (LBL)
     40  */
     41 
     42 /*
     43  * RPC functions used by NFS and bootparams.
     44  * Note that bootparams requires the ability to find out the
     45  * address of the server from which its response has come.
     46  * This is supported by keeping the IP/UDP headers in the
     47  * buffer space provided by the caller.  (See rpc_fromaddr)
     48  */
     49 
     50 #include <sys/param.h>
     51 #include <sys/socket.h>
     52 
     53 #include <netinet/in.h>
     54 #include <netinet/in_systm.h>
     55 
     56 #include <nfs/rpcv2.h>
     57 #include <nfs/nfsv2.h>
     58 #include <nfs/xdr_subs.h>
     59 
     60 #include <string.h>
     61 
     62 #include "stand.h"
     63 #include "net.h"
     64 #include "netif.h"
     65 #include "rpc.h"
     66 
     67 struct auth_info {
     68 	int32_t 	authtype;	/* auth type */
     69 	u_int32_t	authlen;	/* auth length */
     70 };
     71 
     72 struct auth_unix {
     73 	int32_t   ua_time;
     74 	int32_t   ua_hostname;	/* null */
     75 	int32_t   ua_uid;
     76 	int32_t   ua_gid;
     77 	int32_t   ua_gidlist;	/* null */
     78 };
     79 
     80 struct rpc_call {
     81 	u_int32_t	rp_xid;		/* request transaction id */
     82 	int32_t 	rp_direction;	/* call direction (0) */
     83 	u_int32_t	rp_rpcvers;	/* rpc version (2) */
     84 	u_int32_t	rp_prog;	/* program */
     85 	u_int32_t	rp_vers;	/* version */
     86 	u_int32_t	rp_proc;	/* procedure */
     87 };
     88 
     89 struct rpc_reply {
     90 	u_int32_t	rp_xid;		/* request transaction id */
     91 	int32_t 	rp_direction;	/* call direction (1) */
     92 	int32_t 	rp_astatus;	/* accept status (0: accepted) */
     93 	union {
     94 		u_int32_t	rpu_errno;
     95 		struct {
     96 			struct auth_info rok_auth;
     97 			u_int32_t	rok_status;
     98 		} rpu_rok;
     99 	} rp_u;
    100 };
    101 
    102 /* Local forwards */
    103 static	size_t recvrpc __P((struct iodesc *, void *, size_t, time_t));
    104 
    105 int rpc_xid;
    106 int rpc_port = 0x400;	/* predecrement */
    107 
    108 /*
    109  * Make a rpc call; return length of answer
    110  * Note: Caller must leave room for headers.
    111  */
    112 size_t
    113 rpc_call(d, prog, vers, proc, sdata, slen, rdata, rlen)
    114 	register struct iodesc *d;
    115 	register n_long prog, vers, proc;
    116 	register void *sdata;
    117 	register size_t slen;
    118 	register void *rdata;
    119 	register size_t rlen;
    120 {
    121 	register size_t cc;
    122 	struct auth_info *auth;
    123 	struct rpc_call *call;
    124 	struct rpc_reply *reply;
    125 	void *send_head, *send_tail;
    126 	void *recv_head, *recv_tail;
    127 	n_long x;
    128 
    129 #ifdef RPC_DEBUG
    130 	if (debug)
    131 		printf("rpc_call: prog=0x%x vers=%d proc=%d\n",
    132 			   prog, vers, proc);
    133 #endif
    134 
    135 	d->destport = rpc_getport(d, prog, vers);
    136 
    137 	/*
    138 	 * Prepend authorization stuff and headers.
    139 	 * Note, must prepend things in reverse order.
    140 	 */
    141 	send_head = sdata;
    142 	send_tail = sdata + slen;
    143 
    144 	/* Auth verifier is always auth_null */
    145 	send_head -= sizeof(*auth);
    146 	auth = send_head;
    147 	auth->authtype = htonl(RPCAUTH_NULL);
    148 	auth->authlen = 0;
    149 
    150 #if 1
    151 	/* Auth credentials: always auth unix (as root) */
    152 	send_head -= sizeof(struct auth_unix);
    153 	bzero(send_head, sizeof(struct auth_unix));
    154 	send_head -= sizeof(*auth);
    155 	auth = send_head;
    156 	auth->authtype = htonl(RPCAUTH_UNIX);
    157 	auth->authlen = htonl(sizeof(struct auth_unix));
    158 #else
    159 	/* Auth credentials: always auth_null (XXX OK?) */
    160 	send_head -= sizeof(*auth);
    161 	auth = send_head;
    162 	auth->authtype = htonl(RPCAUTH_NULL);
    163 	auth->authlen = 0;
    164 #endif
    165 
    166 	/* RPC call structure. */
    167 	send_head -= sizeof(*call);
    168 	call = send_head;
    169 	rpc_xid++;
    170 	call->rp_xid       = htonl(rpc_xid);
    171 	call->rp_direction = htonl(RPC_CALL);
    172 	call->rp_rpcvers   = htonl(RPC_VER2);
    173 	call->rp_prog = htonl(prog);
    174 	call->rp_vers = htonl(vers);
    175 	call->rp_proc = htonl(proc);
    176 
    177 	/* Make room for the rpc_reply header. */
    178 	recv_head = rdata;
    179 	recv_tail = rdata + rlen;
    180 	recv_head -= sizeof(*reply);
    181 
    182 	cc = sendrecv(d,
    183 	    sendudp, send_head, (send_tail - send_head),
    184 	    recvrpc, recv_head, (recv_tail - recv_head));
    185 #ifdef RPC_DEBUG
    186 	if (debug)
    187 		printf("callrpc: cc=%d rlen=%d\n", cc, rlen);
    188 #endif
    189 	if (cc <= sizeof(*reply))
    190 		return (-1);
    191 	recv_tail = recv_head + cc;
    192 
    193 	/*
    194 	 * Check the RPC reply status.
    195 	 * The xid, dir, astatus were already checked.
    196 	 */
    197 	reply = recv_head;
    198 	auth = &reply->rp_u.rpu_rok.rok_auth;
    199 	x = ntohl(auth->authlen);
    200 	if (x != 0) {
    201 #ifdef RPC_DEBUG
    202 		if (debug)
    203 			printf("callrpc: reply auth != NULL\n");
    204 #endif
    205 		return(-1);
    206 	}
    207 	x = ntohl(reply->rp_u.rpu_rok.rok_status);
    208 	if (x != 0) {
    209 		printf("callrpc: error = %d\n", x);
    210 		return(-1);
    211 	}
    212 	recv_head += sizeof(*reply);
    213 
    214 	return (recv_tail - recv_head);
    215 }
    216 
    217 /*
    218  * Returns true if packet is the one we're waiting for.
    219  * This just checks the XID, direction, acceptance.
    220  * Remaining checks are done by callrpc
    221  */
    222 static size_t
    223 recvrpc(d, pkt, len, tleft)
    224 	register struct iodesc *d;
    225 	register void *pkt;
    226 	register size_t len;
    227 	time_t tleft;
    228 {
    229 	register struct rpc_reply *reply;
    230 	int x;
    231 
    232 	errno = 0;
    233 #ifdef RPC_DEBUG
    234 	if (debug)
    235 		printf("recvrpc: called len=%d\n", len);
    236 #endif
    237 
    238 	len = readudp(d, pkt, len, tleft);
    239 	if (len <= (4 * 4))
    240 		goto bad;
    241 	reply = (struct rpc_reply *)pkt;
    242 
    243 	x = ntohl(reply->rp_xid);
    244 	if (x != rpc_xid) {
    245 #ifdef RPC_DEBUG
    246 		if (debug)
    247 			printf("recvrpc: rp_xid %d != xid %d\n", x, rpc_xid);
    248 #endif
    249 		goto bad;
    250 	}
    251 
    252 	x = ntohl(reply->rp_direction);
    253 	if (x != RPC_REPLY) {
    254 #ifdef RPC_DEBUG
    255 		if (debug)
    256 			printf("recvrpc: rp_direction %d != REPLY\n", x);
    257 #endif
    258 		goto bad;
    259 	}
    260 
    261 	x = ntohl(reply->rp_astatus);
    262 	if (x != RPC_MSGACCEPTED) {
    263 		errno = ntohl(reply->rp_u.rpu_errno);
    264 		printf("recvrpc: reject, astat=%d, errno=%d\n", x, errno);
    265 		goto bad;
    266 	}
    267 
    268 	/* Return data count (thus indicating success) */
    269 	return (len);
    270 
    271 bad:
    272 	return (-1);
    273 }
    274 
    275 /*
    276  * Given a pointer to a reply just received,
    277  * dig out the IP address/port from the headers.
    278  */
    279 void
    280 rpc_fromaddr(void *pkt, n_long *addr, u_short *port)
    281 {
    282 	struct hackhdr {
    283 		/* Tail of IP header: just IP addresses */
    284 		n_long ip_src;
    285 		n_long ip_dst;
    286 		/* UDP header: */
    287 		u_int16_t uh_sport;		/* source port */
    288 		u_int16_t uh_dport;		/* destination port */
    289 		int16_t	  uh_ulen;		/* udp length */
    290 		u_int16_t uh_sum;		/* udp checksum */
    291 		/* RPC reply header: */
    292 		struct rpc_reply rpc;
    293 	} *hhdr;
    294 
    295 	hhdr = ((struct hackhdr *)pkt) - 1;
    296 	*addr = hhdr->ip_src;
    297 	*port = hhdr->uh_sport;
    298 }
    299 
    300 /*
    301  * RPC Portmapper cache
    302  */
    303 
    304 #define PMAP_NUM 8			/* need at most 5 pmap entries */
    305 
    306 int rpc_pmap_num;
    307 struct pmap_list {
    308 	u_long	addr;		/* server, net order */
    309 	u_long	prog;		/* host order */
    310 	u_long	vers;		/* host order */
    311 	u_short	port;		/* net order */
    312 	u_short _pad;
    313 } rpc_pmap_list[PMAP_NUM];
    314 
    315 /* return port number in net order */
    316 int
    317 rpc_pmap_getcache(addr, prog, vers)
    318 	u_long	addr;	/* server, net order */
    319 	u_long	prog;	/* host order */
    320 	u_long	vers;	/* host order */
    321 {
    322 	struct pmap_list *pl;
    323 
    324 	for (pl = rpc_pmap_list; pl < &rpc_pmap_list[rpc_pmap_num]; pl++)
    325 		if (pl->addr == addr &&	pl->prog == prog &&	pl->vers == vers)
    326 			return ((int) pl->port);
    327 	return (-1);
    328 }
    329 
    330 void
    331 rpc_pmap_putcache(addr, prog, vers, port)
    332 	n_long	addr;	/* net order */
    333 	n_long	prog;	/* host order */
    334 	n_long	vers;	/* host order */
    335 	int port;		/* net order */
    336 {
    337 	struct pmap_list *pl;
    338 
    339 	/* Don't overflow cache... */
    340 	if (rpc_pmap_num >= PMAP_NUM) {
    341 		/* ... just re-use the last entry. */
    342 		rpc_pmap_num = PMAP_NUM - 1;
    343 #ifdef	RPC_DEBUG
    344 		printf("rpc_pmap_putcache: cache overflow\n");
    345 #endif
    346 	}
    347 
    348 	pl = &rpc_pmap_list[rpc_pmap_num];
    349 	rpc_pmap_num++;
    350 
    351 	/* Cache answer */
    352 	pl->addr = addr;
    353 	pl->prog = prog;
    354 	pl->vers = vers;
    355 	pl->port = port;
    356 }
    357 
    358 
    359 /*
    360  * Request a port number from the port mapper.
    361  * Returns the port in network order.
    362  */
    363 int
    364 rpc_getport(d, prog, vers)
    365 	register struct iodesc *d;
    366 	n_long prog;	/* host order */
    367 	n_long vers;	/* host order */
    368 {
    369 	struct args {
    370 		u_long	prog;		/* call program */
    371 		u_long	vers;		/* call version */
    372 		u_long	proto;		/* call protocol */
    373 		u_long	port;		/* call port (unused) */
    374 	} *args;
    375 	struct res {
    376 		u_long port;
    377 	} *res;
    378 	struct {
    379 		n_long	h[RPC_HEADER_WORDS];
    380 		struct args d;
    381 	} sdata;
    382 	struct {
    383 		n_long	h[RPC_HEADER_WORDS];
    384 		struct res d;
    385 		n_long  pad;
    386 	} rdata;
    387 	int cc, port;
    388 
    389 #ifdef RPC_DEBUG
    390 	if (debug)
    391 	    printf("getport: prog=0x%x vers=%d\n", prog, vers);
    392 #endif
    393 
    394 	/* This one is fixed forever. */
    395 	if (prog == PMAPPROG)
    396 		return PMAPPORT;
    397 
    398 	/* Try for cached answer first */
    399 	port = rpc_pmap_getcache(d->destip, prog, vers);
    400 	if (port >= 0)
    401 		return (port);
    402 
    403 	args = &sdata.d;
    404 	args->prog = htonl(prog);
    405 	args->vers = htonl(vers);
    406 	args->proto = htonl(IPPROTO_UDP);
    407 	args->port = 0;
    408 	res = &rdata.d;
    409 
    410 	cc = rpc_call(d, PMAPPROG, PMAPVERS, PMAPPROC_GETPORT,
    411 		args, sizeof(*args), res, sizeof(*res));
    412 	if (cc < sizeof(*res)) {
    413 		printf("getport: %s", strerror(errno));
    414 		return(-1);
    415 	}
    416 	port = (u_short)res->port;
    417 
    418 	rpc_pmap_putcache(d->destip, prog, vers, port);
    419 
    420 	return (port);
    421 }
    422