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