1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1989, 1993, 1994 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Rick Macklem at The University of Guelph. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 #ifndef lint 36 static const char copyright[] = 37 "@(#) Copyright (c) 1989, 1993, 1994\n\ 38 The Regents of the University of California. All rights reserved.\n"; 39 #endif /* not lint */ 40 41 #ifndef lint 42 #if 0 43 static char sccsid[] = "@(#)nfsd.c 8.9 (Berkeley) 3/29/95"; 44 #endif 45 static const char rcsid[] = 46 "$FreeBSD$"; 47 #endif /* not lint */ 48 49 #include <sys/param.h> 50 #include <sys/syslog.h> 51 #include <sys/wait.h> 52 #include <sys/mount.h> 53 #include <sys/fcntl.h> 54 #include <sys/linker.h> 55 #include <sys/module.h> 56 #include <sys/types.h> 57 #include <sys/stat.h> 58 #include <sys/sysctl.h> 59 #include <sys/ucred.h> 60 61 #include <rpc/rpc.h> 62 #include <rpc/pmap_clnt.h> 63 #include <rpcsvc/nfs_prot.h> 64 65 #include <netdb.h> 66 #include <arpa/inet.h> 67 #include <nfs/nfssvc.h> 68 69 #include <fs/nfs/nfsproto.h> 70 #include <fs/nfs/nfskpiport.h> 71 #include <fs/nfs/nfs.h> 72 73 #include <err.h> 74 #include <errno.h> 75 #include <signal.h> 76 #include <stdio.h> 77 #include <stdlib.h> 78 #include <string.h> 79 #include <unistd.h> 80 #include <sysexits.h> 81 82 #include <getopt.h> 83 84 static int debug = 0; 85 86 #define NFSD_STABLERESTART "/var/db/nfs-stablerestart" 87 #define NFSD_STABLEBACKUP "/var/db/nfs-stablerestart.bak" 88 #define MAXNFSDCNT 256 89 #define DEFNFSDCNT 4 90 #define NFS_VER2 2 91 #define NFS_VER3 3 92 #define NFS_VER4 4 93 static pid_t children[MAXNFSDCNT]; /* PIDs of children */ 94 static pid_t masterpid; /* PID of master/parent */ 95 static int nfsdcnt; /* number of children */ 96 static int nfsdcnt_set; 97 static int minthreads; 98 static int maxthreads; 99 static int nfssvc_nfsd; /* Set to correct NFSSVC_xxx flag */ 100 static int stablefd = -1; /* Fd for the stable restart file */ 101 static int backupfd; /* Fd for the backup stable restart file */ 102 static const char *getopt_shortopts; 103 static const char *getopt_usage; 104 105 static int minthreads_set; 106 static int maxthreads_set; 107 108 static struct option longopts[] = { 109 { "debug", no_argument, &debug, 1 }, 110 { "minthreads", required_argument, &minthreads_set, 1 }, 111 { "maxthreads", required_argument, &maxthreads_set, 1 }, 112 { "pnfs", required_argument, NULL, 'p' }, 113 { "mirror", required_argument, NULL, 'm' }, 114 { NULL, 0, NULL, 0} 115 }; 116 117 static void cleanup(int); 118 static void child_cleanup(int); 119 static void killchildren(void); 120 static void nfsd_exit(int); 121 static void nonfs(int); 122 static void reapchild(int); 123 static int setbindhost(struct addrinfo **ia, const char *bindhost, 124 struct addrinfo hints); 125 static void start_server(int, struct nfsd_nfsd_args *); 126 static void unregistration(void); 127 static void usage(void); 128 static void open_stable(int *, int *); 129 static void copy_stable(int, int); 130 static void backup_stable(int); 131 static void set_nfsdcnt(int); 132 static void parse_dsserver(const char *, struct nfsd_nfsd_args *); 133 134 /* 135 * Nfs server daemon mostly just a user context for nfssvc() 136 * 137 * 1 - do file descriptor and signal cleanup 138 * 2 - fork the nfsd(s) 139 * 3 - create server socket(s) 140 * 4 - register socket with rpcbind 141 * 142 * For connectionless protocols, just pass the socket into the kernel via. 143 * nfssvc(). 144 * For connection based sockets, loop doing accepts. When you get a new 145 * socket from accept, pass the msgsock into the kernel via. nfssvc(). 146 * The arguments are: 147 * -r - reregister with rpcbind 148 * -d - unregister with rpcbind 149 * -t - support tcp nfs clients 150 * -u - support udp nfs clients 151 * -e - forces it to run a server that supports nfsv4 152 * -p - enable a pNFS service 153 * -m - set the mirroring level for a pNFS service 154 * followed by "n" which is the number of nfsds' to fork off 155 */ 156 int 157 main(int argc, char **argv) 158 { 159 struct nfsd_addsock_args addsockargs; 160 struct addrinfo *ai_udp, *ai_tcp, *ai_udp6, *ai_tcp6, hints; 161 struct netconfig *nconf_udp, *nconf_tcp, *nconf_udp6, *nconf_tcp6; 162 struct netbuf nb_udp, nb_tcp, nb_udp6, nb_tcp6; 163 struct sockaddr_storage peer; 164 fd_set ready, sockbits; 165 int ch, connect_type_cnt, i, maxsock, msgsock; 166 socklen_t len; 167 int on = 1, unregister, reregister, sock; 168 int tcp6sock, ip6flag, tcpflag, tcpsock; 169 int udpflag, ecode, error, s; 170 int bindhostc, bindanyflag, rpcbreg, rpcbregcnt; 171 int nfssvc_addsock; 172 int longindex = 0; 173 int nfs_minvers = NFS_VER2; 174 size_t nfs_minvers_size; 175 const char *lopt; 176 char **bindhost = NULL; 177 pid_t pid; 178 struct nfsd_nfsd_args nfsdargs; 179 180 nfsdargs.mirrorcnt = 1; 181 nfsdargs.addr = NULL; 182 nfsdargs.addrlen = 0; 183 nfsdcnt = DEFNFSDCNT; 184 unregister = reregister = tcpflag = maxsock = 0; 185 bindanyflag = udpflag = connect_type_cnt = bindhostc = 0; 186 getopt_shortopts = "ah:n:rdtuep:m:"; 187 getopt_usage = 188 "usage:\n" 189 " nfsd [-ardtue] [-h bindip]\n" 190 " [-n numservers] [--minthreads #] [--maxthreads #]\n" 191 " [-p/--pnfs dsserver0:/dsserver0-mounted-on-dir,...," 192 "dsserverN:/dsserverN-mounted-on-dir] [-m mirrorlevel]\n"; 193 while ((ch = getopt_long(argc, argv, getopt_shortopts, longopts, 194 &longindex)) != -1) 195 switch (ch) { 196 case 'a': 197 bindanyflag = 1; 198 break; 199 case 'n': 200 set_nfsdcnt(atoi(optarg)); 201 break; 202 case 'h': 203 bindhostc++; 204 bindhost = realloc(bindhost,sizeof(char *)*bindhostc); 205 if (bindhost == NULL) 206 errx(1, "Out of memory"); 207 bindhost[bindhostc-1] = strdup(optarg); 208 if (bindhost[bindhostc-1] == NULL) 209 errx(1, "Out of memory"); 210 break; 211 case 'r': 212 reregister = 1; 213 break; 214 case 'd': 215 unregister = 1; 216 break; 217 case 't': 218 tcpflag = 1; 219 break; 220 case 'u': 221 udpflag = 1; 222 break; 223 case 'e': 224 /* now a no-op, since this is the default */ 225 break; 226 case 'p': 227 /* Parse out the DS server host names and mount pts. */ 228 parse_dsserver(optarg, &nfsdargs); 229 break; 230 case 'm': 231 /* Set the mirror level for a pNFS service. */ 232 i = atoi(optarg); 233 if (i < 2 || i > NFSDEV_MAXMIRRORS) 234 errx(1, "Mirror level out of range 2<-->%d", 235 NFSDEV_MAXMIRRORS); 236 nfsdargs.mirrorcnt = i; 237 break; 238 case 0: 239 lopt = longopts[longindex].name; 240 if (!strcmp(lopt, "minthreads")) { 241 minthreads = atoi(optarg); 242 } else if (!strcmp(lopt, "maxthreads")) { 243 maxthreads = atoi(optarg); 244 } 245 break; 246 default: 247 case '?': 248 usage(); 249 } 250 if (!tcpflag && !udpflag) 251 udpflag = 1; 252 argv += optind; 253 argc -= optind; 254 if (minthreads_set && maxthreads_set && minthreads > maxthreads) 255 errx(EX_USAGE, 256 "error: minthreads(%d) can't be greater than " 257 "maxthreads(%d)", minthreads, maxthreads); 258 259 /* 260 * XXX 261 * Backward compatibility, trailing number is the count of daemons. 262 */ 263 if (argc > 1) 264 usage(); 265 if (argc == 1) 266 set_nfsdcnt(atoi(argv[0])); 267 268 /* 269 * Unless the "-o" option was specified, try and run "nfsd". 270 * If "-o" was specified, try and run "nfsserver". 271 */ 272 if (modfind("nfsd") < 0) { 273 /* Not present in kernel, try loading it */ 274 if (kldload("nfsd") < 0 || modfind("nfsd") < 0) 275 errx(1, "NFS server is not available"); 276 } 277 278 ip6flag = 1; 279 s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP); 280 if (s == -1) { 281 if (errno != EPROTONOSUPPORT && errno != EAFNOSUPPORT) 282 err(1, "socket"); 283 ip6flag = 0; 284 } else if (getnetconfigent("udp6") == NULL || 285 getnetconfigent("tcp6") == NULL) { 286 ip6flag = 0; 287 } 288 if (s != -1) 289 close(s); 290 291 if (bindhostc == 0 || bindanyflag) { 292 bindhostc++; 293 bindhost = realloc(bindhost,sizeof(char *)*bindhostc); 294 if (bindhost == NULL) 295 errx(1, "Out of memory"); 296 bindhost[bindhostc-1] = strdup("*"); 297 if (bindhost[bindhostc-1] == NULL) 298 errx(1, "Out of memory"); 299 } 300 301 nfs_minvers_size = sizeof(nfs_minvers); 302 error = sysctlbyname("vfs.nfsd.server_min_nfsvers", &nfs_minvers, 303 &nfs_minvers_size, NULL, 0); 304 if (error != 0 || nfs_minvers < NFS_VER2 || nfs_minvers > NFS_VER4) { 305 warnx("sysctlbyname(vfs.nfsd.server_min_nfsvers) failed," 306 " defaulting to NFSv2"); 307 nfs_minvers = NFS_VER2; 308 } 309 310 if (unregister) { 311 unregistration(); 312 exit (0); 313 } 314 if (reregister) { 315 if (udpflag) { 316 memset(&hints, 0, sizeof hints); 317 hints.ai_flags = AI_PASSIVE; 318 hints.ai_family = AF_INET; 319 hints.ai_socktype = SOCK_DGRAM; 320 hints.ai_protocol = IPPROTO_UDP; 321 ecode = getaddrinfo(NULL, "nfs", &hints, &ai_udp); 322 if (ecode != 0) 323 err(1, "getaddrinfo udp: %s", gai_strerror(ecode)); 324 nconf_udp = getnetconfigent("udp"); 325 if (nconf_udp == NULL) 326 err(1, "getnetconfigent udp failed"); 327 nb_udp.buf = ai_udp->ai_addr; 328 nb_udp.len = nb_udp.maxlen = ai_udp->ai_addrlen; 329 if (nfs_minvers == NFS_VER2) 330 if (!rpcb_set(NFS_PROGRAM, 2, nconf_udp, 331 &nb_udp)) 332 err(1, "rpcb_set udp failed"); 333 if (nfs_minvers <= NFS_VER3) 334 if (!rpcb_set(NFS_PROGRAM, 3, nconf_udp, 335 &nb_udp)) 336 err(1, "rpcb_set udp failed"); 337 freeaddrinfo(ai_udp); 338 } 339 if (udpflag && ip6flag) { 340 memset(&hints, 0, sizeof hints); 341 hints.ai_flags = AI_PASSIVE; 342 hints.ai_family = AF_INET6; 343 hints.ai_socktype = SOCK_DGRAM; 344 hints.ai_protocol = IPPROTO_UDP; 345 ecode = getaddrinfo(NULL, "nfs", &hints, &ai_udp6); 346 if (ecode != 0) 347 err(1, "getaddrinfo udp6: %s", gai_strerror(ecode)); 348 nconf_udp6 = getnetconfigent("udp6"); 349 if (nconf_udp6 == NULL) 350 err(1, "getnetconfigent udp6 failed"); 351 nb_udp6.buf = ai_udp6->ai_addr; 352 nb_udp6.len = nb_udp6.maxlen = ai_udp6->ai_addrlen; 353 if (nfs_minvers == NFS_VER2) 354 if (!rpcb_set(NFS_PROGRAM, 2, nconf_udp6, 355 &nb_udp6)) 356 err(1, "rpcb_set udp6 failed"); 357 if (nfs_minvers <= NFS_VER3) 358 if (!rpcb_set(NFS_PROGRAM, 3, nconf_udp6, 359 &nb_udp6)) 360 err(1, "rpcb_set udp6 failed"); 361 freeaddrinfo(ai_udp6); 362 } 363 if (tcpflag) { 364 memset(&hints, 0, sizeof hints); 365 hints.ai_flags = AI_PASSIVE; 366 hints.ai_family = AF_INET; 367 hints.ai_socktype = SOCK_STREAM; 368 hints.ai_protocol = IPPROTO_TCP; 369 ecode = getaddrinfo(NULL, "nfs", &hints, &ai_tcp); 370 if (ecode != 0) 371 err(1, "getaddrinfo tcp: %s", gai_strerror(ecode)); 372 nconf_tcp = getnetconfigent("tcp"); 373 if (nconf_tcp == NULL) 374 err(1, "getnetconfigent tcp failed"); 375 nb_tcp.buf = ai_tcp->ai_addr; 376 nb_tcp.len = nb_tcp.maxlen = ai_tcp->ai_addrlen; 377 if (nfs_minvers == NFS_VER2) 378 if (!rpcb_set(NFS_PROGRAM, 2, nconf_tcp, 379 &nb_tcp)) 380 err(1, "rpcb_set tcp failed"); 381 if (nfs_minvers <= NFS_VER3) 382 if (!rpcb_set(NFS_PROGRAM, 3, nconf_tcp, 383 &nb_tcp)) 384 err(1, "rpcb_set tcp failed"); 385 freeaddrinfo(ai_tcp); 386 } 387 if (tcpflag && ip6flag) { 388 memset(&hints, 0, sizeof hints); 389 hints.ai_flags = AI_PASSIVE; 390 hints.ai_family = AF_INET6; 391 hints.ai_socktype = SOCK_STREAM; 392 hints.ai_protocol = IPPROTO_TCP; 393 ecode = getaddrinfo(NULL, "nfs", &hints, &ai_tcp6); 394 if (ecode != 0) 395 err(1, "getaddrinfo tcp6: %s", gai_strerror(ecode)); 396 nconf_tcp6 = getnetconfigent("tcp6"); 397 if (nconf_tcp6 == NULL) 398 err(1, "getnetconfigent tcp6 failed"); 399 nb_tcp6.buf = ai_tcp6->ai_addr; 400 nb_tcp6.len = nb_tcp6.maxlen = ai_tcp6->ai_addrlen; 401 if (nfs_minvers == NFS_VER2) 402 if (!rpcb_set(NFS_PROGRAM, 2, nconf_tcp6, 403 &nb_tcp6)) 404 err(1, "rpcb_set tcp6 failed"); 405 if (nfs_minvers <= NFS_VER3) 406 if (!rpcb_set(NFS_PROGRAM, 3, nconf_tcp6, 407 &nb_tcp6)) 408 err(1, "rpcb_set tcp6 failed"); 409 freeaddrinfo(ai_tcp6); 410 } 411 exit (0); 412 } 413 if (debug == 0) { 414 daemon(0, 0); 415 (void)signal(SIGHUP, SIG_IGN); 416 (void)signal(SIGINT, SIG_IGN); 417 /* 418 * nfsd sits in the kernel most of the time. It needs 419 * to ignore SIGTERM/SIGQUIT in order to stay alive as long 420 * as possible during a shutdown, otherwise loopback 421 * mounts will not be able to unmount. 422 */ 423 (void)signal(SIGTERM, SIG_IGN); 424 (void)signal(SIGQUIT, SIG_IGN); 425 } 426 (void)signal(SIGSYS, nonfs); 427 (void)signal(SIGCHLD, reapchild); 428 (void)signal(SIGUSR2, backup_stable); 429 430 openlog("nfsd", LOG_PID | (debug ? LOG_PERROR : 0), LOG_DAEMON); 431 432 /* 433 * For V4, we open the stablerestart file and call nfssvc() 434 * to get it loaded. This is done before the daemons do the 435 * regular nfssvc() call to service NFS requests. 436 * (This way the file remains open until the last nfsd is killed 437 * off.) 438 * It and the backup copy will be created as empty files 439 * the first time this nfsd is started and should never be 440 * deleted/replaced if at all possible. It should live on a 441 * local, non-volatile storage device that does not do hardware 442 * level write-back caching. (See SCSI doc for more information 443 * on how to prevent write-back caching on SCSI disks.) 444 */ 445 open_stable(&stablefd, &backupfd); 446 if (stablefd < 0) { 447 syslog(LOG_ERR, "Can't open %s: %m\n", NFSD_STABLERESTART); 448 exit(1); 449 } 450 /* This system call will fail for old kernels, but that's ok. */ 451 nfssvc(NFSSVC_BACKUPSTABLE, NULL); 452 if (nfssvc(NFSSVC_STABLERESTART, (caddr_t)&stablefd) < 0) { 453 syslog(LOG_ERR, "Can't read stable storage file: %m\n"); 454 exit(1); 455 } 456 nfssvc_addsock = NFSSVC_NFSDADDSOCK; 457 nfssvc_nfsd = NFSSVC_NFSDNFSD | NFSSVC_NEWSTRUCT; 458 459 if (tcpflag) { 460 /* 461 * For TCP mode, we fork once to start the first 462 * kernel nfsd thread. The kernel will add more 463 * threads as needed. 464 */ 465 masterpid = getpid(); 466 pid = fork(); 467 if (pid == -1) { 468 syslog(LOG_ERR, "fork: %m"); 469 nfsd_exit(1); 470 } 471 if (pid) { 472 children[0] = pid; 473 } else { 474 (void)signal(SIGUSR1, child_cleanup); 475 setproctitle("server"); 476 start_server(0, &nfsdargs); 477 } 478 } 479 480 (void)signal(SIGUSR1, cleanup); 481 FD_ZERO(&sockbits); 482 483 rpcbregcnt = 0; 484 /* Set up the socket for udp and rpcb register it. */ 485 if (udpflag) { 486 rpcbreg = 0; 487 for (i = 0; i < bindhostc; i++) { 488 memset(&hints, 0, sizeof hints); 489 hints.ai_flags = AI_PASSIVE; 490 hints.ai_family = AF_INET; 491 hints.ai_socktype = SOCK_DGRAM; 492 hints.ai_protocol = IPPROTO_UDP; 493 if (setbindhost(&ai_udp, bindhost[i], hints) == 0) { 494 rpcbreg = 1; 495 rpcbregcnt++; 496 if ((sock = socket(ai_udp->ai_family, 497 ai_udp->ai_socktype, 498 ai_udp->ai_protocol)) < 0) { 499 syslog(LOG_ERR, 500 "can't create udp socket"); 501 nfsd_exit(1); 502 } 503 if (bind(sock, ai_udp->ai_addr, 504 ai_udp->ai_addrlen) < 0) { 505 syslog(LOG_ERR, 506 "can't bind udp addr %s: %m", 507 bindhost[i]); 508 nfsd_exit(1); 509 } 510 freeaddrinfo(ai_udp); 511 addsockargs.sock = sock; 512 addsockargs.name = NULL; 513 addsockargs.namelen = 0; 514 if (nfssvc(nfssvc_addsock, &addsockargs) < 0) { 515 syslog(LOG_ERR, "can't Add UDP socket"); 516 nfsd_exit(1); 517 } 518 (void)close(sock); 519 } 520 } 521 if (rpcbreg == 1) { 522 memset(&hints, 0, sizeof hints); 523 hints.ai_flags = AI_PASSIVE; 524 hints.ai_family = AF_INET; 525 hints.ai_socktype = SOCK_DGRAM; 526 hints.ai_protocol = IPPROTO_UDP; 527 ecode = getaddrinfo(NULL, "nfs", &hints, &ai_udp); 528 if (ecode != 0) { 529 syslog(LOG_ERR, "getaddrinfo udp: %s", 530 gai_strerror(ecode)); 531 nfsd_exit(1); 532 } 533 nconf_udp = getnetconfigent("udp"); 534 if (nconf_udp == NULL) 535 err(1, "getnetconfigent udp failed"); 536 nb_udp.buf = ai_udp->ai_addr; 537 nb_udp.len = nb_udp.maxlen = ai_udp->ai_addrlen; 538 if (nfs_minvers == NFS_VER2) 539 if (!rpcb_set(NFS_PROGRAM, 2, nconf_udp, 540 &nb_udp)) 541 err(1, "rpcb_set udp failed"); 542 if (nfs_minvers <= NFS_VER3) 543 if (!rpcb_set(NFS_PROGRAM, 3, nconf_udp, 544 &nb_udp)) 545 err(1, "rpcb_set udp failed"); 546 freeaddrinfo(ai_udp); 547 } 548 } 549 550 /* Set up the socket for udp6 and rpcb register it. */ 551 if (udpflag && ip6flag) { 552 rpcbreg = 0; 553 for (i = 0; i < bindhostc; i++) { 554 memset(&hints, 0, sizeof hints); 555 hints.ai_flags = AI_PASSIVE; 556 hints.ai_family = AF_INET6; 557 hints.ai_socktype = SOCK_DGRAM; 558 hints.ai_protocol = IPPROTO_UDP; 559 if (setbindhost(&ai_udp6, bindhost[i], hints) == 0) { 560 rpcbreg = 1; 561 rpcbregcnt++; 562 if ((sock = socket(ai_udp6->ai_family, 563 ai_udp6->ai_socktype, 564 ai_udp6->ai_protocol)) < 0) { 565 syslog(LOG_ERR, 566 "can't create udp6 socket"); 567 nfsd_exit(1); 568 } 569 if (setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, 570 &on, sizeof on) < 0) { 571 syslog(LOG_ERR, 572 "can't set v6-only binding for " 573 "udp6 socket: %m"); 574 nfsd_exit(1); 575 } 576 if (bind(sock, ai_udp6->ai_addr, 577 ai_udp6->ai_addrlen) < 0) { 578 syslog(LOG_ERR, 579 "can't bind udp6 addr %s: %m", 580 bindhost[i]); 581 nfsd_exit(1); 582 } 583 freeaddrinfo(ai_udp6); 584 addsockargs.sock = sock; 585 addsockargs.name = NULL; 586 addsockargs.namelen = 0; 587 if (nfssvc(nfssvc_addsock, &addsockargs) < 0) { 588 syslog(LOG_ERR, 589 "can't add UDP6 socket"); 590 nfsd_exit(1); 591 } 592 (void)close(sock); 593 } 594 } 595 if (rpcbreg == 1) { 596 memset(&hints, 0, sizeof hints); 597 hints.ai_flags = AI_PASSIVE; 598 hints.ai_family = AF_INET6; 599 hints.ai_socktype = SOCK_DGRAM; 600 hints.ai_protocol = IPPROTO_UDP; 601 ecode = getaddrinfo(NULL, "nfs", &hints, &ai_udp6); 602 if (ecode != 0) { 603 syslog(LOG_ERR, "getaddrinfo udp6: %s", 604 gai_strerror(ecode)); 605 nfsd_exit(1); 606 } 607 nconf_udp6 = getnetconfigent("udp6"); 608 if (nconf_udp6 == NULL) 609 err(1, "getnetconfigent udp6 failed"); 610 nb_udp6.buf = ai_udp6->ai_addr; 611 nb_udp6.len = nb_udp6.maxlen = ai_udp6->ai_addrlen; 612 if (nfs_minvers == NFS_VER2) 613 if (!rpcb_set(NFS_PROGRAM, 2, nconf_udp6, 614 &nb_udp6)) 615 err(1, 616 "rpcb_set udp6 failed"); 617 if (nfs_minvers <= NFS_VER3) 618 if (!rpcb_set(NFS_PROGRAM, 3, nconf_udp6, 619 &nb_udp6)) 620 err(1, 621 "rpcb_set udp6 failed"); 622 freeaddrinfo(ai_udp6); 623 } 624 } 625 626 /* Set up the socket for tcp and rpcb register it. */ 627 if (tcpflag) { 628 rpcbreg = 0; 629 for (i = 0; i < bindhostc; i++) { 630 memset(&hints, 0, sizeof hints); 631 hints.ai_flags = AI_PASSIVE; 632 hints.ai_family = AF_INET; 633 hints.ai_socktype = SOCK_STREAM; 634 hints.ai_protocol = IPPROTO_TCP; 635 if (setbindhost(&ai_tcp, bindhost[i], hints) == 0) { 636 rpcbreg = 1; 637 rpcbregcnt++; 638 if ((tcpsock = socket(AF_INET, SOCK_STREAM, 639 0)) < 0) { 640 syslog(LOG_ERR, 641 "can't create tcp socket"); 642 nfsd_exit(1); 643 } 644 if (setsockopt(tcpsock, SOL_SOCKET, 645 SO_REUSEADDR, 646 (char *)&on, sizeof(on)) < 0) 647 syslog(LOG_ERR, 648 "setsockopt SO_REUSEADDR: %m"); 649 if (bind(tcpsock, ai_tcp->ai_addr, 650 ai_tcp->ai_addrlen) < 0) { 651 syslog(LOG_ERR, 652 "can't bind tcp addr %s: %m", 653 bindhost[i]); 654 nfsd_exit(1); 655 } 656 if (listen(tcpsock, -1) < 0) { 657 syslog(LOG_ERR, "listen failed"); 658 nfsd_exit(1); 659 } 660 freeaddrinfo(ai_tcp); 661 FD_SET(tcpsock, &sockbits); 662 maxsock = tcpsock; 663 connect_type_cnt++; 664 } 665 } 666 if (rpcbreg == 1) { 667 memset(&hints, 0, sizeof hints); 668 hints.ai_flags = AI_PASSIVE; 669 hints.ai_family = AF_INET; 670 hints.ai_socktype = SOCK_STREAM; 671 hints.ai_protocol = IPPROTO_TCP; 672 ecode = getaddrinfo(NULL, "nfs", &hints, 673 &ai_tcp); 674 if (ecode != 0) { 675 syslog(LOG_ERR, "getaddrinfo tcp: %s", 676 gai_strerror(ecode)); 677 nfsd_exit(1); 678 } 679 nconf_tcp = getnetconfigent("tcp"); 680 if (nconf_tcp == NULL) 681 err(1, "getnetconfigent tcp failed"); 682 nb_tcp.buf = ai_tcp->ai_addr; 683 nb_tcp.len = nb_tcp.maxlen = ai_tcp->ai_addrlen; 684 if (nfs_minvers == NFS_VER2) 685 if (!rpcb_set(NFS_PROGRAM, 2, nconf_tcp, 686 &nb_tcp)) 687 err(1, "rpcb_set tcp failed"); 688 if (nfs_minvers <= NFS_VER3) 689 if (!rpcb_set(NFS_PROGRAM, 3, nconf_tcp, 690 &nb_tcp)) 691 err(1, "rpcb_set tcp failed"); 692 freeaddrinfo(ai_tcp); 693 } 694 } 695 696 /* Set up the socket for tcp6 and rpcb register it. */ 697 if (tcpflag && ip6flag) { 698 rpcbreg = 0; 699 for (i = 0; i < bindhostc; i++) { 700 memset(&hints, 0, sizeof hints); 701 hints.ai_flags = AI_PASSIVE; 702 hints.ai_family = AF_INET6; 703 hints.ai_socktype = SOCK_STREAM; 704 hints.ai_protocol = IPPROTO_TCP; 705 if (setbindhost(&ai_tcp6, bindhost[i], hints) == 0) { 706 rpcbreg = 1; 707 rpcbregcnt++; 708 if ((tcp6sock = socket(ai_tcp6->ai_family, 709 ai_tcp6->ai_socktype, 710 ai_tcp6->ai_protocol)) < 0) { 711 syslog(LOG_ERR, 712 "can't create tcp6 socket"); 713 nfsd_exit(1); 714 } 715 if (setsockopt(tcp6sock, SOL_SOCKET, 716 SO_REUSEADDR, 717 (char *)&on, sizeof(on)) < 0) 718 syslog(LOG_ERR, 719 "setsockopt SO_REUSEADDR: %m"); 720 if (setsockopt(tcp6sock, IPPROTO_IPV6, 721 IPV6_V6ONLY, &on, sizeof on) < 0) { 722 syslog(LOG_ERR, 723 "can't set v6-only binding for tcp6 " 724 "socket: %m"); 725 nfsd_exit(1); 726 } 727 if (bind(tcp6sock, ai_tcp6->ai_addr, 728 ai_tcp6->ai_addrlen) < 0) { 729 syslog(LOG_ERR, 730 "can't bind tcp6 addr %s: %m", 731 bindhost[i]); 732 nfsd_exit(1); 733 } 734 if (listen(tcp6sock, -1) < 0) { 735 syslog(LOG_ERR, "listen failed"); 736 nfsd_exit(1); 737 } 738 freeaddrinfo(ai_tcp6); 739 FD_SET(tcp6sock, &sockbits); 740 if (maxsock < tcp6sock) 741 maxsock = tcp6sock; 742 connect_type_cnt++; 743 } 744 } 745 if (rpcbreg == 1) { 746 memset(&hints, 0, sizeof hints); 747 hints.ai_flags = AI_PASSIVE; 748 hints.ai_family = AF_INET6; 749 hints.ai_socktype = SOCK_STREAM; 750 hints.ai_protocol = IPPROTO_TCP; 751 ecode = getaddrinfo(NULL, "nfs", &hints, &ai_tcp6); 752 if (ecode != 0) { 753 syslog(LOG_ERR, "getaddrinfo tcp6: %s", 754 gai_strerror(ecode)); 755 nfsd_exit(1); 756 } 757 nconf_tcp6 = getnetconfigent("tcp6"); 758 if (nconf_tcp6 == NULL) 759 err(1, "getnetconfigent tcp6 failed"); 760 nb_tcp6.buf = ai_tcp6->ai_addr; 761 nb_tcp6.len = nb_tcp6.maxlen = ai_tcp6->ai_addrlen; 762 if (nfs_minvers == NFS_VER2) 763 if (!rpcb_set(NFS_PROGRAM, 2, nconf_tcp6, 764 &nb_tcp6)) 765 err(1, "rpcb_set tcp6 failed"); 766 if (nfs_minvers <= NFS_VER3) 767 if (!rpcb_set(NFS_PROGRAM, 3, nconf_tcp6, 768 &nb_tcp6)) 769 err(1, "rpcb_set tcp6 failed"); 770 freeaddrinfo(ai_tcp6); 771 } 772 } 773 774 if (rpcbregcnt == 0) { 775 syslog(LOG_ERR, "rpcb_set() failed, nothing to do: %m"); 776 nfsd_exit(1); 777 } 778 779 if (tcpflag && connect_type_cnt == 0) { 780 syslog(LOG_ERR, "tcp connects == 0, nothing to do: %m"); 781 nfsd_exit(1); 782 } 783 784 setproctitle("master"); 785 /* 786 * We always want a master to have a clean way to shut nfsd down 787 * (with unregistration): if the master is killed, it unregisters and 788 * kills all children. If we run for UDP only (and so do not have to 789 * loop waiting for accept), we instead make the parent 790 * a "server" too. start_server will not return. 791 */ 792 if (!tcpflag) 793 start_server(1, &nfsdargs); 794 795 /* 796 * Loop forever accepting connections and passing the sockets 797 * into the kernel for the mounts. 798 */ 799 for (;;) { 800 ready = sockbits; 801 if (connect_type_cnt > 1) { 802 if (select(maxsock + 1, 803 &ready, NULL, NULL, NULL) < 1) { 804 error = errno; 805 if (error == EINTR) 806 continue; 807 syslog(LOG_ERR, "select failed: %m"); 808 nfsd_exit(1); 809 } 810 } 811 for (tcpsock = 0; tcpsock <= maxsock; tcpsock++) { 812 if (FD_ISSET(tcpsock, &ready)) { 813 len = sizeof(peer); 814 if ((msgsock = accept(tcpsock, 815 (struct sockaddr *)&peer, &len)) < 0) { 816 error = errno; 817 syslog(LOG_ERR, "accept failed: %m"); 818 if (error == ECONNABORTED || 819 error == EINTR) 820 continue; 821 nfsd_exit(1); 822 } 823 if (setsockopt(msgsock, SOL_SOCKET, 824 SO_KEEPALIVE, (char *)&on, sizeof(on)) < 0) 825 syslog(LOG_ERR, 826 "setsockopt SO_KEEPALIVE: %m"); 827 addsockargs.sock = msgsock; 828 addsockargs.name = (caddr_t)&peer; 829 addsockargs.namelen = len; 830 nfssvc(nfssvc_addsock, &addsockargs); 831 (void)close(msgsock); 832 } 833 } 834 } 835 } 836 837 static int 838 setbindhost(struct addrinfo **ai, const char *bindhost, struct addrinfo hints) 839 { 840 int ecode; 841 u_int32_t host_addr[4]; /* IPv4 or IPv6 */ 842 const char *hostptr; 843 844 if (bindhost == NULL || strcmp("*", bindhost) == 0) 845 hostptr = NULL; 846 else 847 hostptr = bindhost; 848 849 if (hostptr != NULL) { 850 switch (hints.ai_family) { 851 case AF_INET: 852 if (inet_pton(AF_INET, hostptr, host_addr) == 1) { 853 hints.ai_flags = AI_NUMERICHOST; 854 } else { 855 if (inet_pton(AF_INET6, hostptr, 856 host_addr) == 1) 857 return (1); 858 } 859 break; 860 case AF_INET6: 861 if (inet_pton(AF_INET6, hostptr, host_addr) == 1) { 862 hints.ai_flags = AI_NUMERICHOST; 863 } else { 864 if (inet_pton(AF_INET, hostptr, 865 host_addr) == 1) 866 return (1); 867 } 868 break; 869 default: 870 break; 871 } 872 } 873 874 ecode = getaddrinfo(hostptr, "nfs", &hints, ai); 875 if (ecode != 0) { 876 syslog(LOG_ERR, "getaddrinfo %s: %s", bindhost, 877 gai_strerror(ecode)); 878 return (1); 879 } 880 return (0); 881 } 882 883 static void 884 set_nfsdcnt(int proposed) 885 { 886 887 if (proposed < 1) { 888 warnx("nfsd count too low %d; reset to %d", proposed, 889 DEFNFSDCNT); 890 nfsdcnt = DEFNFSDCNT; 891 } else if (proposed > MAXNFSDCNT) { 892 warnx("nfsd count too high %d; truncated to %d", proposed, 893 MAXNFSDCNT); 894 nfsdcnt = MAXNFSDCNT; 895 } else 896 nfsdcnt = proposed; 897 nfsdcnt_set = 1; 898 } 899 900 static void 901 usage(void) 902 { 903 (void)fprintf(stderr, "%s", getopt_usage); 904 exit(1); 905 } 906 907 static void 908 nonfs(__unused int signo) 909 { 910 syslog(LOG_ERR, "missing system call: NFS not available"); 911 } 912 913 static void 914 reapchild(__unused int signo) 915 { 916 pid_t pid; 917 int i; 918 919 while ((pid = wait3(NULL, WNOHANG, NULL)) > 0) { 920 for (i = 0; i < nfsdcnt; i++) 921 if (pid == children[i]) 922 children[i] = -1; 923 } 924 } 925 926 static void 927 unregistration(void) 928 { 929 if ((!rpcb_unset(NFS_PROGRAM, 2, NULL)) || 930 (!rpcb_unset(NFS_PROGRAM, 3, NULL))) 931 syslog(LOG_ERR, "rpcb_unset failed"); 932 } 933 934 static void 935 killchildren(void) 936 { 937 int i; 938 939 for (i = 0; i < nfsdcnt; i++) { 940 if (children[i] > 0) 941 kill(children[i], SIGKILL); 942 } 943 } 944 945 /* 946 * Cleanup master after SIGUSR1. 947 */ 948 static void 949 cleanup(__unused int signo) 950 { 951 nfsd_exit(0); 952 } 953 954 /* 955 * Cleanup child after SIGUSR1. 956 */ 957 static void 958 child_cleanup(__unused int signo) 959 { 960 exit(0); 961 } 962 963 static void 964 nfsd_exit(int status) 965 { 966 killchildren(); 967 unregistration(); 968 exit(status); 969 } 970 971 static int 972 get_tuned_nfsdcount(void) 973 { 974 int ncpu, error, tuned_nfsdcnt; 975 size_t ncpu_size; 976 977 ncpu_size = sizeof(ncpu); 978 error = sysctlbyname("hw.ncpu", &ncpu, &ncpu_size, NULL, 0); 979 if (error) { 980 warnx("sysctlbyname(hw.ncpu) failed defaulting to %d nfs servers", 981 DEFNFSDCNT); 982 tuned_nfsdcnt = DEFNFSDCNT; 983 } else { 984 tuned_nfsdcnt = ncpu * 8; 985 } 986 return tuned_nfsdcnt; 987 } 988 989 static void 990 start_server(int master, struct nfsd_nfsd_args *nfsdargp) 991 { 992 char principal[MAXHOSTNAMELEN + 5]; 993 int status, error; 994 char hostname[MAXHOSTNAMELEN + 1], *cp; 995 struct addrinfo *aip, hints; 996 997 status = 0; 998 gethostname(hostname, sizeof (hostname)); 999 snprintf(principal, sizeof (principal), "nfs@%s", hostname); 1000 if ((cp = strchr(hostname, '.')) == NULL || 1001 *(cp + 1) == '\0') { 1002 /* If not fully qualified, try getaddrinfo() */ 1003 memset((void *)&hints, 0, sizeof (hints)); 1004 hints.ai_flags = AI_CANONNAME; 1005 error = getaddrinfo(hostname, NULL, &hints, &aip); 1006 if (error == 0) { 1007 if (aip->ai_canonname != NULL && 1008 (cp = strchr(aip->ai_canonname, '.')) != 1009 NULL && *(cp + 1) != '\0') 1010 snprintf(principal, sizeof (principal), 1011 "nfs@%s", aip->ai_canonname); 1012 freeaddrinfo(aip); 1013 } 1014 } 1015 nfsdargp->principal = principal; 1016 1017 if (nfsdcnt_set) 1018 nfsdargp->minthreads = nfsdargp->maxthreads = nfsdcnt; 1019 else { 1020 nfsdargp->minthreads = minthreads_set ? minthreads : get_tuned_nfsdcount(); 1021 nfsdargp->maxthreads = maxthreads_set ? maxthreads : nfsdargp->minthreads; 1022 if (nfsdargp->maxthreads < nfsdargp->minthreads) 1023 nfsdargp->maxthreads = nfsdargp->minthreads; 1024 } 1025 error = nfssvc(nfssvc_nfsd, nfsdargp); 1026 if (error < 0 && errno == EAUTH) { 1027 /* 1028 * This indicates that it could not register the 1029 * rpcsec_gss credentials, usually because the 1030 * gssd daemon isn't running. 1031 * (only the experimental server with nfsv4) 1032 */ 1033 syslog(LOG_ERR, "No gssd, using AUTH_SYS only"); 1034 principal[0] = '\0'; 1035 error = nfssvc(nfssvc_nfsd, nfsdargp); 1036 } 1037 if (error < 0) { 1038 if (errno == ENXIO) { 1039 syslog(LOG_ERR, "Bad -p option, cannot run"); 1040 if (masterpid != 0 && master == 0) 1041 kill(masterpid, SIGUSR1); 1042 } else 1043 syslog(LOG_ERR, "nfssvc: %m"); 1044 status = 1; 1045 } 1046 if (master) 1047 nfsd_exit(status); 1048 else 1049 exit(status); 1050 } 1051 1052 /* 1053 * Open the stable restart file and return the file descriptor for it. 1054 */ 1055 static void 1056 open_stable(int *stable_fdp, int *backup_fdp) 1057 { 1058 int stable_fd, backup_fd = -1, ret; 1059 struct stat st, backup_st; 1060 1061 /* Open and stat the stable restart file. */ 1062 stable_fd = open(NFSD_STABLERESTART, O_RDWR, 0); 1063 if (stable_fd < 0) 1064 stable_fd = open(NFSD_STABLERESTART, O_RDWR | O_CREAT, 0600); 1065 if (stable_fd >= 0) { 1066 ret = fstat(stable_fd, &st); 1067 if (ret < 0) { 1068 close(stable_fd); 1069 stable_fd = -1; 1070 } 1071 } 1072 1073 /* Open and stat the backup stable restart file. */ 1074 if (stable_fd >= 0) { 1075 backup_fd = open(NFSD_STABLEBACKUP, O_RDWR, 0); 1076 if (backup_fd < 0) 1077 backup_fd = open(NFSD_STABLEBACKUP, O_RDWR | O_CREAT, 1078 0600); 1079 if (backup_fd >= 0) { 1080 ret = fstat(backup_fd, &backup_st); 1081 if (ret < 0) { 1082 close(backup_fd); 1083 backup_fd = -1; 1084 } 1085 } 1086 if (backup_fd < 0) { 1087 close(stable_fd); 1088 stable_fd = -1; 1089 } 1090 } 1091 1092 *stable_fdp = stable_fd; 1093 *backup_fdp = backup_fd; 1094 if (stable_fd < 0) 1095 return; 1096 1097 /* Sync up the 2 files, as required. */ 1098 if (st.st_size > 0) 1099 copy_stable(stable_fd, backup_fd); 1100 else if (backup_st.st_size > 0) 1101 copy_stable(backup_fd, stable_fd); 1102 } 1103 1104 /* 1105 * Copy the stable restart file to the backup or vice versa. 1106 */ 1107 static void 1108 copy_stable(int from_fd, int to_fd) 1109 { 1110 int cnt, ret; 1111 static char buf[1024]; 1112 1113 ret = lseek(from_fd, (off_t)0, SEEK_SET); 1114 if (ret >= 0) 1115 ret = lseek(to_fd, (off_t)0, SEEK_SET); 1116 if (ret >= 0) 1117 ret = ftruncate(to_fd, (off_t)0); 1118 if (ret >= 0) 1119 do { 1120 cnt = read(from_fd, buf, 1024); 1121 if (cnt > 0) 1122 ret = write(to_fd, buf, cnt); 1123 else if (cnt < 0) 1124 ret = cnt; 1125 } while (cnt > 0 && ret >= 0); 1126 if (ret >= 0) 1127 ret = fsync(to_fd); 1128 if (ret < 0) 1129 syslog(LOG_ERR, "stable restart copy failure: %m"); 1130 } 1131 1132 /* 1133 * Back up the stable restart file when indicated by the kernel. 1134 */ 1135 static void 1136 backup_stable(__unused int signo) 1137 { 1138 1139 if (stablefd >= 0) 1140 copy_stable(stablefd, backupfd); 1141 } 1142 1143 /* 1144 * Parse the pNFS string and extract the DS servers and ports numbers. 1145 */ 1146 static void 1147 parse_dsserver(const char *optionarg, struct nfsd_nfsd_args *nfsdargp) 1148 { 1149 char *cp, *cp2, *dsaddr, *dshost, *dspath, *dsvol, nfsprt[9]; 1150 char *mdspath, *mdsp, ip6[INET6_ADDRSTRLEN]; 1151 const char *ad; 1152 int ecode; 1153 u_int adsiz, dsaddrcnt, dshostcnt, dspathcnt, hostsiz, pathsiz; 1154 u_int mdspathcnt; 1155 size_t dsaddrsiz, dshostsiz, dspathsiz, nfsprtsiz, mdspathsiz; 1156 struct addrinfo hints, *ai_tcp, *res; 1157 struct sockaddr_in sin; 1158 struct sockaddr_in6 sin6; 1159 1160 cp = strdup(optionarg); 1161 if (cp == NULL) 1162 errx(1, "Out of memory"); 1163 1164 /* Now, do the host names. */ 1165 dspathsiz = 1024; 1166 dspathcnt = 0; 1167 dspath = malloc(dspathsiz); 1168 if (dspath == NULL) 1169 errx(1, "Out of memory"); 1170 dshostsiz = 1024; 1171 dshostcnt = 0; 1172 dshost = malloc(dshostsiz); 1173 if (dshost == NULL) 1174 errx(1, "Out of memory"); 1175 dsaddrsiz = 1024; 1176 dsaddrcnt = 0; 1177 dsaddr = malloc(dsaddrsiz); 1178 if (dsaddr == NULL) 1179 errx(1, "Out of memory"); 1180 mdspathsiz = 1024; 1181 mdspathcnt = 0; 1182 mdspath = malloc(mdspathsiz); 1183 if (mdspath == NULL) 1184 errx(1, "Out of memory"); 1185 1186 /* Put the NFS port# in "." form. */ 1187 snprintf(nfsprt, 9, ".%d.%d", 2049 >> 8, 2049 & 0xff); 1188 nfsprtsiz = strlen(nfsprt); 1189 1190 ai_tcp = NULL; 1191 /* Loop around for each DS server name. */ 1192 do { 1193 cp2 = strchr(cp, ','); 1194 if (cp2 != NULL) { 1195 /* Not the last DS in the list. */ 1196 *cp2++ = '\0'; 1197 if (*cp2 == '\0') 1198 usage(); 1199 } 1200 1201 dsvol = strchr(cp, ':'); 1202 if (dsvol == NULL || *(dsvol + 1) == '\0') 1203 usage(); 1204 *dsvol++ = '\0'; 1205 1206 /* Optional path for MDS file system to be stored on DS. */ 1207 mdsp = strchr(dsvol, '#'); 1208 if (mdsp != NULL) { 1209 if (*(mdsp + 1) == '\0' || mdsp <= dsvol) 1210 usage(); 1211 *mdsp++ = '\0'; 1212 } 1213 1214 /* Append this pathname to dspath. */ 1215 pathsiz = strlen(dsvol); 1216 if (dspathcnt + pathsiz + 1 > dspathsiz) { 1217 dspathsiz *= 2; 1218 dspath = realloc(dspath, dspathsiz); 1219 if (dspath == NULL) 1220 errx(1, "Out of memory"); 1221 } 1222 strcpy(&dspath[dspathcnt], dsvol); 1223 dspathcnt += pathsiz + 1; 1224 1225 /* Append this pathname to mdspath. */ 1226 if (mdsp != NULL) 1227 pathsiz = strlen(mdsp); 1228 else 1229 pathsiz = 0; 1230 if (mdspathcnt + pathsiz + 1 > mdspathsiz) { 1231 mdspathsiz *= 2; 1232 mdspath = realloc(mdspath, mdspathsiz); 1233 if (mdspath == NULL) 1234 errx(1, "Out of memory"); 1235 } 1236 if (mdsp != NULL) 1237 strcpy(&mdspath[mdspathcnt], mdsp); 1238 else 1239 mdspath[mdspathcnt] = '\0'; 1240 mdspathcnt += pathsiz + 1; 1241 1242 if (ai_tcp != NULL) 1243 freeaddrinfo(ai_tcp); 1244 1245 /* Get the fully qualified domain name and IP address. */ 1246 memset(&hints, 0, sizeof(hints)); 1247 hints.ai_flags = AI_CANONNAME | AI_ADDRCONFIG; 1248 hints.ai_family = PF_UNSPEC; 1249 hints.ai_socktype = SOCK_STREAM; 1250 hints.ai_protocol = IPPROTO_TCP; 1251 ecode = getaddrinfo(cp, NULL, &hints, &ai_tcp); 1252 if (ecode != 0) 1253 err(1, "getaddrinfo pnfs: %s %s", cp, 1254 gai_strerror(ecode)); 1255 ad = NULL; 1256 for (res = ai_tcp; res != NULL; res = res->ai_next) { 1257 if (res->ai_addr->sa_family == AF_INET) { 1258 if (res->ai_addrlen < sizeof(sin)) 1259 err(1, "getaddrinfo() returned " 1260 "undersized IPv4 address"); 1261 /* 1262 * Mips cares about sockaddr_in alignment, 1263 * so copy the address. 1264 */ 1265 memcpy(&sin, res->ai_addr, sizeof(sin)); 1266 ad = inet_ntoa(sin.sin_addr); 1267 break; 1268 } else if (res->ai_family == AF_INET6) { 1269 if (res->ai_addrlen < sizeof(sin6)) 1270 err(1, "getaddrinfo() returned " 1271 "undersized IPv6 address"); 1272 /* 1273 * Mips cares about sockaddr_in6 alignment, 1274 * so copy the address. 1275 */ 1276 memcpy(&sin6, res->ai_addr, sizeof(sin6)); 1277 ad = inet_ntop(AF_INET6, &sin6.sin6_addr, ip6, 1278 sizeof(ip6)); 1279 1280 /* 1281 * XXX 1282 * Since a link local address will only 1283 * work if the client and DS are in the 1284 * same scope zone, only use it if it is 1285 * the only address. 1286 */ 1287 if (ad != NULL && 1288 !IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr)) 1289 break; 1290 } 1291 } 1292 if (ad == NULL) 1293 err(1, "No IP address for %s", cp); 1294 1295 /* Append this address to dsaddr. */ 1296 adsiz = strlen(ad); 1297 if (dsaddrcnt + adsiz + nfsprtsiz + 1 > dsaddrsiz) { 1298 dsaddrsiz *= 2; 1299 dsaddr = realloc(dsaddr, dsaddrsiz); 1300 if (dsaddr == NULL) 1301 errx(1, "Out of memory"); 1302 } 1303 strcpy(&dsaddr[dsaddrcnt], ad); 1304 strcat(&dsaddr[dsaddrcnt], nfsprt); 1305 dsaddrcnt += adsiz + nfsprtsiz + 1; 1306 1307 /* Append this hostname to dshost. */ 1308 hostsiz = strlen(ai_tcp->ai_canonname); 1309 if (dshostcnt + hostsiz + 1 > dshostsiz) { 1310 dshostsiz *= 2; 1311 dshost = realloc(dshost, dshostsiz); 1312 if (dshost == NULL) 1313 errx(1, "Out of memory"); 1314 } 1315 strcpy(&dshost[dshostcnt], ai_tcp->ai_canonname); 1316 dshostcnt += hostsiz + 1; 1317 1318 cp = cp2; 1319 } while (cp != NULL); 1320 1321 nfsdargp->addr = dsaddr; 1322 nfsdargp->addrlen = dsaddrcnt; 1323 nfsdargp->dnshost = dshost; 1324 nfsdargp->dnshostlen = dshostcnt; 1325 nfsdargp->dspath = dspath; 1326 nfsdargp->dspathlen = dspathcnt; 1327 nfsdargp->mdspath = mdspath; 1328 nfsdargp->mdspathlen = mdspathcnt; 1329 freeaddrinfo(ai_tcp); 1330 } 1331 1332