]>
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 | ||
68 | #include <sys/param.h> | |
69 | #include <sys/conf.h> | |
70 | #include <sys/ioctl.h> | |
71 | #include <sys/proc.h> | |
72 | #include <sys/mount.h> | |
91447636 | 73 | #include <sys/kpi_mbuf.h> |
1c79356b A |
74 | #include <sys/malloc.h> |
75 | #include <sys/socket.h> | |
76 | #include <sys/socketvar.h> | |
77 | #include <sys/systm.h> | |
78 | #include <sys/reboot.h> | |
79 | ||
80 | #include <net/if.h> | |
81 | #include <netinet/in.h> | |
82 | ||
83 | #include <nfs/rpcv2.h> | |
84 | #include <nfs/krpc.h> | |
85 | ||
86 | /* | |
87 | * Kernel support for Sun RPC | |
88 | * | |
89 | * Used currently for bootstrapping in nfs diskless configurations. | |
0a7de745 | 90 | * |
1c79356b A |
91 | * Note: will not work on variable-sized rpc args/results. |
92 | * implicit size-limit of an mbuf. | |
93 | */ | |
94 | ||
95 | /* | |
96 | * Generic RPC headers | |
97 | */ | |
98 | ||
99 | struct auth_info { | |
0a7de745 A |
100 | u_int32_t rp_atype; /* auth type */ |
101 | u_int32_t rp_alen; /* auth length */ | |
1c79356b A |
102 | }; |
103 | ||
104 | struct rpc_call { | |
0a7de745 A |
105 | u_int32_t rp_xid; /* request transaction id */ |
106 | int32_t rp_direction; /* call direction (0) */ | |
107 | u_int32_t rp_rpcvers; /* rpc version (2) */ | |
108 | u_int32_t rp_prog; /* program */ | |
109 | u_int32_t rp_vers; /* version */ | |
110 | u_int32_t rp_proc; /* procedure */ | |
111 | struct auth_info rp_auth; | |
112 | struct auth_info rp_verf; | |
1c79356b A |
113 | }; |
114 | ||
115 | struct rpc_reply { | |
0a7de745 A |
116 | u_int32_t rp_xid; /* request transaction id */ |
117 | int32_t rp_direction; /* call direction (1) */ | |
118 | int32_t rp_astatus; /* accept status (0: accepted) */ | |
1c79356b A |
119 | union { |
120 | u_int32_t rpu_errno; | |
121 | struct { | |
122 | struct auth_info rp_auth; | |
0a7de745 | 123 | u_int32_t rp_rstatus; |
1c79356b A |
124 | } rpu_ok; |
125 | } rp_u; | |
126 | }; | |
127 | ||
0a7de745 A |
128 | #define MIN_REPLY_HDR 16 /* xid, dir, astat, errno */ |
129 | #define REPLY_SIZE 24 /* xid, dir, astat, rpu_ok */ | |
1c79356b A |
130 | |
131 | /* | |
132 | * What is the longest we will wait before re-sending a request? | |
133 | * Note this is also the frequency of "RPC timeout" messages. | |
134 | * The re-send loop count sup linearly to this maximum, so the | |
135 | * first complaint will happen after (1+2+3+4+5)=15 seconds. | |
136 | */ | |
0a7de745 | 137 | #define MAX_RESEND_DELAY 5 /* seconds */ |
1c79356b A |
138 | |
139 | /* copied over from nfs_boot.c for printf format. could put in .h file... */ | |
140 | #define IP_FORMAT "%d.%d.%d.%d" | |
141 | #define IP_CH(ip) ((u_char *)ip) | |
142 | #define IP_LIST(ip) IP_CH(ip)[0],IP_CH(ip)[1],IP_CH(ip)[2],IP_CH(ip)[3] | |
143 | ||
144 | ||
145 | /* | |
146 | * Call portmap to lookup a port number for a particular rpc program | |
147 | * Returns non-zero error on failure. | |
148 | */ | |
149 | int | |
39037602 | 150 | krpc_portmap( |
0a7de745 A |
151 | struct sockaddr_in *sin, /* server address */ |
152 | u_int prog, u_int vers, u_int proto, /* host order */ | |
153 | u_int16_t *portp) /* network order */ | |
1c79356b A |
154 | { |
155 | struct sdata { | |
0a7de745 A |
156 | u_int32_t prog; /* call program */ |
157 | u_int32_t vers; /* call version */ | |
158 | u_int32_t proto; /* call protocol */ | |
159 | u_int32_t port; /* call port (unused) */ | |
1c79356b A |
160 | } *sdata; |
161 | struct rdata { | |
162 | u_int16_t pad; | |
163 | u_int16_t port; | |
164 | } *rdata; | |
91447636 | 165 | mbuf_t m; |
1c79356b A |
166 | int error; |
167 | ||
168 | /* The portmapper port is fixed. */ | |
169 | if (prog == PMAPPROG) { | |
170 | *portp = htons(PMAPPORT); | |
171 | return 0; | |
172 | } | |
173 | ||
91447636 | 174 | error = mbuf_gethdr(MBUF_WAITOK, MBUF_TYPE_DATA, &m); |
0a7de745 | 175 | if (error) { |
91447636 | 176 | return error; |
0a7de745 | 177 | } |
91447636 A |
178 | mbuf_setlen(m, sizeof(*sdata)); |
179 | mbuf_pkthdr_setlen(m, sizeof(*sdata)); | |
180 | sdata = mbuf_data(m); | |
1c79356b A |
181 | |
182 | /* Do the RPC to get it. */ | |
183 | sdata->prog = htonl(prog); | |
184 | sdata->vers = htonl(vers); | |
91447636 | 185 | sdata->proto = htonl(proto); |
1c79356b A |
186 | sdata->port = 0; |
187 | ||
188 | sin->sin_port = htons(PMAPPORT); | |
91447636 | 189 | error = krpc_call(sin, SOCK_DGRAM, PMAPPROG, PMAPVERS, PMAPPROC_GETPORT, &m, NULL); |
0a7de745 | 190 | if (error) { |
1c79356b | 191 | return error; |
0a7de745 | 192 | } |
1c79356b | 193 | |
91447636 | 194 | rdata = mbuf_data(m); |
1c79356b | 195 | |
5c9f4661 A |
196 | if (mbuf_len(m) >= sizeof(*rdata)) { |
197 | *portp = rdata->port; | |
198 | } | |
199 | ||
0a7de745 | 200 | if (mbuf_len(m) < sizeof(*rdata) || !rdata->port) { |
91447636 | 201 | error = EPROGUNAVAIL; |
0a7de745 | 202 | } |
91447636 A |
203 | |
204 | mbuf_freem(m); | |
0a7de745 | 205 | return error; |
1c79356b A |
206 | } |
207 | ||
208 | /* | |
209 | * Do a remote procedure call (RPC) and wait for its reply. | |
210 | * If from_p is non-null, then we are doing broadcast, and | |
211 | * the address from whence the response came is saved there. | |
212 | */ | |
213 | int | |
39037602 A |
214 | krpc_call( |
215 | struct sockaddr_in *sa, | |
216 | u_int sotype, u_int prog, u_int vers, u_int func, | |
0a7de745 A |
217 | mbuf_t *data, /* input/output */ |
218 | struct sockaddr_in *from_p) /* output */ | |
1c79356b | 219 | { |
91447636 | 220 | socket_t so; |
1c79356b | 221 | struct sockaddr_in *sin; |
91447636 | 222 | mbuf_t m, nam, mhead; |
1c79356b A |
223 | struct rpc_call *call; |
224 | struct rpc_reply *reply; | |
2d21ac55 A |
225 | int error, timo, secs; |
226 | size_t len; | |
1c79356b A |
227 | static u_int32_t xid = ~0xFF; |
228 | u_int16_t tport; | |
0a7de745 | 229 | size_t maxpacket = 1 << 16; |
1c79356b A |
230 | |
231 | /* | |
232 | * Validate address family. | |
233 | * Sorry, this is INET specific... | |
234 | */ | |
0a7de745 A |
235 | if (sa->sin_family != AF_INET) { |
236 | return EAFNOSUPPORT; | |
237 | } | |
1c79356b A |
238 | |
239 | /* Free at end if not null. */ | |
240 | nam = mhead = NULL; | |
1c79356b A |
241 | |
242 | /* | |
243 | * Create socket and set its recieve timeout. | |
244 | */ | |
0a7de745 | 245 | if ((error = sock_socket(AF_INET, sotype, 0, 0, 0, &so))) { |
2d21ac55 | 246 | goto out1; |
0a7de745 | 247 | } |
1c79356b A |
248 | |
249 | { | |
250 | struct timeval tv; | |
251 | ||
252 | tv.tv_sec = 1; | |
253 | tv.tv_usec = 0; | |
91447636 | 254 | |
0a7de745 A |
255 | if ((error = sock_setsockopt(so, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)))) { |
256 | goto out; | |
257 | } | |
1c79356b A |
258 | } |
259 | ||
260 | /* | |
261 | * Enable broadcast if necessary. | |
262 | */ | |
263 | ||
91447636 | 264 | if (from_p && (sotype == SOCK_DGRAM)) { |
1c79356b | 265 | int on = 1; |
0a7de745 | 266 | if ((error = sock_setsockopt(so, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)))) { |
1c79356b | 267 | goto out; |
0a7de745 | 268 | } |
1c79356b A |
269 | } |
270 | ||
271 | /* | |
272 | * Bind the local endpoint to a reserved port, | |
273 | * because some NFS servers refuse requests from | |
274 | * non-reserved (non-privileged) ports. | |
275 | */ | |
0a7de745 | 276 | if ((error = mbuf_get(MBUF_WAITOK, MBUF_TYPE_SONAME, &m))) { |
91447636 | 277 | goto out; |
0a7de745 | 278 | } |
91447636 A |
279 | sin = mbuf_data(m); |
280 | bzero(sin, sizeof(*sin)); | |
281 | mbuf_setlen(m, sizeof(*sin)); | |
282 | sin->sin_len = sizeof(*sin); | |
1c79356b A |
283 | sin->sin_family = AF_INET; |
284 | sin->sin_addr.s_addr = INADDR_ANY; | |
285 | tport = IPPORT_RESERVED; | |
286 | do { | |
287 | tport--; | |
288 | sin->sin_port = htons(tport); | |
91447636 | 289 | error = sock_bind(so, (struct sockaddr*)sin); |
1c79356b | 290 | } while (error == EADDRINUSE && |
0a7de745 | 291 | tport > IPPORT_RESERVED / 2); |
91447636 | 292 | mbuf_freem(m); |
e5568f75 | 293 | m = NULL; |
1c79356b A |
294 | if (error) { |
295 | printf("bind failed\n"); | |
296 | goto out; | |
297 | } | |
298 | ||
299 | /* | |
300 | * Setup socket address for the server. | |
301 | */ | |
0a7de745 | 302 | if ((error = mbuf_get(MBUF_WAITOK, MBUF_TYPE_SONAME, &nam))) { |
1c79356b | 303 | goto out; |
0a7de745 | 304 | } |
91447636 A |
305 | sin = mbuf_data(nam); |
306 | mbuf_setlen(nam, sa->sin_len); | |
307 | bcopy((caddr_t)sa, (caddr_t)sin, sa->sin_len); | |
308 | ||
309 | if (sotype == SOCK_STREAM) { | |
310 | struct timeval tv; | |
311 | tv.tv_sec = 60; | |
312 | tv.tv_usec = 0; | |
313 | error = sock_connect(so, mbuf_data(nam), MSG_DONTWAIT); | |
0a7de745 | 314 | if (error && (error != EINPROGRESS)) { |
91447636 | 315 | goto out; |
0a7de745 | 316 | } |
91447636 A |
317 | error = sock_connectwait(so, &tv); |
318 | if (error) { | |
0a7de745 | 319 | if (error == EINPROGRESS) { |
91447636 | 320 | error = ETIMEDOUT; |
0a7de745 | 321 | } |
91447636 A |
322 | printf("krpc_call: error waiting for TCP socket connect: %d\n", error); |
323 | goto out; | |
324 | } | |
1c79356b | 325 | } |
1c79356b A |
326 | |
327 | /* | |
328 | * Prepend RPC message header. | |
329 | */ | |
330 | m = *data; | |
331 | *data = NULL; | |
0a7de745 A |
332 | #if DIAGNOSTIC |
333 | if ((mbuf_flags(m) & MBUF_PKTHDR) == 0) { | |
1c79356b | 334 | panic("krpc_call: send data w/o pkthdr"); |
0a7de745 A |
335 | } |
336 | if (mbuf_pkthdr_len(m) < mbuf_len(m)) { | |
1c79356b | 337 | panic("krpc_call: pkthdr.len not set"); |
0a7de745 | 338 | } |
1c79356b | 339 | #endif |
91447636 | 340 | len = sizeof(*call); |
0a7de745 | 341 | if (sotype == SOCK_STREAM) { |
91447636 | 342 | len += 4; /* account for RPC record marker */ |
0a7de745 | 343 | } |
91447636 | 344 | mhead = m; |
0a7de745 | 345 | if ((error = mbuf_prepend(&mhead, len, MBUF_WAITOK))) { |
91447636 | 346 | goto out; |
0a7de745 A |
347 | } |
348 | if ((error = mbuf_pkthdr_setrcvif(mhead, NULL))) { | |
1c79356b | 349 | goto out; |
0a7de745 | 350 | } |
1c79356b A |
351 | |
352 | /* | |
353 | * Fill in the RPC header | |
354 | */ | |
91447636 A |
355 | if (sotype == SOCK_STREAM) { |
356 | /* first, fill in RPC record marker */ | |
b0d623f7 | 357 | u_int32_t *recmark = mbuf_data(mhead); |
91447636 A |
358 | *recmark = htonl(0x80000000 | (mbuf_pkthdr_len(mhead) - 4)); |
359 | call = (struct rpc_call *)(recmark + 1); | |
360 | } else { | |
361 | call = mbuf_data(mhead); | |
362 | } | |
1c79356b A |
363 | bzero((caddr_t)call, sizeof(*call)); |
364 | xid++; | |
365 | call->rp_xid = htonl(xid); | |
366 | /* call->rp_direction = 0; */ | |
367 | call->rp_rpcvers = htonl(2); | |
368 | call->rp_prog = htonl(prog); | |
369 | call->rp_vers = htonl(vers); | |
370 | call->rp_proc = htonl(func); | |
371 | /* call->rp_auth = 0; */ | |
372 | /* call->rp_verf = 0; */ | |
373 | ||
374 | /* | |
375 | * Send it, repeatedly, until a reply is received, | |
376 | * but delay each re-send by an increasing amount. | |
377 | * If the delay hits the maximum, start complaining. | |
378 | */ | |
379 | timo = 0; | |
380 | for (;;) { | |
91447636 | 381 | struct msghdr msg; |
0a7de745 | 382 | |
1c79356b | 383 | /* Send RPC request (or re-send). */ |
0a7de745 | 384 | if ((error = mbuf_copym(mhead, 0, MBUF_COPYALL, MBUF_WAITOK, &m))) { |
1c79356b | 385 | goto out; |
0a7de745 | 386 | } |
91447636 A |
387 | bzero(&msg, sizeof(msg)); |
388 | if (sotype == SOCK_STREAM) { | |
389 | msg.msg_name = NULL; | |
390 | msg.msg_namelen = 0; | |
391 | } else { | |
392 | msg.msg_name = mbuf_data(nam); | |
393 | msg.msg_namelen = mbuf_len(nam); | |
1c79356b | 394 | } |
91447636 | 395 | error = sock_sendmbuf(so, &msg, m, 0, 0); |
1c79356b A |
396 | if (error) { |
397 | printf("krpc_call: sosend: %d\n", error); | |
398 | goto out; | |
399 | } | |
400 | m = NULL; | |
401 | ||
402 | /* Determine new timeout. */ | |
0a7de745 | 403 | if (timo < MAX_RESEND_DELAY) { |
1c79356b | 404 | timo++; |
0a7de745 A |
405 | } else { |
406 | printf("RPC timeout for server " IP_FORMAT "\n", | |
407 | IP_LIST(&(sin->sin_addr.s_addr))); | |
408 | } | |
1c79356b A |
409 | |
410 | /* | |
411 | * Wait for up to timo seconds for a reply. | |
412 | * The socket receive timeout was set to 1 second. | |
413 | */ | |
414 | secs = timo; | |
415 | while (secs > 0) { | |
91447636 | 416 | size_t readlen; |
0a7de745 | 417 | |
1c79356b | 418 | if (m) { |
91447636 | 419 | mbuf_freem(m); |
1c79356b A |
420 | m = NULL; |
421 | } | |
91447636 A |
422 | if (sotype == SOCK_STREAM) { |
423 | int maxretries = 60; | |
b0d623f7 A |
424 | struct iovec aio; |
425 | aio.iov_base = &len; | |
426 | aio.iov_len = sizeof(u_int32_t); | |
91447636 | 427 | bzero(&msg, sizeof(msg)); |
b0d623f7 | 428 | msg.msg_iov = &aio; |
91447636 A |
429 | msg.msg_iovlen = 1; |
430 | do { | |
0a7de745 A |
431 | error = sock_receive(so, &msg, MSG_WAITALL, &readlen); |
432 | if ((error == EWOULDBLOCK) && (--maxretries <= 0)) { | |
433 | error = ETIMEDOUT; | |
434 | } | |
91447636 A |
435 | } while (error == EWOULDBLOCK); |
436 | if (!error && readlen < aio.iov_len) { | |
0a7de745 A |
437 | /* only log a message if we got a partial word */ |
438 | if (readlen != 0) { | |
439 | printf("short receive (%ld/%ld) from server " IP_FORMAT "\n", | |
440 | readlen, sizeof(u_int32_t), IP_LIST(&(sin->sin_addr.s_addr))); | |
441 | } | |
442 | error = EPIPE; | |
91447636 | 443 | } |
0a7de745 | 444 | if (error) { |
91447636 | 445 | goto out; |
0a7de745 | 446 | } |
91447636 A |
447 | len = ntohl(len) & ~0x80000000; |
448 | /* | |
449 | * This is SERIOUS! We are out of sync with the sender | |
450 | * and forcing a disconnect/reconnect is all I can do. | |
451 | */ | |
452 | if (len > maxpacket) { | |
0a7de745 A |
453 | printf("impossible packet length (%ld) from server " IP_FORMAT "\n", |
454 | len, IP_LIST(&(sin->sin_addr.s_addr))); | |
455 | error = EFBIG; | |
456 | goto out; | |
91447636 | 457 | } |
0a7de745 | 458 | |
91447636 | 459 | do { |
0a7de745 A |
460 | readlen = len; |
461 | error = sock_receivembuf(so, NULL, &m, MSG_WAITALL, &readlen); | |
91447636 A |
462 | } while (error == EWOULDBLOCK); |
463 | ||
2d21ac55 | 464 | if (!error && (len > readlen)) { |
0a7de745 A |
465 | printf("short receive (%ld/%ld) from server " IP_FORMAT "\n", |
466 | readlen, len, IP_LIST(&(sin->sin_addr.s_addr))); | |
467 | error = EPIPE; | |
91447636 A |
468 | } |
469 | } else { | |
470 | len = maxpacket; | |
471 | readlen = len; | |
472 | bzero(&msg, sizeof(msg)); | |
473 | msg.msg_name = from_p; | |
474 | msg.msg_namelen = (from_p == NULL) ? 0 : sizeof(*from_p); | |
475 | error = sock_receivembuf(so, &msg, &m, 0, &readlen); | |
476 | } | |
1c79356b A |
477 | |
478 | if (error == EWOULDBLOCK) { | |
479 | secs--; | |
480 | continue; | |
481 | } | |
0a7de745 | 482 | if (error) { |
1c79356b | 483 | goto out; |
0a7de745 | 484 | } |
91447636 | 485 | len = readlen; |
1c79356b A |
486 | |
487 | /* Does the reply contain at least a header? */ | |
0a7de745 | 488 | if (len < MIN_REPLY_HDR) { |
1c79356b | 489 | continue; |
0a7de745 A |
490 | } |
491 | if (mbuf_len(m) < MIN_REPLY_HDR) { | |
1c79356b | 492 | continue; |
0a7de745 | 493 | } |
91447636 | 494 | reply = mbuf_data(m); |
1c79356b A |
495 | |
496 | /* Is it the right reply? */ | |
0a7de745 | 497 | if (reply->rp_direction != htonl(RPC_REPLY)) { |
1c79356b | 498 | continue; |
0a7de745 | 499 | } |
1c79356b | 500 | |
0a7de745 | 501 | if (reply->rp_xid != htonl(xid)) { |
1c79356b | 502 | continue; |
0a7de745 A |
503 | } |
504 | ||
1c79356b A |
505 | /* Was RPC accepted? (authorization OK) */ |
506 | if (reply->rp_astatus != 0) { | |
507 | error = ntohl(reply->rp_u.rpu_errno); | |
508 | printf("rpc denied, error=%d\n", error); | |
90556fb8 A |
509 | /* convert rpc error to errno */ |
510 | switch (error) { | |
511 | case RPC_MISMATCH: | |
512 | error = ERPCMISMATCH; | |
513 | break; | |
514 | case RPC_AUTHERR: | |
515 | error = EAUTH; | |
516 | break; | |
517 | } | |
518 | goto out; | |
1c79356b A |
519 | } |
520 | ||
a39ff7e2 A |
521 | |
522 | if (mbuf_len(m) < REPLY_SIZE) { | |
523 | error = RPC_SYSTEM_ERR; | |
0a7de745 | 524 | } else { |
a39ff7e2 A |
525 | error = ntohl(reply->rp_u.rpu_ok.rp_rstatus); |
526 | } | |
527 | ||
1c79356b | 528 | /* Did the call succeed? */ |
a39ff7e2 | 529 | if (error != 0) { |
1c79356b | 530 | printf("rpc status=%d\n", error); |
90556fb8 A |
531 | /* convert rpc error to errno */ |
532 | switch (error) { | |
533 | case RPC_PROGUNAVAIL: | |
534 | error = EPROGUNAVAIL; | |
535 | break; | |
536 | case RPC_PROGMISMATCH: | |
537 | error = EPROGMISMATCH; | |
538 | break; | |
539 | case RPC_PROCUNAVAIL: | |
540 | error = EPROCUNAVAIL; | |
541 | break; | |
542 | case RPC_GARBAGE: | |
543 | error = EINVAL; | |
544 | break; | |
545 | case RPC_SYSTEM_ERR: | |
546 | error = EIO; | |
547 | break; | |
548 | } | |
549 | goto out; | |
1c79356b A |
550 | } |
551 | ||
0a7de745 | 552 | goto gotreply; /* break two levels */ |
1c79356b A |
553 | } /* while secs */ |
554 | } /* forever send/receive */ | |
555 | ||
556 | error = ETIMEDOUT; | |
557 | goto out; | |
558 | ||
0a7de745 | 559 | gotreply: |
1c79356b A |
560 | |
561 | /* | |
562 | * Pull as much as we can into first mbuf, to make | |
563 | * result buffer contiguous. Note that if the entire | |
564 | * result won't fit into one mbuf, you're out of luck. | |
565 | * XXX - Should not rely on making the entire reply | |
566 | * contiguous (fix callers instead). -gwr | |
567 | */ | |
0a7de745 A |
568 | #if DIAGNOSTIC |
569 | if ((mbuf_flags(m) & MBUF_PKTHDR) == 0) { | |
1c79356b | 570 | panic("krpc_call: received pkt w/o header?"); |
0a7de745 | 571 | } |
1c79356b | 572 | #endif |
91447636 | 573 | len = mbuf_pkthdr_len(m); |
0a7de745 | 574 | if (sotype == SOCK_STREAM) { |
91447636 | 575 | len -= 4; /* the RPC record marker was read separately */ |
0a7de745 | 576 | } |
91447636 | 577 | if (mbuf_len(m) < len) { |
0a7de745 | 578 | if ((error = mbuf_pullup(&m, len))) { |
1c79356b | 579 | goto out; |
0a7de745 | 580 | } |
91447636 | 581 | reply = mbuf_data(m); |
1c79356b A |
582 | } |
583 | ||
584 | /* | |
585 | * Strip RPC header | |
586 | */ | |
587 | len = sizeof(*reply); | |
588 | if (reply->rp_u.rpu_ok.rp_auth.rp_atype != 0) { | |
589 | len += ntohl(reply->rp_u.rpu_ok.rp_auth.rp_alen); | |
590 | len = (len + 3) & ~3; /* XXX? */ | |
591 | } | |
91447636 | 592 | mbuf_adj(m, len); |
1c79356b A |
593 | |
594 | /* result */ | |
595 | *data = m; | |
0a7de745 | 596 | out: |
2d21ac55 A |
597 | sock_close(so); |
598 | out1: | |
0a7de745 A |
599 | if (nam) { |
600 | mbuf_freem(nam); | |
601 | } | |
602 | if (mhead) { | |
603 | mbuf_freem(mhead); | |
604 | } | |
1c79356b A |
605 | return error; |
606 | } |