rpc.c revision 1.3 1 /* $NetBSD: rpc.c,v 1.3 1995/02/19 23:52:18 mycroft 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 #include <sys/param.h>
43 #include <sys/socket.h>
44
45 #include <netinet/in.h>
46 #include <netinet/in_systm.h>
47
48 #include <nfs/rpcv2.h>
49 #include <nfs/nfsv2.h>
50 #undef NFSX_FATTR
51 #include <string.h>
52
53 #include "stand.h"
54 #include "net.h"
55 #include "netif.h"
56 #include "rpc.h"
57
58 /* XXX Data part of nfs rpc reply (also the largest thing we receive) */
59 struct nfs_reply_data {
60 u_long errno;
61 #ifndef NFSX_FATTR
62 struct nfsv2_fattr fa;
63 #else
64 u_char fa[NFSX_FATTR(0)];
65 #endif
66 u_long count;
67 u_char data[1200];
68 };
69 #define NFSREAD_SIZE sizeof(((struct nfs_reply_data *)0)->data)
70
71 /* Cache stuff */
72 #define PMAP_NUM 8 /* need at most 5 pmap entries */
73
74 static struct pmap_list {
75 u_long addr; /* address of server */
76 u_long prog;
77 u_long vers;
78 u_short port; /* cached port for service */
79 } pmap_list[PMAP_NUM] = {
80 { 0, PMAPPROG, PMAPVERS, PMAPPORT }
81 };
82 static int pmap_num = 1;
83
84 /* Local forwards */
85 static int recvrpc __P((struct iodesc *, void *, int));
86
87 /* Make a rpc call; return length of answer */
88 int
89 callrpc(d, prog, vers, proc, sdata, slen, rdata, rlen)
90 register struct iodesc *d;
91 register u_long prog, vers, proc;
92 register void *sdata;
93 register int slen;
94 register void *rdata;
95 register int rlen;
96 {
97 register int cc;
98 register struct rpc_call *rpc;
99 struct {
100 u_char header[HEADER_SIZE];
101 struct rpc_call wrpc;
102 u_char data[sizeof(struct nfs_reply_data)]; /* XXX */
103 } wbuf;
104 struct {
105 u_char header[HEADER_SIZE];
106 struct rpc_reply rrpc;
107 union {
108 u_long errno;
109 u_char data[sizeof(struct nfs_reply_data)];
110 } ru;
111 } rbuf;
112
113 #ifdef RPC_DEBUG
114 if (debug)
115 printf("callrpc: called\n");
116 #endif
117 if (rlen > sizeof(rbuf.ru.data))
118 panic("callrpc: huge read (%d > %d)",
119 rlen, sizeof(rbuf.ru.data));
120
121 d->destport = getport(d, prog, vers);
122
123 rpc = &wbuf.wrpc;
124
125 bzero(rpc, sizeof(*rpc));
126
127 rpc->rp_xid = d->xid;
128 rpc->rp_rpcvers = htonl(RPC_MSG_VERSION);
129 rpc->rp_prog = htonl(prog);
130 rpc->rp_vers = htonl(vers);
131 rpc->rp_proc = htonl(proc);
132 bcopy(sdata, wbuf.data, slen);
133
134 cc = sendrecv(d, sendudp, rpc, sizeof(*rpc) + slen, recvrpc,
135 ((u_char *)&rbuf.rrpc) - HEADER_SIZE, sizeof(rbuf) - HEADER_SIZE);
136 #ifdef RPC_DEBUG
137 if (debug)
138 printf("callrpc: cc=%d rlen=%d, rp_stat=%d\n", cc, rlen,
139 rbuf.rrpc.rp_stat);
140 #endif
141
142 /* Bump xid so next request will be unique */
143 ++d->xid;
144
145 if (cc < rlen) {
146 /* Check for an error return */
147 if (cc >= sizeof(rbuf.ru.errno) && rbuf.ru.errno != 0) {
148 errno = ntohl(rbuf.ru.errno);
149 return (-1);
150 }
151 panic("callrpc: missing data (%d < %d)", cc, rlen);
152 }
153 if (cc > sizeof(rbuf.ru.data))
154 panic("callrpc: huge return (%d > %d)",
155 cc, sizeof(rbuf.ru.data));
156 bcopy(rbuf.ru.data, rdata, cc);
157 return (cc);
158 }
159
160 /* Returns true if packet is the one we're waiting for */
161 static int
162 recvrpc(d, pkt, len)
163 register struct iodesc *d;
164 register void *pkt;
165 int len;
166 {
167 register struct rpc_reply *rpc;
168
169 errno = 0;
170 #ifdef RPC_DEBUG
171 if (debug)
172 printf("recvrpc: called len=%d\n", len);
173 #endif
174 rpc = (struct rpc_reply *)checkudp(d, pkt, &len);
175 if (rpc == NULL || len < sizeof(*rpc)) {
176 #ifdef RPC_DEBUG
177 if (debug)
178 printf("recvrpc: bad response rpc=%x len=%d\n",
179 (u_int)rpc, len);
180 #endif
181 return (-1);
182 }
183 #ifdef RPC_DEBUG
184 if (debug)
185 printf("recvrpc: got response len=%d\n", len);
186 #endif
187
188 NTOHL(rpc->rp_direction);
189 NTOHL(rpc->rp_stat);
190
191 if (rpc->rp_xid != d->xid || rpc->rp_direction != REPLY ||
192 rpc->rp_stat != MSG_ACCEPTED) {
193 #ifdef RPC_DEBUG
194 if (debug) {
195 if (rpc->rp_xid != d->xid)
196 printf("recvrpc: rp_xid %d != xid %d\n",
197 rpc->rp_xid, d->xid);
198 if (rpc->rp_direction != REPLY)
199 printf("recvrpc: %d != REPLY\n", rpc->rp_direction);
200 if (rpc->rp_stat != MSG_ACCEPTED)
201 printf("recvrpc: %d != MSG_ACCEPTED\n", rpc->rp_stat);
202 }
203 #endif
204 return (-1);
205 }
206
207 /* Return data count (thus indicating success) */
208 return (len - sizeof(*rpc));
209 }
210
211 /* Request a port number from the port mapper */
212 u_short
213 getport(d, prog, vers)
214 register struct iodesc *d;
215 u_long prog;
216 u_long vers;
217 {
218 register int i;
219 register struct pmap_list *pl;
220 u_long port;
221 struct {
222 u_long prog; /* call program */
223 u_long vers; /* call version */
224 u_long proto; /* call protocol */
225 u_long port; /* call port (unused) */
226 } sdata;
227
228 #ifdef RPC_DEBUG
229 if (debug)
230 printf("getport: called\n");
231 #endif
232 /* Try for cached answer first */
233 for (i = 0, pl = pmap_list; i < pmap_num; ++i, ++pl)
234 if ((pl->addr == d->destip || pl->addr == 0) &&
235 pl->prog == prog && pl->vers == vers)
236 return (pl->port);
237
238 /* Don't overflow cache */
239 if (pmap_num > PMAP_NUM - 1)
240 panic("getport: overflowed pmap_list!");
241
242 sdata.prog = htonl(prog);
243 sdata.vers = htonl(vers);
244 sdata.proto = htonl(IPPROTO_UDP);
245 sdata.port = 0;
246
247 if (callrpc(d, PMAPPROG, PMAPVERS, PMAPPROC_GETPORT,
248 &sdata, sizeof(sdata), &port, sizeof(port)) < 0) {
249 printf("getport: %s", strerror(errno));
250 return(-1);
251 }
252
253 /* Cache answer */
254 pl->addr = d->destip;
255 pl->prog = prog;
256 pl->vers = vers;
257 pl->port = port;
258 ++pmap_num;
259
260 return ((u_short)port);
261 }
262