1 /* $NetBSD: rpc.c,v 1.18 1998/01/23 19:27:45 thorpej Exp $ */
2 /* $DragonFly: src/lib/libstand/rpc.c,v 1.2 2004/10/25 19:38:45 drhodus Exp $ */
5 * Copyright (c) 1992 Regents of the University of California.
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.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
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.
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
40 * @(#) Header: rpc.c,v 1.12 93/09/28 08:31:56 leres Exp (LBL)
44 * RPC functions used by NFS and bootparams.
45 * Note that bootparams requires the ability to find out the
46 * address of the server from which its response has come.
47 * This is supported by keeping the IP/UDP headers in the
48 * buffer space provided by the caller. (See rpc_fromaddr)
51 #include <sys/param.h>
52 #include <sys/socket.h>
54 #include <netinet/in.h>
55 #include <netinet/in_systm.h>
67 int32_t authtype; /* auth type */
68 u_int32_t authlen; /* auth length */
73 int32_t ua_hostname; /* null */
76 int32_t ua_gidlist; /* null */
80 u_int32_t rp_xid; /* request transaction id */
81 int32_t rp_direction; /* call direction (0) */
82 u_int32_t rp_rpcvers; /* rpc version (2) */
83 u_int32_t rp_prog; /* program */
84 u_int32_t rp_vers; /* version */
85 u_int32_t rp_proc; /* procedure */
89 u_int32_t rp_xid; /* request transaction id */
90 int32_t rp_direction; /* call direction (1) */
91 int32_t rp_astatus; /* accept status (0: accepted) */
95 struct auth_info rok_auth;
102 static ssize_t recvrpc(struct iodesc *, void *, size_t, time_t);
103 static int rpc_getport(struct iodesc *, n_long, n_long);
106 int rpc_port = 0x400; /* predecrement */
109 * Make a rpc call; return length of answer
110 * Note: Caller must leave room for headers.
113 rpc_call(d, prog, vers, proc, sdata, slen, rdata, rlen)
115 n_long prog, vers, proc;
122 struct auth_info *auth;
123 struct rpc_call *call;
124 struct rpc_reply *reply;
125 char *send_head, *send_tail;
126 char *recv_head, *recv_tail;
128 int port; /* host order */
132 printf("rpc_call: prog=0x%x vers=%d proc=%d\n",
136 port = rpc_getport(d, prog, vers);
140 d->destport = htons(port);
143 * Prepend authorization stuff and headers.
144 * Note, must prepend things in reverse order.
147 send_tail = (char *)sdata + slen;
149 /* Auth verifier is always auth_null */
150 send_head -= sizeof(*auth);
151 auth = (struct auth_info *)send_head;
152 auth->authtype = htonl(RPCAUTH_NULL);
156 /* Auth credentials: always auth unix (as root) */
157 send_head -= sizeof(struct auth_unix);
158 bzero(send_head, sizeof(struct auth_unix));
159 send_head -= sizeof(*auth);
160 auth = (struct auth_info *)send_head;
161 auth->authtype = htonl(RPCAUTH_UNIX);
162 auth->authlen = htonl(sizeof(struct auth_unix));
164 /* Auth credentials: always auth_null (XXX OK?) */
165 send_head -= sizeof(*auth);
167 auth->authtype = htonl(RPCAUTH_NULL);
171 /* RPC call structure. */
172 send_head -= sizeof(*call);
173 call = (struct rpc_call *)send_head;
175 call->rp_xid = htonl(rpc_xid);
176 call->rp_direction = htonl(RPC_CALL);
177 call->rp_rpcvers = htonl(RPC_VER2);
178 call->rp_prog = htonl(prog);
179 call->rp_vers = htonl(vers);
180 call->rp_proc = htonl(proc);
182 /* Make room for the rpc_reply header. */
184 recv_tail = (char *)rdata + rlen;
185 recv_head -= sizeof(*reply);
188 sendudp, send_head, send_tail - send_head,
189 recvrpc, recv_head, recv_tail - recv_head);
193 printf("callrpc: cc=%ld rlen=%lu\n", (long)cc, (u_long)rlen);
198 if (cc <= sizeof(*reply)) {
203 recv_tail = recv_head + cc;
206 * Check the RPC reply status.
207 * The xid, dir, astatus were already checked.
209 reply = (struct rpc_reply *)recv_head;
210 auth = &reply->rp_u.rpu_rok.rok_auth;
211 x = ntohl(auth->authlen);
215 printf("callrpc: reply auth != NULL\n");
220 x = ntohl(reply->rp_u.rpu_rok.rok_status);
222 printf("callrpc: error = %ld\n", (long)x);
226 recv_head += sizeof(*reply);
228 return (ssize_t)(recv_tail - recv_head);
232 * Returns true if packet is the one we're waiting for.
233 * This just checks the XID, direction, acceptance.
234 * Remaining checks are done by callrpc
237 recvrpc(d, pkt, len, tleft)
243 struct rpc_reply *reply;
250 printf("recvrpc: called len=%lu\n", (u_long)len);
253 n = readudp(d, pkt, len, tleft);
257 reply = (struct rpc_reply *)pkt;
259 x = ntohl(reply->rp_xid);
263 printf("recvrpc: rp_xid %d != xid %d\n", x, rpc_xid);
268 x = ntohl(reply->rp_direction);
269 if (x != RPC_REPLY) {
272 printf("recvrpc: rp_direction %d != REPLY\n", x);
277 x = ntohl(reply->rp_astatus);
278 if (x != RPC_MSGACCEPTED) {
279 errno = ntohl(reply->rp_u.rpu_errno);
280 printf("recvrpc: reject, astat=%d, errno=%d\n", x, errno);
284 /* Return data count (thus indicating success) */
289 * Given a pointer to a reply just received,
290 * dig out the IP address/port from the headers.
293 rpc_fromaddr(pkt, addr, port)
295 struct in_addr *addr;
299 /* Tail of IP header: just IP addresses */
303 u_int16_t uh_sport; /* source port */
304 u_int16_t uh_dport; /* destination port */
305 int16_t uh_ulen; /* udp length */
306 u_int16_t uh_sum; /* udp checksum */
307 /* RPC reply header: */
308 struct rpc_reply rpc;
311 hhdr = ((struct hackhdr *)pkt) - 1;
312 addr->s_addr = hhdr->ip_src;
313 *port = hhdr->uh_sport;
317 * RPC Portmapper cache
319 #define PMAP_NUM 8 /* need at most 5 pmap entries */
323 struct in_addr addr; /* server, net order */
324 u_int prog; /* host order */
325 u_int vers; /* host order */
326 int port; /* host order */
327 } rpc_pmap_list[PMAP_NUM];
329 /* return port number in host order, or -1 */
331 rpc_pmap_getcache(addr, prog, vers)
332 struct in_addr addr; /* server, net order */
333 u_int prog; /* host order */
334 u_int vers; /* host order */
336 struct pmap_list *pl;
338 for (pl = rpc_pmap_list; pl < &rpc_pmap_list[rpc_pmap_num]; pl++) {
339 if (pl->addr.s_addr == addr.s_addr &&
340 pl->prog == prog && pl->vers == vers )
349 rpc_pmap_putcache(addr, prog, vers, port)
350 struct in_addr addr; /* server, net order */
351 u_int prog; /* host order */
352 u_int vers; /* host order */
353 int port; /* host order */
355 struct pmap_list *pl;
357 /* Don't overflow cache... */
358 if (rpc_pmap_num >= PMAP_NUM) {
359 /* ... just re-use the last entry. */
360 rpc_pmap_num = PMAP_NUM - 1;
362 printf("rpc_pmap_putcache: cache overflow\n");
366 pl = &rpc_pmap_list[rpc_pmap_num];
378 * Request a port number from the port mapper.
379 * Returns the port in host order.
382 rpc_getport(d, prog, vers)
384 n_long prog; /* host order */
385 n_long vers; /* host order */
388 n_long prog; /* call program */
389 n_long vers; /* call version */
390 n_long proto; /* call protocol */
391 n_long port; /* call port (unused) */
397 n_long h[RPC_HEADER_WORDS];
401 n_long h[RPC_HEADER_WORDS];
410 printf("getport: prog=0x%x vers=%d\n", prog, vers);
413 /* This one is fixed forever. */
414 if (prog == PMAPPROG)
417 /* Try for cached answer first */
418 port = rpc_pmap_getcache(d->destip, prog, vers);
423 args->prog = htonl(prog);
424 args->vers = htonl(vers);
425 args->proto = htonl(IPPROTO_UDP);
429 cc = rpc_call(d, PMAPPROG, PMAPVERS, PMAPPROC_GETPORT,
430 args, sizeof(*args), res, sizeof(*res));
431 if (cc < sizeof(*res)) {
432 printf("getport: %s", strerror(errno));
436 port = (int)ntohl(res->port);
438 rpc_pmap_putcache(d->destip, prog, vers, port);