xref: /freebsd/sys/nfs/krpc_subr.c (revision 2b15cb3d0922bd70ea592f0da9b4a5b167f4d53f)
1 /*	$NetBSD: krpc_subr.c,v 1.12.4.1 1996/06/07 00:52:26 cgd 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/cdefs.h>
46 __FBSDID("$FreeBSD$");
47 
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/jail.h>
51 #include <sys/malloc.h>
52 #include <sys/mbuf.h>
53 #include <sys/proc.h>
54 #include <sys/socket.h>
55 #include <sys/socketvar.h>
56 #include <sys/uio.h>
57 
58 #include <net/if.h>
59 #include <net/vnet.h>
60 
61 #include <netinet/in.h>
62 
63 #include <rpc/types.h>
64 #include <rpc/auth.h>
65 #include <rpc/rpc_msg.h>
66 #include <nfs/krpc.h>
67 #include <nfs/xdr_subs.h>
68 
69 /*
70  * Kernel support for Sun RPC
71  *
72  * Used currently for bootstrapping in nfs diskless configurations.
73  */
74 
75 /*
76  * Generic RPC headers
77  */
78 
79 struct auth_info {
80 	u_int32_t 	authtype;	/* auth type */
81 	u_int32_t	authlen;	/* auth length */
82 };
83 
84 struct auth_unix {
85 	int32_t   ua_time;
86 	int32_t   ua_hostname;	/* null */
87 	int32_t   ua_uid;
88 	int32_t   ua_gid;
89 	int32_t   ua_gidlist;	/* null */
90 };
91 
92 struct krpc_call {
93 	u_int32_t	rp_xid;		/* request transaction id */
94 	int32_t 	rp_direction;	/* call direction (0) */
95 	u_int32_t	rp_rpcvers;	/* rpc version (2) */
96 	u_int32_t	rp_prog;	/* program */
97 	u_int32_t	rp_vers;	/* version */
98 	u_int32_t	rp_proc;	/* procedure */
99 	struct	auth_info rpc_auth;
100 	struct	auth_unix rpc_unix;
101 	struct	auth_info rpc_verf;
102 };
103 
104 struct krpc_reply {
105 	u_int32_t rp_xid;		/* request transaction id */
106 	int32_t  rp_direction;		/* call direction (1) */
107 	int32_t  rp_astatus;		/* accept status (0: accepted) */
108 	union {
109 		u_int32_t rpu_errno;
110 		struct {
111 			struct auth_info rok_auth;
112 			u_int32_t	rok_status;
113 		} rpu_rok;
114 	} rp_u;
115 };
116 #define rp_errno  rp_u.rpu_errno
117 #define rp_auth   rp_u.rpu_rok.rok_auth
118 #define rp_status rp_u.rpu_rok.rok_status
119 
120 #define MIN_REPLY_HDR 16	/* xid, dir, astat, errno */
121 
122 /*
123  * What is the longest we will wait before re-sending a request?
124  * Note this is also the frequency of "RPC timeout" messages.
125  * The re-send loop count sup linearly to this maximum, so the
126  * first complaint will happen after (1+2+3+4+5)=15 seconds.
127  */
128 #define	MAX_RESEND_DELAY 5	/* seconds */
129 
130 /*
131  * Call portmap to lookup a port number for a particular rpc program
132  * Returns non-zero error on failure.
133  */
134 int
135 krpc_portmap(struct sockaddr_in *sin, u_int prog, u_int vers, u_int16_t *portp,
136     struct thread *td)
137 {
138 	struct sdata {
139 		u_int32_t prog;		/* call program */
140 		u_int32_t vers;		/* call version */
141 		u_int32_t proto;	/* call protocol */
142 		u_int32_t port;		/* call port (unused) */
143 	} *sdata;
144 	struct rdata {
145 		u_int16_t pad;
146 		u_int16_t port;
147 	} *rdata;
148 	struct mbuf *m;
149 	int error;
150 
151 	/* The portmapper port is fixed. */
152 	if (prog == PMAPPROG) {
153 		*portp = htons(PMAPPORT);
154 		return 0;
155 	}
156 
157 	m = m_get(M_WAITOK, MT_DATA);
158 	sdata = mtod(m, struct sdata *);
159 	m->m_len = sizeof(*sdata);
160 
161 	/* Do the RPC to get it. */
162 	sdata->prog = txdr_unsigned(prog);
163 	sdata->vers = txdr_unsigned(vers);
164 	sdata->proto = txdr_unsigned(IPPROTO_UDP);
165 	sdata->port = 0;
166 
167 	sin->sin_port = htons(PMAPPORT);
168 	error = krpc_call(sin, PMAPPROG, PMAPVERS,
169 					  PMAPPROC_GETPORT, &m, NULL, td);
170 	if (error)
171 		return error;
172 
173 	if (m->m_len < sizeof(*rdata)) {
174 		m = m_pullup(m, sizeof(*rdata));
175 		if (m == NULL)
176 			return ENOBUFS;
177 	}
178 	rdata = mtod(m, struct rdata *);
179 	*portp = rdata->port;
180 
181 	m_freem(m);
182 	return 0;
183 }
184 
185 /*
186  * Do a remote procedure call (RPC) and wait for its reply.
187  * If from_p is non-null, then we are doing broadcast, and
188  * the address from whence the response came is saved there.
189  */
190 int
191 krpc_call(struct sockaddr_in *sa, u_int prog, u_int vers, u_int func,
192     struct mbuf **data, struct sockaddr **from_p, struct thread *td)
193 {
194 	struct socket *so;
195 	struct sockaddr_in *sin, ssin;
196 	struct sockaddr *from;
197 	struct mbuf *m, *nam, *mhead;
198 	struct krpc_call *call;
199 	struct krpc_reply *reply;
200 	struct sockopt sopt;
201 	struct timeval tv;
202 	struct uio auio;
203 	int error, rcvflg, timo, secs, len;
204 	static u_int32_t xid = ~0xFF;
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 	nam = 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->td_ucred, td)))
223 		return error;
224 
225 	tv.tv_sec = 1;
226 	tv.tv_usec = 0;
227 	bzero(&sopt, sizeof sopt);
228 	sopt.sopt_dir = SOPT_SET;
229 	sopt.sopt_level = SOL_SOCKET;
230 	sopt.sopt_name = SO_RCVTIMEO;
231 	sopt.sopt_val = &tv;
232 	sopt.sopt_valsize = sizeof tv;
233 
234 	if ((error = sosetopt(so, &sopt)) != 0)
235 		goto out;
236 
237 	/*
238 	 * Enable broadcast if necessary.
239 	 */
240 	if (from_p) {
241 		int on = 1;
242 		sopt.sopt_name = SO_BROADCAST;
243 		sopt.sopt_val = &on;
244 		sopt.sopt_valsize = sizeof on;
245 		if ((error = sosetopt(so, &sopt)) != 0)
246 			goto out;
247 	}
248 
249 	/*
250 	 * Bind the local endpoint to a reserved port,
251 	 * because some NFS servers refuse requests from
252 	 * non-reserved (non-privileged) ports.
253 	 */
254 	sin = &ssin;
255 	bzero(sin, sizeof *sin);
256 	sin->sin_len = sizeof(*sin);
257 	sin->sin_family = AF_INET;
258 	sin->sin_addr.s_addr = INADDR_ANY;
259 	tport = IPPORT_RESERVED;
260 	do {
261 		tport--;
262 		sin->sin_port = htons(tport);
263 		error = sobind(so, (struct sockaddr *)sin, td);
264 	} while (error == EADDRINUSE &&
265 			 tport > IPPORT_RESERVED / 2);
266 	if (error) {
267 		printf("bind failed\n");
268 		goto out;
269 	}
270 
271 	/*
272 	 * Setup socket address for the server.
273 	 */
274 
275 	/*
276 	 * Prepend RPC message header.
277 	 */
278 	mhead = m_gethdr(M_WAITOK, MT_DATA);
279 	mhead->m_next = *data;
280 	call = mtod(mhead, struct krpc_call *);
281 	mhead->m_len = sizeof(*call);
282 	bzero((caddr_t)call, sizeof(*call));
283 	/* rpc_call part */
284 	xid++;
285 	call->rp_xid = txdr_unsigned(xid);
286 	/* call->rp_direction = 0; */
287 	call->rp_rpcvers = txdr_unsigned(2);
288 	call->rp_prog = txdr_unsigned(prog);
289 	call->rp_vers = txdr_unsigned(vers);
290 	call->rp_proc = txdr_unsigned(func);
291 	/* rpc_auth part (auth_unix as root) */
292 	call->rpc_auth.authtype = txdr_unsigned(AUTH_UNIX);
293 	call->rpc_auth.authlen  = txdr_unsigned(sizeof(struct auth_unix));
294 	/* rpc_verf part (auth_null) */
295 	call->rpc_verf.authtype = 0;
296 	call->rpc_verf.authlen  = 0;
297 
298 	/*
299 	 * Setup packet header
300 	 */
301 	m_fixhdr(mhead);
302 	mhead->m_pkthdr.rcvif = NULL;
303 
304 	/*
305 	 * Send it, repeatedly, until a reply is received,
306 	 * but delay each re-send by an increasing amount.
307 	 * If the delay hits the maximum, start complaining.
308 	 */
309 	timo = 0;
310 	for (;;) {
311 		/* Send RPC request (or re-send). */
312 		m = m_copym(mhead, 0, M_COPYALL, M_WAITOK);
313 		error = sosend(so, (struct sockaddr *)sa, NULL, m,
314 			       NULL, 0, td);
315 		if (error) {
316 			printf("krpc_call: sosend: %d\n", error);
317 			goto out;
318 		}
319 		m = NULL;
320 
321 		/* Determine new timeout. */
322 		if (timo < MAX_RESEND_DELAY)
323 			timo++;
324 		else {
325 			saddr = ntohl(sa->sin_addr.s_addr);
326 			printf("RPC timeout for server %d.%d.%d.%d\n",
327 			       (saddr >> 24) & 255,
328 			       (saddr >> 16) & 255,
329 			       (saddr >> 8) & 255,
330 			       saddr & 255);
331 		}
332 
333 		/*
334 		 * Wait for up to timo seconds for a reply.
335 		 * The socket receive timeout was set to 1 second.
336 		 */
337 		secs = timo;
338 		while (secs > 0) {
339 			if (from) {
340 				free(from, M_SONAME);
341 				from = NULL;
342 			}
343 			if (m) {
344 				m_freem(m);
345 				m = NULL;
346 			}
347 			bzero(&auio, sizeof(auio));
348 			auio.uio_resid = len = 1<<16;
349 			rcvflg = 0;
350 			error = soreceive(so, &from, &auio, &m, NULL, &rcvflg);
351 			if (error == EWOULDBLOCK) {
352 				secs--;
353 				continue;
354 			}
355 			if (error)
356 				goto out;
357 			len -= auio.uio_resid;
358 
359 			/* Does the reply contain at least a header? */
360 			if (len < MIN_REPLY_HDR)
361 				continue;
362 			if (m->m_len < MIN_REPLY_HDR)
363 				continue;
364 			reply = mtod(m, struct krpc_reply *);
365 
366 			/* Is it the right reply? */
367 			if (reply->rp_direction != txdr_unsigned(REPLY))
368 				continue;
369 
370 			if (reply->rp_xid != txdr_unsigned(xid))
371 				continue;
372 
373 			/* Was RPC accepted? (authorization OK) */
374 			if (reply->rp_astatus != 0) {
375 				error = fxdr_unsigned(u_int32_t, reply->rp_errno);
376 				printf("rpc denied, error=%d\n", error);
377 				continue;
378 			}
379 
380 			/* Did the call succeed? */
381 			if (reply->rp_status != 0) {
382 				error = fxdr_unsigned(u_int32_t, reply->rp_status);
383 				if (error == PROG_MISMATCH) {
384 				  error = EBADRPC;
385 				  goto out;
386 				}
387 				printf("rpc denied, status=%d\n", error);
388 				continue;
389 			}
390 
391 			goto gotreply;	/* break two levels */
392 
393 		} /* while secs */
394 	} /* forever send/receive */
395 
396 	error = ETIMEDOUT;
397 	goto out;
398 
399  gotreply:
400 
401 	/*
402 	 * Get RPC reply header into first mbuf,
403 	 * get its length, then strip it off.
404 	 */
405 	len = sizeof(*reply);
406 	if (m->m_len < len) {
407 		m = m_pullup(m, len);
408 		if (m == NULL) {
409 			error = ENOBUFS;
410 			goto out;
411 		}
412 	}
413 	reply = mtod(m, struct krpc_reply *);
414 	if (reply->rp_auth.authtype != 0) {
415 		len += fxdr_unsigned(u_int32_t, reply->rp_auth.authlen);
416 		len = (len + 3) & ~3; /* XXX? */
417 	}
418 	m_adj(m, len);
419 
420 	/* result */
421 	*data = m;
422 	if (from_p) {
423 		*from_p = from;
424 		from = NULL;
425 	}
426 
427  out:
428 	if (mhead) m_freem(mhead);
429 	if (from) free(from, M_SONAME);
430 	soclose(so);
431 	return error;
432 }
433 
434 /*
435  * eXternal Data Representation routines.
436  * (but with non-standard args...)
437  */
438 
439 /*
440  * String representation for RPC.
441  */
442 struct xdr_string {
443 	u_int32_t len;		/* length without null or padding */
444 	char data[4];	/* data (longer, of course) */
445     /* data is padded to a long-word boundary */
446 };
447 
448 struct mbuf *
449 xdr_string_encode(char *str, int len)
450 {
451 	struct mbuf *m;
452 	struct xdr_string *xs;
453 	int dlen;	/* padded string length */
454 	int mlen;	/* message length */
455 
456 	dlen = (len + 3) & ~3;
457 	mlen = dlen + 4;
458 
459 	if (mlen > MCLBYTES)		/* If too big, we just can't do it. */
460 		return (NULL);
461 
462 	m = m_get2(mlen, M_WAITOK, MT_DATA, 0);
463 	xs = mtod(m, struct xdr_string *);
464 	m->m_len = mlen;
465 	xs->len = txdr_unsigned(len);
466 	bcopy(str, xs->data, len);
467 	return (m);
468 }
469