xref: /freebsd/sys/nfs/krpc_subr.c (revision 0de89efe5c443f213c7ea28773ef2dc6cf3af2ed)
1 /*	$NetBSD: krpc_subr.c,v 1.12.4.1 1996/06/07 00:52:26 cgd Exp $	*/
2 /*	$Id: krpc_subr.c,v 1.4 1997/08/16 19:15:53 wollman 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/sockio.h>
50 #include <sys/proc.h>
51 #include <sys/mount.h>
52 #include <sys/malloc.h>
53 #include <sys/mbuf.h>
54 #include <sys/reboot.h>
55 #include <sys/socket.h>
56 #include <sys/socketvar.h>
57 
58 #include <net/if.h>
59 #include <netinet/in.h>
60 
61 #include <nfs/rpcv2.h>
62 #include <nfs/krpc.h>
63 #include <nfs/xdr_subs.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, procp)
132 	struct sockaddr_in *sin;		/* server address */
133 	u_int prog, vers;	/* host order */
134 	u_int16_t *portp;	/* network order */
135 	struct proc *procp;
136 {
137 	struct sdata {
138 		u_int32_t prog;		/* call program */
139 		u_int32_t vers;		/* call version */
140 		u_int32_t proto;	/* call protocol */
141 		u_int32_t port;		/* call port (unused) */
142 	} *sdata;
143 	struct rdata {
144 		u_int16_t pad;
145 		u_int16_t port;
146 	} *rdata;
147 	struct mbuf *m;
148 	int error;
149 
150 	/* The portmapper port is fixed. */
151 	if (prog == PMAPPROG) {
152 		*portp = htons(PMAPPORT);
153 		return 0;
154 	}
155 
156 	m = m_get(M_WAIT, MT_DATA);
157 	if (m == NULL)
158 		return ENOBUFS;
159 	sdata = mtod(m, struct sdata *);
160 	m->m_len = sizeof(*sdata);
161 
162 	/* Do the RPC to get it. */
163 	sdata->prog = txdr_unsigned(prog);
164 	sdata->vers = txdr_unsigned(vers);
165 	sdata->proto = txdr_unsigned(IPPROTO_UDP);
166 	sdata->port = 0;
167 
168 	sin->sin_port = htons(PMAPPORT);
169 	error = krpc_call(sin, PMAPPROG, PMAPVERS,
170 					  PMAPPROC_GETPORT, &m, NULL, procp);
171 	if (error)
172 		return error;
173 
174 	if (m->m_len < sizeof(*rdata)) {
175 		m = m_pullup(m, sizeof(*rdata));
176 		if (m == NULL)
177 			return ENOBUFS;
178 	}
179 	rdata = mtod(m, struct rdata *);
180 	*portp = rdata->port;
181 
182 	m_freem(m);
183 	return 0;
184 }
185 
186 /*
187  * Do a remote procedure call (RPC) and wait for its reply.
188  * If from_p is non-null, then we are doing broadcast, and
189  * the address from whence the response came is saved there.
190  */
191 int
192 krpc_call(sa, prog, vers, func, data, from_p, procp)
193 	struct sockaddr_in *sa;
194 	u_int prog, vers, func;
195 	struct mbuf **data;	/* input/output */
196 	struct sockaddr **from_p;	/* output */
197 	struct proc *procp;
198 {
199 	struct socket *so;
200 	struct sockaddr_in *sin, ssin;
201 	struct sockaddr *from;
202 	struct mbuf *m, *nam, *mhead;
203 	struct rpc_call *call;
204 	struct rpc_reply *reply;
205 	struct uio auio;
206 	int error, rcvflg, timo, secs, len;
207 	static u_int32_t xid = ~0xFF;
208 	u_int16_t tport;
209 
210 	/*
211 	 * Validate address family.
212 	 * Sorry, this is INET specific...
213 	 */
214 	if (sa->sin_family != AF_INET)
215 		return (EAFNOSUPPORT);
216 
217 	/* Free at end if not null. */
218 	nam = mhead = NULL;
219 	from = NULL;
220 
221 	/*
222 	 * Create socket and set its recieve timeout.
223 	 */
224 	if ((error = socreate(AF_INET, &so, SOCK_DGRAM, 0, procp)))
225 		goto out;
226 
227 	m = m_get(M_WAIT, MT_SOOPTS);
228 	if (m == NULL) {
229 		error = ENOBUFS;
230 		goto out;
231 	} else {
232 		struct timeval *tv;
233 		tv = mtod(m, struct timeval *);
234 		m->m_len = sizeof(*tv);
235 		tv->tv_sec = 1;
236 		tv->tv_usec = 0;
237 		if ((error = sosetopt(so, SOL_SOCKET, SO_RCVTIMEO, m, procp)))
238 			goto out;
239 	}
240 
241 	/*
242 	 * Enable broadcast if necessary.
243 	 */
244 	if (from_p) {
245 		int32_t *on;
246 		m = m_get(M_WAIT, MT_SOOPTS);
247 		if (m == NULL) {
248 			error = ENOBUFS;
249 			goto out;
250 		}
251 		on = mtod(m, int32_t *);
252 		m->m_len = sizeof(*on);
253 		*on = 1;
254 		if ((error = sosetopt(so, SOL_SOCKET, SO_BROADCAST, m, procp)))
255 			goto out;
256 	}
257 
258 	/*
259 	 * Bind the local endpoint to a reserved port,
260 	 * because some NFS servers refuse requests from
261 	 * non-reserved (non-privileged) ports.
262 	 */
263 	sin = &ssin;
264 	bzero(sin, sizeof *sin);
265 	sin->sin_len = sizeof(*sin);
266 	sin->sin_family = AF_INET;
267 	sin->sin_addr.s_addr = INADDR_ANY;
268 	tport = IPPORT_RESERVED;
269 	do {
270 		tport--;
271 		sin->sin_port = htons(tport);
272 		error = sobind(so, (struct sockaddr *)sin, procp);
273 	} while (error == EADDRINUSE &&
274 			 tport > IPPORT_RESERVED / 2);
275 	if (error) {
276 		printf("bind failed\n");
277 		goto out;
278 	}
279 
280 	/*
281 	 * Setup socket address for the server.
282 	 */
283 
284 	/*
285 	 * Prepend RPC message header.
286 	 */
287 	mhead = m_gethdr(M_WAIT, MT_DATA);
288 	mhead->m_next = *data;
289 	call = mtod(mhead, struct rpc_call *);
290 	mhead->m_len = sizeof(*call);
291 	bzero((caddr_t)call, sizeof(*call));
292 	/* rpc_call part */
293 	xid++;
294 	call->rp_xid = txdr_unsigned(xid);
295 	/* call->rp_direction = 0; */
296 	call->rp_rpcvers = txdr_unsigned(2);
297 	call->rp_prog = txdr_unsigned(prog);
298 	call->rp_vers = txdr_unsigned(vers);
299 	call->rp_proc = txdr_unsigned(func);
300 	/* rpc_auth part (auth_unix as root) */
301 	call->rpc_auth.authtype = txdr_unsigned(RPCAUTH_UNIX);
302 	call->rpc_auth.authlen  = txdr_unsigned(sizeof(struct auth_unix));
303 	/* rpc_verf part (auth_null) */
304 	call->rpc_verf.authtype = 0;
305 	call->rpc_verf.authlen  = 0;
306 
307 	/*
308 	 * Setup packet header
309 	 */
310 	len = 0;
311 	m = mhead;
312 	while (m) {
313 		len += m->m_len;
314 		m = m->m_next;
315 	}
316 	mhead->m_pkthdr.len = len;
317 	mhead->m_pkthdr.rcvif = NULL;
318 
319 	/*
320 	 * Send it, repeatedly, until a reply is received,
321 	 * but delay each re-send by an increasing amount.
322 	 * If the delay hits the maximum, start complaining.
323 	 */
324 	timo = 0;
325 	for (;;) {
326 		/* Send RPC request (or re-send). */
327 		m = m_copym(mhead, 0, M_COPYALL, M_WAIT);
328 		if (m == NULL) {
329 			error = ENOBUFS;
330 			goto out;
331 		}
332 		error = sosend(so, (struct sockaddr *)sa, NULL, m,
333 			       NULL, 0, 0);
334 		if (error) {
335 			printf("krpc_call: sosend: %d\n", error);
336 			goto out;
337 		}
338 		m = NULL;
339 
340 		/* Determine new timeout. */
341 		if (timo < MAX_RESEND_DELAY)
342 			timo++;
343 		else
344 			printf("RPC timeout for server 0x%x\n",
345 			       ntohl(sin->sin_addr.s_addr));
346 
347 		/*
348 		 * Wait for up to timo seconds for a reply.
349 		 * The socket receive timeout was set to 1 second.
350 		 */
351 		secs = timo;
352 		while (secs > 0) {
353 			if (from) {
354 				FREE(from, M_SONAME);
355 				from = NULL;
356 			}
357 			if (m) {
358 				m_freem(m);
359 				m = NULL;
360 			}
361 			bzero(&auio,sizeof(auio));
362 			auio.uio_resid = len = 1<<16;
363 			rcvflg = 0;
364 			error = soreceive(so, &from, &auio, &m, NULL, &rcvflg);
365 			if (error == EWOULDBLOCK) {
366 				secs--;
367 				continue;
368 			}
369 			if (error)
370 				goto out;
371 			len -= auio.uio_resid;
372 
373 			/* Does the reply contain at least a header? */
374 			if (len < MIN_REPLY_HDR)
375 				continue;
376 			if (m->m_len < MIN_REPLY_HDR)
377 				continue;
378 			reply = mtod(m, struct rpc_reply *);
379 
380 			/* Is it the right reply? */
381 			if (reply->rp_direction != txdr_unsigned(RPC_REPLY))
382 				continue;
383 
384 			if (reply->rp_xid != txdr_unsigned(xid))
385 				continue;
386 
387 			/* Was RPC accepted? (authorization OK) */
388 			if (reply->rp_astatus != 0) {
389 				error = fxdr_unsigned(u_int32_t, reply->rp_errno);
390 				printf("rpc denied, error=%d\n", error);
391 				continue;
392 			}
393 
394 			/* Did the call succeed? */
395 			if (reply->rp_status != 0) {
396 				error = fxdr_unsigned(u_int32_t, reply->rp_status);
397 				if (error == RPC_PROGMISMATCH) {
398 				  error = EBADRPC;
399 				  goto out;
400 				}
401 				printf("rpc denied, status=%d\n", error);
402 				continue;
403 			}
404 
405 			goto gotreply;	/* break two levels */
406 
407 		} /* while secs */
408 	} /* forever send/receive */
409 
410 	error = ETIMEDOUT;
411 	goto out;
412 
413  gotreply:
414 
415 	/*
416 	 * Get RPC reply header into first mbuf,
417 	 * get its length, then strip it off.
418 	 */
419 	len = sizeof(*reply);
420 	if (m->m_len < len) {
421 		m = m_pullup(m, len);
422 		if (m == NULL) {
423 			error = ENOBUFS;
424 			goto out;
425 		}
426 	}
427 	reply = mtod(m, struct rpc_reply *);
428 	if (reply->rp_auth.authtype != 0) {
429 		len += fxdr_unsigned(u_int32_t, reply->rp_auth.authlen);
430 		len = (len + 3) & ~3; /* XXX? */
431 	}
432 	m_adj(m, len);
433 
434 	/* result */
435 	*data = m;
436 	if (from_p) {
437 		*from_p = from;
438 		from = NULL;
439 	}
440 
441  out:
442 	if (mhead) m_freem(mhead);
443 	if (from) free(from, M_SONAME);
444 	soclose(so);
445 	return error;
446 }
447 
448 /*
449  * eXternal Data Representation routines.
450  * (but with non-standard args...)
451  */
452 
453 /*
454  * String representation for RPC.
455  */
456 struct xdr_string {
457 	u_int32_t len;		/* length without null or padding */
458 	char data[4];	/* data (longer, of course) */
459     /* data is padded to a long-word boundary */
460 };
461 
462 struct mbuf *
463 xdr_string_encode(str, len)
464 	char *str;
465 	int len;
466 {
467 	struct mbuf *m;
468 	struct xdr_string *xs;
469 	int dlen;	/* padded string length */
470 	int mlen;	/* message length */
471 
472 	dlen = (len + 3) & ~3;
473 	mlen = dlen + 4;
474 
475 	if (mlen > MCLBYTES)		/* If too big, we just can't do it. */
476 		return (NULL);
477 
478 	m = m_get(M_WAIT, MT_DATA);
479 	if (mlen > MLEN) {
480 		MCLGET(m, M_WAIT);
481 		if ((m->m_flags & M_EXT) == 0) {
482 			(void) m_free(m);	/* There can be only one. */
483 			return (NULL);
484 		}
485 	}
486 	xs = mtod(m, struct xdr_string *);
487 	m->m_len = mlen;
488 	xs->len = txdr_unsigned(len);
489 	bcopy(str, xs->data, len);
490 	return (m);
491 }
492