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