1 /* $NetBSD: rpc.c,v 1.18 1998/01/23 19:27:45 thorpej Exp $ */
4 * Copyright (c) 1992 Regents of the University of California.
7 * This software was developed by the Computer Systems Engineering group
8 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9 * contributed to Berkeley.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Lawrence Berkeley Laboratory and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * @(#) Header: rpc.c,v 1.12 93/09/28 08:31:56 leres Exp (LBL)
43 * RPC functions used by NFS and bootparams.
44 * Note that bootparams requires the ability to find out the
45 * address of the server from which its response has come.
46 * This is supported by keeping the IP/UDP headers in the
47 * buffer space provided by the caller. (See rpc_fromaddr)
50 #include <sys/param.h>
51 #include <sys/socket.h>
53 #include <netinet/in.h>
54 #include <netinet/in_systm.h>
66 int32_t authtype; /* auth type */
67 u_int32_t authlen; /* auth length */
72 int32_t ua_hostname; /* null */
75 int32_t ua_gidlist; /* null */
79 u_int32_t rp_xid; /* request transaction id */
80 int32_t rp_direction; /* call direction (0) */
81 u_int32_t rp_rpcvers; /* rpc version (2) */
82 u_int32_t rp_prog; /* program */
83 u_int32_t rp_vers; /* version */
84 u_int32_t rp_proc; /* procedure */
88 u_int32_t rp_xid; /* request transaction id */
89 int32_t rp_direction; /* call direction (1) */
90 int32_t rp_astatus; /* accept status (0: accepted) */
94 struct auth_info rok_auth;
101 static ssize_t recvrpc(struct iodesc *, void *, size_t, time_t);
102 static int rpc_getport(struct iodesc *, n_long, n_long);
105 int rpc_port = 0x400; /* predecrement */
108 * Make a rpc call; return length of answer
109 * Note: Caller must leave room for headers.
112 rpc_call(d, prog, vers, proc, sdata, slen, rdata, rlen)
113 register struct iodesc *d;
114 register n_long prog, vers, proc;
115 register void *sdata;
116 register size_t slen;
117 register void *rdata;
118 register size_t rlen;
121 struct auth_info *auth;
122 struct rpc_call *call;
123 struct rpc_reply *reply;
124 char *send_head, *send_tail;
125 char *recv_head, *recv_tail;
127 int port; /* host order */
131 printf("rpc_call: prog=0x%x vers=%d proc=%d\n",
135 port = rpc_getport(d, prog, vers);
139 d->destport = htons(port);
142 * Prepend authorization stuff and headers.
143 * Note, must prepend things in reverse order.
146 send_tail = (char *)sdata + slen;
148 /* Auth verifier is always auth_null */
149 send_head -= sizeof(*auth);
150 auth = (struct auth_info *)send_head;
151 auth->authtype = htonl(RPCAUTH_NULL);
155 /* Auth credentials: always auth unix (as root) */
156 send_head -= sizeof(struct auth_unix);
157 bzero(send_head, sizeof(struct auth_unix));
158 send_head -= sizeof(*auth);
159 auth = (struct auth_info *)send_head;
160 auth->authtype = htonl(RPCAUTH_UNIX);
161 auth->authlen = htonl(sizeof(struct auth_unix));
163 /* Auth credentials: always auth_null (XXX OK?) */
164 send_head -= sizeof(*auth);
166 auth->authtype = htonl(RPCAUTH_NULL);
170 /* RPC call structure. */
171 send_head -= sizeof(*call);
172 call = (struct rpc_call *)send_head;
174 call->rp_xid = htonl(rpc_xid);
175 call->rp_direction = htonl(RPC_CALL);
176 call->rp_rpcvers = htonl(RPC_VER2);
177 call->rp_prog = htonl(prog);
178 call->rp_vers = htonl(vers);
179 call->rp_proc = htonl(proc);
181 /* Make room for the rpc_reply header. */
183 recv_tail = (char *)rdata + rlen;
184 recv_head -= sizeof(*reply);
187 sendudp, send_head, send_tail - send_head,
188 recvrpc, recv_head, recv_tail - recv_head);
192 printf("callrpc: cc=%ld rlen=%lu\n", (long)cc, (u_long)rlen);
197 if (cc <= sizeof(*reply)) {
202 recv_tail = recv_head + cc;
205 * Check the RPC reply status.
206 * The xid, dir, astatus were already checked.
208 reply = (struct rpc_reply *)recv_head;
209 auth = &reply->rp_u.rpu_rok.rok_auth;
210 x = ntohl(auth->authlen);
214 printf("callrpc: reply auth != NULL\n");
219 x = ntohl(reply->rp_u.rpu_rok.rok_status);
221 printf("callrpc: error = %ld\n", (long)x);
225 recv_head += sizeof(*reply);
227 return (ssize_t)(recv_tail - recv_head);
231 * Returns true if packet is the one we're waiting for.
232 * This just checks the XID, direction, acceptance.
233 * Remaining checks are done by callrpc
236 recvrpc(d, pkt, len, tleft)
237 register struct iodesc *d;
242 register struct rpc_reply *reply;
249 printf("recvrpc: called len=%lu\n", (u_long)len);
252 n = readudp(d, pkt, len, tleft);
256 reply = (struct rpc_reply *)pkt;
258 x = ntohl(reply->rp_xid);
262 printf("recvrpc: rp_xid %d != xid %d\n", x, rpc_xid);
267 x = ntohl(reply->rp_direction);
268 if (x != RPC_REPLY) {
271 printf("recvrpc: rp_direction %d != REPLY\n", x);
276 x = ntohl(reply->rp_astatus);
277 if (x != RPC_MSGACCEPTED) {
278 errno = ntohl(reply->rp_u.rpu_errno);
279 printf("recvrpc: reject, astat=%d, errno=%d\n", x, errno);
283 /* Return data count (thus indicating success) */
288 * Given a pointer to a reply just received,
289 * dig out the IP address/port from the headers.
292 rpc_fromaddr(pkt, addr, port)
294 struct in_addr *addr;
298 /* Tail of IP header: just IP addresses */
302 u_int16_t uh_sport; /* source port */
303 u_int16_t uh_dport; /* destination port */
304 int16_t uh_ulen; /* udp length */
305 u_int16_t uh_sum; /* udp checksum */
306 /* RPC reply header: */
307 struct rpc_reply rpc;
310 hhdr = ((struct hackhdr *)pkt) - 1;
311 addr->s_addr = hhdr->ip_src;
312 *port = hhdr->uh_sport;
316 * RPC Portmapper cache
318 #define PMAP_NUM 8 /* need at most 5 pmap entries */
322 struct in_addr addr; /* server, net order */
323 u_int prog; /* host order */
324 u_int vers; /* host order */
325 int port; /* host order */
326 } rpc_pmap_list[PMAP_NUM];
328 /* return port number in host order, or -1 */
330 rpc_pmap_getcache(addr, prog, vers)
331 struct in_addr addr; /* server, net order */
332 u_int prog; /* host order */
333 u_int vers; /* host order */
335 struct pmap_list *pl;
337 for (pl = rpc_pmap_list; pl < &rpc_pmap_list[rpc_pmap_num]; pl++) {
338 if (pl->addr.s_addr == addr.s_addr &&
339 pl->prog == prog && pl->vers == vers )
348 rpc_pmap_putcache(addr, prog, vers, port)
349 struct in_addr addr; /* server, net order */
350 u_int prog; /* host order */
351 u_int vers; /* host order */
352 int port; /* host order */
354 struct pmap_list *pl;
356 /* Don't overflow cache... */
357 if (rpc_pmap_num >= PMAP_NUM) {
358 /* ... just re-use the last entry. */
359 rpc_pmap_num = PMAP_NUM - 1;
361 printf("rpc_pmap_putcache: cache overflow\n");
365 pl = &rpc_pmap_list[rpc_pmap_num];
377 * Request a port number from the port mapper.
378 * Returns the port in host order.
381 rpc_getport(d, prog, vers)
382 register struct iodesc *d;
383 n_long prog; /* host order */
384 n_long vers; /* host order */
387 n_long prog; /* call program */
388 n_long vers; /* call version */
389 n_long proto; /* call protocol */
390 n_long port; /* call port (unused) */
396 n_long h[RPC_HEADER_WORDS];
400 n_long h[RPC_HEADER_WORDS];
409 printf("getport: prog=0x%x vers=%d\n", prog, vers);
412 /* This one is fixed forever. */
413 if (prog == PMAPPROG)
416 /* Try for cached answer first */
417 port = rpc_pmap_getcache(d->destip, prog, vers);
422 args->prog = htonl(prog);
423 args->vers = htonl(vers);
424 args->proto = htonl(IPPROTO_UDP);
428 cc = rpc_call(d, PMAPPROG, PMAPVERS, PMAPPROC_GETPORT,
429 args, sizeof(*args), res, sizeof(*res));
430 if (cc < sizeof(*res)) {
431 printf("getport: %s", strerror(errno));
435 port = (int)ntohl(res->port);
437 rpc_pmap_putcache(d->destip, prog, vers, port);