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