1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 #include <sys/types.h> 30 #include <sys/t_lock.h> 31 #include <sys/param.h> 32 #include <sys/systm.h> 33 #include <sys/buf.h> 34 #include <sys/conf.h> 35 #include <sys/cred.h> 36 #include <sys/kmem.h> 37 #include <sys/sysmacros.h> 38 #include <sys/vfs.h> 39 #include <sys/vfs_opreg.h> 40 #include <sys/vnode.h> 41 #include <sys/debug.h> 42 #include <sys/errno.h> 43 #include <sys/time.h> 44 #include <sys/file.h> 45 #include <sys/open.h> 46 #include <sys/user.h> 47 #include <sys/uio.h> 48 #include <sys/termios.h> 49 #include <sys/stream.h> 50 #include <sys/strsubr.h> 51 #include <sys/strsun.h> 52 #include <sys/esunddi.h> 53 #include <sys/flock.h> 54 #include <sys/modctl.h> 55 #include <sys/cmn_err.h> 56 #include <sys/mkdev.h> 57 #include <sys/pathname.h> 58 #include <sys/ddi.h> 59 #include <sys/stat.h> 60 #include <sys/fs/snode.h> 61 #include <sys/fs/dv_node.h> 62 #include <sys/zone.h> 63 64 #include <sys/socket.h> 65 #include <sys/socketvar.h> 66 #include <netinet/in.h> 67 #include <sys/un.h> 68 69 #include <sys/ucred.h> 70 71 #include <sys/tiuser.h> 72 #define _SUN_TPI_VERSION 2 73 #include <sys/tihdr.h> 74 75 #include <c2/audit.h> 76 77 #include <fs/sockfs/nl7c.h> 78 79 /* 80 * Macros that operate on struct cmsghdr. 81 * The CMSG_VALID macro does not assume that the last option buffer is padded. 82 */ 83 #define CMSG_CONTENT(cmsg) (&((cmsg)[1])) 84 #define CMSG_CONTENTLEN(cmsg) ((cmsg)->cmsg_len - sizeof (struct cmsghdr)) 85 #define CMSG_VALID(cmsg, start, end) \ 86 (ISALIGNED_cmsghdr(cmsg) && \ 87 ((uintptr_t)(cmsg) >= (uintptr_t)(start)) && \ 88 ((uintptr_t)(cmsg) < (uintptr_t)(end)) && \ 89 ((ssize_t)(cmsg)->cmsg_len >= sizeof (struct cmsghdr)) && \ 90 ((uintptr_t)(cmsg) + (cmsg)->cmsg_len <= (uintptr_t)(end))) 91 #define SO_LOCK_WAKEUP_TIME 3000 /* Wakeup time in milliseconds */ 92 93 static struct kmem_cache *socktpi_cache, *socktpi_unix_cache; 94 struct kmem_cache *socktpi_sod_cache; 95 96 dev_t sockdev; /* For fsid in getattr */ 97 98 struct sockparams *sphead; 99 krwlock_t splist_lock; 100 101 struct socklist socklist; 102 103 static int sockfs_update(kstat_t *, int); 104 static int sockfs_snapshot(kstat_t *, void *, int); 105 106 extern void sendfile_init(); 107 108 extern void nl7c_init(void); 109 110 extern int sostr_init(); 111 112 #define ADRSTRLEN (2 * sizeof (void *) + 1) 113 /* 114 * kernel structure for passing the sockinfo data back up to the user. 115 * the strings array allows us to convert AF_UNIX addresses into strings 116 * with a common method regardless of which n-bit kernel we're running. 117 */ 118 struct k_sockinfo { 119 struct sockinfo ks_si; 120 char ks_straddr[3][ADRSTRLEN]; 121 }; 122 123 /* 124 * Translate from a device pathname (e.g. "/dev/tcp") to a vnode. 125 * Returns with the vnode held. 126 */ 127 static int 128 sogetvp(char *devpath, vnode_t **vpp, int uioflag) 129 { 130 struct snode *csp; 131 vnode_t *vp, *dvp; 132 major_t maj; 133 int error; 134 135 ASSERT(uioflag == UIO_SYSSPACE || uioflag == UIO_USERSPACE); 136 /* 137 * Lookup the underlying filesystem vnode. 138 */ 139 error = lookupname(devpath, uioflag, FOLLOW, NULLVPP, &vp); 140 if (error) 141 return (error); 142 143 /* Check that it is the correct vnode */ 144 if (vp->v_type != VCHR) { 145 VN_RELE(vp); 146 return (ENOTSOCK); 147 } 148 149 /* 150 * If devpath went through devfs, the device should already 151 * be configured. If devpath is a mknod file, however, we 152 * need to make sure the device is properly configured. 153 * To do this, we do something similar to spec_open() 154 * except that we resolve to the minor/leaf level since 155 * we need to return a vnode. 156 */ 157 csp = VTOS(VTOS(vp)->s_commonvp); 158 if (!(csp->s_flag & SDIPSET)) { 159 char *pathname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 160 error = ddi_dev_pathname(vp->v_rdev, S_IFCHR, pathname); 161 if (error == 0) 162 error = devfs_lookupname(pathname, NULLVPP, &dvp); 163 VN_RELE(vp); 164 kmem_free(pathname, MAXPATHLEN); 165 if (error != 0) 166 return (ENXIO); 167 vp = dvp; /* use the devfs vp */ 168 } 169 170 /* device is configured at this point */ 171 maj = getmajor(vp->v_rdev); 172 if (!STREAMSTAB(maj)) { 173 VN_RELE(vp); 174 return (ENOSTR); 175 } 176 177 *vpp = vp; 178 return (0); 179 } 180 181 /* 182 * Add or delete (latter if devpath is NULL) an enter to the sockparams 183 * table. If devpathlen is zero the devpath with not be kmem_freed. Otherwise 184 * this routine assumes that the caller has kmem_alloced devpath/devpathlen 185 * for this routine to consume. 186 * The zero devpathlen could be used if the kernel wants to create entries 187 * itself by calling sockconfig(1,2,3, "/dev/tcp", 0); 188 */ 189 int 190 soconfig(int domain, int type, int protocol, 191 char *devpath, int devpathlen) 192 { 193 struct sockparams **spp; 194 struct sockparams *sp; 195 int error = 0; 196 197 dprint(0, ("soconfig(%d,%d,%d,%s,%d)\n", 198 domain, type, protocol, devpath, devpathlen)); 199 200 /* 201 * Look for an existing match. 202 */ 203 rw_enter(&splist_lock, RW_WRITER); 204 for (spp = &sphead; (sp = *spp) != NULL; spp = &sp->sp_next) { 205 if (sp->sp_domain == domain && 206 sp->sp_type == type && 207 sp->sp_protocol == protocol) { 208 break; 209 } 210 } 211 if (devpath == NULL) { 212 ASSERT(devpathlen == 0); 213 214 /* Delete existing entry */ 215 if (sp == NULL) { 216 error = ENXIO; 217 goto done; 218 } 219 /* Unlink and free existing entry */ 220 *spp = sp->sp_next; 221 ASSERT(sp->sp_vnode); 222 VN_RELE(sp->sp_vnode); 223 if (sp->sp_devpathlen != 0) 224 kmem_free(sp->sp_devpath, sp->sp_devpathlen); 225 kmem_free(sp, sizeof (*sp)); 226 } else { 227 vnode_t *vp; 228 229 /* Add new entry */ 230 if (sp != NULL) { 231 error = EEXIST; 232 goto done; 233 } 234 235 error = sogetvp(devpath, &vp, UIO_SYSSPACE); 236 if (error) { 237 dprint(0, ("soconfig: vp %s failed with %d\n", 238 devpath, error)); 239 goto done; 240 } 241 242 dprint(0, ("soconfig: %s => vp %p, dev 0x%lx\n", 243 devpath, (void *)vp, vp->v_rdev)); 244 245 sp = kmem_alloc(sizeof (*sp), KM_SLEEP); 246 sp->sp_domain = domain; 247 sp->sp_type = type; 248 sp->sp_protocol = protocol; 249 sp->sp_devpath = devpath; 250 sp->sp_devpathlen = devpathlen; 251 sp->sp_vnode = vp; 252 sp->sp_next = NULL; 253 *spp = sp; 254 } 255 done: 256 rw_exit(&splist_lock); 257 if (error) { 258 if (devpath != NULL) 259 kmem_free(devpath, devpathlen); 260 #ifdef SOCK_DEBUG 261 eprintline(error); 262 #endif /* SOCK_DEBUG */ 263 } 264 return (error); 265 } 266 267 /* 268 * Lookup an entry in the sockparams list based on the triple. 269 * If no entry is found and devpath is not NULL translate devpath to a 270 * vnode. Note that devpath is a pointer to a user address! 271 * Returns with the vnode held. 272 * 273 * When this routine uses devpath it does not create an entry in the sockparams 274 * list since this routine can run on behalf of any user and one user 275 * should not be able to effect the transport used by another user. 276 * 277 * In order to return the correct error this routine has to do wildcard scans 278 * of the list. The errors are (in decreasing precedence): 279 * EAFNOSUPPORT - address family not in list 280 * EPROTONOSUPPORT - address family supported but not protocol. 281 * EPROTOTYPE - address family and protocol supported but not socket type. 282 */ 283 vnode_t * 284 solookup(int domain, int type, int protocol, char *devpath, int *errorp) 285 { 286 struct sockparams *sp; 287 int error; 288 vnode_t *vp; 289 290 rw_enter(&splist_lock, RW_READER); 291 for (sp = sphead; sp != NULL; sp = sp->sp_next) { 292 if (sp->sp_domain == domain && 293 sp->sp_type == type && 294 sp->sp_protocol == protocol) { 295 break; 296 } 297 } 298 if (sp == NULL) { 299 dprint(0, ("solookup(%d,%d,%d) not found\n", 300 domain, type, protocol)); 301 if (devpath == NULL) { 302 /* Determine correct error code */ 303 int found = 0; 304 305 for (sp = sphead; sp != NULL; sp = sp->sp_next) { 306 if (sp->sp_domain == domain && found < 1) 307 found = 1; 308 if (sp->sp_domain == domain && 309 sp->sp_protocol == protocol && found < 2) 310 found = 2; 311 } 312 rw_exit(&splist_lock); 313 switch (found) { 314 case 0: 315 *errorp = EAFNOSUPPORT; 316 break; 317 case 1: 318 *errorp = EPROTONOSUPPORT; 319 break; 320 case 2: 321 *errorp = EPROTOTYPE; 322 break; 323 } 324 return (NULL); 325 } 326 rw_exit(&splist_lock); 327 328 /* 329 * Return vp based on devpath. 330 * Do not enter into table to avoid random users 331 * modifying the sockparams list. 332 */ 333 error = sogetvp(devpath, &vp, UIO_USERSPACE); 334 if (error) { 335 dprint(0, ("solookup: vp %p failed with %d\n", 336 (void *)devpath, error)); 337 *errorp = EPROTONOSUPPORT; 338 return (NULL); 339 } 340 dprint(0, ("solookup: %p => vp %p, dev 0x%lx\n", 341 (void *)devpath, (void *)vp, vp->v_rdev)); 342 343 return (vp); 344 } 345 dprint(0, ("solookup(%d,%d,%d) vp %p devpath %s\n", 346 domain, type, protocol, (void *)sp->sp_vnode, sp->sp_devpath)); 347 348 vp = sp->sp_vnode; 349 VN_HOLD(vp); 350 rw_exit(&splist_lock); 351 return (vp); 352 } 353 354 /* 355 * Return a socket vnode. 356 * 357 * Assumes that the caller is "passing" an VN_HOLD for accessvp i.e. 358 * when the socket is freed a VN_RELE will take place. 359 * 360 * Note that sockets assume that the driver will clone (either itself 361 * or by using the clone driver) i.e. a socket() call will always 362 * result in a new vnode being created. 363 */ 364 struct vnode * 365 makesockvp(struct vnode *accessvp, int domain, int type, int protocol) 366 { 367 kmem_cache_t *cp; 368 struct sonode *so; 369 struct vnode *vp; 370 time_t now; 371 dev_t dev; 372 373 cp = (domain == AF_UNIX) ? socktpi_unix_cache : socktpi_cache; 374 so = kmem_cache_alloc(cp, KM_SLEEP); 375 so->so_cache = cp; 376 so->so_obj = so; 377 vp = SOTOV(so); 378 now = gethrestime_sec(); 379 380 so->so_flag = 0; 381 ASSERT(so->so_accessvp == NULL); 382 so->so_accessvp = accessvp; 383 dev = accessvp->v_rdev; 384 385 /* 386 * Record in so_flag that it is a clone. 387 */ 388 if (getmajor(dev) == clone_major) { 389 so->so_flag |= SOCLONE; 390 } 391 so->so_dev = dev; 392 393 so->so_state = 0; 394 so->so_mode = 0; 395 396 so->so_fsid = sockdev; 397 so->so_atime = now; 398 so->so_mtime = now; 399 so->so_ctime = now; /* Never modified */ 400 so->so_count = 0; 401 402 so->so_family = (short)domain; 403 so->so_type = (short)type; 404 so->so_protocol = (short)protocol; 405 so->so_pushcnt = 0; 406 407 so->so_options = 0; 408 so->so_linger.l_onoff = 0; 409 so->so_linger.l_linger = 0; 410 so->so_sndbuf = 0; 411 so->so_rcvbuf = 0; 412 so->so_sndlowat = 0; 413 so->so_rcvlowat = 0; 414 #ifdef notyet 415 so->so_sndtimeo = 0; 416 so->so_rcvtimeo = 0; 417 #endif /* notyet */ 418 so->so_error = 0; 419 so->so_delayed_error = 0; 420 421 ASSERT(so->so_oobmsg == NULL); 422 so->so_oobcnt = 0; 423 so->so_oobsigcnt = 0; 424 so->so_pgrp = 0; 425 so->so_provinfo = NULL; 426 427 ASSERT(so->so_laddr_sa == NULL && so->so_faddr_sa == NULL); 428 so->so_laddr_len = so->so_faddr_len = 0; 429 so->so_laddr_maxlen = so->so_faddr_maxlen = 0; 430 so->so_eaddr_mp = NULL; 431 so->so_priv = NULL; 432 433 so->so_peercred = NULL; 434 435 ASSERT(so->so_ack_mp == NULL); 436 ASSERT(so->so_conn_ind_head == NULL); 437 ASSERT(so->so_conn_ind_tail == NULL); 438 ASSERT(so->so_ux_bound_vp == NULL); 439 ASSERT(so->so_unbind_mp == NULL); 440 441 vn_reinit(vp); 442 vp->v_vfsp = rootvfs; 443 vp->v_type = VSOCK; 444 vp->v_rdev = so->so_dev; 445 vn_exists(vp); 446 447 return (vp); 448 } 449 450 void 451 sockfree(struct sonode *so) 452 { 453 mblk_t *mp; 454 vnode_t *vp; 455 456 ASSERT(so->so_count == 0); 457 ASSERT(so->so_accessvp); 458 ASSERT(so->so_discon_ind_mp == NULL); 459 460 vp = so->so_accessvp; 461 VN_RELE(vp); 462 463 /* 464 * Protect so->so_[lf]addr_sa so that sockfs_snapshot() can safely 465 * indirect them. It also uses so_accessvp as a validity test. 466 */ 467 mutex_enter(&so->so_lock); 468 469 so->so_accessvp = NULL; 470 471 if (so->so_laddr_sa) { 472 ASSERT((caddr_t)so->so_faddr_sa == 473 (caddr_t)so->so_laddr_sa + so->so_laddr_maxlen); 474 ASSERT(so->so_faddr_maxlen == so->so_laddr_maxlen); 475 so->so_state &= ~(SS_LADDR_VALID | SS_FADDR_VALID); 476 kmem_free(so->so_laddr_sa, so->so_laddr_maxlen * 2); 477 so->so_laddr_sa = NULL; 478 so->so_laddr_len = so->so_laddr_maxlen = 0; 479 so->so_faddr_sa = NULL; 480 so->so_faddr_len = so->so_faddr_maxlen = 0; 481 } 482 483 mutex_exit(&so->so_lock); 484 485 if ((mp = so->so_eaddr_mp) != NULL) { 486 freemsg(mp); 487 so->so_eaddr_mp = NULL; 488 so->so_delayed_error = 0; 489 } 490 if ((mp = so->so_ack_mp) != NULL) { 491 freemsg(mp); 492 so->so_ack_mp = NULL; 493 } 494 if ((mp = so->so_conn_ind_head) != NULL) { 495 mblk_t *mp1; 496 497 while (mp) { 498 mp1 = mp->b_next; 499 mp->b_next = NULL; 500 freemsg(mp); 501 mp = mp1; 502 } 503 so->so_conn_ind_head = so->so_conn_ind_tail = NULL; 504 so->so_state &= ~SS_HASCONNIND; 505 } 506 #ifdef DEBUG 507 mutex_enter(&so->so_lock); 508 ASSERT(so_verify_oobstate(so)); 509 mutex_exit(&so->so_lock); 510 #endif /* DEBUG */ 511 if ((mp = so->so_oobmsg) != NULL) { 512 freemsg(mp); 513 so->so_oobmsg = NULL; 514 so->so_state &= ~(SS_OOBPEND|SS_HAVEOOBDATA|SS_HADOOBDATA); 515 } 516 517 if ((mp = so->so_nl7c_rcv_mp) != NULL) { 518 so->so_nl7c_rcv_mp = NULL; 519 freemsg(mp); 520 } 521 so->so_nl7c_rcv_rval = 0; 522 if (so->so_nl7c_uri != NULL) { 523 nl7c_urifree(so); 524 /* urifree() cleared nl7c_uri */ 525 } 526 if (so->so_nl7c_flags) { 527 so->so_nl7c_flags = 0; 528 } 529 530 if (so->so_direct != NULL) { 531 sodirect_t *sodp = so->so_direct; 532 533 ASSERT(sodp->sod_uioafh == NULL); 534 535 so->so_direct = NULL; 536 kmem_cache_free(socktpi_sod_cache, sodp); 537 } 538 539 ASSERT(so->so_ux_bound_vp == NULL); 540 if ((mp = so->so_unbind_mp) != NULL) { 541 freemsg(mp); 542 so->so_unbind_mp = NULL; 543 } 544 vn_invalid(SOTOV(so)); 545 546 if (so->so_peercred != NULL) 547 crfree(so->so_peercred); 548 549 kmem_cache_free(so->so_cache, so->so_obj); 550 } 551 552 /* 553 * Update the accessed, updated, or changed times in an sonode 554 * with the current time. 555 * 556 * Note that both SunOS 4.X and 4.4BSD sockets do not present reasonable 557 * attributes in a fstat call. (They return the current time and 0 for 558 * all timestamps, respectively.) We maintain the current timestamps 559 * here primarily so that should sockmod be popped the resulting 560 * file descriptor will behave like a stream w.r.t. the timestamps. 561 */ 562 void 563 so_update_attrs(struct sonode *so, int flag) 564 { 565 time_t now = gethrestime_sec(); 566 567 mutex_enter(&so->so_lock); 568 so->so_flag |= flag; 569 if (flag & SOACC) 570 so->so_atime = now; 571 if (flag & SOMOD) 572 so->so_mtime = now; 573 mutex_exit(&so->so_lock); 574 } 575 576 /*ARGSUSED*/ 577 static int 578 socktpi_constructor(void *buf, void *cdrarg, int kmflags) 579 { 580 struct sonode *so = buf; 581 struct vnode *vp; 582 583 vp = so->so_vnode = vn_alloc(kmflags); 584 if (vp == NULL) { 585 return (-1); 586 } 587 vn_setops(vp, socktpi_vnodeops); 588 vp->v_data = so; 589 590 so->so_direct = NULL; 591 592 so->so_nl7c_flags = 0; 593 so->so_nl7c_uri = NULL; 594 so->so_nl7c_rcv_mp = NULL; 595 596 so->so_oobmsg = NULL; 597 so->so_ack_mp = NULL; 598 so->so_conn_ind_head = NULL; 599 so->so_conn_ind_tail = NULL; 600 so->so_discon_ind_mp = NULL; 601 so->so_ux_bound_vp = NULL; 602 so->so_unbind_mp = NULL; 603 so->so_accessvp = NULL; 604 so->so_laddr_sa = NULL; 605 so->so_faddr_sa = NULL; 606 so->so_ops = &sotpi_sonodeops; 607 608 mutex_init(&so->so_lock, NULL, MUTEX_DEFAULT, NULL); 609 mutex_init(&so->so_plumb_lock, NULL, MUTEX_DEFAULT, NULL); 610 cv_init(&so->so_state_cv, NULL, CV_DEFAULT, NULL); 611 cv_init(&so->so_ack_cv, NULL, CV_DEFAULT, NULL); 612 cv_init(&so->so_connind_cv, NULL, CV_DEFAULT, NULL); 613 cv_init(&so->so_want_cv, NULL, CV_DEFAULT, NULL); 614 615 return (0); 616 } 617 618 /*ARGSUSED1*/ 619 static void 620 socktpi_destructor(void *buf, void *cdrarg) 621 { 622 struct sonode *so = buf; 623 struct vnode *vp = SOTOV(so); 624 625 ASSERT(so->so_direct == NULL); 626 627 ASSERT(so->so_nl7c_flags == 0); 628 ASSERT(so->so_nl7c_uri == NULL); 629 ASSERT(so->so_nl7c_rcv_mp == NULL); 630 631 ASSERT(so->so_oobmsg == NULL); 632 ASSERT(so->so_ack_mp == NULL); 633 ASSERT(so->so_conn_ind_head == NULL); 634 ASSERT(so->so_conn_ind_tail == NULL); 635 ASSERT(so->so_discon_ind_mp == NULL); 636 ASSERT(so->so_ux_bound_vp == NULL); 637 ASSERT(so->so_unbind_mp == NULL); 638 ASSERT(so->so_ops == &sotpi_sonodeops); 639 640 ASSERT(vn_matchops(vp, socktpi_vnodeops)); 641 ASSERT(vp->v_data == so); 642 643 vn_free(vp); 644 645 mutex_destroy(&so->so_lock); 646 mutex_destroy(&so->so_plumb_lock); 647 cv_destroy(&so->so_state_cv); 648 cv_destroy(&so->so_ack_cv); 649 cv_destroy(&so->so_connind_cv); 650 cv_destroy(&so->so_want_cv); 651 } 652 653 static int 654 socktpi_unix_constructor(void *buf, void *cdrarg, int kmflags) 655 { 656 int retval; 657 658 if ((retval = socktpi_constructor(buf, cdrarg, kmflags)) == 0) { 659 struct sonode *so = (struct sonode *)buf; 660 661 mutex_enter(&socklist.sl_lock); 662 663 so->so_next = socklist.sl_list; 664 so->so_prev = NULL; 665 if (so->so_next != NULL) 666 so->so_next->so_prev = so; 667 socklist.sl_list = so; 668 669 mutex_exit(&socklist.sl_lock); 670 671 } 672 return (retval); 673 } 674 675 static void 676 socktpi_unix_destructor(void *buf, void *cdrarg) 677 { 678 struct sonode *so = (struct sonode *)buf; 679 680 mutex_enter(&socklist.sl_lock); 681 682 if (so->so_next != NULL) 683 so->so_next->so_prev = so->so_prev; 684 if (so->so_prev != NULL) 685 so->so_prev->so_next = so->so_next; 686 else 687 socklist.sl_list = so->so_next; 688 689 mutex_exit(&socklist.sl_lock); 690 691 socktpi_destructor(buf, cdrarg); 692 } 693 694 /* 695 * Init function called when sockfs is loaded. 696 */ 697 int 698 sockinit(int fstype, char *name) 699 { 700 static const fs_operation_def_t sock_vfsops_template[] = { 701 NULL, NULL 702 }; 703 int error; 704 major_t dev; 705 char *err_str; 706 707 error = vfs_setfsops(fstype, sock_vfsops_template, NULL); 708 if (error != 0) { 709 zcmn_err(GLOBAL_ZONEID, CE_WARN, 710 "sockinit: bad vfs ops template"); 711 return (error); 712 } 713 714 error = vn_make_ops(name, socktpi_vnodeops_template, &socktpi_vnodeops); 715 if (error != 0) { 716 err_str = "sockinit: bad sock vnode ops template"; 717 /* vn_make_ops() does not reset socktpi_vnodeops on failure. */ 718 socktpi_vnodeops = NULL; 719 goto failure; 720 } 721 722 error = sosctp_init(); 723 if (error != 0) { 724 err_str = NULL; 725 goto failure; 726 } 727 728 error = sosdp_init(); 729 if (error != 0) { 730 err_str = NULL; 731 goto failure; 732 } 733 734 error = sostr_init(); 735 if (error != 0) { 736 err_str = NULL; 737 goto failure; 738 } 739 740 /* 741 * Create sonode caches. We create a special one for AF_UNIX so 742 * that we can track them for netstat(1m). 743 */ 744 socktpi_cache = kmem_cache_create("socktpi_cache", 745 sizeof (struct sonode), 0, socktpi_constructor, 746 socktpi_destructor, NULL, NULL, NULL, 0); 747 748 socktpi_unix_cache = kmem_cache_create("socktpi_unix_cache", 749 sizeof (struct sonode), 0, socktpi_unix_constructor, 750 socktpi_unix_destructor, NULL, NULL, NULL, 0); 751 752 /* 753 * Build initial list mapping socket parameters to vnode. 754 */ 755 rw_init(&splist_lock, NULL, RW_DEFAULT, NULL); 756 757 /* 758 * If sockets are needed before init runs /sbin/soconfig 759 * it is possible to preload the sockparams list here using 760 * calls like: 761 * sockconfig(1,2,3, "/dev/tcp", 0); 762 */ 763 764 /* 765 * Create a unique dev_t for use in so_fsid. 766 */ 767 768 if ((dev = getudev()) == (major_t)-1) 769 dev = 0; 770 sockdev = makedevice(dev, 0); 771 772 mutex_init(&socklist.sl_lock, NULL, MUTEX_DEFAULT, NULL); 773 sendfile_init(); 774 nl7c_init(); 775 776 return (0); 777 778 failure: 779 (void) vfs_freevfsops_by_type(fstype); 780 if (socktpi_vnodeops != NULL) 781 vn_freevnodeops(socktpi_vnodeops); 782 if (err_str != NULL) 783 zcmn_err(GLOBAL_ZONEID, CE_WARN, err_str); 784 return (error); 785 } 786 787 /* 788 * Caller must hold the mutex. Used to set SOLOCKED. 789 */ 790 void 791 so_lock_single(struct sonode *so) 792 { 793 ASSERT(MUTEX_HELD(&so->so_lock)); 794 795 while (so->so_flag & (SOLOCKED | SOASYNC_UNBIND)) { 796 so->so_flag |= SOWANT; 797 cv_wait_stop(&so->so_want_cv, &so->so_lock, 798 SO_LOCK_WAKEUP_TIME); 799 } 800 so->so_flag |= SOLOCKED; 801 } 802 803 /* 804 * Caller must hold the mutex and pass in SOLOCKED or SOASYNC_UNBIND. 805 * Used to clear SOLOCKED or SOASYNC_UNBIND. 806 */ 807 void 808 so_unlock_single(struct sonode *so, int flag) 809 { 810 ASSERT(MUTEX_HELD(&so->so_lock)); 811 ASSERT(flag & (SOLOCKED|SOASYNC_UNBIND)); 812 ASSERT((flag & ~(SOLOCKED|SOASYNC_UNBIND)) == 0); 813 ASSERT(so->so_flag & flag); 814 815 /* 816 * Process the T_DISCON_IND on so_discon_ind_mp. 817 * 818 * Call to so_drain_discon_ind will result in so_lock 819 * being dropped and re-acquired later. 820 */ 821 if (so->so_discon_ind_mp != NULL) 822 so_drain_discon_ind(so); 823 824 if (so->so_flag & SOWANT) 825 cv_broadcast(&so->so_want_cv); 826 so->so_flag &= ~(SOWANT|flag); 827 } 828 829 /* 830 * Caller must hold the mutex. Used to set SOREADLOCKED. 831 * If the caller wants nonblocking behavior it should set fmode. 832 */ 833 int 834 so_lock_read(struct sonode *so, int fmode) 835 { 836 ASSERT(MUTEX_HELD(&so->so_lock)); 837 838 while (so->so_flag & SOREADLOCKED) { 839 if (fmode & (FNDELAY|FNONBLOCK)) 840 return (EWOULDBLOCK); 841 so->so_flag |= SOWANT; 842 cv_wait_stop(&so->so_want_cv, &so->so_lock, 843 SO_LOCK_WAKEUP_TIME); 844 } 845 so->so_flag |= SOREADLOCKED; 846 return (0); 847 } 848 849 /* 850 * Like so_lock_read above but allows signals. 851 */ 852 int 853 so_lock_read_intr(struct sonode *so, int fmode) 854 { 855 ASSERT(MUTEX_HELD(&so->so_lock)); 856 857 while (so->so_flag & SOREADLOCKED) { 858 if (fmode & (FNDELAY|FNONBLOCK)) 859 return (EWOULDBLOCK); 860 so->so_flag |= SOWANT; 861 if (!cv_wait_sig(&so->so_want_cv, &so->so_lock)) 862 return (EINTR); 863 } 864 so->so_flag |= SOREADLOCKED; 865 return (0); 866 } 867 868 /* 869 * Caller must hold the mutex. Used to clear SOREADLOCKED, 870 * set in so_lock_read() or so_lock_read_intr(). 871 */ 872 void 873 so_unlock_read(struct sonode *so) 874 { 875 ASSERT(MUTEX_HELD(&so->so_lock)); 876 ASSERT(so->so_flag & SOREADLOCKED); 877 878 if (so->so_flag & SOWANT) 879 cv_broadcast(&so->so_want_cv); 880 so->so_flag &= ~(SOWANT|SOREADLOCKED); 881 } 882 883 /* 884 * Verify that the specified offset falls within the mblk and 885 * that the resulting pointer is aligned. 886 * Returns NULL if not. 887 */ 888 void * 889 sogetoff(mblk_t *mp, t_uscalar_t offset, 890 t_uscalar_t length, uint_t align_size) 891 { 892 uintptr_t ptr1, ptr2; 893 894 ASSERT(mp && mp->b_wptr >= mp->b_rptr); 895 ptr1 = (uintptr_t)mp->b_rptr + offset; 896 ptr2 = (uintptr_t)ptr1 + length; 897 if (ptr1 < (uintptr_t)mp->b_rptr || ptr2 > (uintptr_t)mp->b_wptr) { 898 eprintline(0); 899 return (NULL); 900 } 901 if ((ptr1 & (align_size - 1)) != 0) { 902 eprintline(0); 903 return (NULL); 904 } 905 return ((void *)ptr1); 906 } 907 908 /* 909 * Return the AF_UNIX underlying filesystem vnode matching a given name. 910 * Makes sure the sending and the destination sonodes are compatible. 911 * The vnode is returned held. 912 * 913 * The underlying filesystem VSOCK vnode has a v_stream pointer that 914 * references the actual stream head (hence indirectly the actual sonode). 915 */ 916 static int 917 so_ux_lookup(struct sonode *so, struct sockaddr_un *soun, int checkaccess, 918 vnode_t **vpp) 919 { 920 vnode_t *vp; /* Underlying filesystem vnode */ 921 vnode_t *svp; /* sockfs vnode */ 922 struct sonode *so2; 923 int error; 924 925 dprintso(so, 1, ("so_ux_lookup(%p) name <%s>\n", (void *)so, soun->sun_path)); 926 927 error = lookupname(soun->sun_path, UIO_SYSSPACE, FOLLOW, NULLVPP, &vp); 928 if (error) { 929 eprintsoline(so, error); 930 return (error); 931 } 932 if (vp->v_type != VSOCK) { 933 error = ENOTSOCK; 934 eprintsoline(so, error); 935 goto done2; 936 } 937 938 if (checkaccess) { 939 /* 940 * Check that we have permissions to access the destination 941 * vnode. This check is not done in BSD but it is required 942 * by X/Open. 943 */ 944 if (error = VOP_ACCESS(vp, VREAD|VWRITE, 0, CRED(), NULL)) { 945 eprintsoline(so, error); 946 goto done2; 947 } 948 } 949 950 /* 951 * Check if the remote socket has been closed. 952 * 953 * Synchronize with vn_rele_stream by holding v_lock while traversing 954 * v_stream->sd_vnode. 955 */ 956 mutex_enter(&vp->v_lock); 957 if (vp->v_stream == NULL) { 958 mutex_exit(&vp->v_lock); 959 if (so->so_type == SOCK_DGRAM) 960 error = EDESTADDRREQ; 961 else 962 error = ECONNREFUSED; 963 964 eprintsoline(so, error); 965 goto done2; 966 } 967 ASSERT(vp->v_stream->sd_vnode); 968 svp = vp->v_stream->sd_vnode; 969 /* 970 * holding v_lock on underlying filesystem vnode and acquiring 971 * it on sockfs vnode. Assumes that no code ever attempts to 972 * acquire these locks in the reverse order. 973 */ 974 VN_HOLD(svp); 975 mutex_exit(&vp->v_lock); 976 977 if (svp->v_type != VSOCK) { 978 error = ENOTSOCK; 979 eprintsoline(so, error); 980 goto done; 981 } 982 983 so2 = VTOSO(svp); 984 985 if (so->so_type != so2->so_type) { 986 error = EPROTOTYPE; 987 eprintsoline(so, error); 988 goto done; 989 } 990 991 VN_RELE(svp); 992 *vpp = vp; 993 return (0); 994 995 done: 996 VN_RELE(svp); 997 done2: 998 VN_RELE(vp); 999 return (error); 1000 } 1001 1002 /* 1003 * Verify peer address for connect and sendto/sendmsg. 1004 * Since sendto/sendmsg would not get synchronous errors from the transport 1005 * provider we have to do these ugly checks in the socket layer to 1006 * preserve compatibility with SunOS 4.X. 1007 */ 1008 int 1009 so_addr_verify(struct sonode *so, const struct sockaddr *name, 1010 socklen_t namelen) 1011 { 1012 int family; 1013 1014 dprintso(so, 1, ("so_addr_verify(%p, %p, %d)\n", 1015 (void *)so, (void *)name, namelen)); 1016 1017 ASSERT(name != NULL); 1018 1019 family = so->so_family; 1020 switch (family) { 1021 case AF_INET: 1022 if (name->sa_family != family) { 1023 eprintsoline(so, EAFNOSUPPORT); 1024 return (EAFNOSUPPORT); 1025 } 1026 if (namelen != (socklen_t)sizeof (struct sockaddr_in)) { 1027 eprintsoline(so, EINVAL); 1028 return (EINVAL); 1029 } 1030 break; 1031 case AF_INET6: { 1032 #ifdef DEBUG 1033 struct sockaddr_in6 *sin6; 1034 #endif /* DEBUG */ 1035 1036 if (name->sa_family != family) { 1037 eprintsoline(so, EAFNOSUPPORT); 1038 return (EAFNOSUPPORT); 1039 } 1040 if (namelen != (socklen_t)sizeof (struct sockaddr_in6)) { 1041 eprintsoline(so, EINVAL); 1042 return (EINVAL); 1043 } 1044 #ifdef DEBUG 1045 /* Verify that apps don't forget to clear sin6_scope_id etc */ 1046 sin6 = (struct sockaddr_in6 *)name; 1047 if (sin6->sin6_scope_id != 0 && 1048 !IN6_IS_ADDR_LINKSCOPE(&sin6->sin6_addr)) { 1049 zcmn_err(getzoneid(), CE_WARN, 1050 "connect/send* with uninitialized sin6_scope_id " 1051 "(%d) on socket. Pid = %d\n", 1052 (int)sin6->sin6_scope_id, (int)curproc->p_pid); 1053 } 1054 #endif /* DEBUG */ 1055 break; 1056 } 1057 case AF_UNIX: 1058 if (so->so_state & SS_FADDR_NOXLATE) { 1059 return (0); 1060 } 1061 if (namelen < (socklen_t)sizeof (short)) { 1062 eprintsoline(so, ENOENT); 1063 return (ENOENT); 1064 } 1065 if (name->sa_family != family) { 1066 eprintsoline(so, EAFNOSUPPORT); 1067 return (EAFNOSUPPORT); 1068 } 1069 /* MAXPATHLEN + soun_family + nul termination */ 1070 if (namelen > (socklen_t)(MAXPATHLEN + sizeof (short) + 1)) { 1071 eprintsoline(so, ENAMETOOLONG); 1072 return (ENAMETOOLONG); 1073 } 1074 1075 break; 1076 1077 default: 1078 /* 1079 * Default is don't do any length or sa_family check 1080 * to allow non-sockaddr style addresses. 1081 */ 1082 break; 1083 } 1084 1085 return (0); 1086 } 1087 1088 1089 /* 1090 * Translate an AF_UNIX sockaddr_un to the transport internal name. 1091 * Assumes caller has called so_addr_verify first. 1092 */ 1093 /*ARGSUSED*/ 1094 int 1095 so_ux_addr_xlate(struct sonode *so, struct sockaddr *name, 1096 socklen_t namelen, int checkaccess, 1097 void **addrp, socklen_t *addrlenp) 1098 { 1099 int error; 1100 struct sockaddr_un *soun; 1101 vnode_t *vp; 1102 void *addr; 1103 socklen_t addrlen; 1104 1105 dprintso(so, 1, ("so_ux_addr_xlate(%p, %p, %d, %d)\n", 1106 (void *)so, (void *)name, namelen, checkaccess)); 1107 1108 ASSERT(name != NULL); 1109 ASSERT(so->so_family == AF_UNIX); 1110 ASSERT(!(so->so_state & SS_FADDR_NOXLATE)); 1111 ASSERT(namelen >= (socklen_t)sizeof (short)); 1112 ASSERT(name->sa_family == AF_UNIX); 1113 soun = (struct sockaddr_un *)name; 1114 /* 1115 * Lookup vnode for the specified path name and verify that 1116 * it is a socket. 1117 */ 1118 error = so_ux_lookup(so, soun, checkaccess, &vp); 1119 if (error) { 1120 eprintsoline(so, error); 1121 return (error); 1122 } 1123 /* 1124 * Use the address of the peer vnode as the address to send 1125 * to. We release the peer vnode here. In case it has been 1126 * closed by the time the T_CONN_REQ or T_UNIDATA_REQ reaches the 1127 * transport the message will get an error or be dropped. 1128 */ 1129 so->so_ux_faddr.soua_vp = vp; 1130 so->so_ux_faddr.soua_magic = SOU_MAGIC_EXPLICIT; 1131 addr = &so->so_ux_faddr; 1132 addrlen = (socklen_t)sizeof (so->so_ux_faddr); 1133 dprintso(so, 1, ("ux_xlate UNIX: addrlen %d, vp %p\n", 1134 addrlen, (void *)vp)); 1135 VN_RELE(vp); 1136 *addrp = addr; 1137 *addrlenp = (socklen_t)addrlen; 1138 return (0); 1139 } 1140 1141 /* 1142 * Esballoc free function for messages that contain SO_FILEP option. 1143 * Decrement the reference count on the file pointers using closef. 1144 */ 1145 void 1146 fdbuf_free(struct fdbuf *fdbuf) 1147 { 1148 int i; 1149 struct file *fp; 1150 1151 dprint(1, ("fdbuf_free: %d fds\n", fdbuf->fd_numfd)); 1152 for (i = 0; i < fdbuf->fd_numfd; i++) { 1153 /* 1154 * We need pointer size alignment for fd_fds. On a LP64 1155 * kernel, the required alignment is 8 bytes while 1156 * the option headers and values are only 4 bytes 1157 * aligned. So its safer to do a bcopy compared to 1158 * assigning fdbuf->fd_fds[i] to fp. 1159 */ 1160 bcopy((char *)&fdbuf->fd_fds[i], (char *)&fp, sizeof (fp)); 1161 dprint(1, ("fdbuf_free: [%d] = %p\n", i, (void *)fp)); 1162 (void) closef(fp); 1163 } 1164 if (fdbuf->fd_ebuf != NULL) 1165 kmem_free(fdbuf->fd_ebuf, fdbuf->fd_ebuflen); 1166 kmem_free(fdbuf, fdbuf->fd_size); 1167 } 1168 1169 /* 1170 * Allocate an esballoc'ed message for AF_UNIX file descriptor passing. 1171 * Waits if memory is not available. 1172 */ 1173 mblk_t * 1174 fdbuf_allocmsg(int size, struct fdbuf *fdbuf) 1175 { 1176 uchar_t *buf; 1177 mblk_t *mp; 1178 1179 dprint(1, ("fdbuf_allocmsg: size %d, %d fds\n", size, fdbuf->fd_numfd)); 1180 buf = kmem_alloc(size, KM_SLEEP); 1181 fdbuf->fd_ebuf = (caddr_t)buf; 1182 fdbuf->fd_ebuflen = size; 1183 fdbuf->fd_frtn.free_func = fdbuf_free; 1184 fdbuf->fd_frtn.free_arg = (caddr_t)fdbuf; 1185 1186 mp = esballoc_wait(buf, size, BPRI_MED, &fdbuf->fd_frtn); 1187 mp->b_datap->db_type = M_PROTO; 1188 return (mp); 1189 } 1190 1191 /* 1192 * Extract file descriptors from a fdbuf. 1193 * Return list in rights/rightslen. 1194 */ 1195 /*ARGSUSED*/ 1196 static int 1197 fdbuf_extract(struct fdbuf *fdbuf, void *rights, int rightslen) 1198 { 1199 int i, fd; 1200 int *rp; 1201 struct file *fp; 1202 int numfd; 1203 1204 dprint(1, ("fdbuf_extract: %d fds, len %d\n", 1205 fdbuf->fd_numfd, rightslen)); 1206 1207 numfd = fdbuf->fd_numfd; 1208 ASSERT(rightslen == numfd * (int)sizeof (int)); 1209 1210 /* 1211 * Allocate a file descriptor and increment the f_count. 1212 * The latter is needed since we always call fdbuf_free 1213 * which performs a closef. 1214 */ 1215 rp = (int *)rights; 1216 for (i = 0; i < numfd; i++) { 1217 if ((fd = ufalloc(0)) == -1) 1218 goto cleanup; 1219 /* 1220 * We need pointer size alignment for fd_fds. On a LP64 1221 * kernel, the required alignment is 8 bytes while 1222 * the option headers and values are only 4 bytes 1223 * aligned. So its safer to do a bcopy compared to 1224 * assigning fdbuf->fd_fds[i] to fp. 1225 */ 1226 bcopy((char *)&fdbuf->fd_fds[i], (char *)&fp, sizeof (fp)); 1227 mutex_enter(&fp->f_tlock); 1228 fp->f_count++; 1229 mutex_exit(&fp->f_tlock); 1230 setf(fd, fp); 1231 *rp++ = fd; 1232 if (audit_active) 1233 audit_fdrecv(fd, fp); 1234 dprint(1, ("fdbuf_extract: [%d] = %d, %p refcnt %d\n", 1235 i, fd, (void *)fp, fp->f_count)); 1236 } 1237 return (0); 1238 1239 cleanup: 1240 /* 1241 * Undo whatever partial work the loop above has done. 1242 */ 1243 { 1244 int j; 1245 1246 rp = (int *)rights; 1247 for (j = 0; j < i; j++) { 1248 dprint(0, 1249 ("fdbuf_extract: cleanup[%d] = %d\n", j, *rp)); 1250 (void) closeandsetf(*rp++, NULL); 1251 } 1252 } 1253 1254 return (EMFILE); 1255 } 1256 1257 /* 1258 * Insert file descriptors into an fdbuf. 1259 * Returns a kmem_alloc'ed fdbuf. The fdbuf should be freed 1260 * by calling fdbuf_free(). 1261 */ 1262 int 1263 fdbuf_create(void *rights, int rightslen, struct fdbuf **fdbufp) 1264 { 1265 int numfd, i; 1266 int *fds; 1267 struct file *fp; 1268 struct fdbuf *fdbuf; 1269 int fdbufsize; 1270 1271 dprint(1, ("fdbuf_create: len %d\n", rightslen)); 1272 1273 numfd = rightslen / (int)sizeof (int); 1274 1275 fdbufsize = (int)FDBUF_HDRSIZE + (numfd * (int)sizeof (struct file *)); 1276 fdbuf = kmem_alloc(fdbufsize, KM_SLEEP); 1277 fdbuf->fd_size = fdbufsize; 1278 fdbuf->fd_numfd = 0; 1279 fdbuf->fd_ebuf = NULL; 1280 fdbuf->fd_ebuflen = 0; 1281 fds = (int *)rights; 1282 for (i = 0; i < numfd; i++) { 1283 if ((fp = getf(fds[i])) == NULL) { 1284 fdbuf_free(fdbuf); 1285 return (EBADF); 1286 } 1287 dprint(1, ("fdbuf_create: [%d] = %d, %p refcnt %d\n", 1288 i, fds[i], (void *)fp, fp->f_count)); 1289 mutex_enter(&fp->f_tlock); 1290 fp->f_count++; 1291 mutex_exit(&fp->f_tlock); 1292 /* 1293 * The maximum alignment for fdbuf (or any option header 1294 * and its value) it 4 bytes. On a LP64 kernel, the alignment 1295 * is not sufficient for pointers (fd_fds in this case). Since 1296 * we just did a kmem_alloc (we get a double word alignment), 1297 * we don't need to do anything on the send side (we loose 1298 * the double word alignment because fdbuf goes after an 1299 * option header (eg T_unitdata_req) which is only 4 byte 1300 * aligned). We take care of this when we extract the file 1301 * descriptor in fdbuf_extract or fdbuf_free. 1302 */ 1303 fdbuf->fd_fds[i] = fp; 1304 fdbuf->fd_numfd++; 1305 releasef(fds[i]); 1306 if (audit_active) 1307 audit_fdsend(fds[i], fp, 0); 1308 } 1309 *fdbufp = fdbuf; 1310 return (0); 1311 } 1312 1313 static int 1314 fdbuf_optlen(int rightslen) 1315 { 1316 int numfd; 1317 1318 numfd = rightslen / (int)sizeof (int); 1319 1320 return ((int)FDBUF_HDRSIZE + (numfd * (int)sizeof (struct file *))); 1321 } 1322 1323 static t_uscalar_t 1324 fdbuf_cmsglen(int fdbuflen) 1325 { 1326 return (t_uscalar_t)((fdbuflen - FDBUF_HDRSIZE) / 1327 (int)sizeof (struct file *) * (int)sizeof (int)); 1328 } 1329 1330 1331 /* 1332 * Return non-zero if the mblk and fdbuf are consistent. 1333 */ 1334 static int 1335 fdbuf_verify(mblk_t *mp, struct fdbuf *fdbuf, int fdbuflen) 1336 { 1337 if (fdbuflen >= FDBUF_HDRSIZE && 1338 fdbuflen == fdbuf->fd_size) { 1339 frtn_t *frp = mp->b_datap->db_frtnp; 1340 /* 1341 * Check that the SO_FILEP portion of the 1342 * message has not been modified by 1343 * the loopback transport. The sending sockfs generates 1344 * a message that is esballoc'ed with the free function 1345 * being fdbuf_free() and where free_arg contains the 1346 * identical information as the SO_FILEP content. 1347 * 1348 * If any of these constraints are not satisfied we 1349 * silently ignore the option. 1350 */ 1351 ASSERT(mp); 1352 if (frp != NULL && 1353 frp->free_func == fdbuf_free && 1354 frp->free_arg != NULL && 1355 bcmp(frp->free_arg, fdbuf, fdbuflen) == 0) { 1356 dprint(1, ("fdbuf_verify: fdbuf %p len %d\n", 1357 (void *)fdbuf, fdbuflen)); 1358 return (1); 1359 } else { 1360 zcmn_err(getzoneid(), CE_WARN, 1361 "sockfs: mismatched fdbuf content (%p)", 1362 (void *)mp); 1363 return (0); 1364 } 1365 } else { 1366 zcmn_err(getzoneid(), CE_WARN, 1367 "sockfs: mismatched fdbuf len %d, %d\n", 1368 fdbuflen, fdbuf->fd_size); 1369 return (0); 1370 } 1371 } 1372 1373 /* 1374 * When the file descriptors returned by sorecvmsg can not be passed 1375 * to the application this routine will cleanup the references on 1376 * the files. Start at startoff bytes into the buffer. 1377 */ 1378 static void 1379 close_fds(void *fdbuf, int fdbuflen, int startoff) 1380 { 1381 int *fds = (int *)fdbuf; 1382 int numfd = fdbuflen / (int)sizeof (int); 1383 int i; 1384 1385 dprint(1, ("close_fds(%p, %d, %d)\n", fdbuf, fdbuflen, startoff)); 1386 1387 for (i = 0; i < numfd; i++) { 1388 if (startoff < 0) 1389 startoff = 0; 1390 if (startoff < (int)sizeof (int)) { 1391 /* 1392 * This file descriptor is partially or fully after 1393 * the offset 1394 */ 1395 dprint(0, 1396 ("close_fds: cleanup[%d] = %d\n", i, fds[i])); 1397 (void) closeandsetf(fds[i], NULL); 1398 } 1399 startoff -= (int)sizeof (int); 1400 } 1401 } 1402 1403 /* 1404 * Close all file descriptors contained in the control part starting at 1405 * the startoffset. 1406 */ 1407 void 1408 so_closefds(void *control, t_uscalar_t controllen, int oldflg, 1409 int startoff) 1410 { 1411 struct cmsghdr *cmsg; 1412 1413 if (control == NULL) 1414 return; 1415 1416 if (oldflg) { 1417 close_fds(control, controllen, startoff); 1418 return; 1419 } 1420 /* Scan control part for file descriptors. */ 1421 for (cmsg = (struct cmsghdr *)control; 1422 CMSG_VALID(cmsg, control, (uintptr_t)control + controllen); 1423 cmsg = CMSG_NEXT(cmsg)) { 1424 if (cmsg->cmsg_level == SOL_SOCKET && 1425 cmsg->cmsg_type == SCM_RIGHTS) { 1426 close_fds(CMSG_CONTENT(cmsg), 1427 (int)CMSG_CONTENTLEN(cmsg), 1428 startoff - (int)sizeof (struct cmsghdr)); 1429 } 1430 startoff -= cmsg->cmsg_len; 1431 } 1432 } 1433 1434 /* 1435 * Returns a pointer/length for the file descriptors contained 1436 * in the control buffer. Returns with *fdlenp == -1 if there are no 1437 * file descriptor options present. This is different than there being 1438 * a zero-length file descriptor option. 1439 * Fail if there are multiple SCM_RIGHT cmsgs. 1440 */ 1441 int 1442 so_getfdopt(void *control, t_uscalar_t controllen, int oldflg, 1443 void **fdsp, int *fdlenp) 1444 { 1445 struct cmsghdr *cmsg; 1446 void *fds; 1447 int fdlen; 1448 1449 if (control == NULL) { 1450 *fdsp = NULL; 1451 *fdlenp = -1; 1452 return (0); 1453 } 1454 1455 if (oldflg) { 1456 *fdsp = control; 1457 if (controllen == 0) 1458 *fdlenp = -1; 1459 else 1460 *fdlenp = controllen; 1461 dprint(1, ("so_getfdopt: old %d\n", *fdlenp)); 1462 return (0); 1463 } 1464 1465 fds = NULL; 1466 fdlen = 0; 1467 1468 for (cmsg = (struct cmsghdr *)control; 1469 CMSG_VALID(cmsg, control, (uintptr_t)control + controllen); 1470 cmsg = CMSG_NEXT(cmsg)) { 1471 if (cmsg->cmsg_level == SOL_SOCKET && 1472 cmsg->cmsg_type == SCM_RIGHTS) { 1473 if (fds != NULL) 1474 return (EINVAL); 1475 fds = CMSG_CONTENT(cmsg); 1476 fdlen = (int)CMSG_CONTENTLEN(cmsg); 1477 dprint(1, ("so_getfdopt: new %lu\n", 1478 (size_t)CMSG_CONTENTLEN(cmsg))); 1479 } 1480 } 1481 if (fds == NULL) { 1482 dprint(1, ("so_getfdopt: NONE\n")); 1483 *fdlenp = -1; 1484 } else 1485 *fdlenp = fdlen; 1486 *fdsp = fds; 1487 return (0); 1488 } 1489 1490 /* 1491 * Return the length of the options including any file descriptor options. 1492 */ 1493 t_uscalar_t 1494 so_optlen(void *control, t_uscalar_t controllen, int oldflg) 1495 { 1496 struct cmsghdr *cmsg; 1497 t_uscalar_t optlen = 0; 1498 t_uscalar_t len; 1499 1500 if (control == NULL) 1501 return (0); 1502 1503 if (oldflg) 1504 return ((t_uscalar_t)(sizeof (struct T_opthdr) + 1505 fdbuf_optlen(controllen))); 1506 1507 for (cmsg = (struct cmsghdr *)control; 1508 CMSG_VALID(cmsg, control, (uintptr_t)control + controllen); 1509 cmsg = CMSG_NEXT(cmsg)) { 1510 if (cmsg->cmsg_level == SOL_SOCKET && 1511 cmsg->cmsg_type == SCM_RIGHTS) { 1512 len = fdbuf_optlen((int)CMSG_CONTENTLEN(cmsg)); 1513 } else { 1514 len = (t_uscalar_t)CMSG_CONTENTLEN(cmsg); 1515 } 1516 optlen += (t_uscalar_t)(_TPI_ALIGN_TOPT(len) + 1517 sizeof (struct T_opthdr)); 1518 } 1519 dprint(1, ("so_optlen: controllen %d, flg %d -> optlen %d\n", 1520 controllen, oldflg, optlen)); 1521 return (optlen); 1522 } 1523 1524 /* 1525 * Copy options from control to the mblk. Skip any file descriptor options. 1526 */ 1527 void 1528 so_cmsg2opt(void *control, t_uscalar_t controllen, int oldflg, mblk_t *mp) 1529 { 1530 struct T_opthdr toh; 1531 struct cmsghdr *cmsg; 1532 1533 if (control == NULL) 1534 return; 1535 1536 if (oldflg) { 1537 /* No real options - caller has handled file descriptors */ 1538 return; 1539 } 1540 for (cmsg = (struct cmsghdr *)control; 1541 CMSG_VALID(cmsg, control, (uintptr_t)control + controllen); 1542 cmsg = CMSG_NEXT(cmsg)) { 1543 /* 1544 * Note: The caller handles file descriptors prior 1545 * to calling this function. 1546 */ 1547 t_uscalar_t len; 1548 1549 if (cmsg->cmsg_level == SOL_SOCKET && 1550 cmsg->cmsg_type == SCM_RIGHTS) 1551 continue; 1552 1553 len = (t_uscalar_t)CMSG_CONTENTLEN(cmsg); 1554 toh.level = cmsg->cmsg_level; 1555 toh.name = cmsg->cmsg_type; 1556 toh.len = len + (t_uscalar_t)sizeof (struct T_opthdr); 1557 toh.status = 0; 1558 1559 soappendmsg(mp, &toh, sizeof (toh)); 1560 soappendmsg(mp, CMSG_CONTENT(cmsg), len); 1561 mp->b_wptr += _TPI_ALIGN_TOPT(len) - len; 1562 ASSERT(mp->b_wptr <= mp->b_datap->db_lim); 1563 } 1564 } 1565 1566 /* 1567 * Return the length of the control message derived from the options. 1568 * Exclude SO_SRCADDR and SO_UNIX_CLOSE options. Include SO_FILEP. 1569 * When oldflg is set only include SO_FILEP. 1570 * so_opt2cmsg and so_cmsglen are inter-related since so_cmsglen 1571 * allocates the space that so_opt2cmsg fills. If one changes, the other should 1572 * also be checked for any possible impacts. 1573 */ 1574 t_uscalar_t 1575 so_cmsglen(mblk_t *mp, void *opt, t_uscalar_t optlen, int oldflg) 1576 { 1577 t_uscalar_t cmsglen = 0; 1578 struct T_opthdr *tohp; 1579 t_uscalar_t len; 1580 t_uscalar_t last_roundup = 0; 1581 1582 ASSERT(__TPI_TOPT_ISALIGNED(opt)); 1583 1584 for (tohp = (struct T_opthdr *)opt; 1585 tohp && _TPI_TOPT_VALID(tohp, opt, (uintptr_t)opt + optlen); 1586 tohp = _TPI_TOPT_NEXTHDR(opt, optlen, tohp)) { 1587 dprint(1, ("so_cmsglen: level 0x%x, name %d, len %d\n", 1588 tohp->level, tohp->name, tohp->len)); 1589 if (tohp->level == SOL_SOCKET && 1590 (tohp->name == SO_SRCADDR || 1591 tohp->name == SO_UNIX_CLOSE)) { 1592 continue; 1593 } 1594 if (tohp->level == SOL_SOCKET && tohp->name == SO_FILEP) { 1595 struct fdbuf *fdbuf; 1596 int fdbuflen; 1597 1598 fdbuf = (struct fdbuf *)_TPI_TOPT_DATA(tohp); 1599 fdbuflen = (int)_TPI_TOPT_DATALEN(tohp); 1600 1601 if (!fdbuf_verify(mp, fdbuf, fdbuflen)) 1602 continue; 1603 if (oldflg) { 1604 cmsglen += fdbuf_cmsglen(fdbuflen); 1605 continue; 1606 } 1607 len = fdbuf_cmsglen(fdbuflen); 1608 } else if (tohp->level == SOL_SOCKET && 1609 tohp->name == SCM_TIMESTAMP) { 1610 if (oldflg) 1611 continue; 1612 1613 if (get_udatamodel() == DATAMODEL_NATIVE) { 1614 len = sizeof (struct timeval); 1615 } else { 1616 len = sizeof (struct timeval32); 1617 } 1618 } else { 1619 if (oldflg) 1620 continue; 1621 len = (t_uscalar_t)_TPI_TOPT_DATALEN(tohp); 1622 } 1623 /* 1624 * Exclude roundup for last option to not set 1625 * MSG_CTRUNC when the cmsg fits but the padding doesn't fit. 1626 */ 1627 last_roundup = (t_uscalar_t) 1628 (ROUNDUP_cmsglen(len + (int)sizeof (struct cmsghdr)) - 1629 (len + (int)sizeof (struct cmsghdr))); 1630 cmsglen += (t_uscalar_t)(len + (int)sizeof (struct cmsghdr)) + 1631 last_roundup; 1632 } 1633 cmsglen -= last_roundup; 1634 dprint(1, ("so_cmsglen: optlen %d, flg %d -> cmsglen %d\n", 1635 optlen, oldflg, cmsglen)); 1636 return (cmsglen); 1637 } 1638 1639 /* 1640 * Copy options from options to the control. Convert SO_FILEP to 1641 * file descriptors. 1642 * Returns errno or zero. 1643 * so_opt2cmsg and so_cmsglen are inter-related since so_cmsglen 1644 * allocates the space that so_opt2cmsg fills. If one changes, the other should 1645 * also be checked for any possible impacts. 1646 */ 1647 int 1648 so_opt2cmsg(mblk_t *mp, void *opt, t_uscalar_t optlen, int oldflg, 1649 void *control, t_uscalar_t controllen) 1650 { 1651 struct T_opthdr *tohp; 1652 struct cmsghdr *cmsg; 1653 struct fdbuf *fdbuf; 1654 int fdbuflen; 1655 int error; 1656 #if defined(DEBUG) || defined(__lint) 1657 struct cmsghdr *cend = (struct cmsghdr *) 1658 (((uint8_t *)control) + ROUNDUP_cmsglen(controllen)); 1659 #endif 1660 cmsg = (struct cmsghdr *)control; 1661 1662 ASSERT(__TPI_TOPT_ISALIGNED(opt)); 1663 1664 for (tohp = (struct T_opthdr *)opt; 1665 tohp && _TPI_TOPT_VALID(tohp, opt, (uintptr_t)opt + optlen); 1666 tohp = _TPI_TOPT_NEXTHDR(opt, optlen, tohp)) { 1667 dprint(1, ("so_opt2cmsg: level 0x%x, name %d, len %d\n", 1668 tohp->level, tohp->name, tohp->len)); 1669 1670 if (tohp->level == SOL_SOCKET && 1671 (tohp->name == SO_SRCADDR || 1672 tohp->name == SO_UNIX_CLOSE)) { 1673 continue; 1674 } 1675 ASSERT((uintptr_t)cmsg <= (uintptr_t)control + controllen); 1676 if (tohp->level == SOL_SOCKET && tohp->name == SO_FILEP) { 1677 fdbuf = (struct fdbuf *)_TPI_TOPT_DATA(tohp); 1678 fdbuflen = (int)_TPI_TOPT_DATALEN(tohp); 1679 1680 if (!fdbuf_verify(mp, fdbuf, fdbuflen)) 1681 return (EPROTO); 1682 if (oldflg) { 1683 error = fdbuf_extract(fdbuf, control, 1684 (int)controllen); 1685 if (error != 0) 1686 return (error); 1687 continue; 1688 } else { 1689 int fdlen; 1690 1691 fdlen = (int)fdbuf_cmsglen( 1692 (int)_TPI_TOPT_DATALEN(tohp)); 1693 1694 cmsg->cmsg_level = tohp->level; 1695 cmsg->cmsg_type = SCM_RIGHTS; 1696 cmsg->cmsg_len = (socklen_t)(fdlen + 1697 sizeof (struct cmsghdr)); 1698 1699 error = fdbuf_extract(fdbuf, 1700 CMSG_CONTENT(cmsg), fdlen); 1701 if (error != 0) 1702 return (error); 1703 } 1704 } else if (tohp->level == SOL_SOCKET && 1705 tohp->name == SCM_TIMESTAMP) { 1706 timestruc_t *timestamp; 1707 1708 if (oldflg) 1709 continue; 1710 1711 cmsg->cmsg_level = tohp->level; 1712 cmsg->cmsg_type = tohp->name; 1713 1714 timestamp = 1715 (timestruc_t *)P2ROUNDUP((intptr_t)&tohp[1], 1716 sizeof (intptr_t)); 1717 1718 if (get_udatamodel() == DATAMODEL_NATIVE) { 1719 struct timeval tv; 1720 1721 cmsg->cmsg_len = sizeof (struct timeval) + 1722 sizeof (struct cmsghdr); 1723 tv.tv_sec = timestamp->tv_sec; 1724 tv.tv_usec = timestamp->tv_nsec / 1725 (NANOSEC / MICROSEC); 1726 /* 1727 * on LP64 systems, the struct timeval in 1728 * the destination will not be 8-byte aligned, 1729 * so use bcopy to avoid alignment trouble 1730 */ 1731 bcopy(&tv, CMSG_CONTENT(cmsg), sizeof (tv)); 1732 } else { 1733 struct timeval32 *time32; 1734 1735 cmsg->cmsg_len = sizeof (struct timeval32) + 1736 sizeof (struct cmsghdr); 1737 time32 = (struct timeval32 *)CMSG_CONTENT(cmsg); 1738 time32->tv_sec = (time32_t)timestamp->tv_sec; 1739 time32->tv_usec = 1740 (int32_t)(timestamp->tv_nsec / 1741 (NANOSEC / MICROSEC)); 1742 } 1743 1744 } else { 1745 if (oldflg) 1746 continue; 1747 1748 cmsg->cmsg_level = tohp->level; 1749 cmsg->cmsg_type = tohp->name; 1750 cmsg->cmsg_len = (socklen_t)(_TPI_TOPT_DATALEN(tohp) + 1751 sizeof (struct cmsghdr)); 1752 1753 /* copy content to control data part */ 1754 bcopy(&tohp[1], CMSG_CONTENT(cmsg), 1755 CMSG_CONTENTLEN(cmsg)); 1756 } 1757 /* move to next CMSG structure! */ 1758 cmsg = CMSG_NEXT(cmsg); 1759 } 1760 dprint(1, ("so_opt2cmsg: buf %p len %d; cend %p; final cmsg %p\n", 1761 control, controllen, (void *)cend, (void *)cmsg)); 1762 ASSERT(cmsg <= cend); 1763 return (0); 1764 } 1765 1766 /* 1767 * Extract the SO_SRCADDR option value if present. 1768 */ 1769 void 1770 so_getopt_srcaddr(void *opt, t_uscalar_t optlen, void **srcp, 1771 t_uscalar_t *srclenp) 1772 { 1773 struct T_opthdr *tohp; 1774 1775 ASSERT(__TPI_TOPT_ISALIGNED(opt)); 1776 1777 ASSERT(srcp != NULL && srclenp != NULL); 1778 *srcp = NULL; 1779 *srclenp = 0; 1780 1781 for (tohp = (struct T_opthdr *)opt; 1782 tohp && _TPI_TOPT_VALID(tohp, opt, (uintptr_t)opt + optlen); 1783 tohp = _TPI_TOPT_NEXTHDR(opt, optlen, tohp)) { 1784 dprint(1, ("so_getopt_srcaddr: level 0x%x, name %d, len %d\n", 1785 tohp->level, tohp->name, tohp->len)); 1786 if (tohp->level == SOL_SOCKET && 1787 tohp->name == SO_SRCADDR) { 1788 *srcp = _TPI_TOPT_DATA(tohp); 1789 *srclenp = (t_uscalar_t)_TPI_TOPT_DATALEN(tohp); 1790 } 1791 } 1792 } 1793 1794 /* 1795 * Verify if the SO_UNIX_CLOSE option is present. 1796 */ 1797 int 1798 so_getopt_unix_close(void *opt, t_uscalar_t optlen) 1799 { 1800 struct T_opthdr *tohp; 1801 1802 ASSERT(__TPI_TOPT_ISALIGNED(opt)); 1803 1804 for (tohp = (struct T_opthdr *)opt; 1805 tohp && _TPI_TOPT_VALID(tohp, opt, (uintptr_t)opt + optlen); 1806 tohp = _TPI_TOPT_NEXTHDR(opt, optlen, tohp)) { 1807 dprint(1, 1808 ("so_getopt_unix_close: level 0x%x, name %d, len %d\n", 1809 tohp->level, tohp->name, tohp->len)); 1810 if (tohp->level == SOL_SOCKET && 1811 tohp->name == SO_UNIX_CLOSE) 1812 return (1); 1813 } 1814 return (0); 1815 } 1816 1817 /* 1818 * Allocate an M_PROTO message. 1819 * 1820 * If allocation fails the behavior depends on sleepflg: 1821 * _ALLOC_NOSLEEP fail immediately 1822 * _ALLOC_INTR sleep for memory until a signal is caught 1823 * _ALLOC_SLEEP sleep forever. Don't return NULL. 1824 */ 1825 mblk_t * 1826 soallocproto(size_t size, int sleepflg) 1827 { 1828 mblk_t *mp; 1829 1830 /* Round up size for reuse */ 1831 size = MAX(size, 64); 1832 mp = allocb(size, BPRI_MED); 1833 if (mp == NULL) { 1834 int error; /* Dummy - error not returned to caller */ 1835 1836 switch (sleepflg) { 1837 case _ALLOC_SLEEP: 1838 mp = allocb_wait(size, BPRI_MED, STR_NOSIG, &error); 1839 ASSERT(mp); 1840 break; 1841 case _ALLOC_INTR: 1842 mp = allocb_wait(size, BPRI_MED, 0, &error); 1843 if (mp == NULL) { 1844 /* Caught signal while sleeping for memory */ 1845 eprintline(ENOBUFS); 1846 return (NULL); 1847 } 1848 break; 1849 case _ALLOC_NOSLEEP: 1850 default: 1851 eprintline(ENOBUFS); 1852 return (NULL); 1853 } 1854 } 1855 DB_TYPE(mp) = M_PROTO; 1856 return (mp); 1857 } 1858 1859 /* 1860 * Allocate an M_PROTO message with a single component. 1861 * len is the length of buf. size is the amount to allocate. 1862 * 1863 * buf can be NULL with a non-zero len. 1864 * This results in a bzero'ed chunk being placed the message. 1865 */ 1866 mblk_t * 1867 soallocproto1(const void *buf, ssize_t len, ssize_t size, int sleepflg) 1868 { 1869 mblk_t *mp; 1870 1871 if (size == 0) 1872 size = len; 1873 1874 ASSERT(size >= len); 1875 /* Round up size for reuse */ 1876 size = MAX(size, 64); 1877 mp = soallocproto(size, sleepflg); 1878 if (mp == NULL) 1879 return (NULL); 1880 mp->b_datap->db_type = M_PROTO; 1881 if (len != 0) { 1882 if (buf != NULL) 1883 bcopy(buf, mp->b_wptr, len); 1884 else 1885 bzero(mp->b_wptr, len); 1886 mp->b_wptr += len; 1887 } 1888 return (mp); 1889 } 1890 1891 /* 1892 * Append buf/len to mp. 1893 * The caller has to ensure that there is enough room in the mblk. 1894 * 1895 * buf can be NULL with a non-zero len. 1896 * This results in a bzero'ed chunk being placed the message. 1897 */ 1898 void 1899 soappendmsg(mblk_t *mp, const void *buf, ssize_t len) 1900 { 1901 ASSERT(mp); 1902 1903 if (len != 0) { 1904 /* Assert for room left */ 1905 ASSERT(mp->b_datap->db_lim - mp->b_wptr >= len); 1906 if (buf != NULL) 1907 bcopy(buf, mp->b_wptr, len); 1908 else 1909 bzero(mp->b_wptr, len); 1910 } 1911 mp->b_wptr += len; 1912 } 1913 1914 /* 1915 * Create a message using two kernel buffers. 1916 * If size is set that will determine the allocation size (e.g. for future 1917 * soappendmsg calls). If size is zero it is derived from the buffer 1918 * lengths. 1919 */ 1920 mblk_t * 1921 soallocproto2(const void *buf1, ssize_t len1, const void *buf2, ssize_t len2, 1922 ssize_t size, int sleepflg) 1923 { 1924 mblk_t *mp; 1925 1926 if (size == 0) 1927 size = len1 + len2; 1928 ASSERT(size >= len1 + len2); 1929 1930 mp = soallocproto1(buf1, len1, size, sleepflg); 1931 if (mp) 1932 soappendmsg(mp, buf2, len2); 1933 return (mp); 1934 } 1935 1936 /* 1937 * Create a message using three kernel buffers. 1938 * If size is set that will determine the allocation size (for future 1939 * soappendmsg calls). If size is zero it is derived from the buffer 1940 * lengths. 1941 */ 1942 mblk_t * 1943 soallocproto3(const void *buf1, ssize_t len1, const void *buf2, ssize_t len2, 1944 const void *buf3, ssize_t len3, ssize_t size, int sleepflg) 1945 { 1946 mblk_t *mp; 1947 1948 if (size == 0) 1949 size = len1 + len2 +len3; 1950 ASSERT(size >= len1 + len2 + len3); 1951 1952 mp = soallocproto1(buf1, len1, size, sleepflg); 1953 if (mp != NULL) { 1954 soappendmsg(mp, buf2, len2); 1955 soappendmsg(mp, buf3, len3); 1956 } 1957 return (mp); 1958 } 1959 1960 #ifdef DEBUG 1961 char * 1962 pr_state(uint_t state, uint_t mode) 1963 { 1964 static char buf[1024]; 1965 1966 buf[0] = 0; 1967 if (state & SS_ISCONNECTED) 1968 (void) strcat(buf, "ISCONNECTED "); 1969 if (state & SS_ISCONNECTING) 1970 (void) strcat(buf, "ISCONNECTING "); 1971 if (state & SS_ISDISCONNECTING) 1972 (void) strcat(buf, "ISDISCONNECTING "); 1973 if (state & SS_CANTSENDMORE) 1974 (void) strcat(buf, "CANTSENDMORE "); 1975 1976 if (state & SS_CANTRCVMORE) 1977 (void) strcat(buf, "CANTRCVMORE "); 1978 if (state & SS_ISBOUND) 1979 (void) strcat(buf, "ISBOUND "); 1980 if (state & SS_NDELAY) 1981 (void) strcat(buf, "NDELAY "); 1982 if (state & SS_NONBLOCK) 1983 (void) strcat(buf, "NONBLOCK "); 1984 1985 if (state & SS_ASYNC) 1986 (void) strcat(buf, "ASYNC "); 1987 if (state & SS_ACCEPTCONN) 1988 (void) strcat(buf, "ACCEPTCONN "); 1989 if (state & SS_HASCONNIND) 1990 (void) strcat(buf, "HASCONNIND "); 1991 if (state & SS_SAVEDEOR) 1992 (void) strcat(buf, "SAVEDEOR "); 1993 1994 if (state & SS_RCVATMARK) 1995 (void) strcat(buf, "RCVATMARK "); 1996 if (state & SS_OOBPEND) 1997 (void) strcat(buf, "OOBPEND "); 1998 if (state & SS_HAVEOOBDATA) 1999 (void) strcat(buf, "HAVEOOBDATA "); 2000 if (state & SS_HADOOBDATA) 2001 (void) strcat(buf, "HADOOBDATA "); 2002 2003 if (state & SS_FADDR_NOXLATE) 2004 (void) strcat(buf, "FADDR_NOXLATE "); 2005 2006 if (mode & SM_PRIV) 2007 (void) strcat(buf, "PRIV "); 2008 if (mode & SM_ATOMIC) 2009 (void) strcat(buf, "ATOMIC "); 2010 if (mode & SM_ADDR) 2011 (void) strcat(buf, "ADDR "); 2012 if (mode & SM_CONNREQUIRED) 2013 (void) strcat(buf, "CONNREQUIRED "); 2014 2015 if (mode & SM_FDPASSING) 2016 (void) strcat(buf, "FDPASSING "); 2017 if (mode & SM_EXDATA) 2018 (void) strcat(buf, "EXDATA "); 2019 if (mode & SM_OPTDATA) 2020 (void) strcat(buf, "OPTDATA "); 2021 if (mode & SM_BYTESTREAM) 2022 (void) strcat(buf, "BYTESTREAM "); 2023 return (buf); 2024 } 2025 2026 char * 2027 pr_addr(int family, struct sockaddr *addr, t_uscalar_t addrlen) 2028 { 2029 static char buf[1024]; 2030 2031 if (addr == NULL || addrlen == 0) { 2032 (void) sprintf(buf, "(len %d) %p", addrlen, (void *)addr); 2033 return (buf); 2034 } 2035 switch (family) { 2036 case AF_INET: { 2037 struct sockaddr_in sin; 2038 2039 bcopy(addr, &sin, sizeof (sin)); 2040 2041 (void) sprintf(buf, "(len %d) %x/%d", 2042 addrlen, ntohl(sin.sin_addr.s_addr), ntohs(sin.sin_port)); 2043 break; 2044 } 2045 case AF_INET6: { 2046 struct sockaddr_in6 sin6; 2047 uint16_t *piece = (uint16_t *)&sin6.sin6_addr; 2048 2049 bcopy((char *)addr, (char *)&sin6, sizeof (sin6)); 2050 (void) sprintf(buf, "(len %d) %x:%x:%x:%x:%x:%x:%x:%x/%d", 2051 addrlen, 2052 ntohs(piece[0]), ntohs(piece[1]), 2053 ntohs(piece[2]), ntohs(piece[3]), 2054 ntohs(piece[4]), ntohs(piece[5]), 2055 ntohs(piece[6]), ntohs(piece[7]), 2056 ntohs(sin6.sin6_port)); 2057 break; 2058 } 2059 case AF_UNIX: { 2060 struct sockaddr_un *soun = (struct sockaddr_un *)addr; 2061 2062 (void) sprintf(buf, "(len %d) %s", addrlen, 2063 (soun == NULL) ? "(none)" : soun->sun_path); 2064 break; 2065 } 2066 default: 2067 (void) sprintf(buf, "(unknown af %d)", family); 2068 break; 2069 } 2070 return (buf); 2071 } 2072 2073 /* The logical equivalence operator (a if-and-only-if b) */ 2074 #define EQUIV(a, b) (((a) && (b)) || (!(a) && (!(b)))) 2075 2076 /* 2077 * Verify limitations and invariants on oob state. 2078 * Return 1 if OK, otherwise 0 so that it can be used as 2079 * ASSERT(verify_oobstate(so)); 2080 */ 2081 int 2082 so_verify_oobstate(struct sonode *so) 2083 { 2084 ASSERT(MUTEX_HELD(&so->so_lock)); 2085 2086 /* 2087 * The possible state combinations are: 2088 * 0 2089 * SS_OOBPEND 2090 * SS_OOBPEND|SS_HAVEOOBDATA 2091 * SS_OOBPEND|SS_HADOOBDATA 2092 * SS_HADOOBDATA 2093 */ 2094 switch (so->so_state & (SS_OOBPEND|SS_HAVEOOBDATA|SS_HADOOBDATA)) { 2095 case 0: 2096 case SS_OOBPEND: 2097 case SS_OOBPEND|SS_HAVEOOBDATA: 2098 case SS_OOBPEND|SS_HADOOBDATA: 2099 case SS_HADOOBDATA: 2100 break; 2101 default: 2102 printf("Bad oob state 1 (%p): counts %d/%d state %s\n", 2103 (void *)so, so->so_oobsigcnt, 2104 so->so_oobcnt, pr_state(so->so_state, so->so_mode)); 2105 return (0); 2106 } 2107 2108 /* SS_RCVATMARK should only be set when SS_OOBPEND is set */ 2109 if ((so->so_state & (SS_RCVATMARK|SS_OOBPEND)) == SS_RCVATMARK) { 2110 printf("Bad oob state 2 (%p): counts %d/%d state %s\n", 2111 (void *)so, so->so_oobsigcnt, 2112 so->so_oobcnt, pr_state(so->so_state, so->so_mode)); 2113 return (0); 2114 } 2115 2116 /* 2117 * (so_oobsigcnt != 0 or SS_RCVATMARK) iff SS_OOBPEND 2118 */ 2119 if (!EQUIV((so->so_oobsigcnt != 0) || (so->so_state & SS_RCVATMARK), 2120 so->so_state & SS_OOBPEND)) { 2121 printf("Bad oob state 3 (%p): counts %d/%d state %s\n", 2122 (void *)so, so->so_oobsigcnt, 2123 so->so_oobcnt, pr_state(so->so_state, so->so_mode)); 2124 return (0); 2125 } 2126 2127 /* 2128 * Unless SO_OOBINLINE we have so_oobmsg != NULL iff SS_HAVEOOBDATA 2129 */ 2130 if (!(so->so_options & SO_OOBINLINE) && 2131 !EQUIV(so->so_oobmsg != NULL, so->so_state & SS_HAVEOOBDATA)) { 2132 printf("Bad oob state 4 (%p): counts %d/%d state %s\n", 2133 (void *)so, so->so_oobsigcnt, 2134 so->so_oobcnt, pr_state(so->so_state, so->so_mode)); 2135 return (0); 2136 } 2137 if (so->so_oobsigcnt < so->so_oobcnt) { 2138 printf("Bad oob state 5 (%p): counts %d/%d state %s\n", 2139 (void *)so, so->so_oobsigcnt, 2140 so->so_oobcnt, pr_state(so->so_state, so->so_mode)); 2141 return (0); 2142 } 2143 return (1); 2144 } 2145 #undef EQUIV 2146 2147 #endif /* DEBUG */ 2148 2149 /* initialize sockfs zone specific kstat related items */ 2150 void * 2151 sock_kstat_init(zoneid_t zoneid) 2152 { 2153 kstat_t *ksp; 2154 2155 ksp = kstat_create_zone("sockfs", 0, "sock_unix_list", "misc", 2156 KSTAT_TYPE_RAW, 0, KSTAT_FLAG_VAR_SIZE|KSTAT_FLAG_VIRTUAL, zoneid); 2157 2158 if (ksp != NULL) { 2159 ksp->ks_update = sockfs_update; 2160 ksp->ks_snapshot = sockfs_snapshot; 2161 ksp->ks_lock = &socklist.sl_lock; 2162 ksp->ks_private = (void *)(uintptr_t)zoneid; 2163 kstat_install(ksp); 2164 } 2165 2166 return (ksp); 2167 } 2168 2169 /* tear down sockfs zone specific kstat related items */ 2170 /*ARGSUSED*/ 2171 void 2172 sock_kstat_fini(zoneid_t zoneid, void *arg) 2173 { 2174 kstat_t *ksp = (kstat_t *)arg; 2175 2176 if (ksp != NULL) { 2177 ASSERT(zoneid == (zoneid_t)(uintptr_t)ksp->ks_private); 2178 kstat_delete(ksp); 2179 } 2180 } 2181 2182 /* 2183 * Zones: 2184 * Note that nactive is going to be different for each zone. 2185 * This means we require kstat to call sockfs_update and then sockfs_snapshot 2186 * for the same zone, or sockfs_snapshot will be taken into the wrong size 2187 * buffer. This is safe, but if the buffer is too small, user will not be 2188 * given details of all sockets. However, as this kstat has a ks_lock, kstat 2189 * driver will keep it locked between the update and the snapshot, so no 2190 * other process (zone) can currently get inbetween resulting in a wrong size 2191 * buffer allocation. 2192 */ 2193 static int 2194 sockfs_update(kstat_t *ksp, int rw) 2195 { 2196 uint_t nactive = 0; /* # of active AF_UNIX sockets */ 2197 struct sonode *so; /* current sonode on socklist */ 2198 zoneid_t myzoneid = (zoneid_t)(uintptr_t)ksp->ks_private; 2199 2200 ASSERT((zoneid_t)(uintptr_t)ksp->ks_private == getzoneid()); 2201 2202 if (rw == KSTAT_WRITE) { /* bounce all writes */ 2203 return (EACCES); 2204 } 2205 2206 for (so = socklist.sl_list; so != NULL; so = so->so_next) { 2207 if (so->so_accessvp != NULL && so->so_zoneid == myzoneid) { 2208 nactive++; 2209 } 2210 } 2211 ksp->ks_ndata = nactive; 2212 ksp->ks_data_size = nactive * sizeof (struct k_sockinfo); 2213 2214 return (0); 2215 } 2216 2217 static int 2218 sockfs_snapshot(kstat_t *ksp, void *buf, int rw) 2219 { 2220 int ns; /* # of sonodes we've copied */ 2221 struct sonode *so; /* current sonode on socklist */ 2222 struct k_sockinfo *pksi; /* where we put sockinfo data */ 2223 t_uscalar_t sn_len; /* soa_len */ 2224 zoneid_t myzoneid = (zoneid_t)(uintptr_t)ksp->ks_private; 2225 2226 ASSERT((zoneid_t)(uintptr_t)ksp->ks_private == getzoneid()); 2227 2228 ksp->ks_snaptime = gethrtime(); 2229 2230 if (rw == KSTAT_WRITE) { /* bounce all writes */ 2231 return (EACCES); 2232 } 2233 2234 /* 2235 * for each sonode on the socklist, we massage the important 2236 * info into buf, in k_sockinfo format. 2237 */ 2238 pksi = (struct k_sockinfo *)buf; 2239 for (ns = 0, so = socklist.sl_list; so != NULL; so = so->so_next) { 2240 /* only stuff active sonodes and the same zone: */ 2241 if (so->so_accessvp == NULL || so->so_zoneid != myzoneid) { 2242 continue; 2243 } 2244 2245 /* 2246 * If the sonode was activated between the update and the 2247 * snapshot, we're done - as this is only a snapshot. 2248 */ 2249 if ((caddr_t)(pksi) >= (caddr_t)buf + ksp->ks_data_size) { 2250 break; 2251 } 2252 2253 /* copy important info into buf: */ 2254 pksi->ks_si.si_size = sizeof (struct k_sockinfo); 2255 pksi->ks_si.si_family = so->so_family; 2256 pksi->ks_si.si_type = so->so_type; 2257 pksi->ks_si.si_flag = so->so_flag; 2258 pksi->ks_si.si_state = so->so_state; 2259 pksi->ks_si.si_serv_type = so->so_serv_type; 2260 pksi->ks_si.si_ux_laddr_sou_magic = so->so_ux_laddr.soua_magic; 2261 pksi->ks_si.si_ux_faddr_sou_magic = so->so_ux_faddr.soua_magic; 2262 pksi->ks_si.si_laddr_soa_len = so->so_laddr.soa_len; 2263 pksi->ks_si.si_faddr_soa_len = so->so_faddr.soa_len; 2264 pksi->ks_si.si_szoneid = so->so_zoneid; 2265 2266 mutex_enter(&so->so_lock); 2267 2268 if (so->so_laddr_sa != NULL) { 2269 ASSERT(so->so_laddr_sa->sa_data != NULL); 2270 sn_len = so->so_laddr_len; 2271 ASSERT(sn_len <= sizeof (short) + 2272 sizeof (pksi->ks_si.si_laddr_sun_path)); 2273 2274 pksi->ks_si.si_laddr_family = 2275 so->so_laddr_sa->sa_family; 2276 if (sn_len != 0) { 2277 /* AF_UNIX socket names are NULL terminated */ 2278 (void) strncpy(pksi->ks_si.si_laddr_sun_path, 2279 so->so_laddr_sa->sa_data, 2280 sizeof (pksi->ks_si.si_laddr_sun_path)); 2281 sn_len = strlen(pksi->ks_si.si_laddr_sun_path); 2282 } 2283 pksi->ks_si.si_laddr_sun_path[sn_len] = 0; 2284 } 2285 2286 if (so->so_faddr_sa != NULL) { 2287 ASSERT(so->so_faddr_sa->sa_data != NULL); 2288 sn_len = so->so_faddr_len; 2289 ASSERT(sn_len <= sizeof (short) + 2290 sizeof (pksi->ks_si.si_faddr_sun_path)); 2291 2292 pksi->ks_si.si_faddr_family = 2293 so->so_faddr_sa->sa_family; 2294 if (sn_len != 0) { 2295 (void) strncpy(pksi->ks_si.si_faddr_sun_path, 2296 so->so_faddr_sa->sa_data, 2297 sizeof (pksi->ks_si.si_faddr_sun_path)); 2298 sn_len = strlen(pksi->ks_si.si_faddr_sun_path); 2299 } 2300 pksi->ks_si.si_faddr_sun_path[sn_len] = 0; 2301 } 2302 2303 mutex_exit(&so->so_lock); 2304 2305 (void) sprintf(pksi->ks_straddr[0], "%p", (void *)so); 2306 (void) sprintf(pksi->ks_straddr[1], "%p", 2307 (void *)so->so_ux_laddr.soua_vp); 2308 (void) sprintf(pksi->ks_straddr[2], "%p", 2309 (void *)so->so_ux_faddr.soua_vp); 2310 2311 ns++; 2312 pksi++; 2313 } 2314 2315 ksp->ks_ndata = ns; 2316 return (0); 2317 } 2318 2319 ssize_t 2320 soreadfile(file_t *fp, uchar_t *buf, u_offset_t fileoff, int *err, size_t size) 2321 { 2322 struct uio auio; 2323 struct iovec aiov[MSG_MAXIOVLEN]; 2324 register vnode_t *vp; 2325 int ioflag, rwflag; 2326 ssize_t cnt; 2327 int error = 0; 2328 int iovcnt = 0; 2329 short fflag; 2330 2331 vp = fp->f_vnode; 2332 fflag = fp->f_flag; 2333 2334 rwflag = 0; 2335 aiov[0].iov_base = (caddr_t)buf; 2336 aiov[0].iov_len = size; 2337 iovcnt = 1; 2338 cnt = (ssize_t)size; 2339 (void) VOP_RWLOCK(vp, rwflag, NULL); 2340 2341 auio.uio_loffset = fileoff; 2342 auio.uio_iov = aiov; 2343 auio.uio_iovcnt = iovcnt; 2344 auio.uio_resid = cnt; 2345 auio.uio_segflg = UIO_SYSSPACE; 2346 auio.uio_llimit = MAXOFFSET_T; 2347 auio.uio_fmode = fflag; 2348 auio.uio_extflg = UIO_COPY_CACHED; 2349 2350 ioflag = auio.uio_fmode & (FAPPEND|FSYNC|FDSYNC|FRSYNC); 2351 2352 /* If read sync is not asked for, filter sync flags */ 2353 if ((ioflag & FRSYNC) == 0) 2354 ioflag &= ~(FSYNC|FDSYNC); 2355 error = VOP_READ(vp, &auio, ioflag, fp->f_cred, NULL); 2356 cnt -= auio.uio_resid; 2357 2358 VOP_RWUNLOCK(vp, rwflag, NULL); 2359 2360 if (error == EINTR && cnt != 0) 2361 error = 0; 2362 out: 2363 if (error != 0) { 2364 *err = error; 2365 return (0); 2366 } else { 2367 *err = 0; 2368 return (cnt); 2369 } 2370 } 2371