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