xref: /openbsd-src/sys/nfs/krpc_subr.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*	$OpenBSD: krpc_subr.c,v 1.11 2001/06/27 05:45:00 nate Exp $	*/
2 /*	$NetBSD: krpc_subr.c,v 1.12.4.1 1996/06/07 00:52:26 cgd Exp $	*/
3 
4 /*
5  * Copyright (c) 1995 Gordon Ross, Adam Glass
6  * Copyright (c) 1992 Regents of the University of California.
7  * All rights reserved.
8  *
9  * This software was developed by the Computer Systems Engineering group
10  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
11  * contributed to Berkeley.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. All advertising materials mentioning features or use of this software
22  *    must display the following acknowledgement:
23  *	This product includes software developed by the University of
24  *	California, Lawrence Berkeley Laboratory and its contributors.
25  * 4. Neither the name of the University nor the names of its contributors
26  *    may be used to endorse or promote products derived from this software
27  *    without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39  * SUCH DAMAGE.
40  *
41  * partially based on:
42  *      libnetboot/rpc.c
43  *               @(#) Header: rpc.c,v 1.12 93/09/28 08:31:56 leres Exp  (LBL)
44  */
45 
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/conf.h>
49 #include <sys/ioctl.h>
50 #include <sys/proc.h>
51 #include <sys/mount.h>
52 #include <sys/mbuf.h>
53 #include <sys/reboot.h>
54 #include <sys/socket.h>
55 #include <sys/socketvar.h>
56 
57 #include <net/if.h>
58 #include <netinet/in.h>
59 
60 #include <nfs/rpcv2.h>
61 #include <nfs/krpc.h>
62 #include <nfs/xdr_subs.h>
63 #include <dev/rndvar.h>
64 
65 /*
66  * Kernel support for Sun RPC
67  *
68  * Used currently for bootstrapping in nfs diskless configurations.
69  */
70 
71 /*
72  * Generic RPC headers
73  */
74 
75 struct auth_info {
76 	u_int32_t 	authtype;	/* auth type */
77 	u_int32_t	authlen;	/* auth length */
78 };
79 
80 struct auth_unix {
81 	int32_t   ua_time;
82 	int32_t   ua_hostname;	/* null */
83 	int32_t   ua_uid;
84 	int32_t   ua_gid;
85 	int32_t   ua_gidlist;	/* null */
86 };
87 
88 struct rpc_call {
89 	u_int32_t	rp_xid;		/* request transaction id */
90 	int32_t 	rp_direction;	/* call direction (0) */
91 	u_int32_t	rp_rpcvers;	/* rpc version (2) */
92 	u_int32_t	rp_prog;	/* program */
93 	u_int32_t	rp_vers;	/* version */
94 	u_int32_t	rp_proc;	/* procedure */
95 	struct	auth_info rpc_auth;
96 	struct	auth_unix rpc_unix;
97 	struct	auth_info rpc_verf;
98 };
99 
100 struct rpc_reply {
101 	u_int32_t rp_xid;		/* request transaction id */
102 	int32_t   rp_direction;		/* call direction (1) */
103 	int32_t   rp_astatus;		/* accept status (0: accepted) */
104 	union {
105 		u_int32_t rpu_errno;
106 		struct {
107 			struct auth_info rok_auth;
108 			u_int32_t	rok_status;
109 		} rpu_rok;
110 	} rp_u;
111 };
112 #define rp_errno  rp_u.rpu_errno
113 #define rp_auth   rp_u.rpu_rok.rok_auth
114 #define rp_status rp_u.rpu_rok.rok_status
115 
116 #define MIN_REPLY_HDR 16	/* xid, dir, astat, errno */
117 
118 /*
119  * What is the longest we will wait before re-sending a request?
120  * Note this is also the frequency of "RPC timeout" messages.
121  * The re-send loop count sup linearly to this maximum, so the
122  * first complaint will happen after (1+2+3+4+5)=15 seconds.
123  */
124 #define	MAX_RESEND_DELAY 5	/* seconds */
125 
126 /*
127  * Call portmap to lookup a port number for a particular rpc program
128  * Returns non-zero error on failure.
129  */
130 int
131 krpc_portmap(sin,  prog, vers, portp)
132 	struct sockaddr_in *sin;		/* server address */
133 	u_int prog, vers;	/* host order */
134 	u_int16_t *portp;	/* network order */
135 {
136 	struct sdata {
137 		u_int32_t prog;		/* call program */
138 		u_int32_t vers;		/* call version */
139 		u_int32_t proto;	/* call protocol */
140 		u_int32_t port;		/* call port (unused) */
141 	} *sdata;
142 	struct rdata {
143 		u_int16_t pad;
144 		u_int16_t port;
145 	} *rdata;
146 	struct mbuf *m;
147 	int error;
148 
149 	/* The portmapper port is fixed. */
150 	if (prog == PMAPPROG) {
151 		*portp = htons(PMAPPORT);
152 		return 0;
153 	}
154 
155 	m = m_get(M_WAIT, MT_DATA);
156 	sdata = mtod(m, struct sdata *);
157 	m->m_len = sizeof(*sdata);
158 
159 	/* Do the RPC to get it. */
160 	sdata->prog = txdr_unsigned(prog);
161 	sdata->vers = txdr_unsigned(vers);
162 	sdata->proto = txdr_unsigned(IPPROTO_UDP);
163 	sdata->port = 0;
164 
165 	sin->sin_port = htons(PMAPPORT);
166 	error = krpc_call(sin, PMAPPROG, PMAPVERS,
167 	    PMAPPROC_GETPORT, &m, NULL);
168 	if (error)
169 		return error;
170 
171 	if (m->m_len < sizeof(*rdata)) {
172 		m = m_pullup(m, sizeof(*rdata));
173 		if (m == NULL)
174 			return ENOBUFS;
175 	}
176 	rdata = mtod(m, struct rdata *);
177 	*portp = rdata->port;
178 
179 	m_freem(m);
180 	return 0;
181 }
182 
183 /*
184  * Do a remote procedure call (RPC) and wait for its reply.
185  * If from_p is non-null, then we are doing broadcast, and
186  * the address from whence the response came is saved there.
187  */
188 int
189 krpc_call(sa, prog, vers, func, data, from_p)
190 	struct sockaddr_in *sa;
191 	u_int prog, vers, func;
192 	struct mbuf **data;	/* input/output */
193 	struct mbuf **from_p;	/* output */
194 {
195 	struct socket *so;
196 	struct sockaddr_in *sin;
197 	struct mbuf *m, *nam, *mhead, *from, *mopt;
198 	struct rpc_call *call;
199 	struct rpc_reply *reply;
200 	struct uio auio;
201 	int error, rcvflg, timo, secs, len;
202 	static u_int32_t xid = 0;
203 	u_int32_t newxid;
204 	int *ip;
205 	struct timeval *tv;
206 
207 	/*
208 	 * Validate address family.
209 	 * Sorry, this is INET specific...
210 	 */
211 	if (sa->sin_family != AF_INET)
212 		return (EAFNOSUPPORT);
213 
214 	/* Free at end if not null. */
215 	nam = mhead = NULL;
216 	from = NULL;
217 
218 	/*
219 	 * Create socket and set its receive timeout.
220 	 */
221 	if ((error = socreate(AF_INET, &so, SOCK_DGRAM, 0)))
222 		goto out;
223 
224 	m = m_get(M_WAIT, MT_SOOPTS);
225 	tv = mtod(m, struct timeval *);
226 	m->m_len = sizeof(*tv);
227 	tv->tv_sec = 1;
228 	tv->tv_usec = 0;
229 	if ((error = sosetopt(so, SOL_SOCKET, SO_RCVTIMEO, m)))
230 		goto out;
231 
232 	/*
233 	 * Enable broadcast if necessary.
234 	 */
235 	if (from_p) {
236 		int32_t *on;
237 		m = m_get(M_WAIT, MT_SOOPTS);
238 		on = mtod(m, int32_t *);
239 		m->m_len = sizeof(*on);
240 		*on = 1;
241 		if ((error = sosetopt(so, SOL_SOCKET, SO_BROADCAST, m)))
242 			goto out;
243 	}
244 
245 	/*
246 	 * Bind the local endpoint to a reserved port,
247 	 * because some NFS servers refuse requests from
248 	 * non-reserved (non-privileged) ports.
249 	 */
250 	m = m_getclr(M_WAIT, MT_SONAME);
251 	sin = mtod(m, struct sockaddr_in *);
252 	sin->sin_len = m->m_len = sizeof(*sin);
253 	sin->sin_family = AF_INET;
254 	sin->sin_addr.s_addr = INADDR_ANY;
255 
256 	MGET(mopt, M_WAIT, MT_SOOPTS);
257 	mopt->m_len = sizeof(int);
258 	ip = mtod(mopt, int *);
259 	*ip = IP_PORTRANGE_LOW;
260 	error = sosetopt(so, IPPROTO_IP, IP_PORTRANGE, mopt);
261 	if (error)
262 		goto out;
263 
264 	MGET(m, M_WAIT, MT_SONAME);
265 	sin = mtod(m, struct sockaddr_in *);
266 	sin->sin_len = m->m_len = sizeof (struct sockaddr_in);
267 	sin->sin_family = AF_INET;
268 	sin->sin_addr.s_addr = INADDR_ANY;
269 	sin->sin_port = htons(0);
270 	error = sobind(so, m);
271 	m_freem(m);
272 	if (error) {
273 		printf("bind failed\n");
274 		goto out;
275 	}
276 
277 	MGET(mopt, M_WAIT, MT_SOOPTS);
278 	mopt->m_len = sizeof(int);
279 	ip = mtod(mopt, int *);
280 	*ip = IP_PORTRANGE_DEFAULT;
281 	error = sosetopt(so, IPPROTO_IP, IP_PORTRANGE, mopt);
282 	if (error)
283 		goto out;
284 
285 	/*
286 	 * Setup socket address for the server.
287 	 */
288 	nam = m_get(M_WAIT, MT_SONAME);
289 	sin = mtod(nam, struct sockaddr_in *);
290 	bcopy((caddr_t)sa, (caddr_t)sin, (nam->m_len = sa->sin_len));
291 
292 	/*
293 	 * Prepend RPC message header.
294 	 */
295 	mhead = m_gethdr(M_WAIT, MT_DATA);
296 	mhead->m_next = *data;
297 	call = mtod(mhead, struct rpc_call *);
298 	mhead->m_len = sizeof(*call);
299 	bzero((caddr_t)call, sizeof(*call));
300 	/* rpc_call part */
301 	while ((newxid = arc4random()) == xid);
302 	xid = newxid;
303 	call->rp_xid = txdr_unsigned(xid);
304 	/* call->rp_direction = 0; */
305 	call->rp_rpcvers = txdr_unsigned(2);
306 	call->rp_prog = txdr_unsigned(prog);
307 	call->rp_vers = txdr_unsigned(vers);
308 	call->rp_proc = txdr_unsigned(func);
309 	/* rpc_auth part (auth_unix as root) */
310 	call->rpc_auth.authtype = txdr_unsigned(RPCAUTH_UNIX);
311 	call->rpc_auth.authlen  = txdr_unsigned(sizeof(struct auth_unix));
312 	/* rpc_verf part (auth_null) */
313 	call->rpc_verf.authtype = 0;
314 	call->rpc_verf.authlen  = 0;
315 
316 	/*
317 	 * Setup packet header
318 	 */
319 	len = 0;
320 	m = mhead;
321 	while (m) {
322 		len += m->m_len;
323 		m = m->m_next;
324 	}
325 	mhead->m_pkthdr.len = len;
326 	mhead->m_pkthdr.rcvif = NULL;
327 
328 	/*
329 	 * Send it, repeatedly, until a reply is received,
330 	 * but delay each re-send by an increasing amount.
331 	 * If the delay hits the maximum, start complaining.
332 	 */
333 	timo = 0;
334 	for (;;) {
335 		/* Send RPC request (or re-send). */
336 		m = m_copym(mhead, 0, M_COPYALL, M_WAIT);
337 		if (m == NULL) {
338 			error = ENOBUFS;
339 			goto out;
340 		}
341 		error = sosend(so, nam, NULL, m, NULL, 0);
342 		if (error) {
343 			printf("krpc_call: sosend: %d\n", error);
344 			goto out;
345 		}
346 		m = NULL;
347 
348 		/* Determine new timeout. */
349 		if (timo < MAX_RESEND_DELAY)
350 			timo++;
351 		else
352 			printf("RPC timeout for server 0x%x\n",
353 			       ntohl(sin->sin_addr.s_addr));
354 
355 		/*
356 		 * Wait for up to timo seconds for a reply.
357 		 * The socket receive timeout was set to 1 second.
358 		 */
359 		secs = timo;
360 		while (secs > 0) {
361 			if (from) {
362 				m_freem(from);
363 				from = NULL;
364 			}
365 			if (m) {
366 				m_freem(m);
367 				m = NULL;
368 			}
369 			auio.uio_resid = len = 1<<16;
370 			rcvflg = 0;
371 			error = soreceive(so, &from, &auio, &m, NULL, &rcvflg);
372 			if (error == EWOULDBLOCK) {
373 				secs--;
374 				continue;
375 			}
376 			if (error)
377 				goto out;
378 			len -= auio.uio_resid;
379 
380 			/* Does the reply contain at least a header? */
381 			if (len < MIN_REPLY_HDR)
382 				continue;
383 			if (m->m_len < MIN_REPLY_HDR)
384 				continue;
385 			reply = mtod(m, struct rpc_reply *);
386 
387 			/* Is it the right reply? */
388 			if (reply->rp_direction != txdr_unsigned(RPC_REPLY))
389 				continue;
390 
391 			if (reply->rp_xid != txdr_unsigned(xid))
392 				continue;
393 
394 			/* Was RPC accepted? (authorization OK) */
395 			if (reply->rp_astatus != 0) {
396 				error = fxdr_unsigned(u_int32_t, reply->rp_errno);
397 				printf("rpc denied, error=%d\n", error);
398 				continue;
399 			}
400 
401 			/* Did the call succeed? */
402 			if (reply->rp_status != 0) {
403 				error = fxdr_unsigned(u_int32_t, reply->rp_status);
404 				printf("rpc denied, status=%d\n", error);
405 				continue;
406 			}
407 
408 			goto gotreply;	/* break two levels */
409 
410 		} /* while secs */
411 	} /* forever send/receive */
412 
413 	error = ETIMEDOUT;
414 	goto out;
415 
416  gotreply:
417 
418 	/*
419 	 * Get RPC reply header into first mbuf,
420 	 * get its length, then strip it off.
421 	 */
422 	len = sizeof(*reply);
423 	if (m->m_len < len) {
424 		m = m_pullup(m, len);
425 		if (m == NULL) {
426 			error = ENOBUFS;
427 			goto out;
428 		}
429 	}
430 	reply = mtod(m, struct rpc_reply *);
431 	if (reply->rp_auth.authtype != 0) {
432 		len += fxdr_unsigned(u_int32_t, reply->rp_auth.authlen);
433 		len = (len + 3) & ~3; /* XXX? */
434 	}
435 	m_adj(m, len);
436 
437 	/* result */
438 	*data = m;
439 	if (from_p) {
440 		*from_p = from;
441 		from = NULL;
442 	}
443 
444  out:
445 	if (nam) m_freem(nam);
446 	if (mhead) m_freem(mhead);
447 	if (from) m_freem(from);
448 	soclose(so);
449 	return error;
450 }
451 
452 /*
453  * eXternal Data Representation routines.
454  * (but with non-standard args...)
455  */
456 
457 /*
458  * String representation for RPC.
459  */
460 struct xdr_string {
461 	u_int32_t len;		/* length without null or padding */
462 	char data[4];	/* data (longer, of course) */
463     /* data is padded to a long-word boundary */
464 };
465 
466 struct mbuf *
467 xdr_string_encode(str, len)
468 	char *str;
469 	int len;
470 {
471 	struct mbuf *m;
472 	struct xdr_string *xs;
473 	int dlen;	/* padded string length */
474 	int mlen;	/* message length */
475 
476 	dlen = (len + 3) & ~3;
477 	mlen = dlen + 4;
478 
479 	if (mlen > MCLBYTES)		/* If too big, we just can't do it. */
480 		return (NULL);
481 
482 	m = m_get(M_WAIT, MT_DATA);
483 	if (mlen > MLEN) {
484 		MCLGET(m, M_WAIT);
485 		if ((m->m_flags & M_EXT) == 0) {
486 			(void) m_free(m);	/* There can be only one. */
487 			return (NULL);
488 		}
489 	}
490 	xs = mtod(m, struct xdr_string *);
491 	m->m_len = mlen;
492 	xs->len = txdr_unsigned(len);
493 	bcopy(str, xs->data, len);
494 	return (m);
495 }
496 
497 struct mbuf *
498 xdr_string_decode(m, str, len_p)
499 	struct mbuf *m;
500 	char *str;
501 	int *len_p;		/* bufsize - 1 */
502 {
503 	struct xdr_string *xs;
504 	int mlen;	/* message length */
505 	int slen;	/* string length */
506 
507 	if (m->m_len < 4) {
508 		m = m_pullup(m, 4);
509 		if (m == NULL)
510 			return (NULL);
511 	}
512 	xs = mtod(m, struct xdr_string *);
513 	slen = fxdr_unsigned(u_int32_t, xs->len);
514 	mlen = 4 + ((slen + 3) & ~3);
515 
516 	if (slen > *len_p)
517 		slen = *len_p;
518 	if (slen > m->m_pkthdr.len) {
519 		m_freem(m);
520 		return (NULL);
521 	}
522 	m_copydata(m, 4, slen, str);
523 	m_adj(m, mlen);
524 
525 	str[slen] = '\0';
526 	*len_p = slen;
527 
528 	return (m);
529 }
530 
531 
532 /*
533  * Inet address in RPC messages
534  * (Note, really four ints, NOT chars.  Blech.)
535  */
536 struct xdr_inaddr {
537 	u_int32_t atype;
538 	u_int32_t addr[4];
539 };
540 
541 struct mbuf *
542 xdr_inaddr_encode(ia)
543 	struct in_addr *ia;		/* already in network order */
544 {
545 	struct mbuf *m;
546 	struct xdr_inaddr *xi;
547 	u_int8_t *cp;
548 	u_int32_t *ip;
549 
550 	m = m_get(M_WAIT, MT_DATA);
551 	xi = mtod(m, struct xdr_inaddr *);
552 	m->m_len = sizeof(*xi);
553 	xi->atype = txdr_unsigned(1);
554 	ip = xi->addr;
555 	cp = (u_int8_t *)&ia->s_addr;
556 	*ip++ = txdr_unsigned(*cp++);
557 	*ip++ = txdr_unsigned(*cp++);
558 	*ip++ = txdr_unsigned(*cp++);
559 	*ip++ = txdr_unsigned(*cp++);
560 
561 	return (m);
562 }
563 
564 struct mbuf *
565 xdr_inaddr_decode(m, ia)
566 	struct mbuf *m;
567 	struct in_addr *ia;		/* already in network order */
568 {
569 	struct xdr_inaddr *xi;
570 	u_int8_t *cp;
571 	u_int32_t *ip;
572 
573 	if (m->m_len < sizeof(*xi)) {
574 		m = m_pullup(m, sizeof(*xi));
575 		if (m == NULL)
576 			return (NULL);
577 	}
578 	xi = mtod(m, struct xdr_inaddr *);
579 	if (xi->atype != txdr_unsigned(1)) {
580 		ia->s_addr = INADDR_ANY;
581 		goto out;
582 	}
583 	ip = xi->addr;
584 	cp = (u_int8_t *)&ia->s_addr;
585 	*cp++ = fxdr_unsigned(u_int8_t, *ip++);
586 	*cp++ = fxdr_unsigned(u_int8_t, *ip++);
587 	*cp++ = fxdr_unsigned(u_int8_t, *ip++);
588 	*cp++ = fxdr_unsigned(u_int8_t, *ip++);
589 
590 out:
591 	m_adj(m, sizeof(*xi));
592 	return (m);
593 }
594