rpc.c revision 1.9 1 /* $NetBSD: rpc.c,v 1.9 1995/09/18 21:19:42 pk 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 p;
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 p = rpc_getport(d, prog, vers);
138 if (p == htonl(-1))
139 return (-1);
140
141 d->destport = htons((short)ntohl(p));
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 n_short port; /* net order */
328 u_short _pad;
329 } rpc_pmap_list[PMAP_NUM];
330
331 /* return port number in net order */
332 int
333 rpc_pmap_getcache(addr, prog, vers)
334 struct in_addr addr; /* server, net order */
335 u_long prog; /* host order */
336 u_long vers; /* host order */
337 {
338 struct pmap_list *pl;
339
340 for (pl = rpc_pmap_list; pl < &rpc_pmap_list[rpc_pmap_num]; pl++)
341 if (pl->addr.s_addr == addr.s_addr && pl->prog == prog &&
342 pl->vers == vers)
343 return ((int) pl->port);
344 return (htonl(-1));
345 }
346
347 void
348 rpc_pmap_putcache(addr, prog, vers, port)
349 struct in_addr addr; /* server, net order */
350 u_long prog; /* host order */
351 u_long vers; /* host order */
352 n_long port; /* net order */
353 {
354 struct pmap_list *pl;
355
356 /* Don't overflow cache... */
357 if (rpc_pmap_num >= PMAP_NUM) {
358 /* ... just re-use the last entry. */
359 rpc_pmap_num = PMAP_NUM - 1;
360 #ifdef RPC_DEBUG
361 printf("rpc_pmap_putcache: cache overflow\n");
362 #endif
363 }
364
365 pl = &rpc_pmap_list[rpc_pmap_num];
366 rpc_pmap_num++;
367
368 /* Cache answer */
369 pl->addr = addr;
370 pl->prog = prog;
371 pl->vers = vers;
372 pl->port = port;
373 }
374
375
376 /*
377 * Request a port number from the port mapper.
378 * Returns the port in network order.
379 */
380 int
381 rpc_getport(d, prog, vers)
382 register struct iodesc *d;
383 n_long prog; /* host order */
384 n_long vers; /* host order */
385 {
386 struct args {
387 n_long prog; /* call program */
388 n_long vers; /* call version */
389 n_long proto; /* call protocol */
390 n_long port; /* call port (unused) */
391 } *args;
392 struct res {
393 n_long port;
394 } *res;
395 struct {
396 n_long h[RPC_HEADER_WORDS];
397 struct args d;
398 } sdata;
399 struct {
400 n_long h[RPC_HEADER_WORDS];
401 struct res d;
402 n_long pad;
403 } rdata;
404 ssize_t cc;
405 n_long port;
406
407 #ifdef RPC_DEBUG
408 if (debug)
409 printf("getport: prog=0x%x vers=%d\n", prog, vers);
410 #endif
411
412 /* This one is fixed forever. */
413 if (prog == PMAPPROG)
414 return (htons(PMAPPORT));
415
416 /* Try for cached answer first */
417 port = rpc_pmap_getcache(d->destip, prog, vers);
418 if (port != htonl(-1))
419 return (port);
420
421 args = &sdata.d;
422 args->prog = htonl(prog);
423 args->vers = htonl(vers);
424 args->proto = htonl(IPPROTO_UDP);
425 args->port = 0;
426 res = &rdata.d;
427
428 cc = rpc_call(d, PMAPPROG, PMAPVERS, PMAPPROC_GETPORT,
429 args, sizeof(*args), res, sizeof(*res));
430 if (cc < sizeof(*res)) {
431 printf("getport: %s", strerror(errno));
432 errno = EBADRPC;
433 return (htonl(-1));
434 }
435 port = res->port;
436
437 rpc_pmap_putcache(d->destip, prog, vers, port);
438
439 return (port);
440 }
441