xref: /titanic_50/usr/src/uts/common/fs/sockfs/socksubr.c (revision fdf4286765e129590dce97b37d12188bf7000b58)
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, 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 			    devpath, error));
337 			*errorp = EPROTONOSUPPORT;
338 			return (NULL);
339 		}
340 		dprint(0, ("solookup: %p => vp %p, dev 0x%lx\n",
341 		    devpath, 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, 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", 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", so, name, namelen));
1015 
1016 	ASSERT(name != NULL);
1017 
1018 	family = so->so_family;
1019 	switch (family) {
1020 	case AF_INET:
1021 		if (name->sa_family != family) {
1022 			eprintsoline(so, EAFNOSUPPORT);
1023 			return (EAFNOSUPPORT);
1024 		}
1025 		if (namelen != (socklen_t)sizeof (struct sockaddr_in)) {
1026 			eprintsoline(so, EINVAL);
1027 			return (EINVAL);
1028 		}
1029 		break;
1030 	case AF_INET6: {
1031 #ifdef DEBUG
1032 		struct sockaddr_in6 *sin6;
1033 #endif /* DEBUG */
1034 
1035 		if (name->sa_family != family) {
1036 			eprintsoline(so, EAFNOSUPPORT);
1037 			return (EAFNOSUPPORT);
1038 		}
1039 		if (namelen != (socklen_t)sizeof (struct sockaddr_in6)) {
1040 			eprintsoline(so, EINVAL);
1041 			return (EINVAL);
1042 		}
1043 #ifdef DEBUG
1044 		/* Verify that apps don't forget to clear sin6_scope_id etc */
1045 		sin6 = (struct sockaddr_in6 *)name;
1046 		if (sin6->sin6_scope_id != 0 &&
1047 		    !IN6_IS_ADDR_LINKSCOPE(&sin6->sin6_addr)) {
1048 			zcmn_err(getzoneid(), CE_WARN,
1049 			    "connect/send* with uninitialized sin6_scope_id "
1050 			    "(%d) on socket. Pid = %d\n",
1051 			    (int)sin6->sin6_scope_id, (int)curproc->p_pid);
1052 		}
1053 #endif /* DEBUG */
1054 		break;
1055 	}
1056 	case AF_UNIX:
1057 		if (so->so_state & SS_FADDR_NOXLATE) {
1058 			return (0);
1059 		}
1060 		if (namelen < (socklen_t)sizeof (short)) {
1061 			eprintsoline(so, ENOENT);
1062 			return (ENOENT);
1063 		}
1064 		if (name->sa_family != family) {
1065 			eprintsoline(so, EAFNOSUPPORT);
1066 			return (EAFNOSUPPORT);
1067 		}
1068 		/* MAXPATHLEN + soun_family + nul termination */
1069 		if (namelen > (socklen_t)(MAXPATHLEN + sizeof (short) + 1)) {
1070 			eprintsoline(so, ENAMETOOLONG);
1071 			return (ENAMETOOLONG);
1072 		}
1073 
1074 		break;
1075 
1076 	default:
1077 		/*
1078 		 * Default is don't do any length or sa_family check
1079 		 * to allow non-sockaddr style addresses.
1080 		 */
1081 		break;
1082 	}
1083 
1084 	return (0);
1085 }
1086 
1087 
1088 /*
1089  * Translate an AF_UNIX sockaddr_un to the transport internal name.
1090  * Assumes caller has called so_addr_verify first.
1091  */
1092 /*ARGSUSED*/
1093 int
1094 so_ux_addr_xlate(struct sonode *so, struct sockaddr *name,
1095     socklen_t namelen, int checkaccess,
1096     void **addrp, socklen_t *addrlenp)
1097 {
1098 	int			error;
1099 	struct sockaddr_un	*soun;
1100 	vnode_t			*vp;
1101 	void			*addr;
1102 	socklen_t		addrlen;
1103 
1104 	dprintso(so, 1, ("so_ux_addr_xlate(%p, %p, %d, %d)\n",
1105 	    so, name, namelen, checkaccess));
1106 
1107 	ASSERT(name != NULL);
1108 	ASSERT(so->so_family == AF_UNIX);
1109 	ASSERT(!(so->so_state & SS_FADDR_NOXLATE));
1110 	ASSERT(namelen >= (socklen_t)sizeof (short));
1111 	ASSERT(name->sa_family == AF_UNIX);
1112 	soun = (struct sockaddr_un *)name;
1113 	/*
1114 	 * Lookup vnode for the specified path name and verify that
1115 	 * it is a socket.
1116 	 */
1117 	error = so_ux_lookup(so, soun, checkaccess, &vp);
1118 	if (error) {
1119 		eprintsoline(so, error);
1120 		return (error);
1121 	}
1122 	/*
1123 	 * Use the address of the peer vnode as the address to send
1124 	 * to. We release the peer vnode here. In case it has been
1125 	 * closed by the time the T_CONN_REQ or T_UNIDATA_REQ reaches the
1126 	 * transport the message will get an error or be dropped.
1127 	 */
1128 	so->so_ux_faddr.soua_vp = vp;
1129 	so->so_ux_faddr.soua_magic = SOU_MAGIC_EXPLICIT;
1130 	addr = &so->so_ux_faddr;
1131 	addrlen = (socklen_t)sizeof (so->so_ux_faddr);
1132 	dprintso(so, 1, ("ux_xlate UNIX: addrlen %d, vp %p\n", addrlen, vp));
1133 	VN_RELE(vp);
1134 	*addrp = addr;
1135 	*addrlenp = (socklen_t)addrlen;
1136 	return (0);
1137 }
1138 
1139 /*
1140  * Esballoc free function for messages that contain SO_FILEP option.
1141  * Decrement the reference count on the file pointers using closef.
1142  */
1143 void
1144 fdbuf_free(struct fdbuf *fdbuf)
1145 {
1146 	int	i;
1147 	struct file *fp;
1148 
1149 	dprint(1, ("fdbuf_free: %d fds\n", fdbuf->fd_numfd));
1150 	for (i = 0; i < fdbuf->fd_numfd; i++) {
1151 		/*
1152 		 * We need pointer size alignment for fd_fds. On a LP64
1153 		 * kernel, the required alignment is 8 bytes while
1154 		 * the option headers and values are only 4 bytes
1155 		 * aligned. So its safer to do a bcopy compared to
1156 		 * assigning fdbuf->fd_fds[i] to fp.
1157 		 */
1158 		bcopy((char *)&fdbuf->fd_fds[i], (char *)&fp, sizeof (fp));
1159 		dprint(1, ("fdbuf_free: [%d] = %p\n", i, fp));
1160 		(void) closef(fp);
1161 	}
1162 	if (fdbuf->fd_ebuf != NULL)
1163 		kmem_free(fdbuf->fd_ebuf, fdbuf->fd_ebuflen);
1164 	kmem_free(fdbuf, fdbuf->fd_size);
1165 }
1166 
1167 /*
1168  * Allocate an esballoc'ed message for AF_UNIX file descriptor passing.
1169  * Waits if memory is not available.
1170  */
1171 mblk_t *
1172 fdbuf_allocmsg(int size, struct fdbuf *fdbuf)
1173 {
1174 	uchar_t	*buf;
1175 	mblk_t	*mp;
1176 
1177 	dprint(1, ("fdbuf_allocmsg: size %d, %d fds\n", size, fdbuf->fd_numfd));
1178 	buf = kmem_alloc(size, KM_SLEEP);
1179 	fdbuf->fd_ebuf = (caddr_t)buf;
1180 	fdbuf->fd_ebuflen = size;
1181 	fdbuf->fd_frtn.free_func = fdbuf_free;
1182 	fdbuf->fd_frtn.free_arg = (caddr_t)fdbuf;
1183 
1184 	mp = esballoc_wait(buf, size, BPRI_MED, &fdbuf->fd_frtn);
1185 	mp->b_datap->db_type = M_PROTO;
1186 	return (mp);
1187 }
1188 
1189 /*
1190  * Extract file descriptors from a fdbuf.
1191  * Return list in rights/rightslen.
1192  */
1193 /*ARGSUSED*/
1194 static int
1195 fdbuf_extract(struct fdbuf *fdbuf, void *rights, int rightslen)
1196 {
1197 	int	i, fd;
1198 	int	*rp;
1199 	struct file *fp;
1200 	int	numfd;
1201 
1202 	dprint(1, ("fdbuf_extract: %d fds, len %d\n",
1203 	    fdbuf->fd_numfd, rightslen));
1204 
1205 	numfd = fdbuf->fd_numfd;
1206 	ASSERT(rightslen == numfd * (int)sizeof (int));
1207 
1208 	/*
1209 	 * Allocate a file descriptor and increment the f_count.
1210 	 * The latter is needed since we always call fdbuf_free
1211 	 * which performs a closef.
1212 	 */
1213 	rp = (int *)rights;
1214 	for (i = 0; i < numfd; i++) {
1215 		if ((fd = ufalloc(0)) == -1)
1216 			goto cleanup;
1217 		/*
1218 		 * We need pointer size alignment for fd_fds. On a LP64
1219 		 * kernel, the required alignment is 8 bytes while
1220 		 * the option headers and values are only 4 bytes
1221 		 * aligned. So its safer to do a bcopy compared to
1222 		 * assigning fdbuf->fd_fds[i] to fp.
1223 		 */
1224 		bcopy((char *)&fdbuf->fd_fds[i], (char *)&fp, sizeof (fp));
1225 		mutex_enter(&fp->f_tlock);
1226 		fp->f_count++;
1227 		mutex_exit(&fp->f_tlock);
1228 		setf(fd, fp);
1229 		*rp++ = fd;
1230 		if (audit_active)
1231 			audit_fdrecv(fd, fp);
1232 		dprint(1, ("fdbuf_extract: [%d] = %d, %p refcnt %d\n",
1233 		    i, fd, fp, fp->f_count));
1234 	}
1235 	return (0);
1236 
1237 cleanup:
1238 	/*
1239 	 * Undo whatever partial work the loop above has done.
1240 	 */
1241 	{
1242 		int j;
1243 
1244 		rp = (int *)rights;
1245 		for (j = 0; j < i; j++) {
1246 			dprint(0,
1247 			    ("fdbuf_extract: cleanup[%d] = %d\n", j, *rp));
1248 			(void) closeandsetf(*rp++, NULL);
1249 		}
1250 	}
1251 
1252 	return (EMFILE);
1253 }
1254 
1255 /*
1256  * Insert file descriptors into an fdbuf.
1257  * Returns a kmem_alloc'ed fdbuf. The fdbuf should be freed
1258  * by calling fdbuf_free().
1259  */
1260 int
1261 fdbuf_create(void *rights, int rightslen, struct fdbuf **fdbufp)
1262 {
1263 	int		numfd, i;
1264 	int		*fds;
1265 	struct file	*fp;
1266 	struct fdbuf	*fdbuf;
1267 	int		fdbufsize;
1268 
1269 	dprint(1, ("fdbuf_create: len %d\n", rightslen));
1270 
1271 	numfd = rightslen / (int)sizeof (int);
1272 
1273 	fdbufsize = (int)FDBUF_HDRSIZE + (numfd * (int)sizeof (struct file *));
1274 	fdbuf = kmem_alloc(fdbufsize, KM_SLEEP);
1275 	fdbuf->fd_size = fdbufsize;
1276 	fdbuf->fd_numfd = 0;
1277 	fdbuf->fd_ebuf = NULL;
1278 	fdbuf->fd_ebuflen = 0;
1279 	fds = (int *)rights;
1280 	for (i = 0; i < numfd; i++) {
1281 		if ((fp = getf(fds[i])) == NULL) {
1282 			fdbuf_free(fdbuf);
1283 			return (EBADF);
1284 		}
1285 		dprint(1, ("fdbuf_create: [%d] = %d, %p refcnt %d\n",
1286 		    i, fds[i], fp, fp->f_count));
1287 		mutex_enter(&fp->f_tlock);
1288 		fp->f_count++;
1289 		mutex_exit(&fp->f_tlock);
1290 		/*
1291 		 * The maximum alignment for fdbuf (or any option header
1292 		 * and its value) it 4 bytes. On a LP64 kernel, the alignment
1293 		 * is not sufficient for pointers (fd_fds in this case). Since
1294 		 * we just did a kmem_alloc (we get a double word alignment),
1295 		 * we don't need to do anything on the send side (we loose
1296 		 * the double word alignment because fdbuf goes after an
1297 		 * option header (eg T_unitdata_req) which is only 4 byte
1298 		 * aligned). We take care of this when we extract the file
1299 		 * descriptor in fdbuf_extract or fdbuf_free.
1300 		 */
1301 		fdbuf->fd_fds[i] = fp;
1302 		fdbuf->fd_numfd++;
1303 		releasef(fds[i]);
1304 		if (audit_active)
1305 			audit_fdsend(fds[i], fp, 0);
1306 	}
1307 	*fdbufp = fdbuf;
1308 	return (0);
1309 }
1310 
1311 static int
1312 fdbuf_optlen(int rightslen)
1313 {
1314 	int numfd;
1315 
1316 	numfd = rightslen / (int)sizeof (int);
1317 
1318 	return ((int)FDBUF_HDRSIZE + (numfd * (int)sizeof (struct file *)));
1319 }
1320 
1321 static t_uscalar_t
1322 fdbuf_cmsglen(int fdbuflen)
1323 {
1324 	return (t_uscalar_t)((fdbuflen - FDBUF_HDRSIZE) /
1325 	    (int)sizeof (struct file *) * (int)sizeof (int));
1326 }
1327 
1328 
1329 /*
1330  * Return non-zero if the mblk and fdbuf are consistent.
1331  */
1332 static int
1333 fdbuf_verify(mblk_t *mp, struct fdbuf *fdbuf, int fdbuflen)
1334 {
1335 	if (fdbuflen >= FDBUF_HDRSIZE &&
1336 	    fdbuflen == fdbuf->fd_size) {
1337 		frtn_t *frp = mp->b_datap->db_frtnp;
1338 		/*
1339 		 * Check that the SO_FILEP portion of the
1340 		 * message has not been modified by
1341 		 * the loopback transport. The sending sockfs generates
1342 		 * a message that is esballoc'ed with the free function
1343 		 * being fdbuf_free() and where free_arg contains the
1344 		 * identical information as the SO_FILEP content.
1345 		 *
1346 		 * If any of these constraints are not satisfied we
1347 		 * silently ignore the option.
1348 		 */
1349 		ASSERT(mp);
1350 		if (frp != NULL &&
1351 		    frp->free_func == fdbuf_free &&
1352 		    frp->free_arg != NULL &&
1353 		    bcmp(frp->free_arg, fdbuf, fdbuflen) == 0) {
1354 			dprint(1, ("fdbuf_verify: fdbuf %p len %d\n",
1355 			    fdbuf, fdbuflen));
1356 			return (1);
1357 		} else {
1358 			zcmn_err(getzoneid(), CE_WARN,
1359 			    "sockfs: mismatched fdbuf content (%p)",
1360 			    (void *)mp);
1361 			return (0);
1362 		}
1363 	} else {
1364 		zcmn_err(getzoneid(), CE_WARN,
1365 		    "sockfs: mismatched fdbuf len %d, %d\n",
1366 		    fdbuflen, fdbuf->fd_size);
1367 		return (0);
1368 	}
1369 }
1370 
1371 /*
1372  * When the file descriptors returned by sorecvmsg can not be passed
1373  * to the application this routine will cleanup the references on
1374  * the files. Start at startoff bytes into the buffer.
1375  */
1376 static void
1377 close_fds(void *fdbuf, int fdbuflen, int startoff)
1378 {
1379 	int *fds = (int *)fdbuf;
1380 	int numfd = fdbuflen / (int)sizeof (int);
1381 	int i;
1382 
1383 	dprint(1, ("close_fds(%p, %d, %d)\n", fdbuf, fdbuflen, startoff));
1384 
1385 	for (i = 0; i < numfd; i++) {
1386 		if (startoff < 0)
1387 			startoff = 0;
1388 		if (startoff < (int)sizeof (int)) {
1389 			/*
1390 			 * This file descriptor is partially or fully after
1391 			 * the offset
1392 			 */
1393 			dprint(0,
1394 			    ("close_fds: cleanup[%d] = %d\n", i, fds[i]));
1395 			(void) closeandsetf(fds[i], NULL);
1396 		}
1397 		startoff -= (int)sizeof (int);
1398 	}
1399 }
1400 
1401 /*
1402  * Close all file descriptors contained in the control part starting at
1403  * the startoffset.
1404  */
1405 void
1406 so_closefds(void *control, t_uscalar_t controllen, int oldflg,
1407     int startoff)
1408 {
1409 	struct cmsghdr *cmsg;
1410 
1411 	if (control == NULL)
1412 		return;
1413 
1414 	if (oldflg) {
1415 		close_fds(control, controllen, startoff);
1416 		return;
1417 	}
1418 	/* Scan control part for file descriptors. */
1419 	for (cmsg = (struct cmsghdr *)control;
1420 	    CMSG_VALID(cmsg, control, (uintptr_t)control + controllen);
1421 	    cmsg = CMSG_NEXT(cmsg)) {
1422 		if (cmsg->cmsg_level == SOL_SOCKET &&
1423 		    cmsg->cmsg_type == SCM_RIGHTS) {
1424 			close_fds(CMSG_CONTENT(cmsg),
1425 			    (int)CMSG_CONTENTLEN(cmsg),
1426 			    startoff - (int)sizeof (struct cmsghdr));
1427 		}
1428 		startoff -= cmsg->cmsg_len;
1429 	}
1430 }
1431 
1432 /*
1433  * Returns a pointer/length for the file descriptors contained
1434  * in the control buffer. Returns with *fdlenp == -1 if there are no
1435  * file descriptor options present. This is different than there being
1436  * a zero-length file descriptor option.
1437  * Fail if there are multiple SCM_RIGHT cmsgs.
1438  */
1439 int
1440 so_getfdopt(void *control, t_uscalar_t controllen, int oldflg,
1441     void **fdsp, int *fdlenp)
1442 {
1443 	struct cmsghdr *cmsg;
1444 	void *fds;
1445 	int fdlen;
1446 
1447 	if (control == NULL) {
1448 		*fdsp = NULL;
1449 		*fdlenp = -1;
1450 		return (0);
1451 	}
1452 
1453 	if (oldflg) {
1454 		*fdsp = control;
1455 		if (controllen == 0)
1456 			*fdlenp = -1;
1457 		else
1458 			*fdlenp = controllen;
1459 		dprint(1, ("so_getfdopt: old %d\n", *fdlenp));
1460 		return (0);
1461 	}
1462 
1463 	fds = NULL;
1464 	fdlen = 0;
1465 
1466 	for (cmsg = (struct cmsghdr *)control;
1467 	    CMSG_VALID(cmsg, control, (uintptr_t)control + controllen);
1468 	    cmsg = CMSG_NEXT(cmsg)) {
1469 		if (cmsg->cmsg_level == SOL_SOCKET &&
1470 		    cmsg->cmsg_type == SCM_RIGHTS) {
1471 			if (fds != NULL)
1472 				return (EINVAL);
1473 			fds = CMSG_CONTENT(cmsg);
1474 			fdlen = (int)CMSG_CONTENTLEN(cmsg);
1475 			dprint(1, ("so_getfdopt: new %lu\n",
1476 			    (size_t)CMSG_CONTENTLEN(cmsg)));
1477 		}
1478 	}
1479 	if (fds == NULL) {
1480 		dprint(1, ("so_getfdopt: NONE\n"));
1481 		*fdlenp = -1;
1482 	} else
1483 		*fdlenp = fdlen;
1484 	*fdsp = fds;
1485 	return (0);
1486 }
1487 
1488 /*
1489  * Return the length of the options including any file descriptor options.
1490  */
1491 t_uscalar_t
1492 so_optlen(void *control, t_uscalar_t controllen, int oldflg)
1493 {
1494 	struct cmsghdr *cmsg;
1495 	t_uscalar_t optlen = 0;
1496 	t_uscalar_t len;
1497 
1498 	if (control == NULL)
1499 		return (0);
1500 
1501 	if (oldflg)
1502 		return ((t_uscalar_t)(sizeof (struct T_opthdr) +
1503 		    fdbuf_optlen(controllen)));
1504 
1505 	for (cmsg = (struct cmsghdr *)control;
1506 	    CMSG_VALID(cmsg, control, (uintptr_t)control + controllen);
1507 	    cmsg = CMSG_NEXT(cmsg)) {
1508 		if (cmsg->cmsg_level == SOL_SOCKET &&
1509 		    cmsg->cmsg_type == SCM_RIGHTS) {
1510 			len = fdbuf_optlen((int)CMSG_CONTENTLEN(cmsg));
1511 		} else {
1512 			len = (t_uscalar_t)CMSG_CONTENTLEN(cmsg);
1513 		}
1514 		optlen += (t_uscalar_t)(_TPI_ALIGN_TOPT(len) +
1515 		    sizeof (struct T_opthdr));
1516 	}
1517 	dprint(1, ("so_optlen: controllen %d, flg %d -> optlen %d\n",
1518 	    controllen, oldflg, optlen));
1519 	return (optlen);
1520 }
1521 
1522 /*
1523  * Copy options from control to the mblk. Skip any file descriptor options.
1524  */
1525 void
1526 so_cmsg2opt(void *control, t_uscalar_t controllen, int oldflg, mblk_t *mp)
1527 {
1528 	struct T_opthdr toh;
1529 	struct cmsghdr *cmsg;
1530 
1531 	if (control == NULL)
1532 		return;
1533 
1534 	if (oldflg) {
1535 		/* No real options - caller has handled file descriptors */
1536 		return;
1537 	}
1538 	for (cmsg = (struct cmsghdr *)control;
1539 	    CMSG_VALID(cmsg, control, (uintptr_t)control + controllen);
1540 	    cmsg = CMSG_NEXT(cmsg)) {
1541 		/*
1542 		 * Note: The caller handles file descriptors prior
1543 		 * to calling this function.
1544 		 */
1545 		t_uscalar_t len;
1546 
1547 		if (cmsg->cmsg_level == SOL_SOCKET &&
1548 		    cmsg->cmsg_type == SCM_RIGHTS)
1549 			continue;
1550 
1551 		len = (t_uscalar_t)CMSG_CONTENTLEN(cmsg);
1552 		toh.level = cmsg->cmsg_level;
1553 		toh.name = cmsg->cmsg_type;
1554 		toh.len = len + (t_uscalar_t)sizeof (struct T_opthdr);
1555 		toh.status = 0;
1556 
1557 		soappendmsg(mp, &toh, sizeof (toh));
1558 		soappendmsg(mp, CMSG_CONTENT(cmsg), len);
1559 		mp->b_wptr += _TPI_ALIGN_TOPT(len) - len;
1560 		ASSERT(mp->b_wptr <= mp->b_datap->db_lim);
1561 	}
1562 }
1563 
1564 /*
1565  * Return the length of the control message derived from the options.
1566  * Exclude SO_SRCADDR and SO_UNIX_CLOSE options. Include SO_FILEP.
1567  * When oldflg is set only include SO_FILEP.
1568  * so_opt2cmsg and so_cmsglen are inter-related since so_cmsglen
1569  * allocates the space that so_opt2cmsg fills. If one changes, the other should
1570  * also be checked for any possible impacts.
1571  */
1572 t_uscalar_t
1573 so_cmsglen(mblk_t *mp, void *opt, t_uscalar_t optlen, int oldflg)
1574 {
1575 	t_uscalar_t cmsglen = 0;
1576 	struct T_opthdr *tohp;
1577 	t_uscalar_t len;
1578 	t_uscalar_t last_roundup = 0;
1579 
1580 	ASSERT(__TPI_TOPT_ISALIGNED(opt));
1581 
1582 	for (tohp = (struct T_opthdr *)opt;
1583 	    tohp && _TPI_TOPT_VALID(tohp, opt, (uintptr_t)opt + optlen);
1584 	    tohp = _TPI_TOPT_NEXTHDR(opt, optlen, tohp)) {
1585 		dprint(1, ("so_cmsglen: level 0x%x, name %d, len %d\n",
1586 		    tohp->level, tohp->name, tohp->len));
1587 		if (tohp->level == SOL_SOCKET &&
1588 		    (tohp->name == SO_SRCADDR ||
1589 		    tohp->name == SO_UNIX_CLOSE)) {
1590 			continue;
1591 		}
1592 		if (tohp->level == SOL_SOCKET && tohp->name == SO_FILEP) {
1593 			struct fdbuf *fdbuf;
1594 			int fdbuflen;
1595 
1596 			fdbuf = (struct fdbuf *)_TPI_TOPT_DATA(tohp);
1597 			fdbuflen = (int)_TPI_TOPT_DATALEN(tohp);
1598 
1599 			if (!fdbuf_verify(mp, fdbuf, fdbuflen))
1600 				continue;
1601 			if (oldflg) {
1602 				cmsglen += fdbuf_cmsglen(fdbuflen);
1603 				continue;
1604 			}
1605 			len = fdbuf_cmsglen(fdbuflen);
1606 		} else if (tohp->level == SOL_SOCKET &&
1607 		    tohp->name == SCM_TIMESTAMP) {
1608 			if (oldflg)
1609 				continue;
1610 
1611 			if (get_udatamodel() == DATAMODEL_NATIVE) {
1612 				len = sizeof (struct timeval);
1613 			} else {
1614 				len = sizeof (struct timeval32);
1615 			}
1616 		} else {
1617 			if (oldflg)
1618 				continue;
1619 			len = (t_uscalar_t)_TPI_TOPT_DATALEN(tohp);
1620 		}
1621 		/*
1622 		 * Exclude roundup for last option to not set
1623 		 * MSG_CTRUNC when the cmsg fits but the padding doesn't fit.
1624 		 */
1625 		last_roundup = (t_uscalar_t)
1626 		    (ROUNDUP_cmsglen(len + (int)sizeof (struct cmsghdr)) -
1627 		    (len + (int)sizeof (struct cmsghdr)));
1628 		cmsglen += (t_uscalar_t)(len + (int)sizeof (struct cmsghdr)) +
1629 		    last_roundup;
1630 	}
1631 	cmsglen -= last_roundup;
1632 	dprint(1, ("so_cmsglen: optlen %d, flg %d -> cmsglen %d\n",
1633 	    optlen, oldflg, cmsglen));
1634 	return (cmsglen);
1635 }
1636 
1637 /*
1638  * Copy options from options to the control. Convert SO_FILEP to
1639  * file descriptors.
1640  * Returns errno or zero.
1641  * so_opt2cmsg and so_cmsglen are inter-related since so_cmsglen
1642  * allocates the space that so_opt2cmsg fills. If one changes, the other should
1643  * also be checked for any possible impacts.
1644  */
1645 int
1646 so_opt2cmsg(mblk_t *mp, void *opt, t_uscalar_t optlen, int oldflg,
1647     void *control, t_uscalar_t controllen)
1648 {
1649 	struct T_opthdr *tohp;
1650 	struct cmsghdr *cmsg;
1651 	struct fdbuf *fdbuf;
1652 	int fdbuflen;
1653 	int error;
1654 #if defined(DEBUG) || defined(__lint)
1655 	struct cmsghdr *cend = (struct cmsghdr *)
1656 	    (((uint8_t *)control) + ROUNDUP_cmsglen(controllen));
1657 #endif
1658 	cmsg = (struct cmsghdr *)control;
1659 
1660 	ASSERT(__TPI_TOPT_ISALIGNED(opt));
1661 
1662 	for (tohp = (struct T_opthdr *)opt;
1663 	    tohp && _TPI_TOPT_VALID(tohp, opt, (uintptr_t)opt + optlen);
1664 	    tohp = _TPI_TOPT_NEXTHDR(opt, optlen, tohp)) {
1665 		dprint(1, ("so_opt2cmsg: level 0x%x, name %d, len %d\n",
1666 		    tohp->level, tohp->name, tohp->len));
1667 
1668 		if (tohp->level == SOL_SOCKET &&
1669 		    (tohp->name == SO_SRCADDR ||
1670 		    tohp->name == SO_UNIX_CLOSE)) {
1671 			continue;
1672 		}
1673 		ASSERT((uintptr_t)cmsg <= (uintptr_t)control + controllen);
1674 		if (tohp->level == SOL_SOCKET && tohp->name == SO_FILEP) {
1675 			fdbuf = (struct fdbuf *)_TPI_TOPT_DATA(tohp);
1676 			fdbuflen = (int)_TPI_TOPT_DATALEN(tohp);
1677 
1678 			if (!fdbuf_verify(mp, fdbuf, fdbuflen))
1679 				return (EPROTO);
1680 			if (oldflg) {
1681 				error = fdbuf_extract(fdbuf, control,
1682 				    (int)controllen);
1683 				if (error != 0)
1684 					return (error);
1685 				continue;
1686 			} else {
1687 				int fdlen;
1688 
1689 				fdlen = (int)fdbuf_cmsglen(
1690 				    (int)_TPI_TOPT_DATALEN(tohp));
1691 
1692 				cmsg->cmsg_level = tohp->level;
1693 				cmsg->cmsg_type = SCM_RIGHTS;
1694 				cmsg->cmsg_len = (socklen_t)(fdlen +
1695 				    sizeof (struct cmsghdr));
1696 
1697 				error = fdbuf_extract(fdbuf,
1698 				    CMSG_CONTENT(cmsg), fdlen);
1699 				if (error != 0)
1700 					return (error);
1701 			}
1702 		} else if (tohp->level == SOL_SOCKET &&
1703 		    tohp->name == SCM_TIMESTAMP) {
1704 			timestruc_t *timestamp;
1705 
1706 			if (oldflg)
1707 				continue;
1708 
1709 			cmsg->cmsg_level = tohp->level;
1710 			cmsg->cmsg_type = tohp->name;
1711 
1712 			timestamp =
1713 			    (timestruc_t *)P2ROUNDUP((intptr_t)&tohp[1],
1714 			    sizeof (intptr_t));
1715 
1716 			if (get_udatamodel() == DATAMODEL_NATIVE) {
1717 				struct timeval tv;
1718 
1719 				cmsg->cmsg_len = sizeof (struct timeval) +
1720 				    sizeof (struct cmsghdr);
1721 				tv.tv_sec = timestamp->tv_sec;
1722 				tv.tv_usec = timestamp->tv_nsec /
1723 				    (NANOSEC / MICROSEC);
1724 				/*
1725 				 * on LP64 systems, the struct timeval in
1726 				 * the destination will not be 8-byte aligned,
1727 				 * so use bcopy to avoid alignment trouble
1728 				 */
1729 				bcopy(&tv, CMSG_CONTENT(cmsg), sizeof (tv));
1730 			} else {
1731 				struct timeval32 *time32;
1732 
1733 				cmsg->cmsg_len = sizeof (struct timeval32) +
1734 				    sizeof (struct cmsghdr);
1735 				time32 = (struct timeval32 *)CMSG_CONTENT(cmsg);
1736 				time32->tv_sec = (time32_t)timestamp->tv_sec;
1737 				time32->tv_usec =
1738 				    (int32_t)(timestamp->tv_nsec /
1739 				    (NANOSEC / MICROSEC));
1740 			}
1741 
1742 		} else {
1743 			if (oldflg)
1744 				continue;
1745 
1746 			cmsg->cmsg_level = tohp->level;
1747 			cmsg->cmsg_type = tohp->name;
1748 			cmsg->cmsg_len = (socklen_t)(_TPI_TOPT_DATALEN(tohp) +
1749 			    sizeof (struct cmsghdr));
1750 
1751 			/* copy content to control data part */
1752 			bcopy(&tohp[1], CMSG_CONTENT(cmsg),
1753 			    CMSG_CONTENTLEN(cmsg));
1754 		}
1755 		/* move to next CMSG structure! */
1756 		cmsg = CMSG_NEXT(cmsg);
1757 	}
1758 	dprint(1, ("so_opt2cmsg: buf %p len %d; cend %p; final cmsg %p\n",
1759 	    control, controllen, cend, cmsg));
1760 	ASSERT(cmsg <= cend);
1761 	return (0);
1762 }
1763 
1764 /*
1765  * Extract the SO_SRCADDR option value if present.
1766  */
1767 void
1768 so_getopt_srcaddr(void *opt, t_uscalar_t optlen, void **srcp,
1769     t_uscalar_t *srclenp)
1770 {
1771 	struct T_opthdr		*tohp;
1772 
1773 	ASSERT(__TPI_TOPT_ISALIGNED(opt));
1774 
1775 	ASSERT(srcp != NULL && srclenp != NULL);
1776 	*srcp = NULL;
1777 	*srclenp = 0;
1778 
1779 	for (tohp = (struct T_opthdr *)opt;
1780 	    tohp && _TPI_TOPT_VALID(tohp, opt, (uintptr_t)opt + optlen);
1781 	    tohp = _TPI_TOPT_NEXTHDR(opt, optlen, tohp)) {
1782 		dprint(1, ("so_getopt_srcaddr: level 0x%x, name %d, len %d\n",
1783 		    tohp->level, tohp->name, tohp->len));
1784 		if (tohp->level == SOL_SOCKET &&
1785 		    tohp->name == SO_SRCADDR) {
1786 			*srcp = _TPI_TOPT_DATA(tohp);
1787 			*srclenp = (t_uscalar_t)_TPI_TOPT_DATALEN(tohp);
1788 		}
1789 	}
1790 }
1791 
1792 /*
1793  * Verify if the SO_UNIX_CLOSE option is present.
1794  */
1795 int
1796 so_getopt_unix_close(void *opt, t_uscalar_t optlen)
1797 {
1798 	struct T_opthdr		*tohp;
1799 
1800 	ASSERT(__TPI_TOPT_ISALIGNED(opt));
1801 
1802 	for (tohp = (struct T_opthdr *)opt;
1803 	    tohp && _TPI_TOPT_VALID(tohp, opt, (uintptr_t)opt + optlen);
1804 	    tohp = _TPI_TOPT_NEXTHDR(opt, optlen, tohp)) {
1805 		dprint(1,
1806 		    ("so_getopt_unix_close: level 0x%x, name %d, len %d\n",
1807 		    tohp->level, tohp->name, tohp->len));
1808 		if (tohp->level == SOL_SOCKET &&
1809 		    tohp->name == SO_UNIX_CLOSE)
1810 			return (1);
1811 	}
1812 	return (0);
1813 }
1814 
1815 /*
1816  * Allocate an M_PROTO message.
1817  *
1818  * If allocation fails the behavior depends on sleepflg:
1819  *	_ALLOC_NOSLEEP	fail immediately
1820  *	_ALLOC_INTR	sleep for memory until a signal is caught
1821  *	_ALLOC_SLEEP	sleep forever. Don't return NULL.
1822  */
1823 mblk_t *
1824 soallocproto(size_t size, int sleepflg)
1825 {
1826 	mblk_t	*mp;
1827 
1828 	/* Round up size for reuse */
1829 	size = MAX(size, 64);
1830 	mp = allocb(size, BPRI_MED);
1831 	if (mp == NULL) {
1832 		int error;	/* Dummy - error not returned to caller */
1833 
1834 		switch (sleepflg) {
1835 		case _ALLOC_SLEEP:
1836 			mp = allocb_wait(size, BPRI_MED, STR_NOSIG, &error);
1837 			ASSERT(mp);
1838 			break;
1839 		case _ALLOC_INTR:
1840 			mp = allocb_wait(size, BPRI_MED, 0, &error);
1841 			if (mp == NULL) {
1842 				/* Caught signal while sleeping for memory */
1843 				eprintline(ENOBUFS);
1844 				return (NULL);
1845 			}
1846 			break;
1847 		case _ALLOC_NOSLEEP:
1848 		default:
1849 			eprintline(ENOBUFS);
1850 			return (NULL);
1851 		}
1852 	}
1853 	DB_TYPE(mp) = M_PROTO;
1854 	return (mp);
1855 }
1856 
1857 /*
1858  * Allocate an M_PROTO message with a single component.
1859  * len is the length of buf. size is the amount to allocate.
1860  *
1861  * buf can be NULL with a non-zero len.
1862  * This results in a bzero'ed chunk being placed the message.
1863  */
1864 mblk_t *
1865 soallocproto1(const void *buf, ssize_t len, ssize_t size, int sleepflg)
1866 {
1867 	mblk_t	*mp;
1868 
1869 	if (size == 0)
1870 		size = len;
1871 
1872 	ASSERT(size >= len);
1873 	/* Round up size for reuse */
1874 	size = MAX(size, 64);
1875 	mp = soallocproto(size, sleepflg);
1876 	if (mp == NULL)
1877 		return (NULL);
1878 	mp->b_datap->db_type = M_PROTO;
1879 	if (len != 0) {
1880 		if (buf != NULL)
1881 			bcopy(buf, mp->b_wptr, len);
1882 		else
1883 			bzero(mp->b_wptr, len);
1884 		mp->b_wptr += len;
1885 	}
1886 	return (mp);
1887 }
1888 
1889 /*
1890  * Append buf/len to mp.
1891  * The caller has to ensure that there is enough room in the mblk.
1892  *
1893  * buf can be NULL with a non-zero len.
1894  * This results in a bzero'ed chunk being placed the message.
1895  */
1896 void
1897 soappendmsg(mblk_t *mp, const void *buf, ssize_t len)
1898 {
1899 	ASSERT(mp);
1900 
1901 	if (len != 0) {
1902 		/* Assert for room left */
1903 		ASSERT(mp->b_datap->db_lim - mp->b_wptr >= len);
1904 		if (buf != NULL)
1905 			bcopy(buf, mp->b_wptr, len);
1906 		else
1907 			bzero(mp->b_wptr, len);
1908 	}
1909 	mp->b_wptr += len;
1910 }
1911 
1912 /*
1913  * Create a message using two kernel buffers.
1914  * If size is set that will determine the allocation size (e.g. for future
1915  * soappendmsg calls). If size is zero it is derived from the buffer
1916  * lengths.
1917  */
1918 mblk_t *
1919 soallocproto2(const void *buf1, ssize_t len1, const void *buf2, ssize_t len2,
1920     ssize_t size, int sleepflg)
1921 {
1922 	mblk_t *mp;
1923 
1924 	if (size == 0)
1925 		size = len1 + len2;
1926 	ASSERT(size >= len1 + len2);
1927 
1928 	mp = soallocproto1(buf1, len1, size, sleepflg);
1929 	if (mp)
1930 		soappendmsg(mp, buf2, len2);
1931 	return (mp);
1932 }
1933 
1934 /*
1935  * Create a message using three kernel buffers.
1936  * If size is set that will determine the allocation size (for future
1937  * soappendmsg calls). If size is zero it is derived from the buffer
1938  * lengths.
1939  */
1940 mblk_t *
1941 soallocproto3(const void *buf1, ssize_t len1, const void *buf2, ssize_t len2,
1942     const void *buf3, ssize_t len3, ssize_t size, int sleepflg)
1943 {
1944 	mblk_t *mp;
1945 
1946 	if (size == 0)
1947 		size = len1 + len2 +len3;
1948 	ASSERT(size >= len1 + len2 + len3);
1949 
1950 	mp = soallocproto1(buf1, len1, size, sleepflg);
1951 	if (mp != NULL) {
1952 		soappendmsg(mp, buf2, len2);
1953 		soappendmsg(mp, buf3, len3);
1954 	}
1955 	return (mp);
1956 }
1957 
1958 #ifdef DEBUG
1959 char *
1960 pr_state(uint_t state, uint_t mode)
1961 {
1962 	static char buf[1024];
1963 
1964 	buf[0] = 0;
1965 	if (state & SS_ISCONNECTED)
1966 		strcat(buf, "ISCONNECTED ");
1967 	if (state & SS_ISCONNECTING)
1968 		strcat(buf, "ISCONNECTING ");
1969 	if (state & SS_ISDISCONNECTING)
1970 		strcat(buf, "ISDISCONNECTING ");
1971 	if (state & SS_CANTSENDMORE)
1972 		strcat(buf, "CANTSENDMORE ");
1973 
1974 	if (state & SS_CANTRCVMORE)
1975 		strcat(buf, "CANTRCVMORE ");
1976 	if (state & SS_ISBOUND)
1977 		strcat(buf, "ISBOUND ");
1978 	if (state & SS_NDELAY)
1979 		strcat(buf, "NDELAY ");
1980 	if (state & SS_NONBLOCK)
1981 		strcat(buf, "NONBLOCK ");
1982 
1983 	if (state & SS_ASYNC)
1984 		strcat(buf, "ASYNC ");
1985 	if (state & SS_ACCEPTCONN)
1986 		strcat(buf, "ACCEPTCONN ");
1987 	if (state & SS_HASCONNIND)
1988 		strcat(buf, "HASCONNIND ");
1989 	if (state & SS_SAVEDEOR)
1990 		strcat(buf, "SAVEDEOR ");
1991 
1992 	if (state & SS_RCVATMARK)
1993 		strcat(buf, "RCVATMARK ");
1994 	if (state & SS_OOBPEND)
1995 		strcat(buf, "OOBPEND ");
1996 	if (state & SS_HAVEOOBDATA)
1997 		strcat(buf, "HAVEOOBDATA ");
1998 	if (state & SS_HADOOBDATA)
1999 		strcat(buf, "HADOOBDATA ");
2000 
2001 	if (state & SS_FADDR_NOXLATE)
2002 		strcat(buf, "FADDR_NOXLATE ");
2003 
2004 	if (mode & SM_PRIV)
2005 		strcat(buf, "PRIV ");
2006 	if (mode & SM_ATOMIC)
2007 		strcat(buf, "ATOMIC ");
2008 	if (mode & SM_ADDR)
2009 		strcat(buf, "ADDR ");
2010 	if (mode & SM_CONNREQUIRED)
2011 		strcat(buf, "CONNREQUIRED ");
2012 
2013 	if (mode & SM_FDPASSING)
2014 		strcat(buf, "FDPASSING ");
2015 	if (mode & SM_EXDATA)
2016 		strcat(buf, "EXDATA ");
2017 	if (mode & SM_OPTDATA)
2018 		strcat(buf, "OPTDATA ");
2019 	if (mode & SM_BYTESTREAM)
2020 		strcat(buf, "BYTESTREAM ");
2021 	return (buf);
2022 }
2023 
2024 char *
2025 pr_addr(int family, struct sockaddr *addr, t_uscalar_t addrlen)
2026 {
2027 	static char buf[1024];
2028 
2029 	if (addr == NULL || addrlen == 0) {
2030 		sprintf(buf, "(len %d) %p", addrlen, addr);
2031 		return (buf);
2032 	}
2033 	switch (family) {
2034 	case AF_INET: {
2035 		struct sockaddr_in sin;
2036 
2037 		bcopy(addr, &sin, sizeof (sin));
2038 
2039 		(void) sprintf(buf, "(len %d) %x/%d",
2040 		    addrlen, ntohl(sin.sin_addr.s_addr), ntohs(sin.sin_port));
2041 		break;
2042 	}
2043 	case AF_INET6: {
2044 		struct sockaddr_in6 sin6;
2045 		uint16_t *piece = (uint16_t *)&sin6.sin6_addr;
2046 
2047 		bcopy((char *)addr, (char *)&sin6, sizeof (sin6));
2048 		sprintf(buf, "(len %d) %x:%x:%x:%x:%x:%x:%x:%x/%d",
2049 		    addrlen,
2050 		    ntohs(piece[0]), ntohs(piece[1]),
2051 		    ntohs(piece[2]), ntohs(piece[3]),
2052 		    ntohs(piece[4]), ntohs(piece[5]),
2053 		    ntohs(piece[6]), ntohs(piece[7]),
2054 		    ntohs(sin6.sin6_port));
2055 		break;
2056 	}
2057 	case AF_UNIX: {
2058 		struct sockaddr_un *soun = (struct sockaddr_un *)addr;
2059 
2060 		(void) sprintf(buf, "(len %d) %s", addrlen,
2061 		    (soun == NULL) ? "(none)" : soun->sun_path);
2062 		break;
2063 	}
2064 	default:
2065 		(void) sprintf(buf, "(unknown af %d)", family);
2066 		break;
2067 	}
2068 	return (buf);
2069 }
2070 
2071 /* The logical equivalence operator (a if-and-only-if b) */
2072 #define	EQUIV(a, b)	(((a) && (b)) || (!(a) && (!(b))))
2073 
2074 /*
2075  * Verify limitations and invariants on oob state.
2076  * Return 1 if OK, otherwise 0 so that it can be used as
2077  *	ASSERT(verify_oobstate(so));
2078  */
2079 int
2080 so_verify_oobstate(struct sonode *so)
2081 {
2082 	ASSERT(MUTEX_HELD(&so->so_lock));
2083 
2084 	/*
2085 	 * The possible state combinations are:
2086 	 *	0
2087 	 *	SS_OOBPEND
2088 	 *	SS_OOBPEND|SS_HAVEOOBDATA
2089 	 *	SS_OOBPEND|SS_HADOOBDATA
2090 	 *	SS_HADOOBDATA
2091 	 */
2092 	switch (so->so_state & (SS_OOBPEND|SS_HAVEOOBDATA|SS_HADOOBDATA)) {
2093 	case 0:
2094 	case SS_OOBPEND:
2095 	case SS_OOBPEND|SS_HAVEOOBDATA:
2096 	case SS_OOBPEND|SS_HADOOBDATA:
2097 	case SS_HADOOBDATA:
2098 		break;
2099 	default:
2100 		printf("Bad oob state 1 (%p): counts %d/%d state %s\n",
2101 		    so, so->so_oobsigcnt,
2102 		    so->so_oobcnt, pr_state(so->so_state, so->so_mode));
2103 		return (0);
2104 	}
2105 
2106 	/* SS_RCVATMARK should only be set when SS_OOBPEND is set */
2107 	if ((so->so_state & (SS_RCVATMARK|SS_OOBPEND)) == SS_RCVATMARK) {
2108 		printf("Bad oob state 2 (%p): counts %d/%d state %s\n",
2109 		    so, so->so_oobsigcnt,
2110 		    so->so_oobcnt, pr_state(so->so_state, so->so_mode));
2111 		return (0);
2112 	}
2113 
2114 	/*
2115 	 * (so_oobsigcnt != 0 or SS_RCVATMARK) iff SS_OOBPEND
2116 	 */
2117 	if (!EQUIV((so->so_oobsigcnt != 0) || (so->so_state & SS_RCVATMARK),
2118 	    so->so_state & SS_OOBPEND)) {
2119 		printf("Bad oob state 3 (%p): counts %d/%d state %s\n",
2120 		    so, so->so_oobsigcnt,
2121 		    so->so_oobcnt, pr_state(so->so_state, so->so_mode));
2122 		return (0);
2123 	}
2124 
2125 	/*
2126 	 * Unless SO_OOBINLINE we have so_oobmsg != NULL iff SS_HAVEOOBDATA
2127 	 */
2128 	if (!(so->so_options & SO_OOBINLINE) &&
2129 	    !EQUIV(so->so_oobmsg != NULL, so->so_state & SS_HAVEOOBDATA)) {
2130 		printf("Bad oob state 4 (%p): counts %d/%d state %s\n",
2131 		    so, so->so_oobsigcnt,
2132 		    so->so_oobcnt, pr_state(so->so_state, so->so_mode));
2133 		return (0);
2134 	}
2135 	if (so->so_oobsigcnt < so->so_oobcnt) {
2136 		printf("Bad oob state 5 (%p): counts %d/%d state %s\n",
2137 		    so, so->so_oobsigcnt,
2138 		    so->so_oobcnt, pr_state(so->so_state, so->so_mode));
2139 		return (0);
2140 	}
2141 	return (1);
2142 }
2143 #undef	EQUIV
2144 
2145 #endif /* DEBUG */
2146 
2147 /* initialize sockfs zone specific kstat related items			*/
2148 void *
2149 sock_kstat_init(zoneid_t zoneid)
2150 {
2151 	kstat_t	*ksp;
2152 
2153 	ksp = kstat_create_zone("sockfs", 0, "sock_unix_list", "misc",
2154 	    KSTAT_TYPE_RAW, 0, KSTAT_FLAG_VAR_SIZE|KSTAT_FLAG_VIRTUAL, zoneid);
2155 
2156 	if (ksp != NULL) {
2157 		ksp->ks_update = sockfs_update;
2158 		ksp->ks_snapshot = sockfs_snapshot;
2159 		ksp->ks_lock = &socklist.sl_lock;
2160 		ksp->ks_private = (void *)(uintptr_t)zoneid;
2161 		kstat_install(ksp);
2162 	}
2163 
2164 	return (ksp);
2165 }
2166 
2167 /* tear down sockfs zone specific kstat related items			*/
2168 /*ARGSUSED*/
2169 void
2170 sock_kstat_fini(zoneid_t zoneid, void *arg)
2171 {
2172 	kstat_t *ksp = (kstat_t *)arg;
2173 
2174 	if (ksp != NULL) {
2175 		ASSERT(zoneid == (zoneid_t)(uintptr_t)ksp->ks_private);
2176 		kstat_delete(ksp);
2177 	}
2178 }
2179 
2180 /*
2181  * Zones:
2182  * Note that nactive is going to be different for each zone.
2183  * This means we require kstat to call sockfs_update and then sockfs_snapshot
2184  * for the same zone, or sockfs_snapshot will be taken into the wrong size
2185  * buffer. This is safe, but if the buffer is too small, user will not be
2186  * given details of all sockets. However, as this kstat has a ks_lock, kstat
2187  * driver will keep it locked between the update and the snapshot, so no
2188  * other process (zone) can currently get inbetween resulting in a wrong size
2189  * buffer allocation.
2190  */
2191 static int
2192 sockfs_update(kstat_t *ksp, int rw)
2193 {
2194 	uint_t	nactive = 0;		/* # of active AF_UNIX sockets	*/
2195 	struct sonode	*so;		/* current sonode on socklist	*/
2196 	zoneid_t	myzoneid = (zoneid_t)(uintptr_t)ksp->ks_private;
2197 
2198 	ASSERT((zoneid_t)(uintptr_t)ksp->ks_private == getzoneid());
2199 
2200 	if (rw == KSTAT_WRITE) {	/* bounce all writes		*/
2201 		return (EACCES);
2202 	}
2203 
2204 	for (so = socklist.sl_list; so != NULL; so = so->so_next) {
2205 		if (so->so_accessvp != NULL && so->so_zoneid == myzoneid) {
2206 			nactive++;
2207 		}
2208 	}
2209 	ksp->ks_ndata = nactive;
2210 	ksp->ks_data_size = nactive * sizeof (struct k_sockinfo);
2211 
2212 	return (0);
2213 }
2214 
2215 static int
2216 sockfs_snapshot(kstat_t *ksp, void *buf, int rw)
2217 {
2218 	int			ns;	/* # of sonodes we've copied	*/
2219 	struct sonode		*so;	/* current sonode on socklist	*/
2220 	struct k_sockinfo	*pksi;	/* where we put sockinfo data	*/
2221 	t_uscalar_t		sn_len;	/* soa_len			*/
2222 	zoneid_t		myzoneid = (zoneid_t)(uintptr_t)ksp->ks_private;
2223 
2224 	ASSERT((zoneid_t)(uintptr_t)ksp->ks_private == getzoneid());
2225 
2226 	ksp->ks_snaptime = gethrtime();
2227 
2228 	if (rw == KSTAT_WRITE) {	/* bounce all writes		*/
2229 		return (EACCES);
2230 	}
2231 
2232 	/*
2233 	 * for each sonode on the socklist, we massage the important
2234 	 * info into buf, in k_sockinfo format.
2235 	 */
2236 	pksi = (struct k_sockinfo *)buf;
2237 	for (ns = 0, so = socklist.sl_list; so != NULL; so = so->so_next) {
2238 		/* only stuff active sonodes and the same zone:		*/
2239 		if (so->so_accessvp == NULL || so->so_zoneid != myzoneid) {
2240 			continue;
2241 		}
2242 
2243 		/*
2244 		 * If the sonode was activated between the update and the
2245 		 * snapshot, we're done - as this is only a snapshot.
2246 		 */
2247 		if ((caddr_t)(pksi) >= (caddr_t)buf + ksp->ks_data_size) {
2248 			break;
2249 		}
2250 
2251 		/* copy important info into buf:			*/
2252 		pksi->ks_si.si_size = sizeof (struct k_sockinfo);
2253 		pksi->ks_si.si_family = so->so_family;
2254 		pksi->ks_si.si_type = so->so_type;
2255 		pksi->ks_si.si_flag = so->so_flag;
2256 		pksi->ks_si.si_state = so->so_state;
2257 		pksi->ks_si.si_serv_type = so->so_serv_type;
2258 		pksi->ks_si.si_ux_laddr_sou_magic = so->so_ux_laddr.soua_magic;
2259 		pksi->ks_si.si_ux_faddr_sou_magic = so->so_ux_faddr.soua_magic;
2260 		pksi->ks_si.si_laddr_soa_len = so->so_laddr.soa_len;
2261 		pksi->ks_si.si_faddr_soa_len = so->so_faddr.soa_len;
2262 		pksi->ks_si.si_szoneid = so->so_zoneid;
2263 
2264 		mutex_enter(&so->so_lock);
2265 
2266 		if (so->so_laddr_sa != NULL) {
2267 			ASSERT(so->so_laddr_sa->sa_data != NULL);
2268 			sn_len = so->so_laddr_len;
2269 			ASSERT(sn_len <= sizeof (short) +
2270 			    sizeof (pksi->ks_si.si_laddr_sun_path));
2271 
2272 			pksi->ks_si.si_laddr_family =
2273 			    so->so_laddr_sa->sa_family;
2274 			if (sn_len != 0) {
2275 				/* AF_UNIX socket names are NULL terminated */
2276 				(void) strncpy(pksi->ks_si.si_laddr_sun_path,
2277 				    so->so_laddr_sa->sa_data,
2278 				    sizeof (pksi->ks_si.si_laddr_sun_path));
2279 				sn_len = strlen(pksi->ks_si.si_laddr_sun_path);
2280 			}
2281 			pksi->ks_si.si_laddr_sun_path[sn_len] = 0;
2282 		}
2283 
2284 		if (so->so_faddr_sa != NULL) {
2285 			ASSERT(so->so_faddr_sa->sa_data != NULL);
2286 			sn_len = so->so_faddr_len;
2287 			ASSERT(sn_len <= sizeof (short) +
2288 			    sizeof (pksi->ks_si.si_faddr_sun_path));
2289 
2290 			pksi->ks_si.si_faddr_family =
2291 			    so->so_faddr_sa->sa_family;
2292 			if (sn_len != 0) {
2293 				(void) strncpy(pksi->ks_si.si_faddr_sun_path,
2294 				    so->so_faddr_sa->sa_data,
2295 				    sizeof (pksi->ks_si.si_faddr_sun_path));
2296 				sn_len = strlen(pksi->ks_si.si_faddr_sun_path);
2297 			}
2298 			pksi->ks_si.si_faddr_sun_path[sn_len] = 0;
2299 		}
2300 
2301 		mutex_exit(&so->so_lock);
2302 
2303 		(void) sprintf(pksi->ks_straddr[0], "%p", (void *)so);
2304 		(void) sprintf(pksi->ks_straddr[1], "%p",
2305 		    (void *)so->so_ux_laddr.soua_vp);
2306 		(void) sprintf(pksi->ks_straddr[2], "%p",
2307 		    (void *)so->so_ux_faddr.soua_vp);
2308 
2309 		ns++;
2310 		pksi++;
2311 	}
2312 
2313 	ksp->ks_ndata = ns;
2314 	return (0);
2315 }
2316 
2317 ssize_t
2318 soreadfile(file_t *fp, uchar_t *buf, u_offset_t fileoff, int *err, size_t size)
2319 {
2320 	struct uio auio;
2321 	struct iovec aiov[MSG_MAXIOVLEN];
2322 	register vnode_t *vp;
2323 	int ioflag, rwflag;
2324 	ssize_t cnt;
2325 	int error = 0;
2326 	int iovcnt = 0;
2327 	short fflag;
2328 
2329 	vp = fp->f_vnode;
2330 	fflag = fp->f_flag;
2331 
2332 	rwflag = 0;
2333 	aiov[0].iov_base = (caddr_t)buf;
2334 	aiov[0].iov_len = size;
2335 	iovcnt = 1;
2336 	cnt = (ssize_t)size;
2337 	(void) VOP_RWLOCK(vp, rwflag, NULL);
2338 
2339 	auio.uio_loffset = fileoff;
2340 	auio.uio_iov = aiov;
2341 	auio.uio_iovcnt = iovcnt;
2342 	auio.uio_resid = cnt;
2343 	auio.uio_segflg = UIO_SYSSPACE;
2344 	auio.uio_llimit = MAXOFFSET_T;
2345 	auio.uio_fmode = fflag;
2346 	auio.uio_extflg = UIO_COPY_CACHED;
2347 
2348 	ioflag = auio.uio_fmode & (FAPPEND|FSYNC|FDSYNC|FRSYNC);
2349 
2350 	/* If read sync is not asked for, filter sync flags */
2351 	if ((ioflag & FRSYNC) == 0)
2352 		ioflag &= ~(FSYNC|FDSYNC);
2353 	error = VOP_READ(vp, &auio, ioflag, fp->f_cred, NULL);
2354 	cnt -= auio.uio_resid;
2355 
2356 	VOP_RWUNLOCK(vp, rwflag, NULL);
2357 
2358 	if (error == EINTR && cnt != 0)
2359 		error = 0;
2360 out:
2361 	if (error != 0) {
2362 		*err = error;
2363 		return (0);
2364 	} else {
2365 		*err = 0;
2366 		return (cnt);
2367 	}
2368 }
2369