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