Home | History | Annotate | Line # | Download | only in libsa
rpc.c revision 1.8
      1 /*	$NetBSD: rpc.c,v 1.8 1995/09/17 00:49:44 pk 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	ssize_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 ssize_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 ssize_t cc;
    122 	struct auth_info *auth;
    123 	struct rpc_call *call;
    124 	struct rpc_reply *reply;
    125 	char *send_head, *send_tail;
    126 	char *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 = (char *)sdata + slen;
    143 
    144 	/* Auth verifier is always auth_null */
    145 	send_head -= sizeof(*auth);
    146 	auth = (struct auth_info *)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 = (struct auth_info *)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 = (struct rpc_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 = (char *)rdata + rlen;
    180 	recv_head -= sizeof(*reply);
    181 
    182 	cc = sendrecv(d,
    183 	    sendudp, send_head, ((int)send_tail - (int)send_head),
    184 	    recvrpc, recv_head, ((int)recv_tail - (int)recv_head));
    185 #ifdef RPC_DEBUG
    186 	if (debug)
    187 		printf("callrpc: cc=%d rlen=%d\n", cc, rlen);
    188 #endif
    189 	if (cc == -1)
    190 		return (-1);
    191 
    192 	if (cc <= sizeof(*reply)) {
    193 		errno = EBADRPC;
    194 		return (-1);
    195 	}
    196 
    197 	recv_tail = recv_head + cc;
    198 
    199 	/*
    200 	 * Check the RPC reply status.
    201 	 * The xid, dir, astatus were already checked.
    202 	 */
    203 	reply = (struct rpc_reply *)recv_head;
    204 	auth = &reply->rp_u.rpu_rok.rok_auth;
    205 	x = ntohl(auth->authlen);
    206 	if (x != 0) {
    207 #ifdef RPC_DEBUG
    208 		if (debug)
    209 			printf("callrpc: reply auth != NULL\n");
    210 #endif
    211 		errno = EBADRPC;
    212 		return(-1);
    213 	}
    214 	x = ntohl(reply->rp_u.rpu_rok.rok_status);
    215 	if (x != 0) {
    216 		printf("callrpc: error = %d\n", x);
    217 		errno = EBADRPC;
    218 		return(-1);
    219 	}
    220 	recv_head += sizeof(*reply);
    221 
    222 	return (ssize_t)((int)recv_tail - (int)recv_head);
    223 }
    224 
    225 /*
    226  * Returns true if packet is the one we're waiting for.
    227  * This just checks the XID, direction, acceptance.
    228  * Remaining checks are done by callrpc
    229  */
    230 static ssize_t
    231 recvrpc(d, pkt, len, tleft)
    232 	register struct iodesc *d;
    233 	register void *pkt;
    234 	register size_t len;
    235 	time_t tleft;
    236 {
    237 	register struct rpc_reply *reply;
    238 	ssize_t	n;
    239 	long	x;
    240 
    241 	errno = 0;
    242 #ifdef RPC_DEBUG
    243 	if (debug)
    244 		printf("recvrpc: called len=%d\n", len);
    245 #endif
    246 
    247 	n = readudp(d, pkt, len, tleft);
    248 	if (n <= (4 * 4))
    249 		return -1;
    250 
    251 	reply = (struct rpc_reply *)pkt;
    252 
    253 	x = ntohl(reply->rp_xid);
    254 	if (x != rpc_xid) {
    255 #ifdef RPC_DEBUG
    256 		if (debug)
    257 			printf("recvrpc: rp_xid %d != xid %d\n", x, rpc_xid);
    258 #endif
    259 		return -1;
    260 	}
    261 
    262 	x = ntohl(reply->rp_direction);
    263 	if (x != RPC_REPLY) {
    264 #ifdef RPC_DEBUG
    265 		if (debug)
    266 			printf("recvrpc: rp_direction %d != REPLY\n", x);
    267 #endif
    268 		return -1;
    269 	}
    270 
    271 	x = ntohl(reply->rp_astatus);
    272 	if (x != RPC_MSGACCEPTED) {
    273 		errno = ntohl(reply->rp_u.rpu_errno);
    274 		printf("recvrpc: reject, astat=%d, errno=%d\n", x, errno);
    275 		return -1;
    276 	}
    277 
    278 	/* Return data count (thus indicating success) */
    279 	return (n);
    280 }
    281 
    282 /*
    283  * Given a pointer to a reply just received,
    284  * dig out the IP address/port from the headers.
    285  */
    286 void
    287 rpc_fromaddr(void *pkt, n_long *addr, u_short *port)
    288 {
    289 	struct hackhdr {
    290 		/* Tail of IP header: just IP addresses */
    291 		n_long ip_src;
    292 		n_long ip_dst;
    293 		/* UDP header: */
    294 		u_int16_t uh_sport;		/* source port */
    295 		u_int16_t uh_dport;		/* destination port */
    296 		int16_t	  uh_ulen;		/* udp length */
    297 		u_int16_t uh_sum;		/* udp checksum */
    298 		/* RPC reply header: */
    299 		struct rpc_reply rpc;
    300 	} *hhdr;
    301 
    302 	hhdr = ((struct hackhdr *)pkt) - 1;
    303 	*addr = hhdr->ip_src;
    304 	*port = hhdr->uh_sport;
    305 }
    306 
    307 /*
    308  * RPC Portmapper cache
    309  */
    310 
    311 #define PMAP_NUM 8			/* need at most 5 pmap entries */
    312 
    313 int rpc_pmap_num;
    314 struct pmap_list {
    315 	u_long	addr;		/* server, net order */
    316 	u_long	prog;		/* host order */
    317 	u_long	vers;		/* host order */
    318 	u_short	port;		/* net order */
    319 	u_short _pad;
    320 } rpc_pmap_list[PMAP_NUM];
    321 
    322 /* return port number in net order */
    323 int
    324 rpc_pmap_getcache(addr, prog, vers)
    325 	u_long	addr;	/* server, net order */
    326 	u_long	prog;	/* host order */
    327 	u_long	vers;	/* host order */
    328 {
    329 	struct pmap_list *pl;
    330 
    331 	for (pl = rpc_pmap_list; pl < &rpc_pmap_list[rpc_pmap_num]; pl++)
    332 		if (pl->addr == addr &&	pl->prog == prog &&	pl->vers == vers)
    333 			return ((int) pl->port);
    334 	return (-1);
    335 }
    336 
    337 void
    338 rpc_pmap_putcache(addr, prog, vers, port)
    339 	n_long	addr;	/* net order */
    340 	n_long	prog;	/* host order */
    341 	n_long	vers;	/* host order */
    342 	int port;		/* net order */
    343 {
    344 	struct pmap_list *pl;
    345 
    346 	/* Don't overflow cache... */
    347 	if (rpc_pmap_num >= PMAP_NUM) {
    348 		/* ... just re-use the last entry. */
    349 		rpc_pmap_num = PMAP_NUM - 1;
    350 #ifdef	RPC_DEBUG
    351 		printf("rpc_pmap_putcache: cache overflow\n");
    352 #endif
    353 	}
    354 
    355 	pl = &rpc_pmap_list[rpc_pmap_num];
    356 	rpc_pmap_num++;
    357 
    358 	/* Cache answer */
    359 	pl->addr = addr;
    360 	pl->prog = prog;
    361 	pl->vers = vers;
    362 	pl->port = port;
    363 }
    364 
    365 
    366 /*
    367  * Request a port number from the port mapper.
    368  * Returns the port in network order.
    369  */
    370 int
    371 rpc_getport(d, prog, vers)
    372 	register struct iodesc *d;
    373 	n_long prog;	/* host order */
    374 	n_long vers;	/* host order */
    375 {
    376 	struct args {
    377 		u_long	prog;		/* call program */
    378 		u_long	vers;		/* call version */
    379 		u_long	proto;		/* call protocol */
    380 		u_long	port;		/* call port (unused) */
    381 	} *args;
    382 	struct res {
    383 		u_long port;
    384 	} *res;
    385 	struct {
    386 		n_long	h[RPC_HEADER_WORDS];
    387 		struct args d;
    388 	} sdata;
    389 	struct {
    390 		n_long	h[RPC_HEADER_WORDS];
    391 		struct res d;
    392 		n_long  pad;
    393 	} rdata;
    394 	int cc, port;
    395 
    396 #ifdef RPC_DEBUG
    397 	if (debug)
    398 	    printf("getport: prog=0x%x vers=%d\n", prog, vers);
    399 #endif
    400 
    401 	/* This one is fixed forever. */
    402 	if (prog == PMAPPROG)
    403 		return PMAPPORT;
    404 
    405 	/* Try for cached answer first */
    406 	port = rpc_pmap_getcache(d->destip, prog, vers);
    407 	if (port >= 0)
    408 		return (port);
    409 
    410 	args = &sdata.d;
    411 	args->prog = htonl(prog);
    412 	args->vers = htonl(vers);
    413 	args->proto = htonl(IPPROTO_UDP);
    414 	args->port = 0;
    415 	res = &rdata.d;
    416 
    417 	cc = rpc_call(d, PMAPPROG, PMAPVERS, PMAPPROC_GETPORT,
    418 		args, sizeof(*args), res, sizeof(*res));
    419 	if (cc < sizeof(*res)) {
    420 		printf("getport: %s", strerror(errno));
    421 		return(-1);
    422 	}
    423 	port = (u_short)res->port;
    424 
    425 	rpc_pmap_putcache(d->destip, prog, vers, port);
    426 
    427 	return (port);
    428 }
    429