krpc_subr.c revision 1.20 1 /* $NetBSD: krpc_subr.c,v 1.20 1997/08/29 16:12:49 gwr 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/param.h>
46 #include <sys/systm.h>
47 #include <sys/conf.h>
48 #include <sys/ioctl.h>
49 #include <sys/proc.h>
50 #include <sys/mount.h>
51 #include <sys/mbuf.h>
52 #include <sys/reboot.h>
53 #include <sys/socket.h>
54 #include <sys/socketvar.h>
55
56 #include <net/if.h>
57 #include <netinet/in.h>
58
59 #include <nfs/rpcv2.h>
60 #include <nfs/krpc.h>
61 #include <nfs/xdr_subs.h>
62
63 /*
64 * Kernel support for Sun RPC
65 *
66 * Used currently for bootstrapping in nfs diskless configurations.
67 */
68
69 /*
70 * Generic RPC headers
71 */
72
73 struct auth_info {
74 u_int32_t authtype; /* auth type */
75 u_int32_t authlen; /* auth length */
76 };
77
78 struct auth_unix {
79 int32_t ua_time;
80 int32_t ua_hostname; /* null */
81 int32_t ua_uid;
82 int32_t ua_gid;
83 int32_t ua_gidlist; /* null */
84 };
85
86 struct rpc_call {
87 u_int32_t rp_xid; /* request transaction id */
88 int32_t rp_direction; /* call direction (0) */
89 u_int32_t rp_rpcvers; /* rpc version (2) */
90 u_int32_t rp_prog; /* program */
91 u_int32_t rp_vers; /* version */
92 u_int32_t rp_proc; /* procedure */
93 struct auth_info rpc_auth;
94 struct auth_unix rpc_unix;
95 struct auth_info rpc_verf;
96 };
97
98 struct rpc_reply {
99 u_int32_t rp_xid; /* request transaction id */
100 int32_t rp_direction; /* call direction (1) */
101 int32_t rp_astatus; /* accept status (0: accepted) */
102 union {
103 /* rejected */
104 struct {
105 u_int32_t rej_stat;
106 u_int32_t rej_val1;
107 u_int32_t rej_val2;
108 } rpu_rej;
109 /* accepted */
110 struct {
111 struct auth_info rok_auth;
112 u_int32_t rok_status;
113 } rpu_rok;
114 } rp_u;
115 };
116 #define rp_rstat rp_u.rpu_rej.rej_stat
117 #define rp_auth rp_u.rpu_rok.rok_auth
118 #define rp_status rp_u.rpu_rok.rok_status
119
120 #define MIN_REPLY_HDR 16 /* xid, dir, astat, errno */
121
122 /*
123 * What is the longest we will wait before re-sending a request?
124 * Note this is also the frequency of "RPC timeout" messages.
125 * The re-send loop counts up linearly to this maximum, so the
126 * first complaint will happen after (1+2+3+4+5)=15 seconds.
127 */
128 #define MAX_RESEND_DELAY 5 /* seconds */
129 #define TOTAL_TIMEOUT 30 /* seconds */
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 /*
189 * Do a remote procedure call (RPC) and wait for its reply.
190 * If from_p is non-null, then we are doing broadcast, and
191 * the address from whence the response came is saved there.
192 */
193 int
194 krpc_call(sa, prog, vers, func, data, from_p)
195 struct sockaddr_in *sa;
196 u_int prog, vers, func;
197 struct mbuf **data; /* input/output */
198 struct mbuf **from_p; /* output */
199 {
200 struct socket *so;
201 struct sockaddr_in *sin;
202 struct mbuf *m, *nam, *mhead, *from;
203 struct rpc_call *call;
204 struct rpc_reply *reply;
205 struct uio auio;
206 int error, len, rcvflg, timo, secs, waited;
207 static u_int32_t xid = ~0xFF;
208 u_int16_t tport;
209 struct timeval *tv;
210
211 /*
212 * Validate address family.
213 * Sorry, this is INET specific...
214 */
215 if (sa->sin_family != AF_INET)
216 return (EAFNOSUPPORT);
217
218 /* Free at end if not null. */
219 nam = mhead = NULL;
220 from = NULL;
221
222 /*
223 * Create socket and set its recieve timeout.
224 */
225 if ((error = socreate(AF_INET, &so, SOCK_DGRAM, 0)))
226 goto out;
227
228 m = m_get(M_WAIT, MT_SOOPTS);
229 tv = mtod(m, struct timeval *);
230 m->m_len = sizeof(*tv);
231 tv->tv_sec = 1;
232 tv->tv_usec = 0;
233 if ((error = sosetopt(so, SOL_SOCKET, SO_RCVTIMEO, m)))
234 goto out;
235
236 /*
237 * Enable broadcast if necessary.
238 */
239 if (from_p) {
240 int32_t *on;
241 m = m_get(M_WAIT, MT_SOOPTS);
242 on = mtod(m, int32_t *);
243 m->m_len = sizeof(*on);
244 *on = 1;
245 if ((error = sosetopt(so, SOL_SOCKET, SO_BROADCAST, m)))
246 goto out;
247 }
248
249 /*
250 * Bind the local endpoint to a reserved port,
251 * because some NFS servers refuse requests from
252 * non-reserved (non-privileged) ports.
253 */
254 m = m_getclr(M_WAIT, MT_SONAME);
255 sin = mtod(m, struct sockaddr_in *);
256 sin->sin_len = m->m_len = sizeof(*sin);
257 sin->sin_family = AF_INET;
258 sin->sin_addr.s_addr = INADDR_ANY;
259 tport = IPPORT_RESERVED;
260 do {
261 tport--;
262 sin->sin_port = htons(tport);
263 error = sobind(so, m);
264 } while (error == EADDRINUSE &&
265 tport > IPPORT_RESERVED / 2);
266 m_freem(m);
267 if (error) {
268 printf("bind failed\n");
269 goto out;
270 }
271
272 /*
273 * Setup socket address for the server.
274 */
275 nam = m_get(M_WAIT, MT_SONAME);
276 sin = mtod(nam, struct sockaddr_in *);
277 bcopy((caddr_t)sa, (caddr_t)sin,
278 (nam->m_len = sa->sin_len));
279
280 /*
281 * Prepend RPC message header.
282 */
283 mhead = m_gethdr(M_WAIT, MT_DATA);
284 mhead->m_next = *data;
285 call = mtod(mhead, struct rpc_call *);
286 mhead->m_len = sizeof(*call);
287 bzero((caddr_t)call, sizeof(*call));
288 /* rpc_call part */
289 xid++;
290 call->rp_xid = txdr_unsigned(xid);
291 /* call->rp_direction = 0; */
292 call->rp_rpcvers = txdr_unsigned(2);
293 call->rp_prog = txdr_unsigned(prog);
294 call->rp_vers = txdr_unsigned(vers);
295 call->rp_proc = txdr_unsigned(func);
296 /* rpc_auth part (auth_unix as root) */
297 call->rpc_auth.authtype = txdr_unsigned(RPCAUTH_UNIX);
298 call->rpc_auth.authlen = txdr_unsigned(sizeof(struct auth_unix));
299 /* rpc_verf part (auth_null) */
300 call->rpc_verf.authtype = 0;
301 call->rpc_verf.authlen = 0;
302
303 /*
304 * Setup packet header
305 */
306 len = 0;
307 m = mhead;
308 while (m) {
309 len += m->m_len;
310 m = m->m_next;
311 }
312 mhead->m_pkthdr.len = len;
313 mhead->m_pkthdr.rcvif = NULL;
314
315 /*
316 * Send it, repeatedly, until a reply is received,
317 * but delay each re-send by an increasing amount.
318 * If the delay hits the maximum, start complaining.
319 */
320 timo = 0;
321 waited = 0;
322 send_again:
323 waited += timo;
324 if (waited >= TOTAL_TIMEOUT) {
325 error = ETIMEDOUT;
326 goto out;
327 }
328 /* Determine new timeout. */
329 if (timo < MAX_RESEND_DELAY)
330 timo++;
331 else
332 printf("RPC timeout for server 0x%x\n",
333 ntohl(sin->sin_addr.s_addr));
334
335 /* Send RPC request (or re-send). */
336 m = m_copym(mhead, 0, M_COPYALL, M_WAIT);
337 if (m == NULL) {
338 error = ENOBUFS;
339 goto out;
340 }
341 error = sosend(so, nam, NULL, m, NULL, 0);
342 if (error) {
343 printf("krpc_call: sosend: %d\n", error);
344 goto out;
345 }
346 m = NULL;
347
348 /*
349 * Wait for up to timo seconds for a reply.
350 * The socket receive timeout was set to 1 second.
351 */
352 secs = timo;
353 for (;;) {
354 if (from) {
355 m_freem(from);
356 from = NULL;
357 }
358 if (m) {
359 m_freem(m);
360 m = NULL;
361 }
362 auio.uio_resid = len = 1<<16;
363 rcvflg = 0;
364 error = soreceive(so, &from, &auio, &m, NULL, &rcvflg);
365 if (error == EWOULDBLOCK) {
366 if (--secs <= 0)
367 goto send_again;
368 continue;
369 }
370 if (error)
371 goto out;
372 len -= auio.uio_resid;
373
374 /* Does the reply contain at least a header? */
375 if (len < MIN_REPLY_HDR)
376 continue;
377 if (m->m_len < MIN_REPLY_HDR)
378 continue;
379 reply = mtod(m, struct rpc_reply *);
380
381 /* Is it the right reply? */
382 if (reply->rp_direction != txdr_unsigned(RPC_REPLY))
383 continue;
384
385 if (reply->rp_xid != txdr_unsigned(xid))
386 continue;
387
388 /* Was RPC accepted? (authorization OK) */
389 if (reply->rp_astatus != 0) {
390 /* Note: This is NOT an error code! */
391 error = fxdr_unsigned(u_int32_t, reply->rp_rstat);
392 switch (error) {
393 case RPC_MISMATCH:
394 /* .re_status = RPC_VERSMISMATCH; */
395 error = ERPCMISMATCH;
396 break;
397 case RPC_AUTHERR:
398 /* .re_status = RPC_AUTHERROR; */
399 error = EAUTH;
400 break;
401 default:
402 /* unexpected */
403 error = EBADRPC;
404 break;
405 }
406 goto out;
407 }
408
409 /* Did the call succeed? */
410 if (reply->rp_status != 0) {
411 /* Note: This is NOT an error code! */
412 error = fxdr_unsigned(u_int32_t, reply->rp_status);
413 switch (error) {
414 case RPC_PROGUNAVAIL:
415 error = EPROGUNAVAIL;
416 break;
417 case RPC_PROGMISMATCH:
418 error = EPROGMISMATCH;
419 break;
420 case RPC_PROCUNAVAIL:
421 error = EPROCUNAVAIL;
422 break;
423 case RPC_GARBAGE:
424 default:
425 error = EBADRPC;
426 }
427 goto out;
428 }
429 break;
430 } /* while secs */
431
432 /*
433 * OK, we have received a good reply!
434 * Get RPC reply header into first mbuf,
435 * get its length, then strip it off.
436 */
437 len = sizeof(*reply);
438 if (m->m_len < len) {
439 m = m_pullup(m, len);
440 if (m == NULL) {
441 error = ENOBUFS;
442 goto out;
443 }
444 }
445 reply = mtod(m, struct rpc_reply *);
446 if (reply->rp_auth.authtype != 0) {
447 len += fxdr_unsigned(u_int32_t, reply->rp_auth.authlen);
448 len = (len + 3) & ~3; /* XXX? */
449 }
450 m_adj(m, len);
451
452 /* result */
453 *data = m;
454 if (from_p) {
455 *from_p = from;
456 from = NULL;
457 }
458
459 out:
460 if (nam) m_freem(nam);
461 if (mhead) m_freem(mhead);
462 if (from) m_freem(from);
463 soclose(so);
464 return error;
465 }
466
467 /*
468 * eXternal Data Representation routines.
469 * (but with non-standard args...)
470 */
471
472 /*
473 * String representation for RPC.
474 */
475 struct xdr_string {
476 u_int32_t len; /* length without null or padding */
477 char data[4]; /* data (longer, of course) */
478 /* data is padded to a long-word boundary */
479 };
480
481 struct mbuf *
482 xdr_string_encode(str, len)
483 char *str;
484 int len;
485 {
486 struct mbuf *m;
487 struct xdr_string *xs;
488 int dlen; /* padded string length */
489 int mlen; /* message length */
490
491 dlen = (len + 3) & ~3;
492 mlen = dlen + 4;
493
494 if (mlen > MCLBYTES) /* If too big, we just can't do it. */
495 return (NULL);
496
497 m = m_get(M_WAIT, MT_DATA);
498 if (mlen > MLEN) {
499 MCLGET(m, M_WAIT);
500 if ((m->m_flags & M_EXT) == 0) {
501 (void) m_free(m); /* There can be only one. */
502 return (NULL);
503 }
504 }
505 xs = mtod(m, struct xdr_string *);
506 m->m_len = mlen;
507 xs->len = txdr_unsigned(len);
508 bcopy(str, xs->data, len);
509 return (m);
510 }
511
512 struct mbuf *
513 xdr_string_decode(m, str, len_p)
514 struct mbuf *m;
515 char *str;
516 int *len_p; /* bufsize - 1 */
517 {
518 struct xdr_string *xs;
519 int mlen; /* message length */
520 int slen; /* string length */
521
522 if (m->m_len < 4) {
523 m = m_pullup(m, 4);
524 if (m == NULL)
525 return (NULL);
526 }
527 xs = mtod(m, struct xdr_string *);
528 slen = fxdr_unsigned(u_int32_t, xs->len);
529 mlen = 4 + ((slen + 3) & ~3);
530
531 if (slen > *len_p)
532 slen = *len_p;
533 m_copydata(m, 4, slen, str);
534 m_adj(m, mlen);
535
536 str[slen] = '\0';
537 *len_p = slen;
538
539 return (m);
540 }
541
542
543 /*
544 * Inet address in RPC messages
545 * (Note, really four ints, NOT chars. Blech.)
546 */
547 struct xdr_inaddr {
548 u_int32_t atype;
549 u_int32_t addr[4];
550 };
551
552 struct mbuf *
553 xdr_inaddr_encode(ia)
554 struct in_addr *ia; /* already in network order */
555 {
556 struct mbuf *m;
557 struct xdr_inaddr *xi;
558 u_int8_t *cp;
559 u_int32_t *ip;
560
561 m = m_get(M_WAIT, MT_DATA);
562 xi = mtod(m, struct xdr_inaddr *);
563 m->m_len = sizeof(*xi);
564 xi->atype = txdr_unsigned(1);
565 ip = xi->addr;
566 cp = (u_int8_t *)&ia->s_addr;
567 *ip++ = txdr_unsigned(*cp++);
568 *ip++ = txdr_unsigned(*cp++);
569 *ip++ = txdr_unsigned(*cp++);
570 *ip++ = txdr_unsigned(*cp++);
571
572 return (m);
573 }
574
575 struct mbuf *
576 xdr_inaddr_decode(m, ia)
577 struct mbuf *m;
578 struct in_addr *ia; /* already in network order */
579 {
580 struct xdr_inaddr *xi;
581 u_int8_t *cp;
582 u_int32_t *ip;
583
584 if (m->m_len < sizeof(*xi)) {
585 m = m_pullup(m, sizeof(*xi));
586 if (m == NULL)
587 return (NULL);
588 }
589 xi = mtod(m, struct xdr_inaddr *);
590 if (xi->atype != txdr_unsigned(1)) {
591 ia->s_addr = INADDR_ANY;
592 goto out;
593 }
594 ip = xi->addr;
595 cp = (u_int8_t *)&ia->s_addr;
596 *cp++ = fxdr_unsigned(u_int8_t, *ip++);
597 *cp++ = fxdr_unsigned(u_int8_t, *ip++);
598 *cp++ = fxdr_unsigned(u_int8_t, *ip++);
599 *cp++ = fxdr_unsigned(u_int8_t, *ip++);
600
601 out:
602 m_adj(m, sizeof(*xi));
603 return (m);
604 }
605