krpc_subr.c revision 34924
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.8 1998/03/14 03:25:16 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
54#include <net/if.h>
55#include <netinet/in.h>
56
57#include <nfs/rpcv2.h>
58#include <nfs/krpc.h>
59#include <nfs/xdr_subs.h>
60
61/*
62 * Kernel support for Sun RPC
63 *
64 * Used currently for bootstrapping in nfs diskless configurations.
65 */
66
67/*
68 * Generic RPC headers
69 */
70
71struct auth_info {
72	u_int32_t 	authtype;	/* auth type */
73	u_int32_t	authlen;	/* auth length */
74};
75
76struct auth_unix {
77	int32_t   ua_time;
78	int32_t   ua_hostname;	/* null */
79	int32_t   ua_uid;
80	int32_t   ua_gid;
81	int32_t   ua_gidlist;	/* null */
82};
83
84struct rpc_call {
85	u_int32_t	rp_xid;		/* request transaction id */
86	int32_t 	rp_direction;	/* call direction (0) */
87	u_int32_t	rp_rpcvers;	/* rpc version (2) */
88	u_int32_t	rp_prog;	/* program */
89	u_int32_t	rp_vers;	/* version */
90	u_int32_t	rp_proc;	/* procedure */
91	struct	auth_info rpc_auth;
92	struct	auth_unix rpc_unix;
93	struct	auth_info rpc_verf;
94};
95
96struct rpc_reply {
97	u_int32_t rp_xid;		/* request transaction id */
98	int32_t  rp_direction;		/* call direction (1) */
99	int32_t  rp_astatus;		/* accept status (0: accepted) */
100	union {
101		u_int32_t rpu_errno;
102		struct {
103			struct auth_info rok_auth;
104			u_int32_t	rok_status;
105		} rpu_rok;
106	} rp_u;
107};
108#define rp_errno  rp_u.rpu_errno
109#define rp_auth   rp_u.rpu_rok.rok_auth
110#define rp_status rp_u.rpu_rok.rok_status
111
112#define MIN_REPLY_HDR 16	/* xid, dir, astat, errno */
113
114/*
115 * What is the longest we will wait before re-sending a request?
116 * Note this is also the frequency of "RPC timeout" messages.
117 * The re-send loop count sup linearly to this maximum, so the
118 * first complaint will happen after (1+2+3+4+5)=15 seconds.
119 */
120#define	MAX_RESEND_DELAY 5	/* seconds */
121
122/*
123 * Call portmap to lookup a port number for a particular rpc program
124 * Returns non-zero error on failure.
125 */
126int
127krpc_portmap(sin,  prog, vers, portp, procp)
128	struct sockaddr_in *sin;		/* server address */
129	u_int prog, vers;	/* host order */
130	u_int16_t *portp;	/* network order */
131	struct proc *procp;
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(M_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, procp);
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 */
187int
188krpc_call(sa, prog, vers, func, data, from_p, procp)
189	struct sockaddr_in *sa;
190	u_int prog, vers, func;
191	struct mbuf **data;	/* input/output */
192	struct sockaddr **from_p;	/* output */
193	struct proc *procp;
194{
195	struct socket *so;
196	struct sockaddr_in *sin, ssin;
197	struct sockaddr *from;
198	struct mbuf *m, *nam, *mhead;
199	struct rpc_call *call;
200	struct rpc_reply *reply;
201	struct uio auio;
202	int error, rcvflg, timo, secs, len;
203	static u_int32_t xid = ~0xFF;
204	u_int16_t tport;
205
206	/*
207	 * Validate address family.
208	 * Sorry, this is INET specific...
209	 */
210	if (sa->sin_family != AF_INET)
211		return (EAFNOSUPPORT);
212
213	/* Free at end if not null. */
214	nam = mhead = NULL;
215	from = NULL;
216
217	/*
218	 * Create socket and set its recieve timeout.
219	 */
220	if ((error = socreate(AF_INET, &so, SOCK_DGRAM, 0, procp)))
221		goto out;
222
223	m = m_get(M_WAIT, MT_SOOPTS);
224	if (m == NULL) {
225		error = ENOBUFS;
226		goto out;
227	} else {
228		struct timeval *tv;
229		tv = mtod(m, struct timeval *);
230		m->m_len = sizeof(*tv);
231		tv->tv_sec = 1;
232		tv->tv_usec = 0;
233		if ((error = sosetopt(so, SOL_SOCKET, SO_RCVTIMEO, m, procp)))
234			goto out;
235	}
236
237	/*
238	 * Enable broadcast if necessary.
239	 */
240	if (from_p) {
241		int32_t *on;
242		m = m_get(M_WAIT, MT_SOOPTS);
243		if (m == NULL) {
244			error = ENOBUFS;
245			goto out;
246		}
247		on = mtod(m, int32_t *);
248		m->m_len = sizeof(*on);
249		*on = 1;
250		if ((error = sosetopt(so, SOL_SOCKET, SO_BROADCAST, m, procp)))
251			goto out;
252	}
253
254	/*
255	 * Bind the local endpoint to a reserved port,
256	 * because some NFS servers refuse requests from
257	 * non-reserved (non-privileged) ports.
258	 */
259	sin = &ssin;
260	bzero(sin, sizeof *sin);
261	sin->sin_len = sizeof(*sin);
262	sin->sin_family = AF_INET;
263	sin->sin_addr.s_addr = INADDR_ANY;
264	tport = IPPORT_RESERVED;
265	do {
266		tport--;
267		sin->sin_port = htons(tport);
268		error = sobind(so, (struct sockaddr *)sin, procp);
269	} while (error == EADDRINUSE &&
270			 tport > IPPORT_RESERVED / 2);
271	if (error) {
272		printf("bind failed\n");
273		goto out;
274	}
275
276	/*
277	 * Setup socket address for the server.
278	 */
279
280	/*
281	 * Prepend RPC message header.
282	 */
283	mhead = m_gethdr(M_WAIT, MT_DATA);
284	mhead->m_next = *data;
285	call = mtod(mhead, struct rpc_call *);
286	mhead->m_len = sizeof(*call);
287	bzero((caddr_t)call, sizeof(*call));
288	/* rpc_call part */
289	xid++;
290	call->rp_xid = txdr_unsigned(xid);
291	/* call->rp_direction = 0; */
292	call->rp_rpcvers = txdr_unsigned(2);
293	call->rp_prog = txdr_unsigned(prog);
294	call->rp_vers = txdr_unsigned(vers);
295	call->rp_proc = txdr_unsigned(func);
296	/* rpc_auth part (auth_unix as root) */
297	call->rpc_auth.authtype = txdr_unsigned(RPCAUTH_UNIX);
298	call->rpc_auth.authlen  = txdr_unsigned(sizeof(struct auth_unix));
299	/* rpc_verf part (auth_null) */
300	call->rpc_verf.authtype = 0;
301	call->rpc_verf.authlen  = 0;
302
303	/*
304	 * Setup packet header
305	 */
306	len = 0;
307	m = mhead;
308	while (m) {
309		len += m->m_len;
310		m = m->m_next;
311	}
312	mhead->m_pkthdr.len = len;
313	mhead->m_pkthdr.rcvif = NULL;
314
315	/*
316	 * Send it, repeatedly, until a reply is received,
317	 * but delay each re-send by an increasing amount.
318	 * If the delay hits the maximum, start complaining.
319	 */
320	timo = 0;
321	for (;;) {
322		/* Send RPC request (or re-send). */
323		m = m_copym(mhead, 0, M_COPYALL, M_WAIT);
324		if (m == NULL) {
325			error = ENOBUFS;
326			goto out;
327		}
328		error = sosend(so, (struct sockaddr *)sa, NULL, m,
329			       NULL, 0, 0);
330		if (error) {
331			printf("krpc_call: sosend: %d\n", error);
332			goto out;
333		}
334		m = NULL;
335
336		/* Determine new timeout. */
337		if (timo < MAX_RESEND_DELAY)
338			timo++;
339		else
340			printf("RPC timeout for server 0x%x\n",
341			       ntohl(sa->sin_addr.s_addr));
342
343		/*
344		 * Wait for up to timo seconds for a reply.
345		 * The socket receive timeout was set to 1 second.
346		 */
347		secs = timo;
348		while (secs > 0) {
349			if (from) {
350				FREE(from, M_SONAME);
351				from = NULL;
352			}
353			if (m) {
354				m_freem(m);
355				m = NULL;
356			}
357			bzero(&auio,sizeof(auio));
358			auio.uio_resid = len = 1<<16;
359			rcvflg = 0;
360			error = soreceive(so, &from, &auio, &m, NULL, &rcvflg);
361			if (error == EWOULDBLOCK) {
362				secs--;
363				continue;
364			}
365			if (error)
366				goto out;
367			len -= auio.uio_resid;
368
369			/* Does the reply contain at least a header? */
370			if (len < MIN_REPLY_HDR)
371				continue;
372			if (m->m_len < MIN_REPLY_HDR)
373				continue;
374			reply = mtod(m, struct rpc_reply *);
375
376			/* Is it the right reply? */
377			if (reply->rp_direction != txdr_unsigned(RPC_REPLY))
378				continue;
379
380			if (reply->rp_xid != txdr_unsigned(xid))
381				continue;
382
383			/* Was RPC accepted? (authorization OK) */
384			if (reply->rp_astatus != 0) {
385				error = fxdr_unsigned(u_int32_t, reply->rp_errno);
386				printf("rpc denied, error=%d\n", error);
387				continue;
388			}
389
390			/* Did the call succeed? */
391			if (reply->rp_status != 0) {
392				error = fxdr_unsigned(u_int32_t, reply->rp_status);
393				if (error == RPC_PROGMISMATCH) {
394				  error = EBADRPC;
395				  goto out;
396				}
397				printf("rpc denied, status=%d\n", error);
398				continue;
399			}
400
401			goto gotreply;	/* break two levels */
402
403		} /* while secs */
404	} /* forever send/receive */
405
406	error = ETIMEDOUT;
407	goto out;
408
409 gotreply:
410
411	/*
412	 * Get RPC reply header into first mbuf,
413	 * get its length, then strip it off.
414	 */
415	len = sizeof(*reply);
416	if (m->m_len < len) {
417		m = m_pullup(m, len);
418		if (m == NULL) {
419			error = ENOBUFS;
420			goto out;
421		}
422	}
423	reply = mtod(m, struct rpc_reply *);
424	if (reply->rp_auth.authtype != 0) {
425		len += fxdr_unsigned(u_int32_t, reply->rp_auth.authlen);
426		len = (len + 3) & ~3; /* XXX? */
427	}
428	m_adj(m, len);
429
430	/* result */
431	*data = m;
432	if (from_p) {
433		*from_p = from;
434		from = NULL;
435	}
436
437 out:
438	if (mhead) m_freem(mhead);
439	if (from) free(from, M_SONAME);
440	soclose(so);
441	return error;
442}
443
444/*
445 * eXternal Data Representation routines.
446 * (but with non-standard args...)
447 */
448
449/*
450 * String representation for RPC.
451 */
452struct xdr_string {
453	u_int32_t len;		/* length without null or padding */
454	char data[4];	/* data (longer, of course) */
455    /* data is padded to a long-word boundary */
456};
457
458struct mbuf *
459xdr_string_encode(str, len)
460	char *str;
461	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_get(M_WAIT, MT_DATA);
475	if (mlen > MLEN) {
476		MCLGET(m, M_WAIT);
477		if ((m->m_flags & M_EXT) == 0) {
478			(void) m_free(m);	/* There can be only one. */
479			return (NULL);
480		}
481	}
482	xs = mtod(m, struct xdr_string *);
483	m->m_len = mlen;
484	xs->len = txdr_unsigned(len);
485	bcopy(str, xs->data, len);
486	return (m);
487}
488