xref: /openbsd-src/sys/nfs/krpc_subr.c (revision 850e275390052b330d93020bf619a739a3c277ac)
1 /*	$OpenBSD: krpc_subr.c,v 1.17 2008/06/10 23:15:32 blambert 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 #include <crypto/idgen.h>
65 
66 /*
67  * Kernel support for Sun RPC
68  *
69  * Used currently for bootstrapping in nfs diskless configurations.
70  */
71 
72 /*
73  * Generic RPC headers
74  */
75 
76 struct auth_info {
77 	u_int32_t 	authtype;	/* auth type */
78 	u_int32_t	authlen;	/* auth length */
79 };
80 
81 struct auth_unix {
82 	int32_t   ua_time;
83 	int32_t   ua_hostname;	/* null */
84 	int32_t   ua_uid;
85 	int32_t   ua_gid;
86 	int32_t   ua_gidlist;	/* null */
87 };
88 
89 struct rpc_call {
90 	u_int32_t	rp_xid;		/* request transaction id */
91 	int32_t 	rp_direction;	/* call direction (0) */
92 	u_int32_t	rp_rpcvers;	/* rpc version (2) */
93 	u_int32_t	rp_prog;	/* program */
94 	u_int32_t	rp_vers;	/* version */
95 	u_int32_t	rp_proc;	/* procedure */
96 	struct	auth_info rpc_auth;
97 	struct	auth_unix rpc_unix;
98 	struct	auth_info rpc_verf;
99 };
100 
101 struct rpc_reply {
102 	u_int32_t rp_xid;		/* request transaction id */
103 	int32_t   rp_direction;		/* call direction (1) */
104 	int32_t   rp_astatus;		/* accept status (0: accepted) */
105 	union {
106 		u_int32_t rpu_errno;
107 		struct {
108 			struct auth_info rok_auth;
109 			u_int32_t	rok_status;
110 		} rpu_rok;
111 	} rp_u;
112 };
113 #define rp_errno  rp_u.rpu_errno
114 #define rp_auth   rp_u.rpu_rok.rok_auth
115 #define rp_status rp_u.rpu_rok.rok_status
116 
117 #define MIN_REPLY_HDR 16	/* xid, dir, astat, errno */
118 
119 u_int32_t krpc_get_xid(void);
120 
121 /*
122  * Return an unpredictable XID.
123  */
124 u_int32_t
125 krpc_get_xid(void)
126 {
127 	static struct idgen32_ctx krpc_xid_ctx;
128 	static int called = 0;
129 
130 	if (!called) {
131 		called = 1;
132 		idgen32_init(&krpc_xid_ctx);
133 	}
134 	return idgen32(&krpc_xid_ctx);
135 }
136 
137 /*
138  * What is the longest we will wait before re-sending a request?
139  * Note this is also the frequency of "RPC timeout" messages.
140  * The re-send loop count sup linearly to this maximum, so the
141  * first complaint will happen after (1+2+3+4+5)=15 seconds.
142  */
143 #define	MAX_RESEND_DELAY 5	/* seconds */
144 
145 /*
146  * Call portmap to lookup a port number for a particular rpc program
147  * Returns non-zero error on failure.
148  */
149 int
150 krpc_portmap(sin,  prog, vers, portp)
151 	struct sockaddr_in *sin;		/* server address */
152 	u_int prog, vers;	/* host order */
153 	u_int16_t *portp;	/* network order */
154 {
155 	struct sdata {
156 		u_int32_t prog;		/* call program */
157 		u_int32_t vers;		/* call version */
158 		u_int32_t proto;	/* call protocol */
159 		u_int32_t port;		/* call port (unused) */
160 	} *sdata;
161 	struct rdata {
162 		u_int16_t pad;
163 		u_int16_t port;
164 	} *rdata;
165 	struct mbuf *m;
166 	int error;
167 
168 	/* The portmapper port is fixed. */
169 	if (prog == PMAPPROG) {
170 		*portp = htons(PMAPPORT);
171 		return 0;
172 	}
173 
174 	m = m_get(M_WAIT, MT_DATA);
175 	sdata = mtod(m, struct sdata *);
176 	m->m_len = sizeof(*sdata);
177 
178 	/* Do the RPC to get it. */
179 	sdata->prog = txdr_unsigned(prog);
180 	sdata->vers = txdr_unsigned(vers);
181 	sdata->proto = txdr_unsigned(IPPROTO_UDP);
182 	sdata->port = 0;
183 
184 	sin->sin_port = htons(PMAPPORT);
185 	error = krpc_call(sin, PMAPPROG, PMAPVERS,
186 	    PMAPPROC_GETPORT, &m, NULL, -1);
187 	if (error)
188 		return error;
189 
190 	if (m->m_len < sizeof(*rdata)) {
191 		m = m_pullup(m, sizeof(*rdata));
192 		if (m == NULL)
193 			return ENOBUFS;
194 	}
195 	rdata = mtod(m, struct rdata *);
196 	*portp = rdata->port;
197 
198 	m_freem(m);
199 	return 0;
200 }
201 
202 /*
203  * Do a remote procedure call (RPC) and wait for its reply.
204  * If from_p is non-null, then we are doing broadcast, and
205  * the address from whence the response came is saved there.
206  */
207 int
208 krpc_call(sa, prog, vers, func, data, from_p, retries)
209 	struct sockaddr_in *sa;
210 	u_int prog, vers, func;
211 	struct mbuf **data;	/* input/output */
212 	struct mbuf **from_p;	/* output */
213 	int retries;
214 {
215 	struct socket *so;
216 	struct sockaddr_in *sin;
217 	struct mbuf *m, *nam, *mhead, *from, *mopt;
218 	struct rpc_call *call;
219 	struct rpc_reply *reply;
220 	struct uio auio;
221 	int error, rcvflg, timo, secs, len;
222 	static u_int32_t xid = 0;
223 	int *ip;
224 	struct timeval *tv;
225 
226 	/*
227 	 * Validate address family.
228 	 * Sorry, this is INET specific...
229 	 */
230 	if (sa->sin_family != AF_INET)
231 		return (EAFNOSUPPORT);
232 
233 	/* Free at end if not null. */
234 	nam = mhead = NULL;
235 	from = NULL;
236 
237 	/*
238 	 * Create socket and set its receive timeout.
239 	 */
240 	if ((error = socreate(AF_INET, &so, SOCK_DGRAM, 0)))
241 		goto out;
242 
243 	m = m_get(M_WAIT, MT_SOOPTS);
244 	tv = mtod(m, struct timeval *);
245 	m->m_len = sizeof(*tv);
246 	tv->tv_sec = 1;
247 	tv->tv_usec = 0;
248 	if ((error = sosetopt(so, SOL_SOCKET, SO_RCVTIMEO, m)))
249 		goto out;
250 
251 	/*
252 	 * Enable broadcast if necessary.
253 	 */
254 	if (from_p) {
255 		int32_t *on;
256 		m = m_get(M_WAIT, MT_SOOPTS);
257 		on = mtod(m, int32_t *);
258 		m->m_len = sizeof(*on);
259 		*on = 1;
260 		if ((error = sosetopt(so, SOL_SOCKET, SO_BROADCAST, m)))
261 			goto out;
262 	}
263 
264 	/*
265 	 * Bind the local endpoint to a reserved port,
266 	 * because some NFS servers refuse requests from
267 	 * non-reserved (non-privileged) ports.
268 	 */
269 	MGET(mopt, M_WAIT, MT_SOOPTS);
270 	mopt->m_len = sizeof(int);
271 	ip = mtod(mopt, int *);
272 	*ip = IP_PORTRANGE_LOW;
273 	error = sosetopt(so, IPPROTO_IP, IP_PORTRANGE, mopt);
274 	if (error)
275 		goto out;
276 
277 	MGET(m, M_WAIT, MT_SONAME);
278 	sin = mtod(m, struct sockaddr_in *);
279 	sin->sin_len = m->m_len = sizeof (struct sockaddr_in);
280 	sin->sin_family = AF_INET;
281 	sin->sin_addr.s_addr = INADDR_ANY;
282 	sin->sin_port = htons(0);
283 	error = sobind(so, m, &proc0);
284 	m_freem(m);
285 	if (error) {
286 		printf("bind failed\n");
287 		goto out;
288 	}
289 
290 	MGET(mopt, M_WAIT, MT_SOOPTS);
291 	mopt->m_len = sizeof(int);
292 	ip = mtod(mopt, int *);
293 	*ip = IP_PORTRANGE_DEFAULT;
294 	error = sosetopt(so, IPPROTO_IP, IP_PORTRANGE, mopt);
295 	if (error)
296 		goto out;
297 
298 	/*
299 	 * Setup socket address for the server.
300 	 */
301 	nam = m_get(M_WAIT, MT_SONAME);
302 	sin = mtod(nam, struct sockaddr_in *);
303 	bcopy((caddr_t)sa, (caddr_t)sin, (nam->m_len = sa->sin_len));
304 
305 	/*
306 	 * Prepend RPC message header.
307 	 */
308 	mhead = m_gethdr(M_WAIT, MT_DATA);
309 	mhead->m_next = *data;
310 	call = mtod(mhead, struct rpc_call *);
311 	mhead->m_len = sizeof(*call);
312 	bzero((caddr_t)call, sizeof(*call));
313 	/* rpc_call part */
314 	xid = krpc_get_xid();
315 	call->rp_xid = txdr_unsigned(xid);
316 	/* call->rp_direction = 0; */
317 	call->rp_rpcvers = txdr_unsigned(2);
318 	call->rp_prog = txdr_unsigned(prog);
319 	call->rp_vers = txdr_unsigned(vers);
320 	call->rp_proc = txdr_unsigned(func);
321 	/* rpc_auth part (auth_unix as root) */
322 	call->rpc_auth.authtype = txdr_unsigned(RPCAUTH_UNIX);
323 	call->rpc_auth.authlen  = txdr_unsigned(sizeof(struct auth_unix));
324 	/* rpc_verf part (auth_null) */
325 	call->rpc_verf.authtype = 0;
326 	call->rpc_verf.authlen  = 0;
327 
328 	/*
329 	 * Setup packet header
330 	 */
331 	len = 0;
332 	m = mhead;
333 	while (m) {
334 		len += m->m_len;
335 		m = m->m_next;
336 	}
337 	mhead->m_pkthdr.len = len;
338 	mhead->m_pkthdr.rcvif = NULL;
339 
340 	/*
341 	 * Send it, repeatedly, until a reply is received,
342 	 * but delay each re-send by an increasing amount.
343 	 * If the delay hits the maximum, start complaining.
344 	 */
345 	for (timo = 0; retries; retries--) {
346 		/* Send RPC request (or re-send). */
347 		m = m_copym(mhead, 0, M_COPYALL, M_WAIT);
348 		if (m == NULL) {
349 			error = ENOBUFS;
350 			goto out;
351 		}
352 		error = sosend(so, nam, NULL, m, NULL, 0);
353 		if (error) {
354 			printf("krpc_call: sosend: %d\n", error);
355 			goto out;
356 		}
357 		m = NULL;
358 
359 		/* Determine new timeout. */
360 		if (timo < MAX_RESEND_DELAY)
361 			timo++;
362 		else
363 			printf("RPC timeout for server %s (0x%x) prog %u\n",
364 			    inet_ntoa(sin->sin_addr),
365 			    ntohl(sin->sin_addr.s_addr), prog);
366 
367 		/*
368 		 * Wait for up to timo seconds for a reply.
369 		 * The socket receive timeout was set to 1 second.
370 		 */
371 		secs = timo;
372 		while (secs > 0) {
373 			if (from) {
374 				m_freem(from);
375 				from = NULL;
376 			}
377 			if (m) {
378 				m_freem(m);
379 				m = NULL;
380 			}
381 			auio.uio_resid = len = 1<<16;
382 			auio.uio_procp = NULL;
383 			rcvflg = 0;
384 			error = soreceive(so, &from, &auio, &m, NULL, &rcvflg);
385 			if (error == EWOULDBLOCK) {
386 				secs--;
387 				continue;
388 			}
389 			if (error)
390 				goto out;
391 			len -= auio.uio_resid;
392 
393 			/* Does the reply contain at least a header? */
394 			if (len < MIN_REPLY_HDR)
395 				continue;
396 			if (m->m_len < MIN_REPLY_HDR)
397 				continue;
398 			reply = mtod(m, struct rpc_reply *);
399 
400 			/* Is it the right reply? */
401 			if (reply->rp_direction != txdr_unsigned(RPC_REPLY))
402 				continue;
403 
404 			if (reply->rp_xid != txdr_unsigned(xid))
405 				continue;
406 
407 			/* Was RPC accepted? (authorization OK) */
408 			if (reply->rp_astatus != 0) {
409 				error = fxdr_unsigned(u_int32_t, reply->rp_errno);
410 				printf("rpc denied, error=%d\n", error);
411 				continue;
412 			}
413 
414 			/* Did the call succeed? */
415 			if (reply->rp_status != 0) {
416 				error = fxdr_unsigned(u_int32_t, reply->rp_status);
417 				printf("rpc denied, status=%d\n", error);
418 				continue;
419 			}
420 
421 			goto gotreply;	/* break two levels */
422 
423 		} /* while secs */
424 	} /* forever send/receive */
425 
426 	error = ETIMEDOUT;
427 	goto out;
428 
429  gotreply:
430 
431 	/*
432 	 * Get RPC reply header into first mbuf,
433 	 * get its length, then strip it off.
434 	 */
435 	len = sizeof(*reply);
436 	if (m->m_len < len) {
437 		m = m_pullup(m, len);
438 		if (m == NULL) {
439 			error = ENOBUFS;
440 			goto out;
441 		}
442 	}
443 	reply = mtod(m, struct rpc_reply *);
444 	if (reply->rp_auth.authtype != 0) {
445 		len += fxdr_unsigned(u_int32_t, reply->rp_auth.authlen);
446 		len = (len + 3) & ~3; /* XXX? */
447 	}
448 	m_adj(m, len);
449 
450 	/* result */
451 	*data = m;
452 	if (from_p) {
453 		*from_p = from;
454 		from = NULL;
455 	}
456 
457  out:
458 	if (nam) m_freem(nam);
459 	if (mhead) m_freem(mhead);
460 	if (from) m_freem(from);
461 	soclose(so);
462 	return error;
463 }
464 
465 /*
466  * eXternal Data Representation routines.
467  * (but with non-standard args...)
468  */
469 
470 /*
471  * String representation for RPC.
472  */
473 struct xdr_string {
474 	u_int32_t len;		/* length without null or padding */
475 	char data[4];	/* data (longer, of course) */
476     /* data is padded to a long-word boundary */
477 };
478 
479 struct mbuf *
480 xdr_string_encode(str, len)
481 	char *str;
482 	int len;
483 {
484 	struct mbuf *m;
485 	struct xdr_string *xs;
486 	int dlen;	/* padded string length */
487 	int mlen;	/* message length */
488 
489 	dlen = (len + 3) & ~3;
490 	mlen = dlen + 4;
491 
492 	if (mlen > MCLBYTES)		/* If too big, we just can't do it. */
493 		return (NULL);
494 
495 	m = m_get(M_WAIT, MT_DATA);
496 	if (mlen > MLEN) {
497 		MCLGET(m, M_WAIT);
498 		if ((m->m_flags & M_EXT) == 0) {
499 			(void) m_free(m);	/* There can be only one. */
500 			return (NULL);
501 		}
502 	}
503 	xs = mtod(m, struct xdr_string *);
504 	m->m_len = mlen;
505 	xs->len = txdr_unsigned(len);
506 	bcopy(str, xs->data, len);
507 	return (m);
508 }
509 
510 struct mbuf *
511 xdr_string_decode(m, str, len_p)
512 	struct mbuf *m;
513 	char *str;
514 	int *len_p;		/* bufsize - 1 */
515 {
516 	struct xdr_string *xs;
517 	int mlen;	/* message length */
518 	int slen;	/* string length */
519 
520 	if (m->m_len < 4) {
521 		m = m_pullup(m, 4);
522 		if (m == NULL)
523 			return (NULL);
524 	}
525 	xs = mtod(m, struct xdr_string *);
526 	slen = fxdr_unsigned(u_int32_t, xs->len);
527 	mlen = 4 + ((slen + 3) & ~3);
528 
529 	if (slen > *len_p)
530 		slen = *len_p;
531 	if (slen > m->m_pkthdr.len) {
532 		m_freem(m);
533 		return (NULL);
534 	}
535 	m_copydata(m, 4, slen, str);
536 	m_adj(m, mlen);
537 
538 	str[slen] = '\0';
539 	*len_p = slen;
540 
541 	return (m);
542 }
543 
544 
545 /*
546  * Inet address in RPC messages
547  * (Note, really four ints, NOT chars.  Blech.)
548  */
549 struct xdr_inaddr {
550 	u_int32_t atype;
551 	u_int32_t addr[4];
552 };
553 
554 struct mbuf *
555 xdr_inaddr_encode(ia)
556 	struct in_addr *ia;		/* already in network order */
557 {
558 	struct mbuf *m;
559 	struct xdr_inaddr *xi;
560 	u_int8_t *cp;
561 	u_int32_t *ip;
562 
563 	m = m_get(M_WAIT, MT_DATA);
564 	xi = mtod(m, struct xdr_inaddr *);
565 	m->m_len = sizeof(*xi);
566 	xi->atype = txdr_unsigned(1);
567 	ip = xi->addr;
568 	cp = (u_int8_t *)&ia->s_addr;
569 	*ip++ = txdr_unsigned(*cp++);
570 	*ip++ = txdr_unsigned(*cp++);
571 	*ip++ = txdr_unsigned(*cp++);
572 	*ip++ = txdr_unsigned(*cp++);
573 
574 	return (m);
575 }
576 
577 struct mbuf *
578 xdr_inaddr_decode(m, ia)
579 	struct mbuf *m;
580 	struct in_addr *ia;		/* already in network order */
581 {
582 	struct xdr_inaddr *xi;
583 	u_int8_t *cp;
584 	u_int32_t *ip;
585 
586 	if (m->m_len < sizeof(*xi)) {
587 		m = m_pullup(m, sizeof(*xi));
588 		if (m == NULL)
589 			return (NULL);
590 	}
591 	xi = mtod(m, struct xdr_inaddr *);
592 	if (xi->atype != txdr_unsigned(1)) {
593 		ia->s_addr = INADDR_ANY;
594 		goto out;
595 	}
596 	ip = xi->addr;
597 	cp = (u_int8_t *)&ia->s_addr;
598 	*cp++ = fxdr_unsigned(u_int8_t, *ip++);
599 	*cp++ = fxdr_unsigned(u_int8_t, *ip++);
600 	*cp++ = fxdr_unsigned(u_int8_t, *ip++);
601 	*cp++ = fxdr_unsigned(u_int8_t, *ip++);
602 
603 out:
604 	m_adj(m, sizeof(*xi));
605 	return (m);
606 }
607