Line data Source code
1 : /* $OpenBSD: krpc_subr.c,v 1.35 2018/09/10 16:14:08 bluhm Exp $ */
2 : /* $NetBSD: krpc_subr.c,v 1.12.4.1 1996/06/07 00:52:26 cgd Exp $ */
3 :
4 : /*
5 : * Copyright (c) 1995 Gordon Ross, Adam Glass
6 : * Copyright (c) 1992 Regents of the University of California.
7 : * All rights reserved.
8 : *
9 : * This software was developed by the Computer Systems Engineering group
10 : * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
11 : * contributed to Berkeley.
12 : *
13 : * Redistribution and use in source and binary forms, with or without
14 : * modification, are permitted provided that the following conditions
15 : * are met:
16 : * 1. Redistributions of source code must retain the above copyright
17 : * notice, this list of conditions and the following disclaimer.
18 : * 2. Redistributions in binary form must reproduce the above copyright
19 : * notice, this list of conditions and the following disclaimer in the
20 : * documentation and/or other materials provided with the distribution.
21 : * 3. All advertising materials mentioning features or use of this software
22 : * must display the following acknowledgement:
23 : * This product includes software developed by the University of
24 : * California, Lawrence Berkeley Laboratory and its contributors.
25 : * 4. Neither the name of the University nor the names of its contributors
26 : * may be used to endorse or promote products derived from this software
27 : * without specific prior written permission.
28 : *
29 : * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 : * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 : * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 : * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 : * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 : * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 : * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 : * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 : * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 : * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 : * SUCH DAMAGE.
40 : *
41 : * partially based on:
42 : * libnetboot/rpc.c
43 : * @(#) Header: rpc.c,v 1.12 93/09/28 08:31:56 leres Exp (LBL)
44 : */
45 :
46 : #include <sys/param.h>
47 : #include <sys/systm.h>
48 : #include <sys/conf.h>
49 : #include <sys/ioctl.h>
50 : #include <sys/proc.h>
51 : #include <sys/mount.h>
52 : #include <sys/mbuf.h>
53 : #include <sys/reboot.h>
54 : #include <sys/socket.h>
55 : #include <sys/socketvar.h>
56 :
57 : #include <netinet/in.h>
58 :
59 : #include <nfs/rpcv2.h>
60 : #include <nfs/krpc.h>
61 : #include <nfs/xdr_subs.h>
62 : #include <crypto/idgen.h>
63 :
64 : /*
65 : * Kernel support for Sun RPC
66 : *
67 : * Used currently for bootstrapping in nfs diskless configurations.
68 : */
69 :
70 : /*
71 : * Generic RPC headers
72 : */
73 :
74 : struct auth_info {
75 : u_int32_t authtype; /* auth type */
76 : u_int32_t authlen; /* auth length */
77 : };
78 :
79 : struct auth_unix {
80 : int32_t ua_time;
81 : int32_t ua_hostname; /* null */
82 : int32_t ua_uid;
83 : int32_t ua_gid;
84 : int32_t ua_gidlist; /* null */
85 : };
86 :
87 : struct rpc_call {
88 : u_int32_t rp_xid; /* request transaction id */
89 : int32_t rp_direction; /* call direction (0) */
90 : u_int32_t rp_rpcvers; /* rpc version (2) */
91 : u_int32_t rp_prog; /* program */
92 : u_int32_t rp_vers; /* version */
93 : u_int32_t rp_proc; /* procedure */
94 : struct auth_info rpc_auth;
95 : struct auth_unix rpc_unix;
96 : struct auth_info rpc_verf;
97 : };
98 :
99 : struct rpc_reply {
100 : u_int32_t rp_xid; /* request transaction id */
101 : int32_t rp_direction; /* call direction (1) */
102 : int32_t rp_astatus; /* accept status (0: accepted) */
103 : union {
104 : u_int32_t rpu_errno;
105 : struct {
106 : struct auth_info rok_auth;
107 : u_int32_t rok_status;
108 : } rpu_rok;
109 : } rp_u;
110 : };
111 : #define rp_errno rp_u.rpu_errno
112 : #define rp_auth rp_u.rpu_rok.rok_auth
113 : #define rp_status rp_u.rpu_rok.rok_status
114 :
115 : #define MIN_REPLY_HDR 16 /* xid, dir, astat, errno */
116 :
117 : u_int32_t krpc_get_xid(void);
118 :
119 : /*
120 : * Return an unpredictable XID.
121 : */
122 : u_int32_t
123 0 : krpc_get_xid(void)
124 : {
125 : static struct idgen32_ctx krpc_xid_ctx;
126 : static int called = 0;
127 :
128 0 : if (!called) {
129 0 : called = 1;
130 0 : idgen32_init(&krpc_xid_ctx);
131 0 : }
132 0 : return idgen32(&krpc_xid_ctx);
133 : }
134 :
135 : /*
136 : * What is the longest we will wait before re-sending a request?
137 : * Note this is also the frequency of "RPC timeout" messages.
138 : * The re-send loop count sup linearly to this maximum, so the
139 : * first complaint will happen after (1+2+3+4+5)=15 seconds.
140 : */
141 : #define MAX_RESEND_DELAY 5 /* seconds */
142 :
143 : /*
144 : * Call portmap to lookup a port number for a particular rpc program
145 : * Returns non-zero error on failure.
146 : */
147 : int
148 0 : krpc_portmap(struct sockaddr_in *sin, u_int prog, u_int vers, u_int16_t *portp)
149 : {
150 : struct sdata {
151 : u_int32_t prog; /* call program */
152 : u_int32_t vers; /* call version */
153 : u_int32_t proto; /* call protocol */
154 : u_int32_t port; /* call port (unused) */
155 : } *sdata;
156 : struct rdata {
157 : u_int16_t pad;
158 : u_int16_t port;
159 : } *rdata;
160 0 : struct mbuf *m;
161 : int error;
162 :
163 : /* The portmapper port is fixed. */
164 0 : if (prog == PMAPPROG) {
165 0 : *portp = htons(PMAPPORT);
166 0 : return 0;
167 : }
168 :
169 0 : m = m_get(M_WAIT, MT_DATA);
170 0 : sdata = mtod(m, struct sdata *);
171 0 : m->m_len = sizeof(*sdata);
172 :
173 : /* Do the RPC to get it. */
174 0 : sdata->prog = txdr_unsigned(prog);
175 0 : sdata->vers = txdr_unsigned(vers);
176 0 : sdata->proto = txdr_unsigned(IPPROTO_UDP);
177 0 : sdata->port = 0;
178 :
179 0 : sin->sin_port = htons(PMAPPORT);
180 0 : error = krpc_call(sin, PMAPPROG, PMAPVERS,
181 : PMAPPROC_GETPORT, &m, NULL, -1);
182 0 : if (error)
183 0 : return error;
184 :
185 0 : if (m->m_len < sizeof(*rdata)) {
186 0 : m = m_pullup(m, sizeof(*rdata));
187 0 : if (m == NULL)
188 0 : return ENOBUFS;
189 : }
190 0 : rdata = mtod(m, struct rdata *);
191 0 : *portp = rdata->port;
192 :
193 0 : m_freem(m);
194 0 : return 0;
195 0 : }
196 :
197 : /*
198 : * Do a remote procedure call (RPC) and wait for its reply.
199 : * If from_p is non-null, then we are doing broadcast, and
200 : * the address from whence the response came is saved there.
201 : * data: input/output
202 : * from_p: output
203 : */
204 : int
205 0 : krpc_call(struct sockaddr_in *sa, u_int prog, u_int vers, u_int func,
206 : struct mbuf **data, struct mbuf **from_p, int retries)
207 : {
208 0 : struct socket *so;
209 : struct sockaddr_in *sin;
210 0 : struct mbuf *m, *nam, *mhead, *from, *mopt;
211 : struct rpc_call *call;
212 : struct rpc_reply *reply;
213 0 : struct uio auio;
214 0 : int s, error, rcvflg, timo, secs, len;
215 : static u_int32_t xid = 0;
216 0 : char addr[INET_ADDRSTRLEN];
217 : int *ip;
218 : struct timeval tv;
219 :
220 : /*
221 : * Validate address family.
222 : * Sorry, this is INET specific...
223 : */
224 0 : if (sa->sin_family != AF_INET)
225 0 : return (EAFNOSUPPORT);
226 :
227 : /* Free at end if not null. */
228 : nam = mhead = NULL;
229 0 : from = NULL;
230 :
231 : /*
232 : * Create socket and set its receive timeout.
233 : */
234 0 : if ((error = socreate(AF_INET, &so, SOCK_DGRAM, 0)))
235 : goto out;
236 :
237 0 : m = m_get(M_WAIT, MT_SOOPTS);
238 : tv.tv_sec = 1;
239 : tv.tv_usec = 0;
240 0 : memcpy(mtod(m, struct timeval *), &tv, sizeof tv);
241 0 : m->m_len = sizeof(tv);
242 0 : s = solock(so);
243 0 : error = sosetopt(so, SOL_SOCKET, SO_RCVTIMEO, m);
244 0 : sounlock(so, s);
245 0 : m_freem(m);
246 0 : if (error)
247 : goto out;
248 :
249 : /*
250 : * Enable broadcast if necessary.
251 : */
252 0 : if (from_p) {
253 : int32_t *on;
254 0 : m = m_get(M_WAIT, MT_SOOPTS);
255 0 : on = mtod(m, int32_t *);
256 0 : m->m_len = sizeof(*on);
257 0 : *on = 1;
258 0 : s = solock(so);
259 0 : error = sosetopt(so, SOL_SOCKET, SO_BROADCAST, m);
260 0 : sounlock(so, s);
261 0 : m_freem(m);
262 0 : if (error)
263 0 : goto out;
264 0 : }
265 :
266 : /*
267 : * Bind the local endpoint to a reserved port,
268 : * because some NFS servers refuse requests from
269 : * non-reserved (non-privileged) ports.
270 : */
271 0 : MGET(mopt, M_WAIT, MT_SOOPTS);
272 0 : mopt->m_len = sizeof(int);
273 0 : ip = mtod(mopt, int *);
274 0 : *ip = IP_PORTRANGE_LOW;
275 0 : s = solock(so);
276 0 : error = sosetopt(so, IPPROTO_IP, IP_PORTRANGE, mopt);
277 0 : sounlock(so, s);
278 0 : m_freem(mopt);
279 0 : if (error)
280 : goto out;
281 :
282 0 : MGET(m, M_WAIT, MT_SONAME);
283 0 : sin = mtod(m, struct sockaddr_in *);
284 0 : memset(sin, 0, sizeof(*sin));
285 0 : sin->sin_len = m->m_len = sizeof(struct sockaddr_in);
286 0 : sin->sin_family = AF_INET;
287 0 : sin->sin_addr.s_addr = INADDR_ANY;
288 0 : sin->sin_port = htons(0);
289 0 : s = solock(so);
290 0 : error = sobind(so, m, &proc0);
291 0 : sounlock(so, s);
292 0 : m_freem(m);
293 0 : if (error) {
294 0 : printf("bind failed\n");
295 0 : goto out;
296 : }
297 :
298 0 : MGET(mopt, M_WAIT, MT_SOOPTS);
299 0 : mopt->m_len = sizeof(int);
300 0 : ip = mtod(mopt, int *);
301 0 : *ip = IP_PORTRANGE_DEFAULT;
302 0 : s = solock(so);
303 0 : error = sosetopt(so, IPPROTO_IP, IP_PORTRANGE, mopt);
304 0 : sounlock(so, s);
305 0 : m_freem(mopt);
306 0 : if (error)
307 : goto out;
308 :
309 : /*
310 : * Setup socket address for the server.
311 : */
312 0 : nam = m_get(M_WAIT, MT_SONAME);
313 0 : sin = mtod(nam, struct sockaddr_in *);
314 0 : bcopy(sa, sin, (nam->m_len = sa->sin_len));
315 :
316 : /*
317 : * Prepend RPC message header.
318 : */
319 0 : mhead = m_gethdr(M_WAIT, MT_DATA);
320 0 : mhead->m_next = *data;
321 0 : call = mtod(mhead, struct rpc_call *);
322 0 : mhead->m_len = sizeof(*call);
323 0 : memset(call, 0, sizeof(*call));
324 : /* rpc_call part */
325 0 : xid = krpc_get_xid();
326 0 : call->rp_xid = txdr_unsigned(xid);
327 : /* call->rp_direction = 0; */
328 0 : call->rp_rpcvers = txdr_unsigned(2);
329 0 : call->rp_prog = txdr_unsigned(prog);
330 0 : call->rp_vers = txdr_unsigned(vers);
331 0 : call->rp_proc = txdr_unsigned(func);
332 : /* rpc_auth part (auth_unix as root) */
333 0 : call->rpc_auth.authtype = txdr_unsigned(RPCAUTH_UNIX);
334 0 : call->rpc_auth.authlen = txdr_unsigned(sizeof(struct auth_unix));
335 : /* rpc_verf part (auth_null) */
336 0 : call->rpc_verf.authtype = 0;
337 0 : call->rpc_verf.authlen = 0;
338 :
339 : /*
340 : * Setup packet header
341 : */
342 0 : m_calchdrlen(mhead);
343 0 : mhead->m_pkthdr.ph_ifidx = 0;
344 :
345 : /*
346 : * Send it, repeatedly, until a reply is received,
347 : * but delay each re-send by an increasing amount.
348 : * If the delay hits the maximum, start complaining.
349 : */
350 0 : for (timo = 0; retries; retries--) {
351 : /* Send RPC request (or re-send). */
352 0 : m = m_copym(mhead, 0, M_COPYALL, M_WAIT);
353 0 : if (m == NULL) {
354 : error = ENOBUFS;
355 0 : goto out;
356 : }
357 0 : error = sosend(so, nam, NULL, m, NULL, 0);
358 0 : if (error) {
359 0 : printf("krpc_call: sosend: %d\n", error);
360 0 : goto out;
361 : }
362 0 : m = NULL;
363 :
364 : /* Determine new timeout. */
365 0 : if (timo < MAX_RESEND_DELAY)
366 0 : timo++;
367 : else
368 0 : printf("RPC timeout for server %s (0x%x) prog %u\n",
369 0 : inet_ntop(AF_INET, &sin->sin_addr,
370 0 : addr, sizeof(addr)),
371 0 : ntohl(sin->sin_addr.s_addr), prog);
372 :
373 : /*
374 : * Wait for up to timo seconds for a reply.
375 : * The socket receive timeout was set to 1 second.
376 : */
377 : secs = timo;
378 0 : while (secs > 0) {
379 0 : m_freem(from);
380 0 : from = NULL;
381 :
382 0 : m_freem(m);
383 0 : m = NULL;
384 :
385 0 : auio.uio_resid = len = 1<<16;
386 0 : auio.uio_procp = NULL;
387 0 : rcvflg = 0;
388 0 : error = soreceive(so, &from, &auio, &m, NULL, &rcvflg,
389 : 0);
390 0 : if (error == EWOULDBLOCK) {
391 0 : secs--;
392 0 : continue;
393 : }
394 0 : if (error)
395 : goto out;
396 0 : len -= auio.uio_resid;
397 :
398 : /* Does the reply contain at least a header? */
399 0 : if (len < MIN_REPLY_HDR)
400 0 : continue;
401 0 : if (m->m_len < MIN_REPLY_HDR)
402 0 : continue;
403 0 : reply = mtod(m, struct rpc_reply *);
404 :
405 : /* Is it the right reply? */
406 0 : if (reply->rp_direction != txdr_unsigned(RPC_REPLY))
407 0 : continue;
408 :
409 0 : if (reply->rp_xid != txdr_unsigned(xid))
410 0 : continue;
411 :
412 : /* Was RPC accepted? (authorization OK) */
413 0 : if (reply->rp_astatus != 0) {
414 0 : error = fxdr_unsigned(u_int32_t, reply->rp_errno);
415 0 : printf("rpc denied, error=%d\n", error);
416 0 : continue;
417 : }
418 :
419 : /* Did the call succeed? */
420 0 : if (reply->rp_status != 0) {
421 0 : error = fxdr_unsigned(u_int32_t, reply->rp_status);
422 0 : printf("rpc denied, status=%d\n", error);
423 0 : continue;
424 : }
425 :
426 : goto gotreply; /* break two levels */
427 :
428 : } /* while secs */
429 : } /* forever send/receive */
430 :
431 : error = ETIMEDOUT;
432 0 : goto out;
433 :
434 : gotreply:
435 :
436 : /*
437 : * Get RPC reply header into first mbuf,
438 : * get its length, then strip it off.
439 : */
440 : len = sizeof(*reply);
441 0 : if (m->m_len < len) {
442 0 : m = m_pullup(m, len);
443 0 : if (m == NULL) {
444 : error = ENOBUFS;
445 0 : goto out;
446 : }
447 : }
448 0 : reply = mtod(m, struct rpc_reply *);
449 0 : if (reply->rp_auth.authtype != 0) {
450 0 : len += fxdr_unsigned(u_int32_t, reply->rp_auth.authlen);
451 0 : len = (len + 3) & ~3; /* XXX? */
452 0 : }
453 0 : m_adj(m, len);
454 :
455 : /* result */
456 0 : *data = m;
457 0 : if (from_p && error == 0) {
458 0 : *from_p = from;
459 0 : from = NULL;
460 0 : }
461 :
462 : out:
463 0 : m_freem(nam);
464 0 : m_freem(mhead);
465 0 : m_freem(from);
466 0 : soclose(so, 0);
467 0 : return error;
468 0 : }
469 :
470 : /*
471 : * eXternal Data Representation routines.
472 : * (but with non-standard args...)
473 : */
474 :
475 : /*
476 : * String representation for RPC.
477 : */
478 : struct xdr_string {
479 : u_int32_t len; /* length without null or padding */
480 : char data[4]; /* data (longer, of course) */
481 : /* data is padded to a long-word boundary */
482 : };
483 :
484 : struct mbuf *
485 0 : xdr_string_encode(char *str, int len)
486 : {
487 : struct mbuf *m;
488 : struct xdr_string *xs;
489 : int dlen; /* padded string length */
490 : int mlen; /* message length */
491 :
492 0 : dlen = (len + 3) & ~3;
493 0 : mlen = dlen + 4;
494 :
495 0 : if (mlen > MCLBYTES) /* If too big, we just can't do it. */
496 0 : return (NULL);
497 :
498 0 : m = m_get(M_WAIT, MT_DATA);
499 0 : if (mlen > MLEN) {
500 0 : MCLGET(m, M_WAIT);
501 0 : if ((m->m_flags & M_EXT) == 0) {
502 0 : (void) m_free(m); /* There can be only one. */
503 0 : return (NULL);
504 : }
505 : }
506 0 : xs = mtod(m, struct xdr_string *);
507 0 : m->m_len = mlen;
508 0 : xs->len = txdr_unsigned(len);
509 0 : bcopy(str, xs->data, len);
510 0 : return (m);
511 0 : }
512 :
513 : struct mbuf *
514 0 : xdr_string_decode(struct mbuf *m, char *str, int *len_p)
515 : {
516 : struct xdr_string *xs;
517 : int mlen; /* message length */
518 : int slen; /* string length */
519 :
520 0 : if (m->m_len < 4) {
521 0 : m = m_pullup(m, 4);
522 0 : if (m == NULL)
523 0 : return (NULL);
524 : }
525 0 : xs = mtod(m, struct xdr_string *);
526 0 : slen = fxdr_unsigned(u_int32_t, xs->len);
527 0 : mlen = 4 + ((slen + 3) & ~3);
528 :
529 0 : if (slen > *len_p)
530 0 : slen = *len_p;
531 0 : if (slen > m->m_pkthdr.len) {
532 0 : m_freem(m);
533 0 : return (NULL);
534 : }
535 0 : m_copydata(m, 4, slen, str);
536 0 : m_adj(m, mlen);
537 :
538 0 : str[slen] = '\0';
539 0 : *len_p = slen;
540 :
541 0 : return (m);
542 0 : }
543 :
544 :
545 : /*
546 : * Inet address in RPC messages
547 : * (Note, really four ints, NOT chars. Blech.)
548 : */
549 : struct xdr_inaddr {
550 : u_int32_t atype;
551 : u_int32_t addr[4];
552 : };
553 :
554 : struct mbuf *
555 0 : xdr_inaddr_encode(struct in_addr *ia)
556 : {
557 : struct mbuf *m;
558 : struct xdr_inaddr *xi;
559 : u_int8_t *cp;
560 : u_int32_t *ip;
561 :
562 0 : m = m_get(M_WAIT, MT_DATA);
563 0 : xi = mtod(m, struct xdr_inaddr *);
564 0 : m->m_len = sizeof(*xi);
565 0 : xi->atype = txdr_unsigned(1);
566 0 : ip = xi->addr;
567 0 : cp = (u_int8_t *)&ia->s_addr;
568 0 : *ip++ = txdr_unsigned(*cp++);
569 0 : *ip++ = txdr_unsigned(*cp++);
570 0 : *ip++ = txdr_unsigned(*cp++);
571 0 : *ip++ = txdr_unsigned(*cp++);
572 :
573 0 : return (m);
574 : }
575 :
576 : struct mbuf *
577 0 : xdr_inaddr_decode(struct mbuf *m, struct in_addr *ia)
578 : {
579 : struct xdr_inaddr *xi;
580 : u_int8_t *cp;
581 : u_int32_t *ip;
582 :
583 0 : if (m->m_len < sizeof(*xi)) {
584 0 : m = m_pullup(m, sizeof(*xi));
585 0 : if (m == NULL)
586 0 : return (NULL);
587 : }
588 0 : xi = mtod(m, struct xdr_inaddr *);
589 0 : if (xi->atype != txdr_unsigned(1)) {
590 0 : ia->s_addr = INADDR_ANY;
591 0 : goto out;
592 : }
593 0 : ip = xi->addr;
594 0 : cp = (u_int8_t *)&ia->s_addr;
595 0 : *cp++ = fxdr_unsigned(u_int8_t, *ip++);
596 0 : *cp++ = fxdr_unsigned(u_int8_t, *ip++);
597 0 : *cp++ = fxdr_unsigned(u_int8_t, *ip++);
598 0 : *cp++ = fxdr_unsigned(u_int8_t, *ip++);
599 :
600 : out:
601 0 : m_adj(m, sizeof(*xi));
602 0 : return (m);
603 0 : }
|