]> git.saurik.com Git - apple/xnu.git/blame - bsd/nfs/krpc_subr.c
xnu-792.10.96.tar.gz
[apple/xnu.git] / bsd / nfs / krpc_subr.c
CommitLineData
1c79356b 1/*
e5568f75 2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
1c79356b
A
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
37839358
A
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
1c79356b 11 *
37839358
A
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
1c79356b
A
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
37839358
A
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
1c79356b
A
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22/* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
23/*
24 * Copyright (c) 1994 Gordon Ross, Adam Glass
25 * Copyright (c) 1992 Regents of the University of California.
26 * All rights reserved.
27 *
28 * This software was developed by the Computer Systems Engineering group
29 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
30 * contributed to Berkeley.
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Lawrence Berkeley Laboratory and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 *
60 */
61
62#include <sys/param.h>
63#include <sys/conf.h>
64#include <sys/ioctl.h>
65#include <sys/proc.h>
66#include <sys/mount.h>
91447636 67#include <sys/kpi_mbuf.h>
1c79356b
A
68#include <sys/malloc.h>
69#include <sys/socket.h>
70#include <sys/socketvar.h>
71#include <sys/systm.h>
72#include <sys/reboot.h>
91447636 73#include <sys/uio_internal.h>
1c79356b
A
74
75#include <net/if.h>
76#include <netinet/in.h>
77
78#include <nfs/rpcv2.h>
79#include <nfs/krpc.h>
80
81/*
82 * Kernel support for Sun RPC
83 *
84 * Used currently for bootstrapping in nfs diskless configurations.
85 *
86 * Note: will not work on variable-sized rpc args/results.
87 * implicit size-limit of an mbuf.
88 */
89
90/*
91 * Generic RPC headers
92 */
93
94struct auth_info {
95 u_int32_t rp_atype; /* auth type */
96 u_int32_t rp_alen; /* auth length */
97};
98
99struct rpc_call {
100 u_int32_t rp_xid; /* request transaction id */
101 int32_t rp_direction; /* call direction (0) */
102 u_int32_t rp_rpcvers; /* rpc version (2) */
103 u_int32_t rp_prog; /* program */
104 u_int32_t rp_vers; /* version */
105 u_int32_t rp_proc; /* procedure */
106 struct auth_info rp_auth;
107 struct auth_info rp_verf;
108};
109
110struct rpc_reply {
111 u_int32_t rp_xid; /* request transaction id */
112 int32_t rp_direction; /* call direction (1) */
113 int32_t rp_astatus; /* accept status (0: accepted) */
114 union {
115 u_int32_t rpu_errno;
116 struct {
117 struct auth_info rp_auth;
118 u_int32_t rp_rstatus;
119 } rpu_ok;
120 } rp_u;
121};
122
123#define MIN_REPLY_HDR 16 /* xid, dir, astat, errno */
124
125/*
126 * What is the longest we will wait before re-sending a request?
127 * Note this is also the frequency of "RPC timeout" messages.
128 * The re-send loop count sup linearly to this maximum, so the
129 * first complaint will happen after (1+2+3+4+5)=15 seconds.
130 */
131#define MAX_RESEND_DELAY 5 /* seconds */
132
133/* copied over from nfs_boot.c for printf format. could put in .h file... */
134#define IP_FORMAT "%d.%d.%d.%d"
135#define IP_CH(ip) ((u_char *)ip)
136#define IP_LIST(ip) IP_CH(ip)[0],IP_CH(ip)[1],IP_CH(ip)[2],IP_CH(ip)[3]
137
138
139/*
140 * Call portmap to lookup a port number for a particular rpc program
141 * Returns non-zero error on failure.
142 */
143int
91447636
A
144krpc_portmap(sin, prog, vers, proto, portp)
145 struct sockaddr_in *sin; /* server address */
146 u_int prog, vers, proto; /* host order */
147 u_int16_t *portp; /* network order */
1c79356b
A
148{
149 struct sdata {
150 u_int32_t prog; /* call program */
151 u_int32_t vers; /* call version */
152 u_int32_t proto; /* call protocol */
153 u_int32_t port; /* call port (unused) */
154 } *sdata;
155 struct rdata {
156 u_int16_t pad;
157 u_int16_t port;
158 } *rdata;
91447636 159 mbuf_t m;
1c79356b
A
160 int error;
161
162 /* The portmapper port is fixed. */
163 if (prog == PMAPPROG) {
164 *portp = htons(PMAPPORT);
165 return 0;
166 }
167
91447636
A
168 error = mbuf_gethdr(MBUF_WAITOK, MBUF_TYPE_DATA, &m);
169 if (error)
170 return error;
171 mbuf_setlen(m, sizeof(*sdata));
172 mbuf_pkthdr_setlen(m, sizeof(*sdata));
173 sdata = mbuf_data(m);
1c79356b
A
174
175 /* Do the RPC to get it. */
176 sdata->prog = htonl(prog);
177 sdata->vers = htonl(vers);
91447636 178 sdata->proto = htonl(proto);
1c79356b
A
179 sdata->port = 0;
180
181 sin->sin_port = htons(PMAPPORT);
91447636 182 error = krpc_call(sin, SOCK_DGRAM, PMAPPROG, PMAPVERS, PMAPPROC_GETPORT, &m, NULL);
1c79356b
A
183 if (error)
184 return error;
185
91447636 186 rdata = mbuf_data(m);
1c79356b
A
187 *portp = rdata->port;
188
91447636
A
189 if (!rdata->port)
190 error = EPROGUNAVAIL;
191
192 mbuf_freem(m);
193 return (error);
1c79356b
A
194}
195
196/*
197 * Do a remote procedure call (RPC) and wait for its reply.
198 * If from_p is non-null, then we are doing broadcast, and
199 * the address from whence the response came is saved there.
200 */
201int
91447636 202krpc_call(sa, sotype, prog, vers, func, data, from_p)
1c79356b 203 struct sockaddr_in *sa;
91447636
A
204 u_int sotype, prog, vers, func;
205 mbuf_t *data; /* input/output */
206 struct sockaddr_in *from_p; /* output */
1c79356b 207{
91447636 208 socket_t so;
1c79356b 209 struct sockaddr_in *sin;
91447636 210 mbuf_t m, nam, mhead;
1c79356b
A
211 struct rpc_call *call;
212 struct rpc_reply *reply;
91447636 213 int error, timo, secs, len;
1c79356b
A
214 static u_int32_t xid = ~0xFF;
215 u_int16_t tport;
91447636 216 int maxpacket = 1<<16;
1c79356b
A
217
218 /*
219 * Validate address family.
220 * Sorry, this is INET specific...
221 */
222 if (sa->sin_family != AF_INET)
223 return (EAFNOSUPPORT);
224
225 /* Free at end if not null. */
226 nam = mhead = NULL;
1c79356b
A
227
228 /*
229 * Create socket and set its recieve timeout.
230 */
91447636 231 if ((error = sock_socket(AF_INET, sotype, 0, 0, 0, &so)))
1c79356b
A
232 goto out;
233
234 {
235 struct timeval tv;
236
237 tv.tv_sec = 1;
238 tv.tv_usec = 0;
91447636
A
239
240 if ((error = sock_setsockopt(so, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv))))
1c79356b
A
241 goto out;
242
243 }
244
245 /*
246 * Enable broadcast if necessary.
247 */
248
91447636 249 if (from_p && (sotype == SOCK_DGRAM)) {
1c79356b 250 int on = 1;
91447636 251 if ((error = sock_setsockopt(so, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on))))
1c79356b
A
252 goto out;
253 }
254
255 /*
256 * Bind the local endpoint to a reserved port,
257 * because some NFS servers refuse requests from
258 * non-reserved (non-privileged) ports.
259 */
91447636
A
260 if ((error = mbuf_get(MBUF_WAITOK, MBUF_TYPE_SONAME, &m)))
261 goto out;
262 sin = mbuf_data(m);
263 bzero(sin, sizeof(*sin));
264 mbuf_setlen(m, sizeof(*sin));
265 sin->sin_len = sizeof(*sin);
1c79356b
A
266 sin->sin_family = AF_INET;
267 sin->sin_addr.s_addr = INADDR_ANY;
268 tport = IPPORT_RESERVED;
269 do {
270 tport--;
271 sin->sin_port = htons(tport);
91447636 272 error = sock_bind(so, (struct sockaddr*)sin);
1c79356b
A
273 } while (error == EADDRINUSE &&
274 tport > IPPORT_RESERVED / 2);
91447636 275 mbuf_freem(m);
e5568f75 276 m = NULL;
1c79356b
A
277 if (error) {
278 printf("bind failed\n");
279 goto out;
280 }
281
282 /*
283 * Setup socket address for the server.
284 */
91447636 285 if ((error = mbuf_get(MBUF_WAITOK, MBUF_TYPE_SONAME, &nam)))
1c79356b 286 goto out;
91447636
A
287 sin = mbuf_data(nam);
288 mbuf_setlen(nam, sa->sin_len);
289 bcopy((caddr_t)sa, (caddr_t)sin, sa->sin_len);
290
291 if (sotype == SOCK_STREAM) {
292 struct timeval tv;
293 tv.tv_sec = 60;
294 tv.tv_usec = 0;
295 error = sock_connect(so, mbuf_data(nam), MSG_DONTWAIT);
296 if (error && (error != EINPROGRESS))
297 goto out;
298 error = sock_connectwait(so, &tv);
299 if (error) {
300 if (error == EINPROGRESS)
301 error = ETIMEDOUT;
302 printf("krpc_call: error waiting for TCP socket connect: %d\n", error);
303 goto out;
304 }
1c79356b 305 }
1c79356b
A
306
307 /*
308 * Prepend RPC message header.
309 */
310 m = *data;
311 *data = NULL;
312#if DIAGNOSTIC
91447636 313 if ((mbuf_flags(m) & MBUF_PKTHDR) == 0)
1c79356b 314 panic("krpc_call: send data w/o pkthdr");
91447636 315 if (mbuf_pkthdr_len(m) < mbuf_len(m))
1c79356b
A
316 panic("krpc_call: pkthdr.len not set");
317#endif
91447636
A
318 len = sizeof(*call);
319 if (sotype == SOCK_STREAM)
320 len += 4; /* account for RPC record marker */
321 mhead = m;
322 if ((error = mbuf_prepend(&mhead, len, MBUF_WAITOK)))
323 goto out;
324 if ((error = mbuf_pkthdr_setrcvif(mhead, NULL)))
1c79356b 325 goto out;
1c79356b
A
326
327 /*
328 * Fill in the RPC header
329 */
91447636
A
330 if (sotype == SOCK_STREAM) {
331 /* first, fill in RPC record marker */
332 u_long *recmark = mbuf_data(mhead);
333 *recmark = htonl(0x80000000 | (mbuf_pkthdr_len(mhead) - 4));
334 call = (struct rpc_call *)(recmark + 1);
335 } else {
336 call = mbuf_data(mhead);
337 }
1c79356b
A
338 bzero((caddr_t)call, sizeof(*call));
339 xid++;
340 call->rp_xid = htonl(xid);
341 /* call->rp_direction = 0; */
342 call->rp_rpcvers = htonl(2);
343 call->rp_prog = htonl(prog);
344 call->rp_vers = htonl(vers);
345 call->rp_proc = htonl(func);
346 /* call->rp_auth = 0; */
347 /* call->rp_verf = 0; */
348
349 /*
350 * Send it, repeatedly, until a reply is received,
351 * but delay each re-send by an increasing amount.
352 * If the delay hits the maximum, start complaining.
353 */
354 timo = 0;
355 for (;;) {
91447636
A
356 struct msghdr msg;
357
1c79356b 358 /* Send RPC request (or re-send). */
91447636 359 if ((error = mbuf_copym(mhead, 0, MBUF_COPYALL, MBUF_WAITOK, &m)))
1c79356b 360 goto out;
91447636
A
361 bzero(&msg, sizeof(msg));
362 if (sotype == SOCK_STREAM) {
363 msg.msg_name = NULL;
364 msg.msg_namelen = 0;
365 } else {
366 msg.msg_name = mbuf_data(nam);
367 msg.msg_namelen = mbuf_len(nam);
1c79356b 368 }
91447636 369 error = sock_sendmbuf(so, &msg, m, 0, 0);
1c79356b
A
370 if (error) {
371 printf("krpc_call: sosend: %d\n", error);
372 goto out;
373 }
374 m = NULL;
375
376 /* Determine new timeout. */
377 if (timo < MAX_RESEND_DELAY)
378 timo++;
379 else
380 printf("RPC timeout for server " IP_FORMAT "\n",
381 IP_LIST(&(sin->sin_addr.s_addr)));
382
383 /*
384 * Wait for up to timo seconds for a reply.
385 * The socket receive timeout was set to 1 second.
386 */
387 secs = timo;
388 while (secs > 0) {
91447636
A
389 size_t readlen;
390
1c79356b 391 if (m) {
91447636 392 mbuf_freem(m);
1c79356b
A
393 m = NULL;
394 }
91447636
A
395 if (sotype == SOCK_STREAM) {
396 int maxretries = 60;
397 struct iovec_32 aio;
398 aio.iov_base = (uintptr_t) &len;
399 aio.iov_len = sizeof(u_long);
400 bzero(&msg, sizeof(msg));
401 msg.msg_iov = (struct iovec *) &aio;
402 msg.msg_iovlen = 1;
403 do {
404 error = sock_receive(so, &msg, MSG_WAITALL, &readlen);
405 if ((error == EWOULDBLOCK) && (--maxretries <= 0))
406 error = ETIMEDOUT;
407 } while (error == EWOULDBLOCK);
408 if (!error && readlen < aio.iov_len) {
409 /* only log a message if we got a partial word */
410 if (readlen != 0)
411 printf("short receive (%d/%d) from server " IP_FORMAT "\n",
412 readlen, sizeof(u_long), IP_LIST(&(sin->sin_addr.s_addr)));
413 error = EPIPE;
414 }
415 if (error)
416 goto out;
417 len = ntohl(len) & ~0x80000000;
418 /*
419 * This is SERIOUS! We are out of sync with the sender
420 * and forcing a disconnect/reconnect is all I can do.
421 */
422 if (len > maxpacket) {
423 printf("impossible packet length (%d) from server %s\n",
424 len, IP_LIST(&(sin->sin_addr.s_addr)));
425 error = EFBIG;
426 goto out;
427 }
428
429 do {
430 readlen = len;
431 error = sock_receivembuf(so, NULL, &m, MSG_WAITALL, &readlen);
432 } while (error == EWOULDBLOCK);
433
434 if (!error && (len > (int)readlen)) {
435 printf("short receive (%d/%d) from server %s\n",
436 readlen, len, IP_LIST(&(sin->sin_addr.s_addr)));
437 error = EPIPE;
438 }
439 } else {
440 len = maxpacket;
441 readlen = len;
442 bzero(&msg, sizeof(msg));
443 msg.msg_name = from_p;
444 msg.msg_namelen = (from_p == NULL) ? 0 : sizeof(*from_p);
445 error = sock_receivembuf(so, &msg, &m, 0, &readlen);
446 }
1c79356b
A
447
448 if (error == EWOULDBLOCK) {
449 secs--;
450 continue;
451 }
452 if (error)
453 goto out;
91447636 454 len = readlen;
1c79356b
A
455
456 /* Does the reply contain at least a header? */
457 if (len < MIN_REPLY_HDR)
458 continue;
91447636 459 if (mbuf_len(m) < MIN_REPLY_HDR)
1c79356b 460 continue;
91447636 461 reply = mbuf_data(m);
1c79356b
A
462
463 /* Is it the right reply? */
464 if (reply->rp_direction != htonl(RPC_REPLY))
465 continue;
466
467 if (reply->rp_xid != htonl(xid))
468 continue;
91447636 469
1c79356b
A
470 /* Was RPC accepted? (authorization OK) */
471 if (reply->rp_astatus != 0) {
472 error = ntohl(reply->rp_u.rpu_errno);
473 printf("rpc denied, error=%d\n", error);
90556fb8
A
474 /* convert rpc error to errno */
475 switch (error) {
476 case RPC_MISMATCH:
477 error = ERPCMISMATCH;
478 break;
479 case RPC_AUTHERR:
480 error = EAUTH;
481 break;
482 }
483 goto out;
1c79356b
A
484 }
485
486 /* Did the call succeed? */
487 if ((error = ntohl(reply->rp_u.rpu_ok.rp_rstatus)) != 0) {
488 printf("rpc status=%d\n", error);
90556fb8
A
489 /* convert rpc error to errno */
490 switch (error) {
491 case RPC_PROGUNAVAIL:
492 error = EPROGUNAVAIL;
493 break;
494 case RPC_PROGMISMATCH:
495 error = EPROGMISMATCH;
496 break;
497 case RPC_PROCUNAVAIL:
498 error = EPROCUNAVAIL;
499 break;
500 case RPC_GARBAGE:
501 error = EINVAL;
502 break;
503 case RPC_SYSTEM_ERR:
504 error = EIO;
505 break;
506 }
507 goto out;
1c79356b
A
508 }
509
510 goto gotreply; /* break two levels */
511
512 } /* while secs */
513 } /* forever send/receive */
514
515 error = ETIMEDOUT;
516 goto out;
517
518 gotreply:
519
520 /*
521 * Pull as much as we can into first mbuf, to make
522 * result buffer contiguous. Note that if the entire
523 * result won't fit into one mbuf, you're out of luck.
524 * XXX - Should not rely on making the entire reply
525 * contiguous (fix callers instead). -gwr
526 */
527#if DIAGNOSTIC
91447636 528 if ((mbuf_flags(m) & MBUF_PKTHDR) == 0)
1c79356b
A
529 panic("krpc_call: received pkt w/o header?");
530#endif
91447636
A
531 len = mbuf_pkthdr_len(m);
532 if (sotype == SOCK_STREAM)
533 len -= 4; /* the RPC record marker was read separately */
534 if (mbuf_len(m) < len) {
535 if ((error = mbuf_pullup(&m, len)))
1c79356b 536 goto out;
91447636 537 reply = mbuf_data(m);
1c79356b
A
538 }
539
540 /*
541 * Strip RPC header
542 */
543 len = sizeof(*reply);
544 if (reply->rp_u.rpu_ok.rp_auth.rp_atype != 0) {
545 len += ntohl(reply->rp_u.rpu_ok.rp_auth.rp_alen);
546 len = (len + 3) & ~3; /* XXX? */
547 }
91447636 548 mbuf_adj(m, len);
1c79356b
A
549
550 /* result */
551 *data = m;
552 out:
91447636
A
553 if (nam) mbuf_freem(nam);
554 if (mhead) mbuf_freem(mhead);
555 sock_close(so);
1c79356b
A
556 return error;
557}