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 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * @(#) Header: rpc.c,v 1.12 93/09/28 08:31:56 leres Exp (LBL)
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
42 * RPC functions used by NFS and bootparams.
43 * Note that bootparams requires the ability to find out the
44 * address of the server from which its response has come.
45 * This is supported by keeping the IP/UDP headers in the
46 * buffer space provided by the caller. (See rpc_fromaddr)
49 #include <sys/param.h>
50 #include <sys/socket.h>
52 #include <netinet/in.h>
53 #include <netinet/in_systm.h>
65 int32_t authtype; /* auth type */
66 u_int32_t authlen; /* auth length */
71 int32_t ua_hostname; /* null */
74 int32_t ua_gidlist; /* null */
78 u_int32_t rp_xid; /* request transaction id */
79 int32_t rp_direction; /* call direction (0) */
80 u_int32_t rp_rpcvers; /* rpc version (2) */
81 u_int32_t rp_prog; /* program */
82 u_int32_t rp_vers; /* version */
83 u_int32_t rp_proc; /* procedure */
87 u_int32_t rp_xid; /* request transaction id */
88 int32_t rp_direction; /* call direction (1) */
89 int32_t rp_astatus; /* accept status (0: accepted) */
93 struct auth_info rok_auth;
100 static ssize_t recvrpc(struct iodesc *, void *, size_t, time_t);
101 static int rpc_getport(struct iodesc *, n_long, n_long);
104 int rpc_port = 0x400; /* predecrement */
107 * Make a rpc call; return length of answer
108 * Note: Caller must leave room for headers.
111 rpc_call(d, prog, vers, proc, sdata, slen, rdata, rlen)
113 n_long prog, vers, proc;
120 struct auth_info *auth;
121 struct rpc_call *call;
122 struct rpc_reply *reply;
123 char *send_head, *send_tail;
124 char *recv_head, *recv_tail;
126 int port; /* host order */
130 printf("rpc_call: prog=0x%x vers=%d proc=%d\n",
134 port = rpc_getport(d, prog, vers);
138 d->destport = htons(port);
141 * Prepend authorization stuff and headers.
142 * Note, must prepend things in reverse order.
145 send_tail = (char *)sdata + slen;
147 /* Auth verifier is always auth_null */
148 send_head -= sizeof(*auth);
149 auth = (struct auth_info *)send_head;
150 auth->authtype = htonl(RPCAUTH_NULL);
154 /* Auth credentials: always auth unix (as root) */
155 send_head -= sizeof(struct auth_unix);
156 bzero(send_head, sizeof(struct auth_unix));
157 send_head -= sizeof(*auth);
158 auth = (struct auth_info *)send_head;
159 auth->authtype = htonl(RPCAUTH_UNIX);
160 auth->authlen = htonl(sizeof(struct auth_unix));
162 /* Auth credentials: always auth_null (XXX OK?) */
163 send_head -= sizeof(*auth);
165 auth->authtype = htonl(RPCAUTH_NULL);
169 /* RPC call structure. */
170 send_head -= sizeof(*call);
171 call = (struct rpc_call *)send_head;
173 call->rp_xid = htonl(rpc_xid);
174 call->rp_direction = htonl(RPC_CALL);
175 call->rp_rpcvers = htonl(RPC_VER2);
176 call->rp_prog = htonl(prog);
177 call->rp_vers = htonl(vers);
178 call->rp_proc = htonl(proc);
180 /* Make room for the rpc_reply header. */
182 recv_tail = (char *)rdata + rlen;
183 recv_head -= sizeof(*reply);
186 sendudp, send_head, send_tail - send_head,
187 recvrpc, recv_head, recv_tail - recv_head);
191 printf("callrpc: cc=%ld rlen=%lu\n", (long)cc, (u_long)rlen);
196 if (cc <= sizeof(*reply)) {
201 recv_tail = recv_head + cc;
204 * Check the RPC reply status.
205 * The xid, dir, astatus were already checked.
207 reply = (struct rpc_reply *)recv_head;
208 auth = &reply->rp_u.rpu_rok.rok_auth;
209 x = ntohl(auth->authlen);
213 printf("callrpc: reply auth != NULL\n");
218 x = ntohl(reply->rp_u.rpu_rok.rok_status);
220 printf("callrpc: error = %ld\n", (long)x);
224 recv_head += sizeof(*reply);
226 return (ssize_t)(recv_tail - recv_head);
230 * Returns true if packet is the one we're waiting for.
231 * This just checks the XID, direction, acceptance.
232 * Remaining checks are done by callrpc
235 recvrpc(d, pkt, len, tleft)
241 struct rpc_reply *reply;
248 printf("recvrpc: called len=%lu\n", (u_long)len);
251 n = readudp(d, pkt, len, tleft);
255 reply = (struct rpc_reply *)pkt;
257 x = ntohl(reply->rp_xid);
261 printf("recvrpc: rp_xid %d != xid %d\n", x, rpc_xid);
266 x = ntohl(reply->rp_direction);
267 if (x != RPC_REPLY) {
270 printf("recvrpc: rp_direction %d != REPLY\n", x);
275 x = ntohl(reply->rp_astatus);
276 if (x != RPC_MSGACCEPTED) {
277 errno = ntohl(reply->rp_u.rpu_errno);
278 printf("recvrpc: reject, astat=%d, errno=%d\n", x, errno);
282 /* Return data count (thus indicating success) */
287 * Given a pointer to a reply just received,
288 * dig out the IP address/port from the headers.
291 rpc_fromaddr(pkt, addr, port)
293 struct in_addr *addr;
297 /* Tail of IP header: just IP addresses */
301 u_int16_t uh_sport; /* source port */
302 u_int16_t uh_dport; /* destination port */
303 int16_t uh_ulen; /* udp length */
304 u_int16_t uh_sum; /* udp checksum */
305 /* RPC reply header: */
306 struct rpc_reply rpc;
309 hhdr = ((struct hackhdr *)pkt) - 1;
310 addr->s_addr = hhdr->ip_src;
311 *port = hhdr->uh_sport;
315 * RPC Portmapper cache
317 #define PMAP_NUM 8 /* need at most 5 pmap entries */
321 struct in_addr addr; /* server, net order */
322 u_int prog; /* host order */
323 u_int vers; /* host order */
324 int port; /* host order */
325 } rpc_pmap_list[PMAP_NUM];
327 /* return port number in host order, or -1 */
329 rpc_pmap_getcache(addr, prog, vers)
330 struct in_addr addr; /* server, net order */
331 u_int prog; /* host order */
332 u_int vers; /* host order */
334 struct pmap_list *pl;
336 for (pl = rpc_pmap_list; pl < &rpc_pmap_list[rpc_pmap_num]; pl++) {
337 if (pl->addr.s_addr == addr.s_addr &&
338 pl->prog == prog && pl->vers == vers )
347 rpc_pmap_putcache(addr, prog, vers, port)
348 struct in_addr addr; /* server, net order */
349 u_int prog; /* host order */
350 u_int vers; /* host order */
351 int port; /* host order */
353 struct pmap_list *pl;
355 /* Don't overflow cache... */
356 if (rpc_pmap_num >= PMAP_NUM) {
357 /* ... just re-use the last entry. */
358 rpc_pmap_num = PMAP_NUM - 1;
360 printf("rpc_pmap_putcache: cache overflow\n");
364 pl = &rpc_pmap_list[rpc_pmap_num];
376 * Request a port number from the port mapper.
377 * Returns the port in host order.
380 rpc_getport(d, prog, vers)
382 n_long prog; /* host order */
383 n_long vers; /* host order */
386 n_long prog; /* call program */
387 n_long vers; /* call version */
388 n_long proto; /* call protocol */
389 n_long port; /* call port (unused) */
395 n_long h[RPC_HEADER_WORDS];
399 n_long h[RPC_HEADER_WORDS];
408 printf("%s: prog=0x%x vers=%d\n", __func__, prog, vers);
411 /* This one is fixed forever. */
412 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);
443 printf("%s: port=%u\n", __func__, port);