1 /* $NetBSD: rpc.c,v 1.29 2009/01/17 14:00:36 tsutsui 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>
57 #include <lib/libkern/libkern.h>
71 int32_t authtype
; /* auth type */
72 u_int32_t authlen
; /* auth length */
77 int32_t ua_hostname
; /* null */
80 int32_t ua_gidlist
; /* null */
84 u_int32_t rp_xid
; /* request transaction id */
85 int32_t rp_direction
; /* call direction (0) */
86 u_int32_t rp_rpcvers
; /* rpc version (2) */
87 u_int32_t rp_prog
; /* program */
88 u_int32_t rp_vers
; /* version */
89 u_int32_t rp_proc
; /* procedure */
93 u_int32_t rp_xid
; /* request transaction id */
94 int32_t rp_direction
; /* call direction (1) */
95 int32_t rp_astatus
; /* accept status (0: accepted) */
99 struct auth_info rok_auth
;
100 u_int32_t rok_status
;
106 static ssize_t
recvrpc(struct iodesc
*, void *, size_t, saseconds_t
);
109 int rpc_port
= 0x400; /* predecrement */
112 * Make a rpc call; return length of answer
113 * Note: Caller must leave room for headers.
116 rpc_call(struct iodesc
*d
, n_long prog
, n_long vers
, n_long proc
,
117 void *sdata
, size_t slen
, void *rdata
, size_t rlen
)
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 (void)memset(send_head
, 0, 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 ((size_t)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 = %d\n", 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(struct iodesc
*d
, void *pkt
, size_t len
, saseconds_t tleft
)
237 struct rpc_reply
*reply
;
244 printf("recvrpc: called len=%lu\n", (u_long
)len
);
247 n
= readudp(d
, pkt
, len
, tleft
);
251 reply
= (struct rpc_reply
*)pkt
;
253 x
= ntohl(reply
->rp_xid
);
257 printf("recvrpc: rp_xid %d != xid %d\n", x
, rpc_xid
);
262 x
= ntohl(reply
->rp_direction
);
263 if (x
!= RPC_REPLY
) {
266 printf("recvrpc: rp_direction %d != REPLY\n", x
);
271 x
= ntohl(reply
->rp_astatus
);
272 if (x
!= RPC_MSGACCEPTED
) {
273 errno
= ntohl(reply
->rp_u
.rpu_errno
);
274 printf("recvrpc: reject, astat=%d, errno=%d\n", x
, errno
);
278 /* Return data count (thus indicating success) */
283 * Given a pointer to a reply just received,
284 * dig out the IP address/port from the headers.
287 rpc_fromaddr(void *pkt
, struct in_addr
*addr
, u_short
*port
)
290 /* Tail of IP header: just IP addresses */
294 u_int16_t uh_sport
; /* source port */
295 u_int16_t uh_dport
; /* destination port */
296 int16_t uh_ulen
; /* udp length */
297 u_int16_t uh_sum
; /* udp checksum */
298 /* RPC reply header: */
299 struct rpc_reply rpc
;
302 hhdr
= ((struct hackhdr
*)pkt
) - 1;
303 addr
->s_addr
= hhdr
->ip_src
;
304 *port
= hhdr
->uh_sport
;
308 #define rpc_pmap_getcache(addr, prog, vers) (-1)
309 #define rpc_pmap_putcache(addr, prog, vers, port)
313 * RPC Portmapper cache
315 #define PMAP_NUM 8 /* need at most 5 pmap entries */
319 struct in_addr addr
; /* server, net order */
320 u_int prog
; /* host order */
321 u_int vers
; /* host order */
322 int port
; /* host order */
323 } rpc_pmap_list
[PMAP_NUM
];
326 * return port number in host order, or -1.
328 * addr .. server, net order.
329 * prog .. host order.
330 * vers .. host order.
333 rpc_pmap_getcache(struct in_addr addr
, u_int prog
, u_int vers
)
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
)
349 * addr .. server, net order.
350 * prog .. host order.
351 * vers .. host order.
352 * port .. host order.
355 rpc_pmap_putcache(struct in_addr addr
, u_int prog
, u_int vers
, int port
)
357 struct pmap_list
*pl
;
359 /* Don't overflow cache... */
360 if (rpc_pmap_num
>= PMAP_NUM
) {
361 /* ... just re-use the last entry. */
362 rpc_pmap_num
= PMAP_NUM
- 1;
364 printf("rpc_pmap_putcache: cache overflow\n");
368 pl
= &rpc_pmap_list
[rpc_pmap_num
];
380 * Request a port number from the port mapper.
381 * Returns the port in host order.
382 * prog and vers are host order.
385 rpc_getport(struct iodesc
*d
, n_long prog
, n_long vers
)
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 ((size_t)cc
< sizeof(*res
)) {
432 printf("getport: %s", strerror(errno
));
436 port
= (int)ntohl(res
->port
);
438 rpc_pmap_putcache(d
->destip
, prog
, vers
, port
);