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