rpc.c revision 1.5 1 /* $NetBSD: rpc.c,v 1.5 1995/06/27 15:28:58 gwr Exp $ */
2
3 /*
4 * Copyright (c) 1992 Regents of the University of California.
5 * All rights reserved.
6 *
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.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
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.
26 *
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
37 * SUCH DAMAGE.
38 *
39 * @(#) Header: rpc.c,v 1.12 93/09/28 08:31:56 leres Exp (LBL)
40 */
41
42 /*
43 * This code was copied from sys/lib/libsa/rpc.c and modified to
44 * avoid copying the args/results to/from local packet buffers,
45 * because that allows the new function rpc_fromaddr() to look
46 * back into the headers to find out where a response is from.
47 * This is needed by the bootparam.c code, and is also a little
48 * more efficient (though efficiency is not a concern here.)
49 * The new calling interface means nfs.c had to change too...
50 */
51
52 #include <sys/param.h>
53 #include <sys/socket.h>
54
55 #include <netinet/in.h>
56 #include <netinet/in_systm.h>
57
58 #include <nfs/rpcv2.h>
59 #include <nfs/nfsv2.h>
60 #include <nfs/xdr_subs.h>
61
62 #include <string.h>
63
64 #include "stand.h"
65 #include "net.h"
66 #include "netif.h"
67 #include "rpc.h"
68
69 struct auth_info {
70 int32_t authtype; /* auth type */
71 u_int32_t authlen; /* auth length */
72 };
73
74 struct auth_unix {
75 int32_t ua_time;
76 int32_t ua_hostname; /* null */
77 int32_t ua_uid;
78 int32_t ua_gid;
79 int32_t ua_gidlist; /* null */
80 };
81
82 struct rpc_call {
83 u_int32_t rp_xid; /* request transaction id */
84 int32_t rp_direction; /* call direction (0) */
85 u_int32_t rp_rpcvers; /* rpc version (2) */
86 u_int32_t rp_prog; /* program */
87 u_int32_t rp_vers; /* version */
88 u_int32_t rp_proc; /* procedure */
89 };
90
91 struct rpc_reply {
92 u_int32_t rp_xid; /* request transaction id */
93 int32_t rp_direction; /* call direction (1) */
94 int32_t rp_astatus; /* accept status (0: accepted) */
95 union {
96 u_int32_t rpu_errno;
97 struct {
98 struct auth_info rok_auth;
99 u_int32_t rok_status;
100 } rpu_rok;
101 } rp_u;
102 };
103
104 /* Local forwards */
105 static size_t recvrpc __P((struct iodesc *, void *, size_t, time_t));
106
107 int rpc_xid;
108 int rpc_port = 0x400; /* predecrement */
109
110 /*
111 * Make a rpc call; return length of answer
112 * XXX - Caller must leave room for headers.
113 */
114 size_t
115 rpc_call(d, prog, vers, proc, sdata, slen, rdata, rlen)
116 register struct iodesc *d;
117 register n_long prog, vers, proc;
118 register void *sdata;
119 register size_t slen;
120 register void *rdata;
121 register size_t rlen;
122 {
123 register size_t cc;
124 struct auth_info *auth;
125 struct rpc_call *call;
126 struct rpc_reply *reply;
127 void *send_head, *send_tail;
128 void *recv_head, *recv_tail;
129 n_long x;
130
131 #ifdef RPC_DEBUG
132 if (debug)
133 printf("rpc_call: prog=0x%x vers=%d proc=%d\n",
134 prog, vers, proc);
135 #endif
136
137 d->destport = rpc_getport(d, prog, vers);
138
139 /*
140 * Prepend authorization stuff and headers.
141 * Note, must prepend things in reverse order.
142 */
143 send_head = sdata;
144 send_tail = sdata + slen;
145
146 /* Auth verifier is always auth_null */
147 send_head -= sizeof(*auth);
148 auth = send_head;
149 auth->authtype = htonl(RPCAUTH_NULL);
150 auth->authlen = 0;
151
152 #if 0 /* XXX */
153 /* Auth credentials: always auth unix (as root) */
154 send_head -= sizeof(struct auth_unix);
155 bzero(send_head, sizeof(struct auth_unix));
156 send_head -= sizeof(*auth);
157 auth = send_head;
158 auth->authtype = htonl(RPCAUTH_UNIX);
159 auth->authlen = htonl(sizeof(struct auth_unix));
160 #else
161 /* Auth credentials: always auth_null (XXX OK?) */
162 send_head -= sizeof(*auth);
163 auth = send_head;
164 auth->authtype = htonl(RPCAUTH_NULL);
165 auth->authlen = 0;
166 #endif
167
168 /* RPC call structure. */
169 send_head -= sizeof(*call);
170 call = send_head;
171 rpc_xid++;
172 call->rp_xid = htonl(rpc_xid);
173 call->rp_direction = htonl(RPC_CALL);
174 call->rp_rpcvers = htonl(RPC_VER2);
175 call->rp_prog = htonl(prog);
176 call->rp_vers = htonl(vers);
177 call->rp_proc = htonl(proc);
178
179 /* Make room for the rpc_reply header. */
180 recv_head = rdata;
181 recv_tail = rdata + rlen;
182 recv_head -= sizeof(*reply);
183
184 cc = sendrecv(d,
185 sendudp, send_head, (send_tail - send_head),
186 recvrpc, recv_head, (recv_tail - recv_head));
187 #ifdef RPC_DEBUG
188 if (debug)
189 printf("callrpc: cc=%d rlen=%d\n", cc, rlen);
190 #endif
191 if (cc <= sizeof(*reply))
192 return (-1);
193 recv_tail = recv_head + cc;
194
195 /*
196 * Check the RPC reply status.
197 * The xid, dir, astatus were already checked.
198 */
199 reply = recv_head;
200 auth = &reply->rp_u.rpu_rok.rok_auth;
201 x = ntohl(auth->authlen);
202 if (x != 0) {
203 #ifdef RPC_DEBUG
204 if (debug)
205 printf("callrpc: reply auth != NULL\n");
206 #endif
207 return(-1);
208 }
209 x = ntohl(reply->rp_u.rpu_rok.rok_status);
210 if (x != 0) {
211 printf("callrpc: error = %d\n", x);
212 return(-1);
213 }
214 recv_head += sizeof(*reply);
215
216 return (recv_tail - recv_head);
217 }
218
219 /*
220 * Returns true if packet is the one we're waiting for.
221 * This just checks the XID, direction, acceptance.
222 * Remaining checks are done by callrpc
223 */
224 static size_t
225 recvrpc(d, pkt, len, tleft)
226 register struct iodesc *d;
227 register void *pkt;
228 register size_t len;
229 time_t tleft;
230 {
231 register struct rpc_reply *reply;
232 int x;
233
234 errno = 0;
235 #ifdef RPC_DEBUG
236 if (debug)
237 printf("recvrpc: called len=%d\n", len);
238 #endif
239
240 len = readudp(d, pkt, len, tleft);
241 if (len <= (4 * 4))
242 goto bad;
243 reply = (struct rpc_reply *)pkt;
244
245 x = ntohl(reply->rp_xid);
246 if (x != rpc_xid) {
247 #ifdef RPC_DEBUG
248 if (debug)
249 printf("recvrpc: rp_xid %d != xid %d\n", x, rpc_xid);
250 #endif
251 goto bad;
252 }
253
254 x = ntohl(reply->rp_direction);
255 if (x != RPC_REPLY) {
256 #ifdef RPC_DEBUG
257 if (debug)
258 printf("recvrpc: rp_direction %d != REPLY\n", x);
259 #endif
260 goto bad;
261 }
262
263 x = ntohl(reply->rp_astatus);
264 if (x != RPC_MSGACCEPTED) {
265 errno = ntohl(reply->rp_u.rpu_errno);
266 printf("recvrpc: reject, astat=%d, errno=%d\n", x, errno);
267 goto bad;
268 }
269
270 /* Return data count (thus indicating success) */
271 return (len);
272
273 bad:
274 return (-1);
275 }
276
277 /*
278 * Given a pointer to a reply just received,
279 * dig out the IP address/port from the headers.
280 */
281 void
282 rpc_fromaddr(void *pkt, n_long *addr, u_short *port)
283 {
284 struct hackhdr {
285 /* Tail of IP header: just IP addresses */
286 n_long ip_src;
287 n_long ip_dst;
288 /* UDP header: */
289 u_int16_t uh_sport; /* source port */
290 u_int16_t uh_dport; /* destination port */
291 int16_t uh_ulen; /* udp length */
292 u_int16_t uh_sum; /* udp checksum */
293 /* RPC reply header: */
294 struct rpc_reply rpc;
295 } *hhdr;
296
297 hhdr = ((struct hackhdr *)pkt) - 1;
298 *addr = hhdr->ip_src;
299 *port = hhdr->uh_sport;
300 }
301
302 /*
303 * RPC Portmapper cache
304 */
305
306 #define PMAP_NUM 8 /* need at most 5 pmap entries */
307
308 int rpc_pmap_num;
309 struct pmap_list {
310 u_long addr; /* server, net order */
311 u_long prog; /* host order */
312 u_long vers; /* host order */
313 u_short port; /* net order */
314 u_short _pad;
315 } rpc_pmap_list[PMAP_NUM];
316
317 /* return port number in net order */
318 int
319 rpc_pmap_getcache(addr, prog, vers)
320 u_long addr; /* server, net order */
321 u_long prog; /* host order */
322 u_long vers; /* host order */
323 {
324 struct pmap_list *pl;
325
326 for (pl = rpc_pmap_list; pl < &rpc_pmap_list[rpc_pmap_num]; pl++)
327 if (pl->addr == addr && pl->prog == prog && pl->vers == vers)
328 return ((int) pl->port);
329 return (-1);
330 }
331
332 void
333 rpc_pmap_putcache(addr, prog, vers, port)
334 n_long addr; /* net order */
335 n_long prog; /* host order */
336 n_long vers; /* host order */
337 int port; /* net order */
338 {
339 struct pmap_list *pl;
340
341 /* Don't overflow cache... */
342 if (rpc_pmap_num >= PMAP_NUM) {
343 /* ... just re-use the last entry. */
344 rpc_pmap_num = PMAP_NUM - 1;
345 #ifdef RPC_DEBUG
346 printf("rpc_pmap_putcache: cache overflow\n");
347 #endif
348 }
349
350 pl = &rpc_pmap_list[rpc_pmap_num];
351 rpc_pmap_num++;
352
353 /* Cache answer */
354 pl->addr = addr;
355 pl->prog = prog;
356 pl->vers = vers;
357 pl->port = port;
358 }
359
360
361 /*
362 * Request a port number from the port mapper.
363 * Returns the port in network order.
364 */
365 int
366 rpc_getport(d, prog, vers)
367 register struct iodesc *d;
368 n_long prog; /* host order */
369 n_long vers; /* host order */
370 {
371 struct args {
372 u_long prog; /* call program */
373 u_long vers; /* call version */
374 u_long proto; /* call protocol */
375 u_long port; /* call port (unused) */
376 } *args;
377 struct res {
378 u_long port;
379 } *res;
380 struct {
381 n_long h[RPC_HEADER_WORDS];
382 struct args d;
383 } sdata;
384 struct {
385 n_long h[RPC_HEADER_WORDS];
386 struct res d;
387 n_long pad;
388 } rdata;
389 int cc, port;
390
391 #ifdef RPC_DEBUG
392 if (debug)
393 printf("getport: prog=0x%x vers=%d\n", prog, vers);
394 #endif
395
396 /* This one is fixed forever. */
397 if (prog == PMAPPROG)
398 return PMAPPORT;
399
400 /* Try for cached answer first */
401 port = rpc_pmap_getcache(d->destip, prog, vers);
402 if (port >= 0)
403 return (port);
404
405 args = &sdata.d;
406 args->prog = htonl(prog);
407 args->vers = htonl(vers);
408 args->proto = htonl(IPPROTO_UDP);
409 args->port = 0;
410 res = &rdata.d;
411
412 cc = rpc_call(d, PMAPPROG, PMAPVERS, PMAPPROC_GETPORT,
413 args, sizeof(*args), res, sizeof(*res));
414 if (cc < sizeof(*res)) {
415 printf("getport: %s", strerror(errno));
416 return(-1);
417 }
418 port = (u_short)res->port;
419
420 rpc_pmap_putcache(d->destip, prog, vers, port);
421
422 return (port);
423 }
424