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