1 /* $NetBSD: krpc_subr.c,v 1.12.4.1 1996/06/07 00:52:26 cgd Exp $ */
2
3 /*
4 * Copyright (c) 1995 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/cdefs.h>
46 __FBSDID("$FreeBSD: releng/5.1/sys/nfsclient/krpc_subr.c 111119 2003-02-19 05:47:46Z imp $");
47
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/malloc.h>
51 #include <sys/mbuf.h>
52 #include <sys/proc.h>
53 #include <sys/socket.h>
54 #include <sys/socketvar.h>
55 #include <sys/uio.h>
56
57 #include <net/if.h>
58 #include <netinet/in.h>
59
60 #include <nfs/rpcv2.h>
61 #include <nfsclient/krpc.h>
62 #include <nfs/xdr_subs.h>
63
64 /*
65 * Kernel support for Sun RPC
66 *
67 * Used currently for bootstrapping in nfs diskless configurations.
68 */
69
70 /*
71 * Generic RPC headers
72 */
73
74 struct auth_info {
75 u_int32_t authtype; /* auth type */
76 u_int32_t authlen; /* auth length */
77 };
78
79 struct auth_unix {
80 int32_t ua_time;
81 int32_t ua_hostname; /* null */
82 int32_t ua_uid;
83 int32_t ua_gid;
84 int32_t ua_gidlist; /* null */
85 };
86
87 struct rpc_call {
88 u_int32_t rp_xid; /* request transaction id */
89 int32_t rp_direction; /* call direction (0) */
90 u_int32_t rp_rpcvers; /* rpc version (2) */
91 u_int32_t rp_prog; /* program */
92 u_int32_t rp_vers; /* version */
93 u_int32_t rp_proc; /* procedure */
94 struct auth_info rpc_auth;
95 struct auth_unix rpc_unix;
96 struct auth_info rpc_verf;
97 };
98
99 struct rpc_reply {
100 u_int32_t rp_xid; /* request transaction id */
101 int32_t rp_direction; /* call direction (1) */
102 int32_t rp_astatus; /* accept status (0: accepted) */
103 union {
104 u_int32_t rpu_errno;
105 struct {
106 struct auth_info rok_auth;
107 u_int32_t rok_status;
108 } rpu_rok;
109 } rp_u;
110 };
111 #define rp_errno rp_u.rpu_errno
112 #define rp_auth rp_u.rpu_rok.rok_auth
113 #define rp_status rp_u.rpu_rok.rok_status
114
115 #define MIN_REPLY_HDR 16 /* xid, dir, astat, errno */
116
117 /*
118 * What is the longest we will wait before re-sending a request?
119 * Note this is also the frequency of "RPC timeout" messages.
120 * The re-send loop count sup linearly to this maximum, so the
121 * first complaint will happen after (1+2+3+4+5)=15 seconds.
122 */
123 #define MAX_RESEND_DELAY 5 /* seconds */
124
125 /*
126 * Call portmap to lookup a port number for a particular rpc program
127 * Returns non-zero error on failure.
128 */
129 int
130 krpc_portmap(struct sockaddr_in *sin, u_int prog, u_int vers, u_int16_t *portp,
131 struct thread *td)
132 {
133 struct sdata {
134 u_int32_t prog; /* call program */
135 u_int32_t vers; /* call version */
136 u_int32_t proto; /* call protocol */
137 u_int32_t port; /* call port (unused) */
138 } *sdata;
139 struct rdata {
140 u_int16_t pad;
141 u_int16_t port;
142 } *rdata;
143 struct mbuf *m;
144 int error;
145
146 /* The portmapper port is fixed. */
147 if (prog == PMAPPROG) {
148 *portp = htons(PMAPPORT);
149 return 0;
150 }
151
152 m = m_get(M_TRYWAIT, MT_DATA);
153 if (m == NULL)
154 return ENOBUFS;
155 sdata = mtod(m, struct sdata *);
156 m->m_len = sizeof(*sdata);
157
158 /* Do the RPC to get it. */
159 sdata->prog = txdr_unsigned(prog);
160 sdata->vers = txdr_unsigned(vers);
161 sdata->proto = txdr_unsigned(IPPROTO_UDP);
162 sdata->port = 0;
163
164 sin->sin_port = htons(PMAPPORT);
165 error = krpc_call(sin, PMAPPROG, PMAPVERS,
166 PMAPPROC_GETPORT, &m, NULL, td);
167 if (error)
168 return error;
169
170 if (m->m_len < sizeof(*rdata)) {
171 m = m_pullup(m, sizeof(*rdata));
172 if (m == NULL)
173 return ENOBUFS;
174 }
175 rdata = mtod(m, struct rdata *);
176 *portp = rdata->port;
177
178 m_freem(m);
179 return 0;
180 }
181
182 /*
183 * Do a remote procedure call (RPC) and wait for its reply.
184 * If from_p is non-null, then we are doing broadcast, and
185 * the address from whence the response came is saved there.
186 */
187 int
188 krpc_call(struct sockaddr_in *sa, u_int prog, u_int vers, u_int func,
189 struct mbuf **data, struct sockaddr **from_p, struct thread *td)
190 {
191 struct socket *so;
192 struct sockaddr_in *sin, ssin;
193 struct sockaddr *from;
194 struct mbuf *m, *nam, *mhead;
195 struct rpc_call *call;
196 struct rpc_reply *reply;
197 struct sockopt sopt;
198 struct timeval tv;
199 struct uio auio;
200 int error, rcvflg, timo, secs, len;
201 static u_int32_t xid = ~0xFF;
202 u_int16_t tport;
203 u_int32_t saddr;
204
205 /*
206 * Validate address family.
207 * Sorry, this is INET specific...
208 */
209 if (sa->sin_family != AF_INET)
210 return (EAFNOSUPPORT);
211
212 /* Free at end if not null. */
213 nam = mhead = NULL;
214 from = NULL;
215
216 /*
217 * Create socket and set its recieve timeout.
218 */
219 if ((error = socreate(AF_INET, &so, SOCK_DGRAM, 0, td->td_ucred, td)))
220 goto out;
221
222 tv.tv_sec = 1;
223 tv.tv_usec = 0;
224 bzero(&sopt, sizeof sopt);
225 sopt.sopt_level = SOL_SOCKET;
226 sopt.sopt_name = SO_RCVTIMEO;
227 sopt.sopt_val = &tv;
228 sopt.sopt_valsize = sizeof tv;
229
230 if ((error = sosetopt(so, &sopt)) != 0)
231 goto out;
232
233 /*
234 * Enable broadcast if necessary.
235 */
236 if (from_p) {
237 int on = 1;
238 sopt.sopt_name = SO_BROADCAST;
239 sopt.sopt_val = &on;
240 sopt.sopt_valsize = sizeof on;
241 if ((error = sosetopt(so, &sopt)) != 0)
242 goto out;
243 }
244
245 /*
246 * Bind the local endpoint to a reserved port,
247 * because some NFS servers refuse requests from
248 * non-reserved (non-privileged) ports.
249 */
250 sin = &ssin;
251 bzero(sin, sizeof *sin);
252 sin->sin_len = sizeof(*sin);
253 sin->sin_family = AF_INET;
254 sin->sin_addr.s_addr = INADDR_ANY;
255 tport = IPPORT_RESERVED;
256 do {
257 tport--;
258 sin->sin_port = htons(tport);
259 error = sobind(so, (struct sockaddr *)sin, td);
260 } while (error == EADDRINUSE &&
261 tport > IPPORT_RESERVED / 2);
262 if (error) {
263 printf("bind failed\n");
264 goto out;
265 }
266
267 /*
268 * Setup socket address for the server.
269 */
270
271 /*
272 * Prepend RPC message header.
273 */
274 mhead = m_gethdr(M_TRYWAIT, MT_DATA);
275 mhead->m_next = *data;
276 call = mtod(mhead, struct rpc_call *);
277 mhead->m_len = sizeof(*call);
278 bzero((caddr_t)call, sizeof(*call));
279 /* rpc_call part */
280 xid++;
281 call->rp_xid = txdr_unsigned(xid);
282 /* call->rp_direction = 0; */
283 call->rp_rpcvers = txdr_unsigned(2);
284 call->rp_prog = txdr_unsigned(prog);
285 call->rp_vers = txdr_unsigned(vers);
286 call->rp_proc = txdr_unsigned(func);
287 /* rpc_auth part (auth_unix as root) */
288 call->rpc_auth.authtype = txdr_unsigned(RPCAUTH_UNIX);
289 call->rpc_auth.authlen = txdr_unsigned(sizeof(struct auth_unix));
290 /* rpc_verf part (auth_null) */
291 call->rpc_verf.authtype = 0;
292 call->rpc_verf.authlen = 0;
293
294 /*
295 * Setup packet header
296 */
297 len = 0;
298 m = mhead;
299 while (m) {
300 len += m->m_len;
301 m = m->m_next;
302 }
303 mhead->m_pkthdr.len = len;
304 mhead->m_pkthdr.rcvif = NULL;
305
306 /*
307 * Send it, repeatedly, until a reply is received,
308 * but delay each re-send by an increasing amount.
309 * If the delay hits the maximum, start complaining.
310 */
311 timo = 0;
312 for (;;) {
313 /* Send RPC request (or re-send). */
314 m = m_copym(mhead, 0, M_COPYALL, M_TRYWAIT);
315 if (m == NULL) {
316 error = ENOBUFS;
317 goto out;
318 }
319 error = sosend(so, (struct sockaddr *)sa, NULL, m,
320 NULL, 0, td);
321 if (error) {
322 printf("krpc_call: sosend: %d\n", error);
323 goto out;
324 }
325 m = NULL;
326
327 /* Determine new timeout. */
328 if (timo < MAX_RESEND_DELAY)
329 timo++;
330 else {
331 saddr = ntohl(sa->sin_addr.s_addr);
332 printf("RPC timeout for server %d.%d.%d.%d\n",
333 (saddr >> 24) & 255,
334 (saddr >> 16) & 255,
335 (saddr >> 8) & 255,
336 saddr & 255);
337 }
338
339 /*
340 * Wait for up to timo seconds for a reply.
341 * The socket receive timeout was set to 1 second.
342 */
343 secs = timo;
344 while (secs > 0) {
345 if (from) {
346 FREE(from, M_SONAME);
347 from = NULL;
348 }
349 if (m) {
350 m_freem(m);
351 m = NULL;
352 }
353 bzero(&auio, sizeof(auio));
354 auio.uio_resid = len = 1<<16;
355 rcvflg = 0;
356 error = soreceive(so, &from, &auio, &m, NULL, &rcvflg);
357 if (error == EWOULDBLOCK) {
358 secs--;
359 continue;
360 }
361 if (error)
362 goto out;
363 len -= auio.uio_resid;
364
365 /* Does the reply contain at least a header? */
366 if (len < MIN_REPLY_HDR)
367 continue;
368 if (m->m_len < MIN_REPLY_HDR)
369 continue;
370 reply = mtod(m, struct rpc_reply *);
371
372 /* Is it the right reply? */
373 if (reply->rp_direction != txdr_unsigned(RPC_REPLY))
374 continue;
375
376 if (reply->rp_xid != txdr_unsigned(xid))
377 continue;
378
379 /* Was RPC accepted? (authorization OK) */
380 if (reply->rp_astatus != 0) {
381 error = fxdr_unsigned(u_int32_t, reply->rp_errno);
382 printf("rpc denied, error=%d\n", error);
383 continue;
384 }
385
386 /* Did the call succeed? */
387 if (reply->rp_status != 0) {
388 error = fxdr_unsigned(u_int32_t, reply->rp_status);
389 if (error == RPC_PROGMISMATCH) {
390 error = EBADRPC;
391 goto out;
392 }
393 printf("rpc denied, status=%d\n", error);
394 continue;
395 }
396
397 goto gotreply; /* break two levels */
398
399 } /* while secs */
400 } /* forever send/receive */
401
402 error = ETIMEDOUT;
403 goto out;
404
405 gotreply:
406
407 /*
408 * Get RPC reply header into first mbuf,
409 * get its length, then strip it off.
410 */
411 len = sizeof(*reply);
412 if (m->m_len < len) {
413 m = m_pullup(m, len);
414 if (m == NULL) {
415 error = ENOBUFS;
416 goto out;
417 }
418 }
419 reply = mtod(m, struct rpc_reply *);
420 if (reply->rp_auth.authtype != 0) {
421 len += fxdr_unsigned(u_int32_t, reply->rp_auth.authlen);
422 len = (len + 3) & ~3; /* XXX? */
423 }
424 m_adj(m, len);
425
426 /* result */
427 *data = m;
428 if (from_p) {
429 *from_p = from;
430 from = NULL;
431 }
432
433 out:
434 if (mhead) m_freem(mhead);
435 if (from) free(from, M_SONAME);
436 soclose(so);
437 return error;
438 }
439
440 /*
441 * eXternal Data Representation routines.
442 * (but with non-standard args...)
443 */
444
445 /*
446 * String representation for RPC.
447 */
448 struct xdr_string {
449 u_int32_t len; /* length without null or padding */
450 char data[4]; /* data (longer, of course) */
451 /* data is padded to a long-word boundary */
452 };
453
454 struct mbuf *
455 xdr_string_encode(char *str, int len)
456 {
457 struct mbuf *m;
458 struct xdr_string *xs;
459 int dlen; /* padded string length */
460 int mlen; /* message length */
461
462 dlen = (len + 3) & ~3;
463 mlen = dlen + 4;
464
465 if (mlen > MCLBYTES) /* If too big, we just can't do it. */
466 return (NULL);
467
468 m = m_get(M_TRYWAIT, MT_DATA);
469 if (mlen > MLEN) {
470 MCLGET(m, M_TRYWAIT);
471 if ((m->m_flags & M_EXT) == 0) {
472 (void) m_free(m); /* There can be only one. */
473 return (NULL);
474 }
475 }
476 xs = mtod(m, struct xdr_string *);
477 m->m_len = mlen;
478 xs->len = txdr_unsigned(len);
479 bcopy(str, xs->data, len);
480 return (m);
481 }
Cache object: bf9efd9a1a31fc5ffce30e7e37f0f33c
|