This repository was archived by the owner on May 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathnfs_socket.c
More file actions
6411 lines (5929 loc) · 187 KB
/
nfs_socket.c
File metadata and controls
6411 lines (5929 loc) · 187 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2000-2015 Apple Inc. All rights reserved.
*
* @APPLE_OSREFERENCE_LICENSE_HEADER_START@
*
* This file contains Original Code and/or Modifications of Original Code
* as defined in and that are subject to the Apple Public Source License
* Version 2.0 (the 'License'). You may not use this file except in
* compliance with the License. The rights granted to you under the License
* may not be used to create, or enable the creation or redistribution of,
* unlawful or unlicensed copies of an Apple operating system, or to
* circumvent, violate, or enable the circumvention or violation of, any
* terms of an Apple operating system software license agreement.
*
* Please obtain a copy of the License at
* http://www.opensource.apple.com/apsl/ and read it before using this file.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
* Please see the License for the specific language governing rights and
* limitations under the License.
*
* @APPLE_OSREFERENCE_LICENSE_HEADER_END@
*/
/* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
/*
* Copyright (c) 1989, 1991, 1993, 1995
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Rick Macklem at The University of Guelph.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)nfs_socket.c 8.5 (Berkeley) 3/30/95
* FreeBSD-Id: nfs_socket.c,v 1.30 1997/10/28 15:59:07 bde Exp $
*/
/*
* Socket operations for use by nfs
*/
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/proc.h>
#include <sys/signalvar.h>
#include <sys/kauth.h>
#include <sys/mount_internal.h>
#include <sys/kernel.h>
#include <sys/kpi_mbuf.h>
#include <sys/malloc.h>
#include <sys/vnode.h>
#include <sys/domain.h>
#include <sys/protosw.h>
#include <sys/socket.h>
#include <sys/syslog.h>
#include <sys/tprintf.h>
#include <libkern/OSAtomic.h>
#include <sys/time.h>
#include <kern/clock.h>
#include <kern/task.h>
#include <kern/thread.h>
#include <kern/thread_call.h>
#include <sys/user.h>
#include <sys/acct.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <nfs/rpcv2.h>
#include <nfs/krpc.h>
#include <nfs/nfsproto.h>
#include <nfs/nfs.h>
#include <nfs/xdr_subs.h>
#include <nfs/nfsm_subs.h>
#include <nfs/nfs_gss.h>
#include <nfs/nfsmount.h>
#include <nfs/nfsnode.h>
#define NFS_SOCK_DBG(...) NFS_DBG(NFS_FAC_SOCK, 7, ## __VA_ARGS__)
/* XXX */
boolean_t current_thread_aborted(void);
kern_return_t thread_terminate(thread_t);
#if NFSSERVER
int nfsrv_sock_max_rec_queue_length = 128; /* max # RPC records queued on (UDP) socket */
int nfsrv_getstream(struct nfsrv_sock *,int);
int nfsrv_getreq(struct nfsrv_descript *);
extern int nfsv3_procid[NFS_NPROCS];
#endif /* NFSSERVER */
/*
* compare two sockaddr structures
*/
int
nfs_sockaddr_cmp(struct sockaddr *sa1, struct sockaddr *sa2)
{
if (!sa1)
return (-1);
if (!sa2)
return (1);
if (sa1->sa_family != sa2->sa_family)
return ((sa1->sa_family < sa2->sa_family) ? -1 : 1);
if (sa1->sa_len != sa2->sa_len)
return ((sa1->sa_len < sa2->sa_len) ? -1 : 1);
if (sa1->sa_family == AF_INET)
return (bcmp(&((struct sockaddr_in*)sa1)->sin_addr,
&((struct sockaddr_in*)sa2)->sin_addr, sizeof(((struct sockaddr_in*)sa1)->sin_addr)));
if (sa1->sa_family == AF_INET6)
return (bcmp(&((struct sockaddr_in6*)sa1)->sin6_addr,
&((struct sockaddr_in6*)sa2)->sin6_addr, sizeof(((struct sockaddr_in6*)sa1)->sin6_addr)));
return (-1);
}
#if NFSCLIENT
int nfs_connect_search_new_socket(struct nfsmount *, struct nfs_socket_search *, struct timeval *);
int nfs_connect_search_socket_connect(struct nfsmount *, struct nfs_socket *, int);
int nfs_connect_search_ping(struct nfsmount *, struct nfs_socket *, struct timeval *);
void nfs_connect_search_socket_found(struct nfsmount *, struct nfs_socket_search *, struct nfs_socket *);
void nfs_connect_search_socket_reap(struct nfsmount *, struct nfs_socket_search *, struct timeval *);
int nfs_connect_search_check(struct nfsmount *, struct nfs_socket_search *, struct timeval *);
int nfs_reconnect(struct nfsmount *);
int nfs_connect_setup(struct nfsmount *);
void nfs_mount_sock_thread(void *, wait_result_t);
void nfs_udp_rcv(socket_t, void*, int);
void nfs_tcp_rcv(socket_t, void*, int);
void nfs_sock_poke(struct nfsmount *);
void nfs_request_match_reply(struct nfsmount *, mbuf_t);
void nfs_reqdequeue(struct nfsreq *);
void nfs_reqbusy(struct nfsreq *);
struct nfsreq *nfs_reqnext(struct nfsreq *);
int nfs_wait_reply(struct nfsreq *);
void nfs_softterm(struct nfsreq *);
int nfs_can_squish(struct nfsmount *);
int nfs_is_squishy(struct nfsmount *);
int nfs_is_dead(int, struct nfsmount *);
/*
* Estimate rto for an nfs rpc sent via. an unreliable datagram.
* Use the mean and mean deviation of rtt for the appropriate type of rpc
* for the frequent rpcs and a default for the others.
* The justification for doing "other" this way is that these rpcs
* happen so infrequently that timer est. would probably be stale.
* Also, since many of these rpcs are
* non-idempotent, a conservative timeout is desired.
* getattr, lookup - A+2D
* read, write - A+4D
* other - nm_timeo
*/
#define NFS_RTO(n, t) \
((t) == 0 ? (n)->nm_timeo : \
((t) < 3 ? \
(((((n)->nm_srtt[t-1] + 3) >> 2) + (n)->nm_sdrtt[t-1] + 1) >> 1) : \
((((n)->nm_srtt[t-1] + 7) >> 3) + (n)->nm_sdrtt[t-1] + 1)))
#define NFS_SRTT(r) (r)->r_nmp->nm_srtt[proct[(r)->r_procnum] - 1]
#define NFS_SDRTT(r) (r)->r_nmp->nm_sdrtt[proct[(r)->r_procnum] - 1]
/*
* Defines which timer to use for the procnum.
* 0 - default
* 1 - getattr
* 2 - lookup
* 3 - read
* 4 - write
*/
static int proct[NFS_NPROCS] = {
0, 1, 0, 2, 1, 3, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 0, 0, 0, 0, 0
};
/*
* There is a congestion window for outstanding rpcs maintained per mount
* point. The cwnd size is adjusted in roughly the way that:
* Van Jacobson, Congestion avoidance and Control, In "Proceedings of
* SIGCOMM '88". ACM, August 1988.
* describes for TCP. The cwnd size is chopped in half on a retransmit timeout
* and incremented by 1/cwnd when each rpc reply is received and a full cwnd
* of rpcs is in progress.
* (The sent count and cwnd are scaled for integer arith.)
* Variants of "slow start" were tried and were found to be too much of a
* performance hit (ave. rtt 3 times larger),
* I suspect due to the large rtt that nfs rpcs have.
*/
#define NFS_CWNDSCALE 256
#define NFS_MAXCWND (NFS_CWNDSCALE * 32)
static int nfs_backoff[8] = { 2, 4, 8, 16, 32, 64, 128, 256, };
/*
* Increment location index to next address/server/location.
*/
void
nfs_location_next(struct nfs_fs_locations *nlp, struct nfs_location_index *nlip)
{
uint8_t loc = nlip->nli_loc;
uint8_t serv = nlip->nli_serv;
uint8_t addr = nlip->nli_addr;
/* move to next address */
addr++;
if (addr >= nlp->nl_locations[loc]->nl_servers[serv]->ns_addrcount) {
/* no more addresses on current server, go to first address of next server */
next_server:
addr = 0;
serv++;
if (serv >= nlp->nl_locations[loc]->nl_servcount) {
/* no more servers on current location, go to first server of next location */
serv = 0;
loc++;
if (loc >= nlp->nl_numlocs)
loc = 0; /* after last location, wrap back around to first location */
}
}
/*
* It's possible for this next server to not have any addresses.
* Check for that here and go to the next server.
* But bail out if we've managed to come back around to the original
* location that was passed in. (That would mean no servers had any
* addresses. And we don't want to spin here forever.)
*/
if ((loc == nlip->nli_loc) && (serv == nlip->nli_serv) && (addr == nlip->nli_addr))
return;
if (addr >= nlp->nl_locations[loc]->nl_servers[serv]->ns_addrcount)
goto next_server;
nlip->nli_loc = loc;
nlip->nli_serv = serv;
nlip->nli_addr = addr;
}
/*
* Compare two location indices.
*/
int
nfs_location_index_cmp(struct nfs_location_index *nlip1, struct nfs_location_index *nlip2)
{
if (nlip1->nli_loc != nlip2->nli_loc)
return (nlip1->nli_loc - nlip2->nli_loc);
if (nlip1->nli_serv != nlip2->nli_serv)
return (nlip1->nli_serv - nlip2->nli_serv);
return (nlip1->nli_addr - nlip2->nli_addr);
}
/*
* Get the mntfromname (or path portion only) for a given location.
*/
void
nfs_location_mntfromname(struct nfs_fs_locations *locs, struct nfs_location_index idx, char *s, int size, int pathonly)
{
struct nfs_fs_location *fsl = locs->nl_locations[idx.nli_loc];
char *p;
int cnt, i;
p = s;
if (!pathonly) {
cnt = snprintf(p, size, "%s:", fsl->nl_servers[idx.nli_serv]->ns_name);
p += cnt;
size -= cnt;
}
if (fsl->nl_path.np_compcount == 0) {
/* mounting root export on server */
if (size > 0) {
*p++ = '/';
*p++ = '\0';
}
return;
}
/* append each server path component */
for (i=0; (size > 0) && (i < (int)fsl->nl_path.np_compcount); i++) {
cnt = snprintf(p, size, "/%s", fsl->nl_path.np_components[i]);
p += cnt;
size -= cnt;
}
}
/*
* NFS client connect socket upcall.
* (Used only during socket connect/search.)
*/
void
nfs_connect_upcall(socket_t so, void *arg, __unused int waitflag)
{
struct nfs_socket *nso = arg;
size_t rcvlen;
mbuf_t m;
int error = 0, recv = 1;
if (nso->nso_flags & NSO_CONNECTING) {
NFS_SOCK_DBG("nfs connect - socket %p upcall - connecting\n", nso);
wakeup(nso->nso_wake);
return;
}
lck_mtx_lock(&nso->nso_lock);
if ((nso->nso_flags & (NSO_UPCALL|NSO_DISCONNECTING|NSO_DEAD)) || !(nso->nso_flags & NSO_PINGING)) {
NFS_SOCK_DBG("nfs connect - socket %p upcall - nevermind\n", nso);
lck_mtx_unlock(&nso->nso_lock);
return;
}
NFS_SOCK_DBG("nfs connect - socket %p upcall\n", nso);
nso->nso_flags |= NSO_UPCALL;
/* loop while we make error-free progress */
while (!error && recv) {
/* make sure we're still interested in this socket */
if (nso->nso_flags & (NSO_DISCONNECTING|NSO_DEAD))
break;
lck_mtx_unlock(&nso->nso_lock);
m = NULL;
if (nso->nso_sotype == SOCK_STREAM) {
error = nfs_rpc_record_read(so, &nso->nso_rrs, MSG_DONTWAIT, &recv, &m);
} else {
rcvlen = 1000000;
error = sock_receivembuf(so, NULL, &m, MSG_DONTWAIT, &rcvlen);
recv = m ? 1 : 0;
}
lck_mtx_lock(&nso->nso_lock);
if (m) {
/* match response with request */
struct nfsm_chain nmrep;
uint32_t reply = 0, rxid = 0, verf_type, verf_len;
uint32_t reply_status, rejected_status, accepted_status;
nfsm_chain_dissect_init(error, &nmrep, m);
nfsm_chain_get_32(error, &nmrep, rxid);
nfsm_chain_get_32(error, &nmrep, reply);
if (!error && ((reply != RPC_REPLY) || (rxid != nso->nso_pingxid)))
error = EBADRPC;
nfsm_chain_get_32(error, &nmrep, reply_status);
if (!error && (reply_status == RPC_MSGDENIED)) {
nfsm_chain_get_32(error, &nmrep, rejected_status);
if (!error)
error = (rejected_status == RPC_MISMATCH) ? ERPCMISMATCH : EACCES;
}
nfsm_chain_get_32(error, &nmrep, verf_type); /* verifier flavor */
nfsm_chain_get_32(error, &nmrep, verf_len); /* verifier length */
nfsmout_if(error);
if (verf_len)
nfsm_chain_adv(error, &nmrep, nfsm_rndup(verf_len));
nfsm_chain_get_32(error, &nmrep, accepted_status);
nfsmout_if(error);
if ((accepted_status == RPC_PROGMISMATCH) && !nso->nso_version) {
uint32_t minvers, maxvers;
nfsm_chain_get_32(error, &nmrep, minvers);
nfsm_chain_get_32(error, &nmrep, maxvers);
nfsmout_if(error);
if (nso->nso_protocol == PMAPPROG) {
if ((minvers > RPCBVERS4) || (maxvers < PMAPVERS))
error = EPROGMISMATCH;
else if ((nso->nso_saddr->sa_family == AF_INET) &&
(PMAPVERS >= minvers) && (PMAPVERS <= maxvers))
nso->nso_version = PMAPVERS;
else if (nso->nso_saddr->sa_family == AF_INET6) {
if ((RPCBVERS4 >= minvers) && (RPCBVERS4 <= maxvers))
nso->nso_version = RPCBVERS4;
else if ((RPCBVERS3 >= minvers) && (RPCBVERS3 <= maxvers))
nso->nso_version = RPCBVERS3;
}
} else if (nso->nso_protocol == NFS_PROG) {
int vers;
/*
* N.B. Both portmapper and rpcbind V3 are happy to return
* addresses for other versions than the one you ask (getport or
* getaddr) and thus we may have fallen to this code path. So if
* we get a version that we support, use highest supported
* version. This assumes that the server supports all versions
* between minvers and maxvers. Note for IPv6 we will try and
* use rpcbind V4 which has getversaddr and we should not get
* here if that was successful.
*/
for (vers = nso->nso_nfs_max_vers; vers >= (int)nso->nso_nfs_min_vers; vers--) {
if (vers >= (int)minvers && vers <= (int)maxvers)
break;
}
nso->nso_version = (vers < (int)nso->nso_nfs_min_vers) ? 0 : vers;
}
if (!error && nso->nso_version)
accepted_status = RPC_SUCCESS;
}
if (!error) {
switch (accepted_status) {
case RPC_SUCCESS:
error = 0;
break;
case RPC_PROGUNAVAIL:
error = EPROGUNAVAIL;
break;
case RPC_PROGMISMATCH:
error = EPROGMISMATCH;
break;
case RPC_PROCUNAVAIL:
error = EPROCUNAVAIL;
break;
case RPC_GARBAGE:
error = EBADRPC;
break;
case RPC_SYSTEM_ERR:
default:
error = EIO;
break;
}
}
nfsmout:
nso->nso_flags &= ~NSO_PINGING;
if (error) {
nso->nso_error = error;
nso->nso_flags |= NSO_DEAD;
} else {
nso->nso_flags |= NSO_VERIFIED;
}
mbuf_freem(m);
/* wake up search thread */
wakeup(nso->nso_wake);
break;
}
}
nso->nso_flags &= ~NSO_UPCALL;
if ((error != EWOULDBLOCK) && (error || !recv)) {
/* problems with the socket... */
nso->nso_error = error ? error : EPIPE;
nso->nso_flags |= NSO_DEAD;
wakeup(nso->nso_wake);
}
if (nso->nso_flags & NSO_DISCONNECTING)
wakeup(&nso->nso_flags);
lck_mtx_unlock(&nso->nso_lock);
}
/*
* Create/initialize an nfs_socket structure.
*/
int
nfs_socket_create(
struct nfsmount *nmp,
struct sockaddr *sa,
int sotype,
in_port_t port,
uint32_t protocol,
uint32_t vers,
int resvport,
struct nfs_socket **nsop)
{
struct nfs_socket *nso;
struct timeval now;
int error;
#ifdef NFS_SOCKET_DEBUGGING
char naddr[MAX_IPv6_STR_LEN];
void *sinaddr;
if (sa->sa_family == AF_INET)
sinaddr = &((struct sockaddr_in*)sa)->sin_addr;
else
sinaddr = &((struct sockaddr_in6*)sa)->sin6_addr;
if (inet_ntop(sa->sa_family, sinaddr, naddr, sizeof(naddr)) != naddr)
strlcpy(naddr, "<unknown>", sizeof(naddr));
#else
char naddr[1] = { 0 };
#endif
*nsop = NULL;
/* Create the socket. */
MALLOC(nso, struct nfs_socket *, sizeof(struct nfs_socket), M_TEMP, M_WAITOK|M_ZERO);
if (nso)
MALLOC(nso->nso_saddr, struct sockaddr *, sa->sa_len, M_SONAME, M_WAITOK|M_ZERO);
if (!nso || !nso->nso_saddr) {
if (nso)
FREE(nso, M_TEMP);
return (ENOMEM);
}
lck_mtx_init(&nso->nso_lock, nfs_request_grp, LCK_ATTR_NULL);
nso->nso_sotype = sotype;
if (nso->nso_sotype == SOCK_STREAM)
nfs_rpc_record_state_init(&nso->nso_rrs);
microuptime(&now);
nso->nso_timestamp = now.tv_sec;
bcopy(sa, nso->nso_saddr, sa->sa_len);
if (sa->sa_family == AF_INET)
((struct sockaddr_in*)nso->nso_saddr)->sin_port = htons(port);
else if (sa->sa_family == AF_INET6)
((struct sockaddr_in6*)nso->nso_saddr)->sin6_port = htons(port);
nso->nso_protocol = protocol;
nso->nso_version = vers;
nso->nso_nfs_min_vers = PVER2MAJOR(nmp->nm_min_vers);
nso->nso_nfs_max_vers = PVER2MAJOR(nmp->nm_max_vers);
error = sock_socket(sa->sa_family, nso->nso_sotype, 0, NULL, NULL, &nso->nso_so);
/* Some servers require that the client port be a reserved port number. */
if (!error && resvport && ((sa->sa_family == AF_INET) || (sa->sa_family == AF_INET6))) {
struct sockaddr_storage ss;
int level = (sa->sa_family == AF_INET) ? IPPROTO_IP : IPPROTO_IPV6;
int optname = (sa->sa_family == AF_INET) ? IP_PORTRANGE : IPV6_PORTRANGE;
int portrange = IP_PORTRANGE_LOW;
error = sock_setsockopt(nso->nso_so, level, optname, &portrange, sizeof(portrange));
if (!error) { /* bind now to check for failure */
ss.ss_len = sa->sa_len;
ss.ss_family = sa->sa_family;
if (ss.ss_family == AF_INET) {
((struct sockaddr_in*)&ss)->sin_addr.s_addr = INADDR_ANY;
((struct sockaddr_in*)&ss)->sin_port = htons(0);
} else if (ss.ss_family == AF_INET6) {
((struct sockaddr_in6*)&ss)->sin6_addr = in6addr_any;
((struct sockaddr_in6*)&ss)->sin6_port = htons(0);
} else {
error = EINVAL;
}
if (!error)
error = sock_bind(nso->nso_so, (struct sockaddr*)&ss);
}
}
if (error) {
NFS_SOCK_DBG("nfs connect %s error %d creating socket %p %s type %d%s port %d prot %d %d\n",
vfs_statfs(nmp->nm_mountp)->f_mntfromname, error, nso, naddr, sotype,
resvport ? "r" : "", port, protocol, vers);
nfs_socket_destroy(nso);
} else {
NFS_SOCK_DBG("nfs connect %s created socket %p %s type %d%s port %d prot %d %d\n",
vfs_statfs(nmp->nm_mountp)->f_mntfromname, nso, naddr,
sotype, resvport ? "r" : "", port, protocol, vers);
*nsop = nso;
}
return (error);
}
/*
* Destroy an nfs_socket structure.
*/
void
nfs_socket_destroy(struct nfs_socket *nso)
{
struct timespec ts = { 4, 0 };
lck_mtx_lock(&nso->nso_lock);
nso->nso_flags |= NSO_DISCONNECTING;
if (nso->nso_flags & NSO_UPCALL) /* give upcall a chance to complete */
msleep(&nso->nso_flags, &nso->nso_lock, PZERO-1, "nfswaitupcall", &ts);
lck_mtx_unlock(&nso->nso_lock);
sock_shutdown(nso->nso_so, SHUT_RDWR);
sock_close(nso->nso_so);
if (nso->nso_sotype == SOCK_STREAM)
nfs_rpc_record_state_cleanup(&nso->nso_rrs);
lck_mtx_destroy(&nso->nso_lock, nfs_request_grp);
if (nso->nso_saddr)
FREE(nso->nso_saddr, M_SONAME);
if (nso->nso_saddr2)
FREE(nso->nso_saddr2, M_SONAME);
NFS_SOCK_DBG("nfs connect - socket %p destroyed\n", nso);
FREE(nso, M_TEMP);
}
/*
* Set common socket options on an nfs_socket.
*/
void
nfs_socket_options(struct nfsmount *nmp, struct nfs_socket *nso)
{
/*
* Set socket send/receive timeouts
* - Receive timeout shouldn't matter because most receives are performed
* in the socket upcall non-blocking.
* - Send timeout should allow us to react to a blocked socket.
* Soft mounts will want to abort sooner.
*/
struct timeval timeo;
int on = 1, proto;
timeo.tv_usec = 0;
timeo.tv_sec = (NMFLAG(nmp, SOFT) || nfs_can_squish(nmp)) ? 5 : 60;
sock_setsockopt(nso->nso_so, SOL_SOCKET, SO_RCVTIMEO, &timeo, sizeof(timeo));
sock_setsockopt(nso->nso_so, SOL_SOCKET, SO_SNDTIMEO, &timeo, sizeof(timeo));
if (nso->nso_sotype == SOCK_STREAM) {
/* Assume that SOCK_STREAM always requires a connection */
sock_setsockopt(nso->nso_so, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof(on));
/* set nodelay for TCP */
sock_gettype(nso->nso_so, NULL, NULL, &proto);
if (proto == IPPROTO_TCP)
sock_setsockopt(nso->nso_so, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on));
}
if (nso->nso_sotype == SOCK_DGRAM) { /* set socket buffer sizes for UDP */
int reserve = NFS_UDPSOCKBUF;
sock_setsockopt(nso->nso_so, SOL_SOCKET, SO_SNDBUF, &reserve, sizeof(reserve));
sock_setsockopt(nso->nso_so, SOL_SOCKET, SO_RCVBUF, &reserve, sizeof(reserve));
}
/* set SO_NOADDRERR to detect network changes ASAP */
sock_setsockopt(nso->nso_so, SOL_SOCKET, SO_NOADDRERR, &on, sizeof(on));
/* just playin' it safe with upcalls */
sock_setsockopt(nso->nso_so, SOL_SOCKET, SO_UPCALLCLOSEWAIT, &on, sizeof(on));
/* socket should be interruptible if the mount is */
if (!NMFLAG(nmp, INTR))
sock_nointerrupt(nso->nso_so, 1);
}
/*
* Release resources held in an nfs_socket_search.
*/
void
nfs_socket_search_cleanup(struct nfs_socket_search *nss)
{
struct nfs_socket *nso, *nsonext;
TAILQ_FOREACH_SAFE(nso, &nss->nss_socklist, nso_link, nsonext) {
TAILQ_REMOVE(&nss->nss_socklist, nso, nso_link);
nss->nss_sockcnt--;
nfs_socket_destroy(nso);
}
if (nss->nss_sock) {
nfs_socket_destroy(nss->nss_sock);
nss->nss_sock = NULL;
}
}
/*
* Prefer returning certain errors over others.
* This function returns a ranking of the given error.
*/
int
nfs_connect_error_class(int error)
{
switch (error) {
case 0:
return (0);
case ETIMEDOUT:
case EAGAIN:
return (1);
case EPIPE:
case EADDRNOTAVAIL:
case ENETDOWN:
case ENETUNREACH:
case ENETRESET:
case ECONNABORTED:
case ECONNRESET:
case EISCONN:
case ENOTCONN:
case ESHUTDOWN:
case ECONNREFUSED:
case EHOSTDOWN:
case EHOSTUNREACH:
return (2);
case ERPCMISMATCH:
case EPROCUNAVAIL:
case EPROGMISMATCH:
case EPROGUNAVAIL:
return (3);
case EBADRPC:
return (4);
default:
return (5);
}
}
/*
* Make sure a socket search returns the best error.
*/
void
nfs_socket_search_update_error(struct nfs_socket_search *nss, int error)
{
if (nfs_connect_error_class(error) >= nfs_connect_error_class(nss->nss_error))
nss->nss_error = error;
}
/* nfs_connect_search_new_socket:
* Given a socket search structure for an nfs mount try to find a new socket from the set of addresses specified
* by nss.
*
* nss_last is set to -1 at initialization to indicate the first time. Its set to -2 if address was found but
* could not be used or if a socket timed out.
*/
int
nfs_connect_search_new_socket(struct nfsmount *nmp, struct nfs_socket_search *nss, struct timeval *now)
{
struct nfs_fs_location *fsl;
struct nfs_fs_server *fss;
struct sockaddr_storage ss;
struct nfs_socket *nso;
char *addrstr;
int error = 0;
NFS_SOCK_DBG("nfs connect %s nss_addrcnt = %d\n",
vfs_statfs(nmp->nm_mountp)->f_mntfromname, nss->nss_addrcnt);
/*
* while there are addresses and:
* we have no sockets or
* the last address failed and did not produce a socket (nss_last < 0) or
* Its been a while (2 seconds) and we have less than the max number of concurrent sockets to search (4)
* then attempt to create a socket with the current address.
*/
while (nss->nss_addrcnt > 0 && ((nss->nss_last < 0) || (nss->nss_sockcnt == 0) ||
((nss->nss_sockcnt < 4) && (now->tv_sec >= (nss->nss_last + 2))))) {
if (nmp->nm_sockflags & NMSOCK_UNMOUNT)
return (EINTR);
/* Can we convert the address to a sockaddr? */
fsl = nmp->nm_locations.nl_locations[nss->nss_nextloc.nli_loc];
fss = fsl->nl_servers[nss->nss_nextloc.nli_serv];
addrstr = fss->ns_addresses[nss->nss_nextloc.nli_addr];
if (!nfs_uaddr2sockaddr(addrstr, (struct sockaddr*)&ss)) {
nfs_location_next(&nmp->nm_locations, &nss->nss_nextloc);
nss->nss_addrcnt -= 1;
nss->nss_last = -2;
continue;
}
/* Check that socket family is acceptable. */
if (nmp->nm_sofamily && (ss.ss_family != nmp->nm_sofamily)) {
nfs_location_next(&nmp->nm_locations, &nss->nss_nextloc);
nss->nss_addrcnt -= 1;
nss->nss_last = -2;
continue;
}
/* Create the socket. */
error = nfs_socket_create(nmp, (struct sockaddr*)&ss, nss->nss_sotype,
nss->nss_port, nss->nss_protocol, nss->nss_version,
((nss->nss_protocol == NFS_PROG) && NMFLAG(nmp, RESVPORT)), &nso);
if (error)
return (error);
nso->nso_location = nss->nss_nextloc;
nso->nso_wake = nss;
error = sock_setupcall(nso->nso_so, nfs_connect_upcall, nso);
if (error) {
lck_mtx_lock(&nso->nso_lock);
nso->nso_error = error;
nso->nso_flags |= NSO_DEAD;
lck_mtx_unlock(&nso->nso_lock);
}
TAILQ_INSERT_TAIL(&nss->nss_socklist, nso, nso_link);
nss->nss_sockcnt++;
nfs_location_next(&nmp->nm_locations, &nss->nss_nextloc);
nss->nss_addrcnt -= 1;
nss->nss_last = now->tv_sec;
}
if (nss->nss_addrcnt == 0 && nss->nss_last < 0)
nss->nss_last = now->tv_sec;
return (error);
}
/*
* nfs_connect_search_socket_connect: Connect an nfs socket nso for nfsmount nmp.
* If successful set the socket options for the socket as require from the mount.
*
* Assumes: nso->nso_lock is held on entry and return.
*/
int
nfs_connect_search_socket_connect(struct nfsmount *nmp, struct nfs_socket *nso, int verbose)
{
int error;
if ((nso->nso_sotype != SOCK_STREAM) && NMFLAG(nmp, NOCONNECT)) {
/* no connection needed, just say it's already connected */
NFS_SOCK_DBG("nfs connect %s UDP socket %p noconnect\n",
vfs_statfs(nmp->nm_mountp)->f_mntfromname, nso);
nso->nso_flags |= NSO_CONNECTED;
nfs_socket_options(nmp, nso);
return (1); /* Socket is connected and setup */
} else if (!(nso->nso_flags & NSO_CONNECTING)) {
/* initiate the connection */
nso->nso_flags |= NSO_CONNECTING;
lck_mtx_unlock(&nso->nso_lock);
NFS_SOCK_DBG("nfs connect %s connecting socket %p\n",
vfs_statfs(nmp->nm_mountp)->f_mntfromname, nso);
error = sock_connect(nso->nso_so, nso->nso_saddr, MSG_DONTWAIT);
lck_mtx_lock(&nso->nso_lock);
if (error && (error != EINPROGRESS)) {
nso->nso_error = error;
nso->nso_flags |= NSO_DEAD;
return (0);
}
}
if (nso->nso_flags & NSO_CONNECTING) {
/* check the connection */
if (sock_isconnected(nso->nso_so)) {
NFS_SOCK_DBG("nfs connect %s socket %p is connected\n",
vfs_statfs(nmp->nm_mountp)->f_mntfromname, nso);
nso->nso_flags &= ~NSO_CONNECTING;
nso->nso_flags |= NSO_CONNECTED;
nfs_socket_options(nmp, nso);
return (1); /* Socket is connected and setup */
} else {
int optlen = sizeof(error);
error = 0;
sock_getsockopt(nso->nso_so, SOL_SOCKET, SO_ERROR, &error, &optlen);
if (error) { /* we got an error on the socket */
NFS_SOCK_DBG("nfs connect %s socket %p connection error %d\n",
vfs_statfs(nmp->nm_mountp)->f_mntfromname, nso, error);
if (verbose)
printf("nfs connect socket error %d for %s\n",
error, vfs_statfs(nmp->nm_mountp)->f_mntfromname);
nso->nso_error = error;
nso->nso_flags |= NSO_DEAD;
return (0);
}
}
}
return (0); /* Waiting to be connected */
}
/*
* nfs_connect_search_ping: Send a null proc on the nso socket.
*/
int
nfs_connect_search_ping(struct nfsmount *nmp, struct nfs_socket *nso, struct timeval *now)
{
/* initiate a NULL RPC request */
uint64_t xid = nso->nso_pingxid;
mbuf_t m, mreq = NULL;
struct msghdr msg;
size_t reqlen, sentlen;
uint32_t vers = nso->nso_version;
int error;
if (!vers) {
if (nso->nso_protocol == PMAPPROG)
vers = (nso->nso_saddr->sa_family == AF_INET) ? PMAPVERS : RPCBVERS4;
else if (nso->nso_protocol == NFS_PROG)
vers = PVER2MAJOR(nmp->nm_max_vers);
}
lck_mtx_unlock(&nso->nso_lock);
error = nfsm_rpchead2(nmp, nso->nso_sotype, nso->nso_protocol, vers, 0, RPCAUTH_SYS,
vfs_context_ucred(vfs_context_kernel()), NULL, NULL, &xid, &mreq);
lck_mtx_lock(&nso->nso_lock);
if (!error) {
nso->nso_flags |= NSO_PINGING;
nso->nso_pingxid = R_XID32(xid);
nso->nso_reqtimestamp = now->tv_sec;
bzero(&msg, sizeof(msg));
if ((nso->nso_sotype != SOCK_STREAM) && !sock_isconnected(nso->nso_so)) {
msg.msg_name = nso->nso_saddr;
msg.msg_namelen = nso->nso_saddr->sa_len;
}
for (reqlen=0, m=mreq; m; m = mbuf_next(m))
reqlen += mbuf_len(m);
lck_mtx_unlock(&nso->nso_lock);
error = sock_sendmbuf(nso->nso_so, &msg, mreq, 0, &sentlen);
NFS_SOCK_DBG("nfs connect %s verifying socket %p send rv %d\n",
vfs_statfs(nmp->nm_mountp)->f_mntfromname, nso, error);
lck_mtx_lock(&nso->nso_lock);
if (!error && (sentlen != reqlen))
error = ETIMEDOUT;
}
if (error) {
nso->nso_error = error;
nso->nso_flags |= NSO_DEAD;
return (0);
}
return (1);
}
/*
* nfs_connect_search_socket_found: Take the found socket of the socket search list and assign it to the searched socket.
* Set the nfs socket protocol and version if needed.
*/
void
nfs_connect_search_socket_found(struct nfsmount *nmp, struct nfs_socket_search *nss, struct nfs_socket *nso)
{
NFS_SOCK_DBG("nfs connect %s socket %p verified\n",
vfs_statfs(nmp->nm_mountp)->f_mntfromname, nso);
if (!nso->nso_version) {
/* If the version isn't set, the default must have worked. */
if (nso->nso_protocol == PMAPPROG)
nso->nso_version = (nso->nso_saddr->sa_family == AF_INET) ? PMAPVERS : RPCBVERS4;
if (nso->nso_protocol == NFS_PROG)
nso->nso_version = PVER2MAJOR(nmp->nm_max_vers);
}
TAILQ_REMOVE(&nss->nss_socklist, nso, nso_link);
nss->nss_sockcnt--;
nss->nss_sock = nso;
}
/*
* nfs_connect_search_socket_reap: For each socket in the search list mark any timed out socket as dead and remove from
* the list. Dead socket are then destroyed.
*/
void
nfs_connect_search_socket_reap(struct nfsmount *nmp __unused, struct nfs_socket_search *nss, struct timeval *now)
{
struct nfs_socket *nso, *nsonext;
TAILQ_FOREACH_SAFE(nso, &nss->nss_socklist, nso_link, nsonext) {
lck_mtx_lock(&nso->nso_lock);
if (now->tv_sec >= (nso->nso_timestamp + nss->nss_timeo)) {
/* took too long */
NFS_SOCK_DBG("nfs connect %s socket %p timed out\n",
vfs_statfs(nmp->nm_mountp)->f_mntfromname, nso);
nso->nso_error = ETIMEDOUT;
nso->nso_flags |= NSO_DEAD;
}
if (!(nso->nso_flags & NSO_DEAD)) {
lck_mtx_unlock(&nso->nso_lock);
continue;
}
lck_mtx_unlock(&nso->nso_lock);
NFS_SOCK_DBG("nfs connect %s reaping socket %p %d\n",
vfs_statfs(nmp->nm_mountp)->f_mntfromname, nso, nso->nso_error);
nfs_socket_search_update_error(nss, nso->nso_error);
TAILQ_REMOVE(&nss->nss_socklist, nso, nso_link);
nss->nss_sockcnt--;
nfs_socket_destroy(nso);
/* If there are more sockets to try, force the starting of another socket */
if (nss->nss_addrcnt > 0)
nss->nss_last = -2;
}
}
/*
* nfs_connect_search_check: Check on the status of search and wait for replies if needed.
*/
int
nfs_connect_search_check(struct nfsmount *nmp, struct nfs_socket_search *nss, struct timeval *now)
{
int error;
/* log a warning if connect is taking a while */
if (((now->tv_sec - nss->nss_timestamp) >= 8) && ((nss->nss_flags & (NSS_VERBOSE|NSS_WARNED)) == NSS_VERBOSE)) {
printf("nfs_connect: socket connect taking a while for %s\n", vfs_statfs(nmp->nm_mountp)->f_mntfromname);
nss->nss_flags |= NSS_WARNED;
}
if (nmp->nm_sockflags & NMSOCK_UNMOUNT)
return (EINTR);
if ((error = nfs_sigintr(nmp, NULL, current_thread(), 0)))
return (error);
/* If we were succesfull at sending a ping, wait up to a second for a reply */
if (nss->nss_last >= 0)
tsleep(nss, PSOCK, "nfs_connect_search_wait", hz);
return (0);
}
/*
* Continue the socket search until we have something to report.
*/
int
nfs_connect_search_loop(struct nfsmount *nmp, struct nfs_socket_search *nss)
{
struct nfs_socket *nso;
struct timeval now;
int error;
int verbose = (nss->nss_flags & NSS_VERBOSE);
loop:
microuptime(&now);
NFS_SOCK_DBG("nfs connect %s search %ld\n", vfs_statfs(nmp->nm_mountp)->f_mntfromname, now.tv_sec);
/* add a new socket to the socket list if needed and available */
error = nfs_connect_search_new_socket(nmp, nss, &now);
if (error) {
NFS_SOCK_DBG("nfs connect returned %d\n", error);
return (error);
}
/* check each active socket on the list and try to push it along */
TAILQ_FOREACH(nso, &nss->nss_socklist, nso_link) {
lck_mtx_lock(&nso->nso_lock);
/* If not connected connect it */