xref: /netbsd/sys/nfs/krpc_subr.c (revision bf9ec67e)
1 /*	$NetBSD: krpc_subr.c,v 1.25 2001/11/10 10:59:08 lukem 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 __KERNEL_RCSID(0, "$NetBSD: krpc_subr.c,v 1.25 2001/11/10 10:59:08 lukem Exp $");
47 
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/conf.h>
51 #include <sys/ioctl.h>
52 #include <sys/proc.h>
53 #include <sys/mount.h>
54 #include <sys/mbuf.h>
55 #include <sys/reboot.h>
56 #include <sys/socket.h>
57 #include <sys/socketvar.h>
58 
59 #include <net/if.h>
60 #include <netinet/in.h>
61 
62 #include <nfs/rpcv2.h>
63 #include <nfs/krpc.h>
64 #include <nfs/xdr_subs.h>
65 #include <nfs/nfsproto.h> /* XXX NFSX_V3FHMAX for next */
66 #include <nfs/nfs.h>
67 #include <nfs/nfsmount.h>
68 #include <nfs/nfsdiskless.h> /* XXX decl nfs_boot_sendrecv */
69 
70 /*
71  * Kernel support for Sun RPC
72  *
73  * Used currently for bootstrapping in nfs diskless configurations.
74  */
75 
76 /*
77  * Generic RPC headers
78  */
79 
80 struct auth_info {
81 	u_int32_t 	authtype;	/* auth type */
82 	u_int32_t	authlen;	/* auth length */
83 };
84 
85 struct auth_unix {
86 	int32_t   ua_time;
87 	int32_t   ua_hostname;	/* null */
88 	int32_t   ua_uid;
89 	int32_t   ua_gid;
90 	int32_t   ua_gidlist;	/* null */
91 };
92 
93 struct rpc_call {
94 	u_int32_t	rp_xid;		/* request transaction id */
95 	int32_t 	rp_direction;	/* call direction (0) */
96 	u_int32_t	rp_rpcvers;	/* rpc version (2) */
97 	u_int32_t	rp_prog;	/* program */
98 	u_int32_t	rp_vers;	/* version */
99 	u_int32_t	rp_proc;	/* procedure */
100 	struct	auth_info rpc_auth;
101 	struct	auth_unix rpc_unix;
102 	struct	auth_info rpc_verf;
103 };
104 
105 struct rpc_reply {
106 	u_int32_t rp_xid;		/* request transaction id */
107 	int32_t  rp_direction;		/* call direction (1) */
108 	int32_t  rp_astatus;		/* accept status (0: accepted) */
109 	union {
110 		/* rejected */
111 		struct {
112 			u_int32_t rej_stat;
113 			u_int32_t rej_val1;
114 			u_int32_t rej_val2;
115 		} rpu_rej;
116 		/* accepted */
117 		struct {
118 			struct auth_info rok_auth;
119 			u_int32_t	rok_status;
120 		} rpu_rok;
121 	} rp_u;
122 };
123 #define rp_rstat  rp_u.rpu_rej.rej_stat
124 #define rp_auth   rp_u.rpu_rok.rok_auth
125 #define rp_status rp_u.rpu_rok.rok_status
126 
127 #define MIN_REPLY_HDR 16	/* xid, dir, astat, errno */
128 
129 static int krpccheck __P((struct mbuf*, void*));
130 
131 /*
132  * Call portmap to lookup a port number for a particular rpc program
133  * Returns non-zero error on failure.
134  */
135 int
136 krpc_portmap(sin,  prog, vers, proto, portp)
137 	struct sockaddr_in *sin;		/* server address */
138 	u_int prog, vers, proto;	/* host order */
139 	u_int16_t *portp;	/* network order */
140 {
141 	struct sdata {
142 		u_int32_t prog;		/* call program */
143 		u_int32_t vers;		/* call version */
144 		u_int32_t proto;	/* call protocol */
145 		u_int32_t port;		/* call port (unused) */
146 	} *sdata;
147 	struct rdata {
148 		u_int16_t pad;
149 		u_int16_t port;
150 	} *rdata;
151 	struct mbuf *m;
152 	int error;
153 
154 	/* The portmapper port is fixed. */
155 	if (prog == PMAPPROG) {
156 		*portp = htons(PMAPPORT);
157 		return 0;
158 	}
159 
160 	m = m_get(M_WAIT, MT_DATA);
161 	sdata = mtod(m, struct sdata *);
162 	m->m_len = sizeof(*sdata);
163 
164 	/* Do the RPC to get it. */
165 	sdata->prog = txdr_unsigned(prog);
166 	sdata->vers = txdr_unsigned(vers);
167 	sdata->proto = txdr_unsigned(proto);
168 	sdata->port = 0;
169 
170 	sin->sin_port = htons(PMAPPORT);
171 	error = krpc_call(sin, PMAPPROG, PMAPVERS,
172 					  PMAPPROC_GETPORT, &m, NULL);
173 	if (error)
174 		return error;
175 
176 	if (m->m_len < sizeof(*rdata)) {
177 		m = m_pullup(m, sizeof(*rdata));
178 		if (m == NULL)
179 			return ENOBUFS;
180 	}
181 	rdata = mtod(m, struct rdata *);
182 	*portp = rdata->port;
183 
184 	m_freem(m);
185 	return 0;
186 }
187 
188 static int krpccheck(m, context)
189 struct mbuf *m;
190 void *context;
191 {
192 	struct rpc_reply *reply;
193 
194 	/* Does the reply contain at least a header? */
195 	if (m->m_pkthdr.len < MIN_REPLY_HDR)
196 		return(-1);
197 	if (m->m_len < sizeof(struct rpc_reply)) {
198 		m = m_pullup(m, sizeof(struct rpc_reply));
199 		if (m == NULL)
200 			return(-1);
201 	}
202 	reply = mtod(m, struct rpc_reply *);
203 
204 	/* Is it the right reply? */
205 	if (reply->rp_direction != txdr_unsigned(RPC_REPLY))
206 		return(-1);
207 
208 	if (reply->rp_xid != txdr_unsigned(*(u_int32_t*)context))
209 		return(-1);
210 
211 	return(0);
212 }
213 
214 /*
215  * Do a remote procedure call (RPC) and wait for its reply.
216  * If from_p is non-null, then we are doing broadcast, and
217  * the address from whence the response came is saved there.
218  */
219 int
220 krpc_call(sa, prog, vers, func, data, from_p)
221 	struct sockaddr_in *sa;
222 	u_int prog, vers, func;
223 	struct mbuf **data;	/* input/output */
224 	struct mbuf **from_p;	/* output */
225 {
226 	struct socket *so;
227 	struct sockaddr_in *sin;
228 	struct mbuf *m, *nam, *mhead, *from;
229 	struct rpc_call *call;
230 	struct rpc_reply *reply;
231 	int error, len;
232 	static u_int32_t xid = ~0xFF;
233 	u_int16_t tport;
234 
235 	/*
236 	 * Validate address family.
237 	 * Sorry, this is INET specific...
238 	 */
239 	if (sa->sin_family != AF_INET)
240 		return (EAFNOSUPPORT);
241 
242 	/* Free at end if not null. */
243 	nam = mhead = NULL;
244 	from = NULL;
245 
246 	/*
247 	 * Create socket and set its receive timeout.
248 	 */
249 	if ((error = socreate(AF_INET, &so, SOCK_DGRAM, 0)))
250 		goto out;
251 
252 	if ((error = nfs_boot_setrecvtimo(so)))
253 		goto out;
254 
255 	/*
256 	 * Enable broadcast if necessary.
257 	 */
258 	if (from_p) {
259 		if ((error = nfs_boot_enbroadcast(so)))
260 			goto out;
261 	}
262 
263 	/*
264 	 * Bind the local endpoint to a reserved port,
265 	 * because some NFS servers refuse requests from
266 	 * non-reserved (non-privileged) ports.
267 	 */
268 	tport = IPPORT_RESERVED;
269 	do {
270 		tport--;
271 		error = nfs_boot_sobind_ipport(so, tport);
272 	} while (error == EADDRINUSE &&
273 			 tport > IPPORT_RESERVED / 2);
274 	if (error) {
275 		printf("bind failed\n");
276 		goto out;
277 	}
278 
279 	/*
280 	 * Setup socket address for the server.
281 	 */
282 	nam = m_get(M_WAIT, MT_SONAME);
283 	sin = mtod(nam, struct sockaddr_in *);
284 	memcpy((caddr_t)sin, (caddr_t)sa,
285 		  (nam->m_len = sa->sin_len));
286 
287 	/*
288 	 * Prepend RPC message header.
289 	 */
290 	mhead = m_gethdr(M_WAIT, MT_DATA);
291 	mhead->m_next = *data;
292 	call = mtod(mhead, struct rpc_call *);
293 	mhead->m_len = sizeof(*call);
294 	memset((caddr_t)call, 0, sizeof(*call));
295 	/* rpc_call part */
296 	xid++;
297 	call->rp_xid = txdr_unsigned(xid);
298 	/* call->rp_direction = 0; */
299 	call->rp_rpcvers = txdr_unsigned(2);
300 	call->rp_prog = txdr_unsigned(prog);
301 	call->rp_vers = txdr_unsigned(vers);
302 	call->rp_proc = txdr_unsigned(func);
303 	/* rpc_auth part (auth_unix as root) */
304 	call->rpc_auth.authtype = txdr_unsigned(RPCAUTH_UNIX);
305 	call->rpc_auth.authlen  = txdr_unsigned(sizeof(struct auth_unix));
306 	/* rpc_verf part (auth_null) */
307 	call->rpc_verf.authtype = 0;
308 	call->rpc_verf.authlen  = 0;
309 
310 	/*
311 	 * Setup packet header
312 	 */
313 	len = 0;
314 	m = mhead;
315 	while (m) {
316 		len += m->m_len;
317 		m = m->m_next;
318 	}
319 	mhead->m_pkthdr.len = len;
320 	mhead->m_pkthdr.rcvif = NULL;
321 
322 	error = nfs_boot_sendrecv(so, nam, 0, mhead, krpccheck, &m, &from, &xid);
323 	if (error)
324 		goto out;
325 
326 	/* m_pullup() was done in krpccheck() */
327 	reply = mtod(m, struct rpc_reply *);
328 
329 	/* Was RPC accepted? (authorization OK) */
330 	if (reply->rp_astatus != 0) {
331 		/* Note: This is NOT an error code! */
332 		error = fxdr_unsigned(u_int32_t, reply->rp_rstat);
333 		switch (error) {
334 		    case RPC_MISMATCH:
335 			/* .re_status = RPC_VERSMISMATCH; */
336 			error = ERPCMISMATCH;
337 			break;
338 		    case RPC_AUTHERR:
339 			/* .re_status = RPC_AUTHERROR; */
340 			error = EAUTH;
341 			break;
342 		    default:
343 			/* unexpected */
344 			error = EBADRPC;
345 			break;
346 		}
347 		goto out;
348 	}
349 
350 	/* Did the call succeed? */
351 	if (reply->rp_status != 0) {
352 		/* Note: This is NOT an error code! */
353 		error = fxdr_unsigned(u_int32_t, reply->rp_status);
354 		switch (error) {
355 		    case RPC_PROGUNAVAIL:
356 			error = EPROGUNAVAIL;
357 			break;
358 		    case RPC_PROGMISMATCH:
359 			error = EPROGMISMATCH;
360 			break;
361 		    case RPC_PROCUNAVAIL:
362 			error = EPROCUNAVAIL;
363 			break;
364 		    case RPC_GARBAGE:
365 		    default:
366 			error = EBADRPC;
367 		}
368 		goto out;
369 	}
370 
371 	/*
372 	 * OK, we have received a good reply!
373 	 * Get its length, then strip it off.
374 	 */
375 	len = sizeof(*reply);
376 	if (reply->rp_auth.authtype != 0) {
377 		len += fxdr_unsigned(u_int32_t, reply->rp_auth.authlen);
378 		len = (len + 3) & ~3; /* XXX? */
379 	}
380 	m_adj(m, len);
381 
382 	/* result */
383 	*data = m;
384 	if (from_p) {
385 		*from_p = from;
386 		from = NULL;
387 	}
388 
389  out:
390 	if (nam) m_freem(nam);
391 	if (mhead) m_freem(mhead);
392 	if (from) m_freem(from);
393 	soclose(so);
394 	return error;
395 }
396 
397 /*
398  * eXternal Data Representation routines.
399  * (but with non-standard args...)
400  */
401 
402 /*
403  * String representation for RPC.
404  */
405 struct xdr_string {
406 	u_int32_t len;		/* length without null or padding */
407 	char data[4];	/* data (longer, of course) */
408     /* data is padded to a long-word boundary */
409 };
410 
411 struct mbuf *
412 xdr_string_encode(str, len)
413 	char *str;
414 	int len;
415 {
416 	struct mbuf *m;
417 	struct xdr_string *xs;
418 	int dlen;	/* padded string length */
419 	int mlen;	/* message length */
420 
421 	dlen = (len + 3) & ~3;
422 	mlen = dlen + 4;
423 
424 	if (mlen > MCLBYTES)		/* If too big, we just can't do it. */
425 		return (NULL);
426 
427 	m = m_get(M_WAIT, MT_DATA);
428 	if (mlen > MLEN) {
429 		MCLGET(m, M_WAIT);
430 		if ((m->m_flags & M_EXT) == 0) {
431 			(void) m_free(m);	/* There can be only one. */
432 			return (NULL);
433 		}
434 	}
435 	xs = mtod(m, struct xdr_string *);
436 	m->m_len = mlen;
437 	xs->len = txdr_unsigned(len);
438 	memcpy(xs->data, str, len);
439 	return (m);
440 }
441 
442 struct mbuf *
443 xdr_string_decode(m, str, len_p)
444 	struct mbuf *m;
445 	char *str;
446 	int *len_p;		/* bufsize - 1 */
447 {
448 	struct xdr_string *xs;
449 	int mlen;	/* message length */
450 	int slen;	/* string length */
451 
452 	if (m->m_len < 4) {
453 		m = m_pullup(m, 4);
454 		if (m == NULL)
455 			return (NULL);
456 	}
457 	xs = mtod(m, struct xdr_string *);
458 	slen = fxdr_unsigned(u_int32_t, xs->len);
459 	mlen = 4 + ((slen + 3) & ~3);
460 
461 	if (slen > *len_p)
462 		slen = *len_p;
463 	m_copydata(m, 4, slen, str);
464 	m_adj(m, mlen);
465 
466 	str[slen] = '\0';
467 	*len_p = slen;
468 
469 	return (m);
470 }
471 
472 
473 /*
474  * Inet address in RPC messages
475  * (Note, really four ints, NOT chars.  Blech.)
476  */
477 struct xdr_inaddr {
478 	u_int32_t atype;
479 	u_int32_t addr[4];
480 };
481 
482 struct mbuf *
483 xdr_inaddr_encode(ia)
484 	struct in_addr *ia;		/* already in network order */
485 {
486 	struct mbuf *m;
487 	struct xdr_inaddr *xi;
488 	u_int8_t *cp;
489 	u_int32_t *ip;
490 
491 	m = m_get(M_WAIT, MT_DATA);
492 	xi = mtod(m, struct xdr_inaddr *);
493 	m->m_len = sizeof(*xi);
494 	xi->atype = txdr_unsigned(1);
495 	ip = xi->addr;
496 	cp = (u_int8_t *)&ia->s_addr;
497 	*ip++ = txdr_unsigned(*cp++);
498 	*ip++ = txdr_unsigned(*cp++);
499 	*ip++ = txdr_unsigned(*cp++);
500 	*ip++ = txdr_unsigned(*cp++);
501 
502 	return (m);
503 }
504 
505 struct mbuf *
506 xdr_inaddr_decode(m, ia)
507 	struct mbuf *m;
508 	struct in_addr *ia;		/* already in network order */
509 {
510 	struct xdr_inaddr *xi;
511 	u_int8_t *cp;
512 	u_int32_t *ip;
513 
514 	if (m->m_len < sizeof(*xi)) {
515 		m = m_pullup(m, sizeof(*xi));
516 		if (m == NULL)
517 			return (NULL);
518 	}
519 	xi = mtod(m, struct xdr_inaddr *);
520 	if (xi->atype != txdr_unsigned(1)) {
521 		ia->s_addr = INADDR_ANY;
522 		goto out;
523 	}
524 	ip = xi->addr;
525 	cp = (u_int8_t *)&ia->s_addr;
526 	*cp++ = fxdr_unsigned(u_int8_t, *ip++);
527 	*cp++ = fxdr_unsigned(u_int8_t, *ip++);
528 	*cp++ = fxdr_unsigned(u_int8_t, *ip++);
529 	*cp++ = fxdr_unsigned(u_int8_t, *ip++);
530 
531 out:
532 	m_adj(m, sizeof(*xi));
533 	return (m);
534 }
535