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