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