xref: /illumos-gate/usr/src/boot/libsa/rpc.c (revision d48be21240dfd051b689384ce2b23479d757f2d8)
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  * 3. 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, void *);
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, NULL);
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     void *extra __unused)
221 {
222 	void *ptr;
223 	struct rpc_reply *reply;
224 	ssize_t	n;
225 	int	x;
226 
227 	errno = 0;
228 #ifdef RPC_DEBUG
229 	if (debug)
230 		printf("recvrpc: called\n");
231 #endif
232 
233 	ptr = NULL;
234 	n = readudp(d, &ptr, (void **)&reply, tleft);
235 	if (n <= (4 * 4)) {
236 		free(ptr);
237 		return (-1);
238 	}
239 
240 	x = ntohl(reply->rp_xid);
241 	if (x != rpc_xid) {
242 #ifdef RPC_DEBUG
243 		if (debug)
244 			printf("recvrpc: rp_xid %d != xid %d\n", x, rpc_xid);
245 #endif
246 		free(ptr);
247 		return (-1);
248 	}
249 
250 	x = ntohl(reply->rp_direction);
251 	if (x != RPC_REPLY) {
252 #ifdef RPC_DEBUG
253 		if (debug)
254 			printf("recvrpc: rp_direction %d != REPLY\n", x);
255 #endif
256 		free(ptr);
257 		return (-1);
258 	}
259 
260 	x = ntohl(reply->rp_astatus);
261 	if (x != RPC_MSGACCEPTED) {
262 		errno = ntohl(reply->rp_u.rpu_errno);
263 		printf("recvrpc: reject, astat=%d, errno=%d\n", x, errno);
264 		free(ptr);
265 		return (-1);
266 	}
267 
268 	*pkt = ptr;
269 	*payload = reply;
270 	/* Return data count (thus indicating success) */
271 	return (n);
272 }
273 
274 /*
275  * Given a pointer to a reply just received,
276  * dig out the IP address/port from the headers.
277  */
278 void
279 rpc_fromaddr(void *pkt, struct in_addr *addr, u_short *port)
280 {
281 	struct hackhdr {
282 		/* Tail of IP header: just IP addresses */
283 		n_long ip_src;
284 		n_long ip_dst;
285 		/* UDP header: */
286 		u_int16_t uh_sport;		/* source port */
287 		u_int16_t uh_dport;		/* destination port */
288 		int16_t	  uh_ulen;		/* udp length */
289 		u_int16_t uh_sum;		/* udp checksum */
290 		/* RPC reply header: */
291 		struct rpc_reply rpc;
292 	} *hhdr;
293 
294 	hhdr = ((struct hackhdr *)pkt) - 1;
295 	addr->s_addr = hhdr->ip_src;
296 	*port = hhdr->uh_sport;
297 }
298 
299 /*
300  * RPC Portmapper cache
301  */
302 #define PMAP_NUM 8			/* need at most 5 pmap entries */
303 
304 int rpc_pmap_num;
305 struct pmap_list {
306 	struct in_addr	addr;	/* server, net order */
307 	u_int	prog;		/* host order */
308 	u_int	vers;		/* host order */
309 	int 	port;		/* host order */
310 } rpc_pmap_list[PMAP_NUM];
311 
312 /*
313  * return port number in host order, or -1.
314  * arguments are:
315  *  addr .. server, net order.
316  *  prog .. host order.
317  *  vers .. host order.
318  */
319 int
320 rpc_pmap_getcache(struct in_addr addr, u_int prog, u_int vers)
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.s_addr == addr.s_addr &&
326 			pl->prog == prog && pl->vers == vers )
327 		{
328 			return (pl->port);
329 		}
330 	}
331 	return (-1);
332 }
333 
334 /*
335  * arguments are:
336  *  addr .. server, net order.
337  *  prog .. host order.
338  *  vers .. host order.
339  *  port .. host order.
340  */
341 void
342 rpc_pmap_putcache(struct in_addr addr, u_int prog, u_int vers, int port)
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 host order.
369  * prog and vers are host order.
370  */
371 int
372 rpc_getport(struct iodesc *d, n_long prog, n_long vers)
373 {
374 	struct args {
375 		n_long	prog;		/* call program */
376 		n_long	vers;		/* call version */
377 		n_long	proto;		/* call protocol */
378 		n_long	port;		/* call port (unused) */
379 	} *args;
380 	struct res {
381 		n_long port;
382 	} *res;
383 	struct {
384 		n_long	h[RPC_HEADER_WORDS];
385 		struct args d;
386 	} sdata;
387 	void *pkt;
388 	ssize_t cc;
389 	int port;
390 
391 #ifdef RPC_DEBUG
392 	if (debug)
393 		printf("%s: prog=0x%x vers=%d\n", __func__, prog, vers);
394 #endif
395 
396 	/* This one is fixed forever. */
397 	if (prog == PMAPPROG) {
398 		port = PMAPPORT;
399 		goto out;
400 	}
401 
402 	/* Try for cached answer first */
403 	port = rpc_pmap_getcache(d->destip, prog, vers);
404 	if (port != -1)
405 		goto out;
406 
407 	args = &sdata.d;
408 	args->prog = htonl(prog);
409 	args->vers = htonl(vers);
410 	args->proto = htonl(IPPROTO_UDP);
411 	args->port = 0;
412 	pkt = NULL;
413 
414 	cc = rpc_call(d, PMAPPROG, PMAPVERS, PMAPPROC_GETPORT,
415 	    args, sizeof(*args), (void **)&res, &pkt);
416 	if (cc < sizeof(*res)) {
417 		printf("getport: %s", strerror(errno));
418 		errno = EBADRPC;
419 		free(pkt);
420 		return (-1);
421 	}
422 	port = (int)ntohl(res->port);
423 	free(pkt);
424 
425 	rpc_pmap_putcache(d->destip, prog, vers, port);
426 
427 out:
428 #ifdef RPC_DEBUG
429 	if (debug)
430 		printf("%s: port=%u\n", __func__, port);
431 #endif
432 	return (port);
433 }
434