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