xref: /netbsd/sys/fs/nfs/common/krpc_subr.c (revision 86c9ab39)
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: head/sys/nfs/krpc_subr.c 298788 2016-04-29 16:07:25Z pfg "); */
47 __RCSID("$NetBSD: krpc_subr.c,v 1.6 2016/11/18 22:37:50 pgoyette Exp $");
48 
49 #include <sys/param.h>
50 #include <sys/systm.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 
60 #include <netinet/in.h>
61 
62 #include <fs/nfs/common/krpc.h>
63 #include <fs/nfs/common/xdr_subs.h>
64 
65 /*
66  * Kernel support for Sun RPC
67  *
68  * Used currently for bootstrapping in nfs diskless configurations.
69  */
70 
71 /*
72  * Generic RPC headers
73  */
74 
75 struct auth_info {
76 	u_int32_t 	authtype;	/* auth type */
77 	u_int32_t	authlen;	/* auth length */
78 };
79 
80 struct auth_unix {
81 	int32_t   ua_time;
82 	int32_t   ua_hostname;	/* null */
83 	int32_t   ua_uid;
84 	int32_t   ua_gid;
85 	int32_t   ua_gidlist;	/* null */
86 };
87 
88 struct krpc_call {
89 	u_int32_t	rp_xid;		/* request transaction id */
90 	int32_t 	rp_direction;	/* call direction (0) */
91 	u_int32_t	rp_rpcvers;	/* rpc version (2) */
92 	u_int32_t	rp_prog;	/* program */
93 	u_int32_t	rp_vers;	/* version */
94 	u_int32_t	rp_proc;	/* procedure */
95 	struct	auth_info rpc_auth;
96 	struct	auth_unix rpc_unix;
97 	struct	auth_info rpc_verf;
98 };
99 
100 struct krpc_reply {
101 	u_int32_t rp_xid;		/* request transaction id */
102 	int32_t  rp_direction;		/* call direction (1) */
103 	int32_t  rp_astatus;		/* accept status (0: accepted) */
104 	union {
105 		u_int32_t rpu_errno;
106 		struct {
107 			struct auth_info rok_auth;
108 			u_int32_t	rok_status;
109 		} rpu_rok;
110 	} rp_u;
111 };
112 #define rp_errno  rp_u.rpu_errno
113 #define rp_auth   rp_u.rpu_rok.rok_auth
114 #define rp_status rp_u.rpu_rok.rok_status
115 
116 #define MIN_REPLY_HDR 16	/* xid, dir, astat, errno */
117 
118 /*
119  * What is the longest we will wait before re-sending a request?
120  * Note this is also the frequency of "RPC timeout" messages.
121  * The re-send loop count sup linearly to this maximum, so the
122  * first complaint will happen after (1+2+3+4+5)=15 seconds.
123  */
124 #define	MAX_RESEND_DELAY 5	/* seconds */
125 
126 /*
127  * Call portmap to lookup a port number for a particular rpc program
128  * Returns non-zero error on failure.
129  */
130 int
krpc_portmap(struct sockaddr_in * sin,u_int prog,u_int vers,u_int16_t * portp,struct thread * td)131 krpc_portmap(struct sockaddr_in *sin, u_int prog, u_int vers, u_int16_t *portp,
132     struct thread *td)
133 {
134 	struct sdata {
135 		u_int32_t prog;		/* call program */
136 		u_int32_t vers;		/* call version */
137 		u_int32_t proto;	/* call protocol */
138 		u_int32_t port;		/* call port (unused) */
139 	} *sdata;
140 	struct rdata {
141 		u_int16_t pad;
142 		u_int16_t port;
143 	} *rdata;
144 	struct mbuf *m;
145 	int error;
146 
147 	/* The portmapper port is fixed. */
148 	if (prog == PMAPPROG) {
149 		*portp = htons(PMAPPORT);
150 		return 0;
151 	}
152 
153 	m = m_get(M_WAITOK, MT_DATA);
154 	sdata = mtod(m, struct sdata *);
155 	m->m_len = sizeof(*sdata);
156 
157 	/* Do the RPC to get it. */
158 	sdata->prog = txdr_unsigned(prog);
159 	sdata->vers = txdr_unsigned(vers);
160 	sdata->proto = txdr_unsigned(IPPROTO_UDP);
161 	sdata->port = 0;
162 
163 	sin->sin_port = htons(PMAPPORT);
164 	error = krpc_call(sin, PMAPPROG, PMAPVERS,
165 					  PMAPPROC_GETPORT, &m, NULL, td);
166 	if (error)
167 		goto out;
168 
169 	if (m->m_len < sizeof(*rdata)) {
170 		m = m_pullup(m, sizeof(*rdata));
171 		if (m == NULL)
172 			return ENOBUFS;
173 	}
174 	rdata = mtod(m, struct rdata *);
175 	*portp = rdata->port;
176 
177 out:
178 	m_freem(m);
179 	return error;
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  */
187 int
krpc_call(struct sockaddr_in * sa,u_int prog,u_int vers,u_int func,struct mbuf ** data,struct sockaddr ** from_p,struct thread * td)188 krpc_call(struct sockaddr_in *sa, u_int prog, u_int vers, u_int func,
189     struct mbuf **data, struct sockaddr **from_p, struct thread *td)
190 {
191 	struct socket *so;
192 	struct sockaddr_in *sin, ssin;
193 	struct sockaddr *from;
194 	struct mbuf *m, *nam, *mhead;
195 	struct krpc_call *call;
196 	struct krpc_reply *reply;
197 	struct sockopt sopt;
198 	struct timeval tv;
199 	struct uio auio;
200 	int error, rcvflg, timo, secs, len;
201 	static u_int32_t xid = ~0xFF;
202 	u_int16_t tport;
203 	u_int32_t saddr;
204 
205 	/*
206 	 * Validate address family.
207 	 * Sorry, this is INET specific...
208 	 */
209 	if (sa->sin_family != AF_INET)
210 		return (EAFNOSUPPORT);
211 
212 	/* Free at end if not null. */
213 	nam = mhead = NULL;
214 	from = NULL;
215 
216 	/*
217 	 * Create socket and set its receive timeout.
218 	 */
219 	if ((error = socreate(AF_INET, &so, SOCK_DGRAM, 0, td->td_ucred, td)))
220 		return error;
221 
222 	tv.tv_sec = 1;
223 	tv.tv_usec = 0;
224 	bzero(&sopt, sizeof sopt);
225 	sopt.sopt_dir = SOPT_SET;
226 	sopt.sopt_level = SOL_SOCKET;
227 	sopt.sopt_name = SO_RCVTIMEO;
228 	sopt.sopt_val = &tv;
229 	sopt.sopt_valsize = sizeof tv;
230 
231 	if ((error = sosetopt(so, &sopt)) != 0)
232 		goto out;
233 
234 	/*
235 	 * Enable broadcast if necessary.
236 	 */
237 	if (from_p) {
238 		int on = 1;
239 		sopt.sopt_name = SO_BROADCAST;
240 		sopt.sopt_val = &on;
241 		sopt.sopt_valsize = sizeof on;
242 		if ((error = sosetopt(so, &sopt)) != 0)
243 			goto out;
244 	}
245 
246 	/*
247 	 * Bind the local endpoint to a reserved port,
248 	 * because some NFS servers refuse requests from
249 	 * non-reserved (non-privileged) ports.
250 	 */
251 	sin = &ssin;
252 	bzero(sin, sizeof *sin);
253 	sin->sin_len = sizeof(*sin);
254 	sin->sin_family = AF_INET;
255 	sin->sin_addr.s_addr = INADDR_ANY;
256 	tport = IPPORT_RESERVED;
257 	do {
258 		tport--;
259 		sin->sin_port = htons(tport);
260 		error = sobind(so, (struct sockaddr *)sin, td);
261 	} while (error == EADDRINUSE &&
262 			 tport > IPPORT_RESERVED / 2);
263 	if (error) {
264 		printf("bind failed\n");
265 		goto out;
266 	}
267 
268 	/*
269 	 * Setup socket address for the server.
270 	 */
271 
272 	/*
273 	 * Prepend RPC message header.
274 	 */
275 	mhead = m_gethdr(M_WAITOK, MT_DATA);
276 	mhead->m_next = *data;
277 	*data = NULL;
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 	m_reset_rcvif(mhead);
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 *
xdr_string_encode(char * str,int len)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