]>
git.saurik.com Git - apple/xnu.git/blob - bsd/nfs/krpc_subr.c
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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.
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
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
24 * Copyright (c) 1994 Gordon Ross, Adam Glass
25 * Copyright (c) 1992 Regents of the University of California.
26 * All rights reserved.
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.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
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.
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
62 #include <sys/param.h>
64 #include <sys/ioctl.h>
66 #include <sys/mount.h>
67 #include <sys/kpi_mbuf.h>
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>
73 #include <sys/uio_internal.h>
76 #include <netinet/in.h>
78 #include <nfs/rpcv2.h>
82 * Kernel support for Sun RPC
84 * Used currently for bootstrapping in nfs diskless configurations.
86 * Note: will not work on variable-sized rpc args/results.
87 * implicit size-limit of an mbuf.
95 u_int32_t rp_atype
; /* auth type */
96 u_int32_t rp_alen
; /* auth length */
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
;
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) */
117 struct auth_info rp_auth
;
118 u_int32_t rp_rstatus
;
123 #define MIN_REPLY_HDR 16 /* xid, dir, astat, errno */
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.
131 #define MAX_RESEND_DELAY 5 /* seconds */
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]
140 * Call portmap to lookup a port number for a particular rpc program
141 * Returns non-zero error on failure.
144 krpc_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 */
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) */
162 /* The portmapper port is fixed. */
163 if (prog
== PMAPPROG
) {
164 *portp
= htons(PMAPPORT
);
168 error
= mbuf_gethdr(MBUF_WAITOK
, MBUF_TYPE_DATA
, &m
);
171 mbuf_setlen(m
, sizeof(*sdata
));
172 mbuf_pkthdr_setlen(m
, sizeof(*sdata
));
173 sdata
= mbuf_data(m
);
175 /* Do the RPC to get it. */
176 sdata
->prog
= htonl(prog
);
177 sdata
->vers
= htonl(vers
);
178 sdata
->proto
= htonl(proto
);
181 sin
->sin_port
= htons(PMAPPORT
);
182 error
= krpc_call(sin
, SOCK_DGRAM
, PMAPPROG
, PMAPVERS
, PMAPPROC_GETPORT
, &m
, NULL
);
186 rdata
= mbuf_data(m
);
187 *portp
= rdata
->port
;
190 error
= EPROGUNAVAIL
;
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.
202 krpc_call(sa
, sotype
, prog
, vers
, func
, data
, from_p
)
203 struct sockaddr_in
*sa
;
204 u_int sotype
, prog
, vers
, func
;
205 mbuf_t
*data
; /* input/output */
206 struct sockaddr_in
*from_p
; /* output */
209 struct sockaddr_in
*sin
;
210 mbuf_t m
, nam
, mhead
;
211 struct rpc_call
*call
;
212 struct rpc_reply
*reply
;
213 int error
, timo
, secs
, len
;
214 static u_int32_t xid
= ~0xFF;
216 int maxpacket
= 1<<16;
219 * Validate address family.
220 * Sorry, this is INET specific...
222 if (sa
->sin_family
!= AF_INET
)
223 return (EAFNOSUPPORT
);
225 /* Free at end if not null. */
229 * Create socket and set its recieve timeout.
231 if ((error
= sock_socket(AF_INET
, sotype
, 0, 0, 0, &so
)))
240 if ((error
= sock_setsockopt(so
, SOL_SOCKET
, SO_RCVTIMEO
, &tv
, sizeof(tv
))))
246 * Enable broadcast if necessary.
249 if (from_p
&& (sotype
== SOCK_DGRAM
)) {
251 if ((error
= sock_setsockopt(so
, SOL_SOCKET
, SO_BROADCAST
, &on
, sizeof(on
))))
256 * Bind the local endpoint to a reserved port,
257 * because some NFS servers refuse requests from
258 * non-reserved (non-privileged) ports.
260 if ((error
= mbuf_get(MBUF_WAITOK
, MBUF_TYPE_SONAME
, &m
)))
263 bzero(sin
, sizeof(*sin
));
264 mbuf_setlen(m
, sizeof(*sin
));
265 sin
->sin_len
= sizeof(*sin
);
266 sin
->sin_family
= AF_INET
;
267 sin
->sin_addr
.s_addr
= INADDR_ANY
;
268 tport
= IPPORT_RESERVED
;
271 sin
->sin_port
= htons(tport
);
272 error
= sock_bind(so
, (struct sockaddr
*)sin
);
273 } while (error
== EADDRINUSE
&&
274 tport
> IPPORT_RESERVED
/ 2);
278 printf("bind failed\n");
283 * Setup socket address for the server.
285 if ((error
= mbuf_get(MBUF_WAITOK
, MBUF_TYPE_SONAME
, &nam
)))
287 sin
= mbuf_data(nam
);
288 mbuf_setlen(nam
, sa
->sin_len
);
289 bcopy((caddr_t
)sa
, (caddr_t
)sin
, sa
->sin_len
);
291 if (sotype
== SOCK_STREAM
) {
295 error
= sock_connect(so
, mbuf_data(nam
), MSG_DONTWAIT
);
296 if (error
&& (error
!= EINPROGRESS
))
298 error
= sock_connectwait(so
, &tv
);
300 if (error
== EINPROGRESS
)
302 printf("krpc_call: error waiting for TCP socket connect: %d\n", error
);
308 * Prepend RPC message header.
313 if ((mbuf_flags(m
) & MBUF_PKTHDR
) == 0)
314 panic("krpc_call: send data w/o pkthdr");
315 if (mbuf_pkthdr_len(m
) < mbuf_len(m
))
316 panic("krpc_call: pkthdr.len not set");
319 if (sotype
== SOCK_STREAM
)
320 len
+= 4; /* account for RPC record marker */
322 if ((error
= mbuf_prepend(&mhead
, len
, MBUF_WAITOK
)))
324 if ((error
= mbuf_pkthdr_setrcvif(mhead
, NULL
)))
328 * Fill in the RPC header
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);
336 call
= mbuf_data(mhead
);
338 bzero((caddr_t
)call
, sizeof(*call
));
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; */
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.
358 /* Send RPC request (or re-send). */
359 if ((error
= mbuf_copym(mhead
, 0, MBUF_COPYALL
, MBUF_WAITOK
, &m
)))
361 bzero(&msg
, sizeof(msg
));
362 if (sotype
== SOCK_STREAM
) {
366 msg
.msg_name
= mbuf_data(nam
);
367 msg
.msg_namelen
= mbuf_len(nam
);
369 error
= sock_sendmbuf(so
, &msg
, m
, 0, 0);
371 printf("krpc_call: sosend: %d\n", error
);
376 /* Determine new timeout. */
377 if (timo
< MAX_RESEND_DELAY
)
380 printf("RPC timeout for server " IP_FORMAT
"\n",
381 IP_LIST(&(sin
->sin_addr
.s_addr
)));
384 * Wait for up to timo seconds for a reply.
385 * The socket receive timeout was set to 1 second.
395 if (sotype
== SOCK_STREAM
) {
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
;
404 error
= sock_receive(so
, &msg
, MSG_WAITALL
, &readlen
);
405 if ((error
== EWOULDBLOCK
) && (--maxretries
<= 0))
407 } while (error
== EWOULDBLOCK
);
408 if (!error
&& readlen
< aio
.iov_len
) {
409 /* only log a message if we got a partial word */
411 printf("short receive (%d/%d) from server " IP_FORMAT
"\n",
412 readlen
, sizeof(u_long
), IP_LIST(&(sin
->sin_addr
.s_addr
)));
417 len
= ntohl(len
) & ~0x80000000;
419 * This is SERIOUS! We are out of sync with the sender
420 * and forcing a disconnect/reconnect is all I can do.
422 if (len
> maxpacket
) {
423 printf("impossible packet length (%d) from server %s\n",
424 len
, IP_LIST(&(sin
->sin_addr
.s_addr
)));
431 error
= sock_receivembuf(so
, NULL
, &m
, MSG_WAITALL
, &readlen
);
432 } while (error
== EWOULDBLOCK
);
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
)));
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
);
448 if (error
== EWOULDBLOCK
) {
456 /* Does the reply contain at least a header? */
457 if (len
< MIN_REPLY_HDR
)
459 if (mbuf_len(m
) < MIN_REPLY_HDR
)
461 reply
= mbuf_data(m
);
463 /* Is it the right reply? */
464 if (reply
->rp_direction
!= htonl(RPC_REPLY
))
467 if (reply
->rp_xid
!= htonl(xid
))
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
);
474 /* convert rpc error to errno */
477 error
= ERPCMISMATCH
;
486 /* Did the call succeed? */
487 if ((error
= ntohl(reply
->rp_u
.rpu_ok
.rp_rstatus
)) != 0) {
488 printf("rpc status=%d\n", error
);
489 /* convert rpc error to errno */
491 case RPC_PROGUNAVAIL
:
492 error
= EPROGUNAVAIL
;
494 case RPC_PROGMISMATCH
:
495 error
= EPROGMISMATCH
;
497 case RPC_PROCUNAVAIL
:
498 error
= EPROCUNAVAIL
;
510 goto gotreply
; /* break two levels */
513 } /* forever send/receive */
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
528 if ((mbuf_flags(m
) & MBUF_PKTHDR
) == 0)
529 panic("krpc_call: received pkt w/o header?");
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
)))
537 reply
= mbuf_data(m
);
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? */
553 if (nam
) mbuf_freem(nam
);
554 if (mhead
) mbuf_freem(mhead
);