krpc_subr.c revision 1.3 1 /* $NetBSD: krpc_subr.c,v 1.3 1994/06/29 06:42:02 cgd Exp $ */
2
3 /*
4 * Copyright (c) 1994 Gordon Ross, Adam Glass
5 * Copyright (c) 1992 Regents of the University of California.
6 * All rights reserved.
7 *
8 * This software was developed by the Computer Systems Engineering group
9 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
10 * contributed to Berkeley.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Lawrence Berkeley Laboratory and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 *
40 * partially based on:
41 * libnetboot/rpc.c
42 * @(#) Header: rpc.c,v 1.12 93/09/28 08:31:56 leres Exp (LBL)
43 */
44
45 #include <sys/param.h>
46 #include <sys/conf.h>
47 #include <sys/ioctl.h>
48 #include <sys/proc.h>
49 #include <sys/mount.h>
50 #include <sys/mbuf.h>
51 #include <sys/socket.h>
52 #include <sys/systm.h>
53 #include <sys/reboot.h>
54
55 #include <net/if.h>
56 #include <netinet/in.h>
57
58 #include <nfs/rpcv2.h>
59
60 /*
61 * Kernel support for Sun RPC
62 *
63 * Used currently for bootstrapping in nfs diskless configurations.
64 *
65 * Note: will not work on variable-sized rpc args/results.
66 * implicit size-limit of an mbuf.
67 */
68
69 #define PMAPPORT 111
70 #define PMAPPROG 100000
71 #define PMAPVERS 2
72 #define PMAPPROC_GETPORT 3
73
74 /*
75 * Generic RPC headers
76 */
77
78 struct auth_info {
79 int rp_atype; /* auth type */
80 u_long rp_alen; /* auth length */
81 };
82
83 struct rpc_call {
84 u_long rp_xid; /* request transaction id */
85 int rp_direction; /* call direction (0) */
86 u_long rp_rpcvers; /* rpc version (2) */
87 u_long rp_prog; /* program */
88 u_long rp_vers; /* version */
89 u_long rp_proc; /* procedure */
90 struct auth_info rp_auth;
91 struct auth_info rp_verf;
92 };
93
94 struct rpc_reply {
95 u_long rp_xid; /* request transaction id */
96 int rp_direction; /* call direction (1) */
97 int rp_astatus; /* accept status (0: accepted) */
98 union {
99 u_long rpu_errno;
100 struct {
101 struct auth_info rp_auth;
102 u_long rp_rstatus; /* reply status */
103 } rpu_ok;
104 } rp_u;
105 };
106
107 #define MIN_REPLY_HDR 16 /* xid, dir, astat, errno */
108
109 /*
110 * What is the longest we will wait before re-sending a request?
111 * Note this is also the frequency of "RPC timeout" messages.
112 * The re-send loop count sup linearly to this maximum, so the
113 * first complaint will happen after (1+2+3+4+5)=15 seconds.
114 */
115 #define MAX_RESEND_DELAY 5 /* seconds */
116
117 /*
118 * Call portmap to lookup a port number for a particular rpc program
119 * Returns non-zero error on failure.
120 */
121 int
122 krpc_portmap(sa, prog, vers, portp)
123 struct sockaddr *sa; /* server address */
124 u_long prog, vers; /* host order */
125 u_short *portp; /* network order */
126 {
127 struct sdata {
128 u_long prog; /* call program */
129 u_long vers; /* call version */
130 u_long proto; /* call protocol */
131 u_long port; /* call port (unused) */
132 } *sdata;
133 struct rdata {
134 u_short pad;
135 u_short port;
136 } *rdata;
137 struct mbuf *m;
138 int error;
139
140 /* The portmapper port is fixed. */
141 if (prog == PMAPPROG) {
142 *portp = htons(PMAPPORT);
143 return 0;
144 }
145
146 m = m_gethdr(M_WAIT, MT_DATA);
147 if (m == NULL)
148 return ENOBUFS;
149 m->m_len = sizeof(*sdata);
150 m->m_pkthdr.len = m->m_len;
151 sdata = mtod(m, struct sdata *);
152
153 /* Do the RPC to get it. */
154 sdata->prog = htonl(prog);
155 sdata->vers = htonl(vers);
156 sdata->proto = htonl(IPPROTO_UDP);
157 sdata->port = 0;
158
159 error = krpc_call(sa, PMAPPROG, PMAPVERS,
160 PMAPPROC_GETPORT, &m);
161 if (error)
162 return error;
163
164 rdata = mtod(m, struct rdata *);
165 *portp = rdata->port;
166
167 m_freem(m);
168 return 0;
169 }
170
171 /*
172 * Do a remote procedure call (RPC) and wait for its reply.
173 */
174 int
175 krpc_call(sa, prog, vers, func, data)
176 struct sockaddr *sa;
177 u_long prog, vers, func;
178 struct mbuf **data; /* input/output */
179 {
180 struct socket *so;
181 struct sockaddr_in *sin;
182 struct timeval *tv;
183 struct mbuf *m, *nam, *mhead;
184 struct rpc_call *call;
185 struct rpc_reply *reply;
186 struct uio auio;
187 int error, rcvflg, timo, secs, len;
188 static u_long xid = ~0xFF;
189
190 /*
191 * Validate address family.
192 * Sorry, this is INET specific...
193 */
194 if (sa->sa_family != AF_INET)
195 return (EAFNOSUPPORT);
196
197 /* Free at end if not null. */
198 nam = mhead = NULL;
199
200 /*
201 * Create socket and set its recieve timeout.
202 */
203 if ((error = socreate(AF_INET, &so, SOCK_DGRAM, 0)))
204 goto out;
205
206 m = m_get(M_WAIT, MT_SOOPTS);
207 if (m == NULL) {
208 error = ENOBUFS;
209 goto out;
210 }
211 tv = mtod(m, struct timeval *);
212 m->m_len = sizeof(*tv);
213 tv->tv_sec = 1;
214 tv->tv_usec = 0;
215 if ((error = sosetopt(so, SOL_SOCKET, SO_RCVTIMEO, m)))
216 goto out;
217
218 /*
219 * Setup socket address for the server.
220 */
221 nam = m_get(M_WAIT, MT_SONAME);
222 if (nam == NULL) {
223 error = ENOBUFS;
224 goto out;
225 }
226 sin = mtod(nam, struct sockaddr_in *);
227 bcopy((caddr_t)sa, (caddr_t)sin, (nam->m_len = sa->sa_len));
228
229 /*
230 * Set the port number that the request will use.
231 */
232 if ((error = krpc_portmap(sa, prog, vers, &sin->sin_port)))
233 goto out;
234
235 /*
236 * Prepend RPC message header.
237 */
238 m = *data;
239 *data = NULL;
240 #ifdef DIAGNOSTIC
241 if ((m->m_flags & M_PKTHDR) == 0)
242 panic("krpc_call: send data w/o pkthdr");
243 if (m->m_pkthdr.len < m->m_len)
244 panic("krpc_call: pkthdr.len not set");
245 #endif
246 mhead = m_prepend(m, sizeof(*call), M_WAIT);
247 if (mhead == NULL) {
248 error = ENOBUFS;
249 goto out;
250 }
251 mhead->m_pkthdr.len += sizeof(*call);
252 mhead->m_pkthdr.rcvif = NULL;
253
254 /*
255 * Fill in the RPC header
256 */
257 call = mtod(mhead, struct rpc_call *);
258 bzero((caddr_t)call, sizeof(*call));
259 call->rp_xid = ++xid; /* no need to put in network order */
260 /* call->rp_direction = 0; */
261 call->rp_rpcvers = htonl(2);
262 call->rp_prog = htonl(prog);
263 call->rp_vers = htonl(vers);
264 call->rp_proc = htonl(func);
265 /* call->rp_auth = 0; */
266 /* call->rp_verf = 0; */
267
268 /*
269 * Send it, repeatedly, until a reply is received,
270 * but delay each re-send by an increasing amount.
271 * If the delay hits the maximum, start complaining.
272 */
273 timo = 0;
274 for (;;) {
275 /* Send RPC request (or re-send). */
276 m = m_copym(mhead, 0, M_COPYALL, M_WAIT);
277 if (m == NULL) {
278 error = ENOBUFS;
279 goto out;
280 }
281 error = sosend(so, nam, NULL, m, NULL, 0);
282 if (error) {
283 printf("krpc_call: sosend: %d\n", error);
284 goto out;
285 }
286 m = NULL;
287
288 /* Determine new timeout. */
289 if (timo < MAX_RESEND_DELAY)
290 timo++;
291 else
292 printf("RPC timeout for server 0x%x\n",
293 ntohl(sin->sin_addr.s_addr));
294
295 /*
296 * Wait for up to timo seconds for a reply.
297 * The socket receive timeout was set to 1 second.
298 */
299 secs = timo;
300 while (secs > 0) {
301 auio.uio_resid = len = 1<<16;
302 rcvflg = 0;
303 error = soreceive(so, NULL, &auio, &m, NULL, &rcvflg);
304 if (error == EWOULDBLOCK) {
305 secs--;
306 continue;
307 }
308 if (error)
309 goto out;
310 len -= auio.uio_resid;
311
312 /* Is the reply complete and the right one? */
313 if (len < MIN_REPLY_HDR) {
314 m_freem(m);
315 continue;
316 }
317 if (m->m_len < MIN_REPLY_HDR) {
318 m = m_pullup(m, MIN_REPLY_HDR);
319 if (!m)
320 continue;
321 }
322 reply = mtod(m, struct rpc_reply *);
323 if ((reply->rp_direction == htonl(RPC_REPLY)) &&
324 (reply->rp_xid == xid))
325 goto gotreply; /* break two levels */
326 } /* while secs */
327 } /* forever send/receive */
328 gotreply:
329
330 /*
331 * Make result buffer contiguous.
332 */
333 #ifdef DIAGNOSTIC
334 if ((m->m_flags & M_PKTHDR) == 0)
335 panic("krpc_call: received pkt w/o header?");
336 #endif
337 len = m->m_pkthdr.len;
338 if (m->m_len < len) {
339 m = m_pullup(m, len);
340 if (m == NULL) {
341 error = ENOBUFS;
342 goto out;
343 }
344 }
345 reply = mtod(m, struct rpc_reply *);
346
347 /*
348 * Check RPC acceptance and status.
349 */
350 if (reply->rp_astatus != 0) {
351 error = reply->rp_u.rpu_errno;
352 printf("rpc denied, error=%d\n", error);
353 m_freem(m);
354 goto out;
355 }
356 if ((error = reply->rp_u.rpu_ok.rp_rstatus) != 0) {
357 printf("rpc status=%d\n", error);
358 m_freem(m);
359 goto out;
360 }
361
362 /*
363 * Strip RPC header
364 */
365 len = sizeof(*reply);
366 if (reply->rp_u.rpu_ok.rp_auth.rp_atype != 0) {
367 len += ntohl(reply->rp_u.rpu_ok.rp_auth.rp_alen);
368 len = (len + 3) & ~3; /* XXX? */
369 }
370 m_adj(m, len);
371
372 /* result */
373 *data = m;
374
375 out:
376 if (nam) m_freem(nam);
377 if (mhead) m_freem(mhead);
378 soclose(so);
379 return error;
380 }
381