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