krpc_subr.c revision 1.13 1 /* $NetBSD: krpc_subr.c,v 1.13 1996/06/07 00:48:10 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/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 u_int32_t rpu_errno;
104 struct {
105 struct auth_info rok_auth;
106 u_int32_t rok_status;
107 } rpu_rok;
108 } rp_u;
109 };
110 #define rp_errno rp_u.rpu_errno
111 #define rp_auth rp_u.rpu_rok.rok_auth
112 #define rp_status rp_u.rpu_rok.rok_status
113
114 #define MIN_REPLY_HDR 16 /* xid, dir, astat, errno */
115
116 /*
117 * What is the longest we will wait before re-sending a request?
118 * Note this is also the frequency of "RPC timeout" messages.
119 * The re-send loop count sup linearly to this maximum, so the
120 * first complaint will happen after (1+2+3+4+5)=15 seconds.
121 */
122 #define MAX_RESEND_DELAY 5 /* seconds */
123
124 /*
125 * Call portmap to lookup a port number for a particular rpc program
126 * Returns non-zero error on failure.
127 */
128 int
129 krpc_portmap(sin, prog, vers, portp)
130 struct sockaddr_in *sin; /* server address */
131 u_int prog, vers; /* host order */
132 u_int16_t *portp; /* network order */
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_WAIT, MT_DATA);
154 if (m == NULL)
155 return ENOBUFS;
156 sdata = mtod(m, struct sdata *);
157 m->m_len = sizeof(*sdata);
158
159 /* Do the RPC to get it. */
160 sdata->prog = txdr_unsigned(prog);
161 sdata->vers = txdr_unsigned(vers);
162 sdata->proto = txdr_unsigned(IPPROTO_UDP);
163 sdata->port = 0;
164
165 sin->sin_port = htons(PMAPPORT);
166 error = krpc_call(sin, PMAPPROG, PMAPVERS,
167 PMAPPROC_GETPORT, &m, NULL);
168 if (error)
169 return error;
170
171 if (m->m_len < sizeof(*rdata)) {
172 m = m_pullup(m, sizeof(*rdata));
173 if (m == NULL)
174 return ENOBUFS;
175 }
176 rdata = mtod(m, struct rdata *);
177 *portp = rdata->port;
178
179 m_freem(m);
180 return 0;
181 }
182
183 /*
184 * Do a remote procedure call (RPC) and wait for its reply.
185 * If from_p is non-null, then we are doing broadcast, and
186 * the address from whence the response came is saved there.
187 */
188 int
189 krpc_call(sa, prog, vers, func, data, from_p)
190 struct sockaddr_in *sa;
191 u_int prog, vers, func;
192 struct mbuf **data; /* input/output */
193 struct mbuf **from_p; /* output */
194 {
195 struct socket *so;
196 struct sockaddr_in *sin;
197 struct mbuf *m, *nam, *mhead, *from;
198 struct rpc_call *call;
199 struct rpc_reply *reply;
200 struct uio auio;
201 int error, rcvflg, timo, secs, len;
202 static u_int32_t xid = ~0xFF;
203 u_int16_t tport;
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 recieve timeout.
218 */
219 if ((error = socreate(AF_INET, &so, SOCK_DGRAM, 0)))
220 goto out;
221
222 m = m_get(M_WAIT, MT_SOOPTS);
223 if (m == NULL) {
224 error = ENOBUFS;
225 goto out;
226 } else {
227 struct timeval *tv;
228 tv = mtod(m, struct timeval *);
229 m->m_len = sizeof(*tv);
230 tv->tv_sec = 1;
231 tv->tv_usec = 0;
232 if ((error = sosetopt(so, SOL_SOCKET, SO_RCVTIMEO, m)))
233 goto out;
234 }
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 if (m == NULL) {
243 error = ENOBUFS;
244 goto out;
245 }
246 on = mtod(m, int32_t *);
247 m->m_len = sizeof(*on);
248 *on = 1;
249 if ((error = sosetopt(so, SOL_SOCKET, SO_BROADCAST, m)))
250 goto out;
251 }
252
253 /*
254 * Bind the local endpoint to a reserved port,
255 * because some NFS servers refuse requests from
256 * non-reserved (non-privileged) ports.
257 */
258 m = m_getclr(M_WAIT, MT_SONAME);
259 sin = mtod(m, struct sockaddr_in *);
260 sin->sin_len = m->m_len = sizeof(*sin);
261 sin->sin_family = AF_INET;
262 sin->sin_addr.s_addr = INADDR_ANY;
263 tport = IPPORT_RESERVED;
264 do {
265 tport--;
266 sin->sin_port = htons(tport);
267 error = sobind(so, m);
268 } while (error == EADDRINUSE &&
269 tport > IPPORT_RESERVED / 2);
270 m_freem(m);
271 if (error) {
272 printf("bind failed\n");
273 goto out;
274 }
275
276 /*
277 * Setup socket address for the server.
278 */
279 nam = m_get(M_WAIT, MT_SONAME);
280 if (nam == NULL) {
281 error = ENOBUFS;
282 goto out;
283 }
284 sin = mtod(nam, struct sockaddr_in *);
285 bcopy((caddr_t)sa, (caddr_t)sin,
286 (nam->m_len = sa->sin_len));
287
288 /*
289 * Prepend RPC message header.
290 */
291 mhead = m_gethdr(M_WAIT, MT_DATA);
292 mhead->m_next = *data;
293 call = mtod(mhead, struct rpc_call *);
294 mhead->m_len = sizeof(*call);
295 bzero((caddr_t)call, sizeof(*call));
296 /* rpc_call part */
297 xid++;
298 call->rp_xid = txdr_unsigned(xid);
299 /* call->rp_direction = 0; */
300 call->rp_rpcvers = txdr_unsigned(2);
301 call->rp_prog = txdr_unsigned(prog);
302 call->rp_vers = txdr_unsigned(vers);
303 call->rp_proc = txdr_unsigned(func);
304 /* rpc_auth part (auth_unix as root) */
305 call->rpc_auth.authtype = txdr_unsigned(RPCAUTH_UNIX);
306 call->rpc_auth.authlen = txdr_unsigned(sizeof(struct auth_unix));
307 /* rpc_verf part (auth_null) */
308 call->rpc_verf.authtype = 0;
309 call->rpc_verf.authlen = 0;
310
311 /*
312 * Setup packet header
313 */
314 len = 0;
315 m = mhead;
316 while (m) {
317 len += m->m_len;
318 m = m->m_next;
319 }
320 mhead->m_pkthdr.len = len;
321 mhead->m_pkthdr.rcvif = NULL;
322
323 /*
324 * Send it, repeatedly, until a reply is received,
325 * but delay each re-send by an increasing amount.
326 * If the delay hits the maximum, start complaining.
327 */
328 timo = 0;
329 for (;;) {
330 /* Send RPC request (or re-send). */
331 m = m_copym(mhead, 0, M_COPYALL, M_WAIT);
332 if (m == NULL) {
333 error = ENOBUFS;
334 goto out;
335 }
336 error = sosend(so, nam, NULL, m, NULL, 0);
337 if (error) {
338 printf("krpc_call: sosend: %d\n", error);
339 goto out;
340 }
341 m = NULL;
342
343 /* Determine new timeout. */
344 if (timo < MAX_RESEND_DELAY)
345 timo++;
346 else
347 printf("RPC timeout for server 0x%x\n",
348 ntohl(sin->sin_addr.s_addr));
349
350 /*
351 * Wait for up to timo seconds for a reply.
352 * The socket receive timeout was set to 1 second.
353 */
354 secs = timo;
355 while (secs > 0) {
356 if (from) {
357 m_freem(from);
358 from = NULL;
359 }
360 if (m) {
361 m_freem(m);
362 m = NULL;
363 }
364 auio.uio_resid = len = 1<<16;
365 rcvflg = 0;
366 error = soreceive(so, &from, &auio, &m, NULL, &rcvflg);
367 if (error == EWOULDBLOCK) {
368 secs--;
369 continue;
370 }
371 if (error)
372 goto out;
373 len -= auio.uio_resid;
374
375 /* Does the reply contain at least a header? */
376 if (len < MIN_REPLY_HDR)
377 continue;
378 if (m->m_len < MIN_REPLY_HDR)
379 continue;
380 reply = mtod(m, struct rpc_reply *);
381
382 /* Is it the right reply? */
383 if (reply->rp_direction != txdr_unsigned(RPC_REPLY))
384 continue;
385
386 if (reply->rp_xid != txdr_unsigned(xid))
387 continue;
388
389 /* Was RPC accepted? (authorization OK) */
390 if (reply->rp_astatus != 0) {
391 error = fxdr_unsigned(u_int32_t, reply->rp_errno);
392 printf("rpc denied, error=%d\n", error);
393 continue;
394 }
395
396 /* Did the call succeed? */
397 if (reply->rp_status != 0) {
398 error = fxdr_unsigned(u_int32_t, reply->rp_status);
399 printf("rpc denied, status=%d\n", error);
400 continue;
401 }
402
403 goto gotreply; /* break two levels */
404
405 } /* while secs */
406 } /* forever send/receive */
407
408 error = ETIMEDOUT;
409 goto out;
410
411 gotreply:
412
413 /*
414 * Get RPC reply header into first mbuf,
415 * get its length, then strip it off.
416 */
417 len = sizeof(*reply);
418 if (m->m_len < len) {
419 m = m_pullup(m, len);
420 if (m == NULL) {
421 error = ENOBUFS;
422 goto out;
423 }
424 }
425 reply = mtod(m, struct rpc_reply *);
426 if (reply->rp_auth.authtype != 0) {
427 len += fxdr_unsigned(u_int32_t, reply->rp_auth.authlen);
428 len = (len + 3) & ~3; /* XXX? */
429 }
430 m_adj(m, len);
431
432 /* result */
433 *data = m;
434 if (from_p) {
435 *from_p = from;
436 from = NULL;
437 }
438
439 out:
440 if (nam) m_freem(nam);
441 if (mhead) m_freem(mhead);
442 if (from) m_freem(from);
443 soclose(so);
444 return error;
445 }
446
447 /*
448 * eXternal Data Representation routines.
449 * (but with non-standard args...)
450 */
451
452 /*
453 * String representation for RPC.
454 */
455 struct xdr_string {
456 u_int32_t len; /* length without null or padding */
457 char data[4]; /* data (longer, of course) */
458 /* data is padded to a long-word boundary */
459 };
460
461 struct mbuf *
462 xdr_string_encode(str, len)
463 char *str;
464 int len;
465 {
466 struct mbuf *m;
467 struct xdr_string *xs;
468 int dlen; /* padded string length */
469 int mlen; /* message length */
470
471 dlen = (len + 3) & ~3;
472 mlen = dlen + 4;
473
474 if (mlen > MCLBYTES) /* If too big, we just can't do it. */
475 return (NULL);
476
477 m = m_get(M_WAIT, MT_DATA);
478 if (mlen > MLEN) {
479 MCLGET(m, M_WAIT);
480 if ((m->m_flags & M_EXT) == 0) {
481 (void) m_free(m); /* There can be only one. */
482 return (NULL);
483 }
484 }
485 xs = mtod(m, struct xdr_string *);
486 m->m_len = mlen;
487 xs->len = txdr_unsigned(len);
488 bcopy(str, xs->data, len);
489 return (m);
490 }
491
492 struct mbuf *
493 xdr_string_decode(m, str, len_p)
494 struct mbuf *m;
495 char *str;
496 int *len_p; /* bufsize - 1 */
497 {
498 struct xdr_string *xs;
499 int mlen; /* message length */
500 int slen; /* string length */
501
502 if (m->m_len < 4) {
503 m = m_pullup(m, 4);
504 if (m == NULL)
505 return (NULL);
506 }
507 xs = mtod(m, struct xdr_string *);
508 slen = fxdr_unsigned(u_int32_t, xs->len);
509 mlen = 4 + ((slen + 3) & ~3);
510
511 if (slen > *len_p)
512 slen = *len_p;
513 m_copydata(m, 4, slen, str);
514 m_adj(m, mlen);
515
516 str[slen] = '\0';
517 *len_p = slen;
518
519 return (m);
520 }
521
522
523 /*
524 * Inet address in RPC messages
525 * (Note, really four ints, NOT chars. Blech.)
526 */
527 struct xdr_inaddr {
528 u_int32_t atype;
529 u_int32_t addr[4];
530 };
531
532 struct mbuf *
533 xdr_inaddr_encode(ia)
534 struct in_addr *ia; /* already in network order */
535 {
536 struct mbuf *m;
537 struct xdr_inaddr *xi;
538 u_int8_t *cp;
539 u_int32_t *ip;
540
541 m = m_get(M_WAIT, MT_DATA);
542 xi = mtod(m, struct xdr_inaddr *);
543 m->m_len = sizeof(*xi);
544 xi->atype = txdr_unsigned(1);
545 ip = xi->addr;
546 cp = (u_int8_t *)&ia->s_addr;
547 *ip++ = txdr_unsigned(*cp++);
548 *ip++ = txdr_unsigned(*cp++);
549 *ip++ = txdr_unsigned(*cp++);
550 *ip++ = txdr_unsigned(*cp++);
551
552 return (m);
553 }
554
555 struct mbuf *
556 xdr_inaddr_decode(m, ia)
557 struct mbuf *m;
558 struct in_addr *ia; /* already in network order */
559 {
560 struct xdr_inaddr *xi;
561 u_int8_t *cp;
562 u_int32_t *ip;
563
564 if (m->m_len < sizeof(*xi)) {
565 m = m_pullup(m, sizeof(*xi));
566 if (m == NULL)
567 return (NULL);
568 }
569 xi = mtod(m, struct xdr_inaddr *);
570 if (xi->atype != txdr_unsigned(1)) {
571 ia->s_addr = INADDR_ANY;
572 goto out;
573 }
574 ip = xi->addr;
575 cp = (u_int8_t *)&ia->s_addr;
576 *cp++ = fxdr_unsigned(u_int8_t, *ip++);
577 *cp++ = fxdr_unsigned(u_int8_t, *ip++);
578 *cp++ = fxdr_unsigned(u_int8_t, *ip++);
579 *cp++ = fxdr_unsigned(u_int8_t, *ip++);
580
581 out:
582 m_adj(m, sizeof(*xi));
583 return (m);
584 }
585