xref: /dragonfly/sys/vfs/nfs/krpc_subr.c (revision d4ef6694)
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 
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/malloc.h>
49 #include <sys/mbuf.h>
50 #include <sys/socket.h>
51 #include <sys/socketvar.h>
52 #include <sys/uio.h>
53 #include <sys/fcntl.h>
54 
55 #include <net/if.h>
56 #include <netinet/in.h>
57 
58 #include "rpcv2.h"
59 #include "krpc.h"
60 #include "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(struct sockaddr_in *sin,	/* server address */
129 	     u_int prog, u_int vers,	/* host order */
130 	     u_int16_t *portp,		/* network order */
131 	     struct thread *td)
132 {
133 	struct sdata {
134 		u_int32_t prog;		/* call program */
135 		u_int32_t vers;		/* call version */
136 		u_int32_t proto;	/* call protocol */
137 		u_int32_t port;		/* call port (unused) */
138 	} *sdata;
139 	struct rdata {
140 		u_int16_t pad;
141 		u_int16_t port;
142 	} *rdata;
143 	struct mbuf *m;
144 	int error;
145 
146 	/* The portmapper port is fixed. */
147 	if (prog == PMAPPROG) {
148 		*portp = htons(PMAPPORT);
149 		return 0;
150 	}
151 
152 	m = m_get(MB_WAIT, MT_DATA);
153 	if (m == NULL)
154 		return ENOBUFS;
155 	sdata = mtod(m, struct sdata *);
156 	m->m_len = sizeof(*sdata);
157 
158 	/* Do the RPC to get it. */
159 	sdata->prog = txdr_unsigned(prog);
160 	sdata->vers = txdr_unsigned(vers);
161 	sdata->proto = txdr_unsigned(IPPROTO_UDP);
162 	sdata->port = 0;
163 
164 	sin->sin_port = htons(PMAPPORT);
165 	error = krpc_call(sin, PMAPPROG, PMAPVERS,
166 					  PMAPPROC_GETPORT, &m, NULL, td);
167 	if (error)
168 		return error;
169 
170 	if (m->m_len < sizeof(*rdata)) {
171 		m = m_pullup(m, sizeof(*rdata));
172 		if (m == NULL)
173 			return ENOBUFS;
174 	}
175 	rdata = mtod(m, struct rdata *);
176 	*portp = rdata->port;
177 
178 	m_freem(m);
179 	return 0;
180 }
181 
182 /*
183  * Do a remote procedure call (RPC) and wait for its reply.
184  * If from_p is non-null, then we are doing broadcast, and
185  * the address from whence the response came is saved there.
186  */
187 int
188 krpc_call(struct sockaddr_in *sa, u_int prog, u_int vers, u_int func,
189 	  struct mbuf **data,		/* input/output */
190 	  struct sockaddr **from_p,	/* output */
191 	  struct thread *td)
192 {
193 	struct socket *so;
194 	struct sockaddr_in *sin, ssin;
195 	struct sockaddr *from;
196 	struct mbuf *m, *mhead;
197 	struct rpc_call *call;
198 	struct rpc_reply *reply;
199 	struct sockopt sopt;
200 	struct timeval tv;
201 	struct sockbuf sio;
202 	int error, rcvflg, timo, secs, len;
203 	static u_int32_t static_xid = ~0xFF;
204 	u_int32_t xid;
205 	u_int16_t tport;
206 	u_int32_t saddr;
207 
208 	/*
209 	 * Validate address family.
210 	 * Sorry, this is INET specific...
211 	 */
212 	if (sa->sin_family != AF_INET)
213 		return (EAFNOSUPPORT);
214 
215 	/* Free at end if not null. */
216 	mhead = NULL;
217 	from = NULL;
218 
219 	/*
220 	 * Create socket and set its recieve timeout.
221 	 */
222 	if ((error = socreate(AF_INET, &so, SOCK_DGRAM, 0, td)))
223 		goto out;
224 
225 	tv.tv_sec = 1;
226 	tv.tv_usec = 0;
227 	bzero(&sopt, sizeof sopt);
228 	sopt.sopt_level = SOL_SOCKET;
229 	sopt.sopt_name = SO_RCVTIMEO;
230 	sopt.sopt_val = &tv;
231 	sopt.sopt_valsize = sizeof tv;
232 
233 	if ((error = sosetopt(so, &sopt)) != 0)
234 		goto out;
235 
236 	/*
237 	 * Enable broadcast if necessary.
238 	 */
239 	if (from_p) {
240 		int on = 1;
241 		sopt.sopt_name = SO_BROADCAST;
242 		sopt.sopt_val = &on;
243 		sopt.sopt_valsize = sizeof on;
244 		if ((error = sosetopt(so, &sopt)) != 0)
245 			goto out;
246 	}
247 
248 	/*
249 	 * Bind the local endpoint to a reserved port,
250 	 * because some NFS servers refuse requests from
251 	 * non-reserved (non-privileged) ports.
252 	 */
253 	sin = &ssin;
254 	bzero(sin, sizeof *sin);
255 	sin->sin_len = sizeof(*sin);
256 	sin->sin_family = AF_INET;
257 	sin->sin_addr.s_addr = INADDR_ANY;
258 	tport = IPPORT_RESERVED;
259 	do {
260 		tport--;
261 		sin->sin_port = htons(tport);
262 		error = sobind(so, (struct sockaddr *)sin, td);
263 	} while (error == EADDRINUSE &&
264 			 tport > IPPORT_RESERVED / 2);
265 	if (error) {
266 		kprintf("bind failed\n");
267 		goto out;
268 	}
269 
270 	/*
271 	 * Setup socket address for the server.
272 	 */
273 
274 	/*
275 	 * Prepend RPC message header.
276 	 */
277 	mhead = m_gethdr(MB_WAIT, MT_DATA);
278 	mhead->m_next = *data;
279 	call = mtod(mhead, struct rpc_call *);
280 	mhead->m_len = sizeof(*call);
281 	bzero((caddr_t)call, sizeof(*call));
282 	/* rpc_call part */
283 	do {
284 		xid = atomic_fetchadd_int(&static_xid, 1);
285 	} while (xid == 0);
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, MB_WAIT);
320 		if (m == NULL) {
321 			error = ENOBUFS;
322 			goto out;
323 		}
324 		error = sosend(so, (struct sockaddr *)sa, NULL, m,
325 			       NULL, 0, td);
326 		if (error) {
327 			kprintf("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 			kprintf("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 				kfree(from, M_SONAME);
352 				from = NULL;
353 			}
354 			if (m) {
355 				m_freem(m);
356 				m = NULL;
357 			}
358 			len = 1 << 16;
359 			sbinit(&sio, len);
360 			rcvflg = 0;
361 			error = soreceive(so, &from, NULL, &sio, NULL, &rcvflg);
362 			if (error == EWOULDBLOCK) {
363 				secs--;
364 				continue;
365 			}
366 			if (error)
367 				goto out;
368 			len -= sio.sb_cc;
369 			m = sio.sb_mb;
370 
371 			/* Does the reply contain at least a header? */
372 			if (len < MIN_REPLY_HDR)
373 				continue;
374 			if (m->m_len < MIN_REPLY_HDR)
375 				continue;
376 			reply = mtod(m, struct rpc_reply *);
377 
378 			/* Is it the right reply? */
379 			if (reply->rp_direction != txdr_unsigned(RPC_REPLY))
380 				continue;
381 
382 			if (reply->rp_xid != txdr_unsigned(xid))
383 				continue;
384 
385 			/* Was RPC accepted? (authorization OK) */
386 			if (reply->rp_astatus != 0) {
387 				error = fxdr_unsigned(u_int32_t, reply->rp_errno);
388 				kprintf("rpc denied, error=%d\n", error);
389 				continue;
390 			}
391 
392 			/* Did the call succeed? */
393 			if (reply->rp_status != 0) {
394 				error = fxdr_unsigned(u_int32_t, reply->rp_status);
395 				if (error == RPC_PROGMISMATCH) {
396 				  error = EBADRPC;
397 				  goto out;
398 				}
399 				kprintf("rpc denied, status=%d\n", error);
400 				continue;
401 			}
402 
403 			goto gotreply;	/* break two levels */
404 
405 		} /* while secs */
406 	} /* forever send/receive */
407 
408 	error = ETIMEDOUT;
409 	goto out;
410 
411  gotreply:
412 
413 	/*
414 	 * Get RPC reply header into first mbuf,
415 	 * get its length, then strip it off.
416 	 */
417 	len = sizeof(*reply);
418 	if (m->m_len < len) {
419 		m = m_pullup(m, len);
420 		if (m == NULL) {
421 			error = ENOBUFS;
422 			goto out;
423 		}
424 	}
425 	reply = mtod(m, struct rpc_reply *);
426 	if (reply->rp_auth.authtype != 0) {
427 		len += fxdr_unsigned(u_int32_t, reply->rp_auth.authlen);
428 		len = (len + 3) & ~3; /* XXX? */
429 	}
430 	m_adj(m, len);
431 
432 	/* result */
433 	*data = m;
434 	if (from_p) {
435 		*from_p = from;
436 		from = NULL;
437 	}
438 
439  out:
440 	if (mhead) m_freem(mhead);
441 	if (from) kfree(from, M_SONAME);
442 	soclose(so, FNONBLOCK);
443 	return error;
444 }
445 
446 /*
447  * eXternal Data Representation routines.
448  * (but with non-standard args...)
449  */
450 
451 /*
452  * String representation for RPC.
453  */
454 struct xdr_string {
455 	u_int32_t len;		/* length without null or padding */
456 	char data[4];	/* data (longer, of course) */
457     /* data is padded to a long-word boundary */
458 };
459 
460 struct mbuf *
461 xdr_string_encode(char *str, int len)
462 {
463 	struct mbuf *m;
464 	struct xdr_string *xs;
465 	int dlen;	/* padded string length */
466 	int mlen;	/* message length */
467 
468 	dlen = (len + 3) & ~3;
469 	mlen = dlen + 4;
470 
471 	if (mlen > MCLBYTES)		/* If too big, we just can't do it. */
472 		return (NULL);
473 
474 	m = m_getl(mlen, MB_WAIT, MT_DATA, 0, NULL);
475 	xs = mtod(m, struct xdr_string *);
476 	m->m_len = mlen;
477 	xs->len = txdr_unsigned(len);
478 	bcopy(str, xs->data, len);
479 	return (m);
480 }
481