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