1 /* $NetBSD: rpc.c,v 1.18 1998/01/23 19:27:45 thorpej Exp $ */
2 /* $DragonFly: src/lib/libstand/rpc.c,v 1.3 2005/12/11 02:27:26 swildner 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(struct iodesc
*d
, n_long prog
, n_long vers
, n_long proc
, void *sdata
,
114 size_t slen
, void *rdata
, size_t rlen
)
117 struct auth_info
*auth
;
118 struct rpc_call
*call
;
119 struct rpc_reply
*reply
;
120 char *send_head
, *send_tail
;
121 char *recv_head
, *recv_tail
;
123 int port
; /* host order */
127 printf("rpc_call: prog=0x%x vers=%d proc=%d\n",
131 port
= rpc_getport(d
, prog
, vers
);
135 d
->destport
= htons(port
);
138 * Prepend authorization stuff and headers.
139 * Note, must prepend things in reverse order.
142 send_tail
= (char *)sdata
+ slen
;
144 /* Auth verifier is always auth_null */
145 send_head
-= sizeof(*auth
);
146 auth
= (struct auth_info
*)send_head
;
147 auth
->authtype
= htonl(RPCAUTH_NULL
);
151 /* Auth credentials: always auth unix (as root) */
152 send_head
-= sizeof(struct auth_unix
);
153 bzero(send_head
, sizeof(struct auth_unix
));
154 send_head
-= sizeof(*auth
);
155 auth
= (struct auth_info
*)send_head
;
156 auth
->authtype
= htonl(RPCAUTH_UNIX
);
157 auth
->authlen
= htonl(sizeof(struct auth_unix
));
159 /* Auth credentials: always auth_null (XXX OK?) */
160 send_head
-= sizeof(*auth
);
162 auth
->authtype
= htonl(RPCAUTH_NULL
);
166 /* RPC call structure. */
167 send_head
-= sizeof(*call
);
168 call
= (struct rpc_call
*)send_head
;
170 call
->rp_xid
= htonl(rpc_xid
);
171 call
->rp_direction
= htonl(RPC_CALL
);
172 call
->rp_rpcvers
= htonl(RPC_VER2
);
173 call
->rp_prog
= htonl(prog
);
174 call
->rp_vers
= htonl(vers
);
175 call
->rp_proc
= htonl(proc
);
177 /* Make room for the rpc_reply header. */
179 recv_tail
= (char *)rdata
+ rlen
;
180 recv_head
-= sizeof(*reply
);
183 sendudp
, send_head
, send_tail
- send_head
,
184 recvrpc
, recv_head
, recv_tail
- recv_head
);
188 printf("callrpc: cc=%ld rlen=%lu\n", (long)cc
, (u_long
)rlen
);
193 if (cc
<= sizeof(*reply
)) {
198 recv_tail
= recv_head
+ cc
;
201 * Check the RPC reply status.
202 * The xid, dir, astatus were already checked.
204 reply
= (struct rpc_reply
*)recv_head
;
205 auth
= &reply
->rp_u
.rpu_rok
.rok_auth
;
206 x
= ntohl(auth
->authlen
);
210 printf("callrpc: reply auth != NULL\n");
215 x
= ntohl(reply
->rp_u
.rpu_rok
.rok_status
);
217 printf("callrpc: error = %ld\n", (long)x
);
221 recv_head
+= sizeof(*reply
);
223 return (ssize_t
)(recv_tail
- recv_head
);
227 * Returns true if packet is the one we're waiting for.
228 * This just checks the XID, direction, acceptance.
229 * Remaining checks are done by callrpc
232 recvrpc(struct iodesc
*d
, void *pkt
, size_t len
, time_t tleft
)
234 struct rpc_reply
*reply
;
241 printf("recvrpc: called len=%lu\n", (u_long
)len
);
244 n
= readudp(d
, pkt
, len
, tleft
);
248 reply
= (struct rpc_reply
*)pkt
;
250 x
= ntohl(reply
->rp_xid
);
254 printf("recvrpc: rp_xid %d != xid %d\n", x
, rpc_xid
);
259 x
= ntohl(reply
->rp_direction
);
260 if (x
!= RPC_REPLY
) {
263 printf("recvrpc: rp_direction %d != REPLY\n", x
);
268 x
= ntohl(reply
->rp_astatus
);
269 if (x
!= RPC_MSGACCEPTED
) {
270 errno
= ntohl(reply
->rp_u
.rpu_errno
);
271 printf("recvrpc: reject, astat=%d, errno=%d\n", x
, errno
);
275 /* Return data count (thus indicating success) */
280 * Given a pointer to a reply just received,
281 * dig out the IP address/port from the headers.
284 rpc_fromaddr(void *pkt
, struct in_addr
*addr
, u_short
*port
)
287 /* Tail of IP header: just IP addresses */
291 u_int16_t uh_sport
; /* source port */
292 u_int16_t uh_dport
; /* destination port */
293 int16_t uh_ulen
; /* udp length */
294 u_int16_t uh_sum
; /* udp checksum */
295 /* RPC reply header: */
296 struct rpc_reply rpc
;
299 hhdr
= ((struct hackhdr
*)pkt
) - 1;
300 addr
->s_addr
= hhdr
->ip_src
;
301 *port
= hhdr
->uh_sport
;
305 * RPC Portmapper cache
307 #define PMAP_NUM 8 /* need at most 5 pmap entries */
311 struct in_addr addr
; /* server, net order */
312 u_int prog
; /* host order */
313 u_int vers
; /* host order */
314 int port
; /* host order */
315 } rpc_pmap_list
[PMAP_NUM
];
318 * return port number in host order, or -1
321 * addr: server, net order
326 rpc_pmap_getcache(struct in_addr addr
, u_int prog
, u_int vers
)
328 struct pmap_list
*pl
;
330 for (pl
= rpc_pmap_list
; pl
< &rpc_pmap_list
[rpc_pmap_num
]; pl
++) {
331 if (pl
->addr
.s_addr
== addr
.s_addr
&&
332 pl
->prog
== prog
&& pl
->vers
== vers
)
342 * addr: server, net order
348 rpc_pmap_putcache(struct in_addr addr
, u_int prog
, u_int vers
, int port
)
350 struct pmap_list
*pl
;
352 /* Don't overflow cache... */
353 if (rpc_pmap_num
>= PMAP_NUM
) {
354 /* ... just re-use the last entry. */
355 rpc_pmap_num
= PMAP_NUM
- 1;
357 printf("rpc_pmap_putcache: cache overflow\n");
361 pl
= &rpc_pmap_list
[rpc_pmap_num
];
373 * Request a port number from the port mapper.
374 * Returns the port in host order.
381 rpc_getport(struct iodesc
*d
, n_long prog
, n_long vers
)
384 n_long prog
; /* call program */
385 n_long vers
; /* call version */
386 n_long proto
; /* call protocol */
387 n_long port
; /* call port (unused) */
393 n_long h
[RPC_HEADER_WORDS
];
397 n_long h
[RPC_HEADER_WORDS
];
406 printf("getport: prog=0x%x vers=%d\n", prog
, vers
);
409 /* This one is fixed forever. */
410 if (prog
== PMAPPROG
)
413 /* Try for cached answer first */
414 port
= rpc_pmap_getcache(d
->destip
, prog
, vers
);
419 args
->prog
= htonl(prog
);
420 args
->vers
= htonl(vers
);
421 args
->proto
= htonl(IPPROTO_UDP
);
425 cc
= rpc_call(d
, PMAPPROG
, PMAPVERS
, PMAPPROC_GETPORT
,
426 args
, sizeof(*args
), res
, sizeof(*res
));
427 if (cc
< sizeof(*res
)) {
428 printf("getport: %s", strerror(errno
));
432 port
= (int)ntohl(res
->port
);
434 rpc_pmap_putcache(d
->destip
, prog
, vers
, port
);