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