xref: /illumos-gate/usr/src/uts/common/fs/sockfs/socktpi.c (revision 942c5e3c2dd127463517e5cc1694ee94ca45e021)
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 2007 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/vnode.h>
40 #include <sys/debug.h>
41 #include <sys/errno.h>
42 #include <sys/time.h>
43 #include <sys/file.h>
44 #include <sys/open.h>
45 #include <sys/user.h>
46 #include <sys/termios.h>
47 #include <sys/stream.h>
48 #include <sys/strsubr.h>
49 #include <sys/strsun.h>
50 #include <sys/ddi.h>
51 #include <sys/esunddi.h>
52 #include <sys/flock.h>
53 #include <sys/modctl.h>
54 #include <sys/vtrace.h>
55 #include <sys/cmn_err.h>
56 #include <sys/pathname.h>
57 
58 #include <sys/socket.h>
59 #include <sys/socketvar.h>
60 #include <sys/sockio.h>
61 #include <netinet/in.h>
62 #include <sys/un.h>
63 #include <sys/strsun.h>
64 
65 #include <sys/tiuser.h>
66 #define	_SUN_TPI_VERSION	2
67 #include <sys/tihdr.h>
68 #include <sys/timod.h>		/* TI_GETMYNAME, TI_GETPEERNAME */
69 
70 #include <c2/audit.h>
71 
72 #include <inet/common.h>
73 #include <inet/ip.h>
74 #include <inet/ip6.h>
75 #include <inet/tcp.h>
76 #include <inet/udp_impl.h>
77 
78 #include <sys/zone.h>
79 
80 #include <fs/sockfs/nl7c.h>
81 #include <fs/sockfs/nl7curi.h>
82 
83 #include <inet/kssl/ksslapi.h>
84 
85 /*
86  * Possible failures when memory can't be allocated. The documented behavior:
87  *
88  * 		5.5:			4.X:		XNET:
89  * accept:	ENOMEM/ENOSR/EINTR	- (EINTR)	ENOMEM/ENOBUFS/ENOSR/
90  *							EINTR
91  *	(4.X does not document EINTR but returns it)
92  * bind:	ENOSR			-		ENOBUFS/ENOSR
93  * connect: 	EINTR			EINTR		ENOBUFS/ENOSR/EINTR
94  * getpeername:	ENOMEM/ENOSR		ENOBUFS (-)	ENOBUFS/ENOSR
95  * getsockname:	ENOMEM/ENOSR		ENOBUFS (-)	ENOBUFS/ENOSR
96  *	(4.X getpeername and getsockname do not fail in practice)
97  * getsockopt:	ENOMEM/ENOSR		-		ENOBUFS/ENOSR
98  * listen:	-			-		ENOBUFS
99  * recv:	ENOMEM/ENOSR/EINTR	EINTR		ENOBUFS/ENOMEM/ENOSR/
100  *							EINTR
101  * send:	ENOMEM/ENOSR/EINTR	ENOBUFS/EINTR	ENOBUFS/ENOMEM/ENOSR/
102  *							EINTR
103  * setsockopt:	ENOMEM/ENOSR		-		ENOBUFS/ENOMEM/ENOSR
104  * shutdown:	ENOMEM/ENOSR		-		ENOBUFS/ENOSR
105  * socket:	ENOMEM/ENOSR		ENOBUFS		ENOBUFS/ENOMEM/ENOSR
106  * socketpair:	ENOMEM/ENOSR		-		ENOBUFS/ENOMEM/ENOSR
107  *
108  * Resolution. When allocation fails:
109  *	recv: return EINTR
110  *	send: return EINTR
111  *	connect, accept: EINTR
112  *	bind, listen, shutdown (unbind, unix_close, disconnect): sleep
113  *	socket, socketpair: ENOBUFS
114  *	getpeername, getsockname: sleep
115  *	getsockopt, setsockopt: sleep
116  */
117 
118 #ifdef SOCK_TEST
119 /*
120  * Variables that make sockfs do something other than the standard TPI
121  * for the AF_INET transports.
122  *
123  * solisten_tpi_tcp:
124  *	TCP can handle a O_T_BIND_REQ with an increased backlog even though
125  *	the transport is already bound. This is needed to avoid loosing the
126  *	port number should listen() do a T_UNBIND_REQ followed by a
127  *	O_T_BIND_REQ.
128  *
129  * soconnect_tpi_udp:
130  *	UDP and ICMP can handle a T_CONN_REQ.
131  *	This is needed to make the sequence of connect(), getsockname()
132  *	return the local IP address used to send packets to the connected to
133  *	destination.
134  *
135  * soconnect_tpi_tcp:
136  *	TCP can handle a T_CONN_REQ without seeing a O_T_BIND_REQ.
137  *	Set this to non-zero to send TPI conformant messages to TCP in this
138  *	respect. This is a performance optimization.
139  *
140  * soaccept_tpi_tcp:
141  *	TCP can handle a T_CONN_REQ without the acceptor being bound.
142  *	This is a performance optimization that has been picked up in XTI.
143  *
144  * soaccept_tpi_multioptions:
145  *	When inheriting SOL_SOCKET options from the listener to the accepting
146  *	socket send them as a single message for AF_INET{,6}.
147  */
148 int solisten_tpi_tcp = 0;
149 int soconnect_tpi_udp = 0;
150 int soconnect_tpi_tcp = 0;
151 int soaccept_tpi_tcp = 0;
152 int soaccept_tpi_multioptions = 1;
153 #else /* SOCK_TEST */
154 #define	soconnect_tpi_tcp	0
155 #define	soconnect_tpi_udp	0
156 #define	solisten_tpi_tcp	0
157 #define	soaccept_tpi_tcp	0
158 #define	soaccept_tpi_multioptions	1
159 #endif /* SOCK_TEST */
160 
161 #ifdef SOCK_TEST
162 extern int do_useracc;
163 extern clock_t sock_test_timelimit;
164 #endif /* SOCK_TEST */
165 
166 /*
167  * Some X/Open added checks might have to be backed out to keep SunOS 4.X
168  * applications working. Turn on this flag to disable these checks.
169  */
170 int xnet_skip_checks = 0;
171 int xnet_check_print = 0;
172 int xnet_truncate_print = 0;
173 
174 extern	void sigintr(k_sigset_t *, int);
175 extern	void sigunintr(k_sigset_t *);
176 
177 extern	void *nl7c_lookup_addr(void *, t_uscalar_t);
178 extern	void *nl7c_add_addr(void *, t_uscalar_t);
179 extern	void nl7c_listener_addr(void *, struct sonode *);
180 
181 /* Sockets acting as an in-kernel SSL proxy */
182 extern mblk_t	*strsock_kssl_input(vnode_t *, mblk_t *, strwakeup_t *,
183 		    strsigset_t *, strsigset_t *, strpollset_t *);
184 extern mblk_t	*strsock_kssl_output(vnode_t *, mblk_t *, strwakeup_t *,
185 		    strsigset_t *, strsigset_t *, strpollset_t *);
186 
187 static int	sotpi_unbind(struct sonode *, int);
188 
189 /* TPI sockfs sonode operations */
190 static int	sotpi_accept(struct sonode *, int, struct sonode **);
191 static int	sotpi_bind(struct sonode *, struct sockaddr *, socklen_t,
192 		    int);
193 static int	sotpi_connect(struct sonode *, const struct sockaddr *,
194 		    socklen_t, int, int);
195 static int	sotpi_listen(struct sonode *, int);
196 static int	sotpi_sendmsg(struct sonode *, struct nmsghdr *,
197 		    struct uio *);
198 static int	sotpi_shutdown(struct sonode *, int);
199 static int	sotpi_getsockname(struct sonode *);
200 static int	sosend_dgramcmsg(struct sonode *, struct sockaddr *, socklen_t,
201 		    struct uio *, void *, t_uscalar_t, int);
202 static int	sodgram_direct(struct sonode *, struct sockaddr *,
203 		    socklen_t, struct uio *, int);
204 
205 sonodeops_t sotpi_sonodeops = {
206 	sotpi_accept,		/* sop_accept		*/
207 	sotpi_bind,		/* sop_bind		*/
208 	sotpi_listen,		/* sop_listen		*/
209 	sotpi_connect,		/* sop_connect		*/
210 	sotpi_recvmsg,		/* sop_recvmsg		*/
211 	sotpi_sendmsg,		/* sop_sendmsg		*/
212 	sotpi_getpeername,	/* sop_getpeername	*/
213 	sotpi_getsockname,	/* sop_getsockname	*/
214 	sotpi_shutdown,		/* sop_shutdown		*/
215 	sotpi_getsockopt,	/* sop_getsockopt	*/
216 	sotpi_setsockopt	/* sop_setsockopt	*/
217 };
218 
219 /*
220  * Common create code for socket and accept. If tso is set the values
221  * from that node is used instead of issuing a T_INFO_REQ.
222  *
223  * Assumes that the caller has a VN_HOLD on accessvp.
224  * The VN_RELE will occur either when sotpi_create() fails or when
225  * the returned sonode is freed.
226  */
227 struct sonode *
228 sotpi_create(vnode_t *accessvp, int domain, int type, int protocol, int version,
229     struct sonode *tso, int *errorp)
230 {
231 	struct sonode	*so;
232 	vnode_t		*vp;
233 	int		flags, error;
234 
235 	ASSERT(accessvp != NULL);
236 	vp = makesockvp(accessvp, domain, type, protocol);
237 	ASSERT(vp != NULL);
238 	so = VTOSO(vp);
239 
240 	flags = FREAD|FWRITE;
241 
242 	if ((type == SOCK_STREAM || type == SOCK_DGRAM) &&
243 	    (domain == AF_INET || domain == AF_INET6) &&
244 	    (protocol == IPPROTO_TCP || protocol == IPPROTO_UDP ||
245 	    protocol == IPPROTO_IP)) {
246 		/* Tell tcp or udp that it's talking to sockets */
247 		flags |= SO_SOCKSTR;
248 
249 		/*
250 		 * Here we indicate to socktpi_open() our attempt to
251 		 * make direct calls between sockfs and transport.
252 		 * The final decision is left to socktpi_open().
253 		 */
254 		so->so_state |= SS_DIRECT;
255 
256 		ASSERT(so->so_type != SOCK_DGRAM || tso == NULL);
257 		if (so->so_type == SOCK_STREAM && tso != NULL) {
258 			if (tso->so_state & SS_DIRECT) {
259 				/*
260 				 * Inherit SS_DIRECT from listener and pass
261 				 * SO_ACCEPTOR open flag to tcp, indicating
262 				 * that this is an accept fast-path instance.
263 				 */
264 				flags |= SO_ACCEPTOR;
265 			} else {
266 				/*
267 				 * SS_DIRECT is not set on listener, meaning
268 				 * that the listener has been converted from
269 				 * a socket to a stream.  Ensure that the
270 				 * acceptor inherits these settings.
271 				 */
272 				so->so_state &= ~SS_DIRECT;
273 				flags &= ~SO_SOCKSTR;
274 			}
275 		}
276 	}
277 
278 	/*
279 	 * Tell local transport that it is talking to sockets.
280 	 */
281 	if (so->so_family == AF_UNIX) {
282 		flags |= SO_SOCKSTR;
283 	}
284 
285 	/* Initialize the kernel SSL proxy fields */
286 	so->so_kssl_type = KSSL_NO_PROXY;
287 	so->so_kssl_ent = NULL;
288 	so->so_kssl_ctx = NULL;
289 
290 	if (error = socktpi_open(&vp, flags, CRED(), NULL)) {
291 		VN_RELE(vp);
292 		*errorp = error;
293 		return (NULL);
294 	}
295 
296 	if (error = so_strinit(so, tso)) {
297 		(void) VOP_CLOSE(vp, 0, 1, 0, CRED(), NULL);
298 		VN_RELE(vp);
299 		*errorp = error;
300 		return (NULL);
301 	}
302 
303 	if (version == SOV_DEFAULT)
304 		version = so_default_version;
305 
306 	so->so_version = (short)version;
307 
308 	return (so);
309 }
310 
311 /*
312  * Bind the socket to an unspecified address in sockfs only.
313  * Used for TCP/UDP transports where we know that the O_T_BIND_REQ isn't
314  * required in all cases.
315  */
316 static void
317 so_automatic_bind(struct sonode *so)
318 {
319 	ASSERT(so->so_family == AF_INET || so->so_family == AF_INET6);
320 
321 	ASSERT(MUTEX_HELD(&so->so_lock));
322 	ASSERT(!(so->so_state & SS_ISBOUND));
323 	ASSERT(so->so_unbind_mp);
324 
325 	ASSERT(so->so_laddr_len <= so->so_laddr_maxlen);
326 	bzero(so->so_laddr_sa, so->so_laddr_len);
327 	so->so_laddr_sa->sa_family = so->so_family;
328 	so->so_state |= SS_ISBOUND;
329 }
330 
331 
332 /*
333  * bind the socket.
334  *
335  * If the socket is already bound and none of _SOBIND_SOCKBSD or _SOBIND_XPG4_2
336  * are passed in we allow rebinding. Note that for backwards compatibility
337  * even "svr4" sockets pass in _SOBIND_SOCKBSD/SOV_SOCKBSD to sobind/bind.
338  * Thus the rebinding code is currently not executed.
339  *
340  * The constraints for rebinding are:
341  * - it is a SOCK_DGRAM, or
342  * - it is a SOCK_STREAM/SOCK_SEQPACKET that has not been connected
343  *   and no listen() has been done.
344  * This rebinding code was added based on some language in the XNET book
345  * about not returning EINVAL it the protocol allows rebinding. However,
346  * this language is not present in the Posix socket draft. Thus maybe the
347  * rebinding logic should be deleted from the source.
348  *
349  * A null "name" can be used to unbind the socket if:
350  * - it is a SOCK_DGRAM, or
351  * - it is a SOCK_STREAM/SOCK_SEQPACKET that has not been connected
352  *   and no listen() has been done.
353  */
354 static int
355 sotpi_bindlisten(struct sonode *so, struct sockaddr *name,
356     socklen_t namelen, int backlog, int flags)
357 {
358 	struct T_bind_req	bind_req;
359 	struct T_bind_ack	*bind_ack;
360 	int			error = 0;
361 	mblk_t			*mp;
362 	void			*addr;
363 	t_uscalar_t		addrlen;
364 	int			unbind_on_err = 1;
365 	boolean_t		clear_acceptconn_on_err = B_FALSE;
366 	boolean_t		restore_backlog_on_err = B_FALSE;
367 	int			save_so_backlog;
368 	t_scalar_t		PRIM_type = O_T_BIND_REQ;
369 	boolean_t		tcp_udp_xport;
370 	void			*nl7c = NULL;
371 
372 	dprintso(so, 1, ("sotpi_bindlisten(%p, %p, %d, %d, 0x%x) %s\n",
373 	    so, name, namelen, backlog, flags,
374 	    pr_state(so->so_state, so->so_mode)));
375 
376 	tcp_udp_xport = so->so_type == SOCK_STREAM || so->so_type == SOCK_DGRAM;
377 
378 	if (!(flags & _SOBIND_LOCK_HELD)) {
379 		mutex_enter(&so->so_lock);
380 		so_lock_single(so);	/* Set SOLOCKED */
381 	} else {
382 		ASSERT(MUTEX_HELD(&so->so_lock));
383 		ASSERT(so->so_flag & SOLOCKED);
384 	}
385 
386 	/*
387 	 * Make sure that there is a preallocated unbind_req message
388 	 * before binding. This message allocated when the socket is
389 	 * created  but it might be have been consumed.
390 	 */
391 	if (so->so_unbind_mp == NULL) {
392 		dprintso(so, 1, ("sobind: allocating unbind_req\n"));
393 		/* NOTE: holding so_lock while sleeping */
394 		so->so_unbind_mp =
395 		    soallocproto(sizeof (struct T_unbind_req), _ALLOC_SLEEP);
396 	}
397 
398 	if (flags & _SOBIND_REBIND) {
399 		/*
400 		 * Called from solisten after doing an sotpi_unbind() or
401 		 * potentially without the unbind (latter for AF_INET{,6}).
402 		 */
403 		ASSERT(name == NULL && namelen == 0);
404 
405 		if (so->so_family == AF_UNIX) {
406 			ASSERT(so->so_ux_bound_vp);
407 			addr = &so->so_ux_laddr;
408 			addrlen = (t_uscalar_t)sizeof (so->so_ux_laddr);
409 			dprintso(so, 1, ("sobind rebind UNIX: addrlen %d, "
410 			    "addr 0x%p, vp %p\n",
411 			    addrlen,
412 			    ((struct so_ux_addr *)addr)->soua_vp,
413 			    so->so_ux_bound_vp));
414 		} else {
415 			addr = so->so_laddr_sa;
416 			addrlen = (t_uscalar_t)so->so_laddr_len;
417 		}
418 	} else if (flags & _SOBIND_UNSPEC) {
419 		ASSERT(name == NULL && namelen == 0);
420 
421 		/*
422 		 * The caller checked SS_ISBOUND but not necessarily
423 		 * under so_lock
424 		 */
425 		if (so->so_state & SS_ISBOUND) {
426 			/* No error */
427 			goto done;
428 		}
429 
430 		/* Set an initial local address */
431 		switch (so->so_family) {
432 		case AF_UNIX:
433 			/*
434 			 * Use an address with same size as struct sockaddr
435 			 * just like BSD.
436 			 */
437 			so->so_laddr_len =
438 			    (socklen_t)sizeof (struct sockaddr);
439 			ASSERT(so->so_laddr_len <= so->so_laddr_maxlen);
440 			bzero(so->so_laddr_sa, so->so_laddr_len);
441 			so->so_laddr_sa->sa_family = so->so_family;
442 
443 			/*
444 			 * Pass down an address with the implicit bind
445 			 * magic number and the rest all zeros.
446 			 * The transport will return a unique address.
447 			 */
448 			so->so_ux_laddr.soua_vp = NULL;
449 			so->so_ux_laddr.soua_magic = SOU_MAGIC_IMPLICIT;
450 			addr = &so->so_ux_laddr;
451 			addrlen = (t_uscalar_t)sizeof (so->so_ux_laddr);
452 			break;
453 
454 		case AF_INET:
455 		case AF_INET6:
456 			/*
457 			 * An unspecified bind in TPI has a NULL address.
458 			 * Set the address in sockfs to have the sa_family.
459 			 */
460 			so->so_laddr_len = (so->so_family == AF_INET) ?
461 			    (socklen_t)sizeof (sin_t) :
462 			    (socklen_t)sizeof (sin6_t);
463 			ASSERT(so->so_laddr_len <= so->so_laddr_maxlen);
464 			bzero(so->so_laddr_sa, so->so_laddr_len);
465 			so->so_laddr_sa->sa_family = so->so_family;
466 			addr = NULL;
467 			addrlen = 0;
468 			break;
469 
470 		default:
471 			/*
472 			 * An unspecified bind in TPI has a NULL address.
473 			 * Set the address in sockfs to be zero length.
474 			 *
475 			 * Can not assume there is a sa_family for all
476 			 * protocol families. For example, AF_X25 does not
477 			 * have a family field.
478 			 */
479 			bzero(so->so_laddr_sa, so->so_laddr_len);
480 			so->so_laddr_len = 0;	/* XXX correct? */
481 			addr = NULL;
482 			addrlen = 0;
483 			break;
484 		}
485 
486 	} else {
487 		if (so->so_state & SS_ISBOUND) {
488 			/*
489 			 * If it is ok to rebind the socket, first unbind
490 			 * with the transport. A rebind to the NULL address
491 			 * is interpreted as an unbind.
492 			 * Note that a bind to NULL in BSD does unbind the
493 			 * socket but it fails with EINVAL.
494 			 * Note that regular sockets set SOV_SOCKBSD i.e.
495 			 * _SOBIND_SOCKBSD gets set here hence no type of
496 			 * socket does currently allow rebinding.
497 			 *
498 			 * If the name is NULL just do an unbind.
499 			 */
500 			if (flags & (_SOBIND_SOCKBSD|_SOBIND_XPG4_2) &&
501 			    name != NULL) {
502 				error = EINVAL;
503 				unbind_on_err = 0;
504 				eprintsoline(so, error);
505 				goto done;
506 			}
507 			if ((so->so_mode & SM_CONNREQUIRED) &&
508 			    (so->so_state & SS_CANTREBIND)) {
509 				error = EINVAL;
510 				unbind_on_err = 0;
511 				eprintsoline(so, error);
512 				goto done;
513 			}
514 			error = sotpi_unbind(so, 0);
515 			if (error) {
516 				eprintsoline(so, error);
517 				goto done;
518 			}
519 			ASSERT(!(so->so_state & SS_ISBOUND));
520 			if (name == NULL) {
521 				so->so_state &=
522 				    ~(SS_ISCONNECTED|SS_ISCONNECTING);
523 				goto done;
524 			}
525 		}
526 		/* X/Open requires this check */
527 		if ((so->so_state & SS_CANTSENDMORE) && !xnet_skip_checks) {
528 			if (xnet_check_print) {
529 				printf("sockfs: X/Open bind state check "
530 				    "caused EINVAL\n");
531 			}
532 			error = EINVAL;
533 			goto done;
534 		}
535 
536 		switch (so->so_family) {
537 		case AF_UNIX:
538 			/*
539 			 * All AF_UNIX addresses are nul terminated
540 			 * when copied (copyin_name) in so the minimum
541 			 * length is 3 bytes.
542 			 */
543 			if (name == NULL ||
544 			    (ssize_t)namelen <= sizeof (short) + 1) {
545 				error = EISDIR;
546 				eprintsoline(so, error);
547 				goto done;
548 			}
549 			/*
550 			 * Verify so_family matches the bound family.
551 			 * BSD does not check this for AF_UNIX resulting
552 			 * in funny mknods.
553 			 */
554 			if (name->sa_family != so->so_family) {
555 				error = EAFNOSUPPORT;
556 				goto done;
557 			}
558 			break;
559 		case AF_INET:
560 			if (name == NULL) {
561 				error = EINVAL;
562 				eprintsoline(so, error);
563 				goto done;
564 			}
565 			if ((size_t)namelen != sizeof (sin_t)) {
566 				error = name->sa_family != so->so_family ?
567 				    EAFNOSUPPORT : EINVAL;
568 				eprintsoline(so, error);
569 				goto done;
570 			}
571 			if ((flags & _SOBIND_XPG4_2) &&
572 			    (name->sa_family != so->so_family)) {
573 				/*
574 				 * This check has to be made for X/Open
575 				 * sockets however application failures have
576 				 * been observed when it is applied to
577 				 * all sockets.
578 				 */
579 				error = EAFNOSUPPORT;
580 				eprintsoline(so, error);
581 				goto done;
582 			}
583 			/*
584 			 * Force a zero sa_family to match so_family.
585 			 *
586 			 * Some programs like inetd(1M) don't set the
587 			 * family field. Other programs leave
588 			 * sin_family set to garbage - SunOS 4.X does
589 			 * not check the family field on a bind.
590 			 * We use the family field that
591 			 * was passed in to the socket() call.
592 			 */
593 			name->sa_family = so->so_family;
594 			break;
595 
596 		case AF_INET6: {
597 #ifdef DEBUG
598 			sin6_t *sin6 = (sin6_t *)name;
599 #endif /* DEBUG */
600 
601 			if (name == NULL) {
602 				error = EINVAL;
603 				eprintsoline(so, error);
604 				goto done;
605 			}
606 			if ((size_t)namelen != sizeof (sin6_t)) {
607 				error = name->sa_family != so->so_family ?
608 				    EAFNOSUPPORT : EINVAL;
609 				eprintsoline(so, error);
610 				goto done;
611 			}
612 			if (name->sa_family != so->so_family) {
613 				/*
614 				 * With IPv6 we require the family to match
615 				 * unlike in IPv4.
616 				 */
617 				error = EAFNOSUPPORT;
618 				eprintsoline(so, error);
619 				goto done;
620 			}
621 #ifdef DEBUG
622 			/*
623 			 * Verify that apps don't forget to clear
624 			 * sin6_scope_id etc
625 			 */
626 			if (sin6->sin6_scope_id != 0 &&
627 			    !IN6_IS_ADDR_LINKSCOPE(&sin6->sin6_addr)) {
628 				zcmn_err(getzoneid(), CE_WARN,
629 				    "bind with uninitialized sin6_scope_id "
630 				    "(%d) on socket. Pid = %d\n",
631 				    (int)sin6->sin6_scope_id,
632 				    (int)curproc->p_pid);
633 			}
634 			if (sin6->__sin6_src_id != 0) {
635 				zcmn_err(getzoneid(), CE_WARN,
636 				    "bind with uninitialized __sin6_src_id "
637 				    "(%d) on socket. Pid = %d\n",
638 				    (int)sin6->__sin6_src_id,
639 				    (int)curproc->p_pid);
640 			}
641 #endif /* DEBUG */
642 			break;
643 		}
644 		default:
645 			/*
646 			 * Don't do any length or sa_family check to allow
647 			 * non-sockaddr style addresses.
648 			 */
649 			if (name == NULL) {
650 				error = EINVAL;
651 				eprintsoline(so, error);
652 				goto done;
653 			}
654 			break;
655 		}
656 
657 		if (namelen > (t_uscalar_t)so->so_laddr_maxlen) {
658 			error = ENAMETOOLONG;
659 			eprintsoline(so, error);
660 			goto done;
661 		}
662 		/*
663 		 * Save local address.
664 		 */
665 		so->so_laddr_len = (socklen_t)namelen;
666 		ASSERT(so->so_laddr_len <= so->so_laddr_maxlen);
667 		bcopy(name, so->so_laddr_sa, namelen);
668 
669 		addr = so->so_laddr_sa;
670 		addrlen = (t_uscalar_t)so->so_laddr_len;
671 		switch (so->so_family) {
672 		case AF_INET6:
673 		case AF_INET:
674 			break;
675 		case AF_UNIX: {
676 			struct sockaddr_un *soun =
677 			    (struct sockaddr_un *)so->so_laddr_sa;
678 			struct vnode *vp;
679 			struct vattr vattr;
680 
681 			ASSERT(so->so_ux_bound_vp == NULL);
682 			/*
683 			 * Create vnode for the specified path name.
684 			 * Keep vnode held with a reference in so_ux_bound_vp.
685 			 * Use the vnode pointer as the address used in the
686 			 * bind with the transport.
687 			 *
688 			 * Use the same mode as in BSD. In particular this does
689 			 * not observe the umask.
690 			 */
691 			/* MAXPATHLEN + soun_family + nul termination */
692 			if (so->so_laddr_len >
693 			    (socklen_t)(MAXPATHLEN + sizeof (short) + 1)) {
694 				error = ENAMETOOLONG;
695 				eprintsoline(so, error);
696 				goto done;
697 			}
698 			vattr.va_type = VSOCK;
699 			vattr.va_mode = 0777 & ~PTOU(curproc)->u_cmask;
700 			vattr.va_mask = AT_TYPE|AT_MODE;
701 			/* NOTE: holding so_lock */
702 			error = vn_create(soun->sun_path, UIO_SYSSPACE, &vattr,
703 			    EXCL, 0, &vp, CRMKNOD, 0, 0);
704 			if (error) {
705 				if (error == EEXIST)
706 					error = EADDRINUSE;
707 				eprintsoline(so, error);
708 				goto done;
709 			}
710 			/*
711 			 * Establish pointer from the underlying filesystem
712 			 * vnode to the socket node.
713 			 * so_ux_bound_vp and v_stream->sd_vnode form the
714 			 * cross-linkage between the underlying filesystem
715 			 * node and the socket node.
716 			 */
717 			ASSERT(SOTOV(so)->v_stream);
718 			mutex_enter(&vp->v_lock);
719 			vp->v_stream = SOTOV(so)->v_stream;
720 			so->so_ux_bound_vp = vp;
721 			mutex_exit(&vp->v_lock);
722 
723 			/*
724 			 * Use the vnode pointer value as a unique address
725 			 * (together with the magic number to avoid conflicts
726 			 * with implicit binds) in the transport provider.
727 			 */
728 			so->so_ux_laddr.soua_vp = (void *)so->so_ux_bound_vp;
729 			so->so_ux_laddr.soua_magic = SOU_MAGIC_EXPLICIT;
730 			addr = &so->so_ux_laddr;
731 			addrlen = (t_uscalar_t)sizeof (so->so_ux_laddr);
732 			dprintso(so, 1, ("sobind UNIX: addrlen %d, addr %p\n",
733 			    addrlen,
734 			    ((struct so_ux_addr *)addr)->soua_vp));
735 			break;
736 		}
737 		} /* end switch (so->so_family) */
738 	}
739 
740 	/*
741 	 * set SS_ACCEPTCONN before sending down O_T_BIND_REQ since
742 	 * the transport can start passing up T_CONN_IND messages
743 	 * as soon as it receives the bind req and strsock_proto()
744 	 * insists that SS_ACCEPTCONN is set when processing T_CONN_INDs.
745 	 */
746 	if (flags & _SOBIND_LISTEN) {
747 		if ((so->so_state & SS_ACCEPTCONN) == 0)
748 			clear_acceptconn_on_err = B_TRUE;
749 		save_so_backlog = so->so_backlog;
750 		restore_backlog_on_err = B_TRUE;
751 		so->so_state |= SS_ACCEPTCONN;
752 		so->so_backlog = backlog;
753 	}
754 
755 	/*
756 	 * If NL7C addr(s) have been configured check for addr/port match,
757 	 * or if an implicit NL7C socket via AF_NCA mark socket as NL7C.
758 	 *
759 	 * NL7C supports the TCP transport only so check AF_INET and AF_INET6
760 	 * family sockets only. If match mark as such.
761 	 */
762 	if (nl7c_enabled && ((addr != NULL &&
763 	    (so->so_family == AF_INET || so->so_family == AF_INET6) &&
764 	    (nl7c = nl7c_lookup_addr(addr, addrlen))) ||
765 	    so->so_nl7c_flags == NL7C_AF_NCA)) {
766 		/*
767 		 * NL7C is not supported in non-global zones,
768 		 * we enforce this restriction here.
769 		 */
770 		if (so->so_zoneid == GLOBAL_ZONEID) {
771 			/* An NL7C socket, mark it */
772 			so->so_nl7c_flags |= NL7C_ENABLED;
773 			if (nl7c == NULL) {
774 				/*
775 				 * Was an AF_NCA bind() so add it to the
776 				 * addr list for reporting purposes.
777 				 */
778 				nl7c = nl7c_add_addr(addr, addrlen);
779 			}
780 		} else
781 			nl7c = NULL;
782 	}
783 	/*
784 	 * We send a T_BIND_REQ for TCP/UDP since we know it supports it,
785 	 * for other transports we will send in a O_T_BIND_REQ.
786 	 */
787 	if (tcp_udp_xport &&
788 	    (so->so_family == AF_INET || so->so_family == AF_INET6))
789 		PRIM_type = T_BIND_REQ;
790 
791 	bind_req.PRIM_type = PRIM_type;
792 	bind_req.ADDR_length = addrlen;
793 	bind_req.ADDR_offset = (t_scalar_t)sizeof (bind_req);
794 	bind_req.CONIND_number = backlog;
795 	/* NOTE: holding so_lock while sleeping */
796 	mp = soallocproto2(&bind_req, sizeof (bind_req),
797 	    addr, addrlen, 0, _ALLOC_SLEEP);
798 	so->so_state &= ~SS_LADDR_VALID;
799 
800 	/* Done using so_laddr_sa - can drop the lock */
801 	mutex_exit(&so->so_lock);
802 
803 	/*
804 	 * Intercept the bind_req message here to check if this <address/port>
805 	 * was configured as an SSL proxy server, or if another endpoint was
806 	 * already configured to act as a proxy for us.
807 	 *
808 	 * Note, only if NL7C not enabled for this socket.
809 	 */
810 	if (nl7c == NULL &&
811 	    (so->so_family == AF_INET || so->so_family == AF_INET6) &&
812 	    so->so_type == SOCK_STREAM) {
813 
814 		if (so->so_kssl_ent != NULL) {
815 			kssl_release_ent(so->so_kssl_ent, so, so->so_kssl_type);
816 			so->so_kssl_ent = NULL;
817 		}
818 
819 		so->so_kssl_type = kssl_check_proxy(mp, so, &so->so_kssl_ent);
820 		switch (so->so_kssl_type) {
821 		case KSSL_NO_PROXY:
822 			break;
823 
824 		case KSSL_HAS_PROXY:
825 			mutex_enter(&so->so_lock);
826 			goto skip_transport;
827 
828 		case KSSL_IS_PROXY:
829 			break;
830 		}
831 	}
832 
833 	error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0,
834 	    MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0);
835 	if (error) {
836 		eprintsoline(so, error);
837 		mutex_enter(&so->so_lock);
838 		goto done;
839 	}
840 
841 	mutex_enter(&so->so_lock);
842 	error = sowaitprim(so, PRIM_type, T_BIND_ACK,
843 	    (t_uscalar_t)sizeof (*bind_ack), &mp, 0);
844 	if (error) {
845 		eprintsoline(so, error);
846 		goto done;
847 	}
848 skip_transport:
849 	ASSERT(mp);
850 	/*
851 	 * Even if some TPI message (e.g. T_DISCON_IND) was received in
852 	 * strsock_proto while the lock was dropped above, the bind
853 	 * is allowed to complete.
854 	 */
855 
856 	/* Mark as bound. This will be undone if we detect errors below. */
857 	if (flags & _SOBIND_NOXLATE) {
858 		ASSERT(so->so_family == AF_UNIX);
859 		so->so_state |= SS_FADDR_NOXLATE;
860 	}
861 	ASSERT(!(so->so_state & SS_ISBOUND) || (flags & _SOBIND_REBIND));
862 	so->so_state |= SS_ISBOUND;
863 	ASSERT(so->so_unbind_mp);
864 
865 	/* note that we've already set SS_ACCEPTCONN above */
866 
867 	/*
868 	 * Recompute addrlen - an unspecied bind sent down an
869 	 * address of length zero but we expect the appropriate length
870 	 * in return.
871 	 */
872 	addrlen = (t_uscalar_t)(so->so_family == AF_UNIX ?
873 	    sizeof (so->so_ux_laddr) : so->so_laddr_len);
874 
875 	bind_ack = (struct T_bind_ack *)mp->b_rptr;
876 	/*
877 	 * The alignment restriction is really too strict but
878 	 * we want enough alignment to inspect the fields of
879 	 * a sockaddr_in.
880 	 */
881 	addr = sogetoff(mp, bind_ack->ADDR_offset,
882 	    bind_ack->ADDR_length,
883 	    __TPI_ALIGN_SIZE);
884 	if (addr == NULL) {
885 		freemsg(mp);
886 		error = EPROTO;
887 		eprintsoline(so, error);
888 		goto done;
889 	}
890 	if (!(flags & _SOBIND_UNSPEC)) {
891 		/*
892 		 * Verify that the transport didn't return something we
893 		 * did not want e.g. an address other than what we asked for.
894 		 *
895 		 * NOTE: These checks would go away if/when we switch to
896 		 * using the new TPI (in which the transport would fail
897 		 * the request instead of assigning a different address).
898 		 *
899 		 * NOTE2: For protocols that we don't know (i.e. any
900 		 * other than AF_INET6, AF_INET and AF_UNIX), we
901 		 * cannot know if the transport should be expected to
902 		 * return the same address as that requested.
903 		 *
904 		 * NOTE3: For AF_INET and AF_INET6, TCP/UDP, we send
905 		 * down a T_BIND_REQ. We use O_T_BIND_REQ for others.
906 		 *
907 		 * For example, in the case of netatalk it may be
908 		 * inappropriate for the transport to return the
909 		 * requested address (as it may have allocated a local
910 		 * port number in behaviour similar to that of an
911 		 * AF_INET bind request with a port number of zero).
912 		 *
913 		 * Given the definition of O_T_BIND_REQ, where the
914 		 * transport may bind to an address other than the
915 		 * requested address, it's not possible to determine
916 		 * whether a returned address that differs from the
917 		 * requested address is a reason to fail (because the
918 		 * requested address was not available) or succeed
919 		 * (because the transport allocated an appropriate
920 		 * address and/or port).
921 		 *
922 		 * sockfs currently requires that the transport return
923 		 * the requested address in the T_BIND_ACK, unless
924 		 * there is code here to allow for any discrepancy.
925 		 * Such code exists for AF_INET and AF_INET6.
926 		 *
927 		 * Netatalk chooses to return the requested address
928 		 * rather than the (correct) allocated address.  This
929 		 * means that netatalk violates the TPI specification
930 		 * (and would not function correctly if used from a
931 		 * TLI application), but it does mean that it works
932 		 * with sockfs.
933 		 *
934 		 * As noted above, using the newer XTI bind primitive
935 		 * (T_BIND_REQ) in preference to O_T_BIND_REQ would
936 		 * allow sockfs to be more sure about whether or not
937 		 * the bind request had succeeded (as transports are
938 		 * not permitted to bind to a different address than
939 		 * that requested - they must return failure).
940 		 * Unfortunately, support for T_BIND_REQ may not be
941 		 * present in all transport implementations (netatalk,
942 		 * for example, doesn't have it), making the
943 		 * transition difficult.
944 		 */
945 		if (bind_ack->ADDR_length != addrlen) {
946 			/* Assumes that the requested address was in use */
947 			freemsg(mp);
948 			error = EADDRINUSE;
949 			eprintsoline(so, error);
950 			goto done;
951 		}
952 
953 		switch (so->so_family) {
954 		case AF_INET6:
955 		case AF_INET: {
956 			sin_t *rname, *aname;
957 
958 			rname = (sin_t *)addr;
959 			aname = (sin_t *)so->so_laddr_sa;
960 
961 			/*
962 			 * Take advantage of the alignment
963 			 * of sin_port and sin6_port which fall
964 			 * in the same place in their data structures.
965 			 * Just use sin_port for either address family.
966 			 *
967 			 * This may become a problem if (heaven forbid)
968 			 * there's a separate ipv6port_reserved... :-P
969 			 *
970 			 * Binding to port 0 has the semantics of letting
971 			 * the transport bind to any port.
972 			 *
973 			 * If the transport is TCP or UDP since we had sent
974 			 * a T_BIND_REQ we would not get a port other than
975 			 * what we asked for.
976 			 */
977 			if (tcp_udp_xport) {
978 				/*
979 				 * Pick up the new port number if we bound to
980 				 * port 0.
981 				 */
982 				if (aname->sin_port == 0)
983 					aname->sin_port = rname->sin_port;
984 				so->so_state |= SS_LADDR_VALID;
985 				break;
986 			}
987 			if (aname->sin_port != 0 &&
988 			    aname->sin_port != rname->sin_port) {
989 				freemsg(mp);
990 				error = EADDRINUSE;
991 				eprintsoline(so, error);
992 				goto done;
993 			}
994 			/*
995 			 * Pick up the new port number if we bound to port 0.
996 			 */
997 			aname->sin_port = rname->sin_port;
998 
999 			/*
1000 			 * Unfortunately, addresses aren't _quite_ the same.
1001 			 */
1002 			if (so->so_family == AF_INET) {
1003 				if (aname->sin_addr.s_addr !=
1004 				    rname->sin_addr.s_addr) {
1005 					freemsg(mp);
1006 					error = EADDRNOTAVAIL;
1007 					eprintsoline(so, error);
1008 					goto done;
1009 				}
1010 			} else {
1011 				sin6_t *rname6 = (sin6_t *)rname;
1012 				sin6_t *aname6 = (sin6_t *)aname;
1013 
1014 				if (!IN6_ARE_ADDR_EQUAL(&aname6->sin6_addr,
1015 				    &rname6->sin6_addr)) {
1016 					freemsg(mp);
1017 					error = EADDRNOTAVAIL;
1018 					eprintsoline(so, error);
1019 					goto done;
1020 				}
1021 			}
1022 			break;
1023 		}
1024 		case AF_UNIX:
1025 			if (bcmp(addr, &so->so_ux_laddr, addrlen) != 0) {
1026 				freemsg(mp);
1027 				error = EADDRINUSE;
1028 				eprintsoline(so, error);
1029 				eprintso(so,
1030 				    ("addrlen %d, addr 0x%x, vp %p\n",
1031 				    addrlen, *((int *)addr),
1032 				    so->so_ux_bound_vp));
1033 				goto done;
1034 			}
1035 			so->so_state |= SS_LADDR_VALID;
1036 			break;
1037 		default:
1038 			/*
1039 			 * NOTE: This assumes that addresses can be
1040 			 * byte-compared for equivalence.
1041 			 */
1042 			if (bcmp(addr, so->so_laddr_sa, addrlen) != 0) {
1043 				freemsg(mp);
1044 				error = EADDRINUSE;
1045 				eprintsoline(so, error);
1046 				goto done;
1047 			}
1048 			/*
1049 			 * Don't mark SS_LADDR_VALID, as we cannot be
1050 			 * sure that the returned address is the real
1051 			 * bound address when talking to an unknown
1052 			 * transport.
1053 			 */
1054 			break;
1055 		}
1056 	} else {
1057 		/*
1058 		 * Save for returned address for getsockname.
1059 		 * Needed for unspecific bind unless transport supports
1060 		 * the TI_GETMYNAME ioctl.
1061 		 * Do this for AF_INET{,6} even though they do, as
1062 		 * caching info here is much better performance than
1063 		 * a TPI/STREAMS trip to the transport for getsockname.
1064 		 * Any which can't for some reason _must_ _not_ set
1065 		 * LADDR_VALID here for the caching version of getsockname
1066 		 * to not break;
1067 		 */
1068 		switch (so->so_family) {
1069 		case AF_UNIX:
1070 			/*
1071 			 * Record the address bound with the transport
1072 			 * for use by socketpair.
1073 			 */
1074 			bcopy(addr, &so->so_ux_laddr, addrlen);
1075 			so->so_state |= SS_LADDR_VALID;
1076 			break;
1077 		case AF_INET:
1078 		case AF_INET6:
1079 			ASSERT(so->so_laddr_len <= so->so_laddr_maxlen);
1080 			bcopy(addr, so->so_laddr_sa, so->so_laddr_len);
1081 			so->so_state |= SS_LADDR_VALID;
1082 			break;
1083 		default:
1084 			/*
1085 			 * Don't mark SS_LADDR_VALID, as we cannot be
1086 			 * sure that the returned address is the real
1087 			 * bound address when talking to an unknown
1088 			 * transport.
1089 			 */
1090 			break;
1091 		}
1092 	}
1093 
1094 	if (nl7c != NULL) {
1095 		/* Register listen()er sonode pointer with NL7C */
1096 		nl7c_listener_addr(nl7c, so);
1097 	}
1098 
1099 	freemsg(mp);
1100 
1101 done:
1102 	if (error) {
1103 		/* reset state & backlog to values held on entry */
1104 		if (clear_acceptconn_on_err == B_TRUE)
1105 			so->so_state &= ~SS_ACCEPTCONN;
1106 		if (restore_backlog_on_err == B_TRUE)
1107 			so->so_backlog = save_so_backlog;
1108 
1109 		if (unbind_on_err && so->so_state & SS_ISBOUND) {
1110 			int err;
1111 
1112 			err = sotpi_unbind(so, 0);
1113 			/* LINTED - statement has no consequent: if */
1114 			if (err) {
1115 				eprintsoline(so, error);
1116 			} else {
1117 				ASSERT(!(so->so_state & SS_ISBOUND));
1118 			}
1119 		}
1120 	}
1121 	if (!(flags & _SOBIND_LOCK_HELD)) {
1122 		so_unlock_single(so, SOLOCKED);
1123 		mutex_exit(&so->so_lock);
1124 	} else {
1125 		/* If the caller held the lock don't release it here */
1126 		ASSERT(MUTEX_HELD(&so->so_lock));
1127 		ASSERT(so->so_flag & SOLOCKED);
1128 	}
1129 	return (error);
1130 }
1131 
1132 /* bind the socket */
1133 static int
1134 sotpi_bind(struct sonode *so, struct sockaddr *name, socklen_t namelen,
1135     int flags)
1136 {
1137 	if ((flags & _SOBIND_SOCKETPAIR) == 0)
1138 		return (sotpi_bindlisten(so, name, namelen, 0, flags));
1139 
1140 	flags &= ~_SOBIND_SOCKETPAIR;
1141 	return (sotpi_bindlisten(so, name, namelen, 1, flags));
1142 }
1143 
1144 /*
1145  * Unbind a socket - used when bind() fails, when bind() specifies a NULL
1146  * address, or when listen needs to unbind and bind.
1147  * If the _SOUNBIND_REBIND flag is specified the addresses are retained
1148  * so that a sobind can pick them up.
1149  */
1150 static int
1151 sotpi_unbind(struct sonode *so, int flags)
1152 {
1153 	struct T_unbind_req	unbind_req;
1154 	int			error = 0;
1155 	mblk_t			*mp;
1156 
1157 	dprintso(so, 1, ("sotpi_unbind(%p, 0x%x) %s\n",
1158 	    so, flags, pr_state(so->so_state, so->so_mode)));
1159 
1160 	ASSERT(MUTEX_HELD(&so->so_lock));
1161 	ASSERT(so->so_flag & SOLOCKED);
1162 
1163 	if (!(so->so_state & SS_ISBOUND)) {
1164 		error = EINVAL;
1165 		eprintsoline(so, error);
1166 		goto done;
1167 	}
1168 
1169 	mutex_exit(&so->so_lock);
1170 
1171 	/*
1172 	 * Flush the read and write side (except stream head read queue)
1173 	 * and send down T_UNBIND_REQ.
1174 	 */
1175 	(void) putnextctl1(strvp2wq(SOTOV(so)), M_FLUSH, FLUSHRW);
1176 
1177 	unbind_req.PRIM_type = T_UNBIND_REQ;
1178 	mp = soallocproto1(&unbind_req, sizeof (unbind_req),
1179 	    0, _ALLOC_SLEEP);
1180 	error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0,
1181 	    MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0);
1182 	mutex_enter(&so->so_lock);
1183 	if (error) {
1184 		eprintsoline(so, error);
1185 		goto done;
1186 	}
1187 
1188 	error = sowaitokack(so, T_UNBIND_REQ);
1189 	if (error) {
1190 		eprintsoline(so, error);
1191 		goto done;
1192 	}
1193 
1194 	/*
1195 	 * Even if some TPI message (e.g. T_DISCON_IND) was received in
1196 	 * strsock_proto while the lock was dropped above, the unbind
1197 	 * is allowed to complete.
1198 	 */
1199 	if (!(flags & _SOUNBIND_REBIND)) {
1200 		/*
1201 		 * Clear out bound address.
1202 		 */
1203 		vnode_t *vp;
1204 
1205 		if ((vp = so->so_ux_bound_vp) != NULL) {
1206 
1207 			/* Undo any SSL proxy setup */
1208 			if ((so->so_family == AF_INET ||
1209 			    so->so_family == AF_INET6) &&
1210 			    (so->so_type == SOCK_STREAM) &&
1211 			    (so->so_kssl_ent != NULL)) {
1212 				kssl_release_ent(so->so_kssl_ent, so,
1213 				    so->so_kssl_type);
1214 				so->so_kssl_ent = NULL;
1215 				so->so_kssl_type = KSSL_NO_PROXY;
1216 			}
1217 
1218 			so->so_ux_bound_vp = NULL;
1219 			vn_rele_stream(vp);
1220 		}
1221 		/* Clear out address */
1222 		so->so_laddr_len = 0;
1223 	}
1224 	so->so_state &= ~(SS_ISBOUND|SS_ACCEPTCONN|SS_LADDR_VALID);
1225 
1226 done:
1227 
1228 	/* If the caller held the lock don't release it here */
1229 	ASSERT(MUTEX_HELD(&so->so_lock));
1230 	ASSERT(so->so_flag & SOLOCKED);
1231 
1232 	return (error);
1233 }
1234 
1235 /*
1236  * listen on the socket.
1237  * For TPI conforming transports this has to first unbind with the transport
1238  * and then bind again using the new backlog.
1239  */
1240 int
1241 sotpi_listen(struct sonode *so, int backlog)
1242 {
1243 	int		error = 0;
1244 
1245 	dprintso(so, 1, ("sotpi_listen(%p, %d) %s\n",
1246 	    so, backlog, pr_state(so->so_state, so->so_mode)));
1247 
1248 	if (so->so_serv_type == T_CLTS)
1249 		return (EOPNOTSUPP);
1250 
1251 	/*
1252 	 * If the socket is ready to accept connections already, then
1253 	 * return without doing anything.  This avoids a problem where
1254 	 * a second listen() call fails if a connection is pending and
1255 	 * leaves the socket unbound. Only when we are not unbinding
1256 	 * with the transport can we safely increase the backlog.
1257 	 */
1258 	if (so->so_state & SS_ACCEPTCONN &&
1259 	    !((so->so_family == AF_INET || so->so_family == AF_INET6) &&
1260 	    /*CONSTCOND*/
1261 	    !solisten_tpi_tcp))
1262 		return (0);
1263 
1264 	if (so->so_state & SS_ISCONNECTED)
1265 		return (EINVAL);
1266 
1267 	mutex_enter(&so->so_lock);
1268 	so_lock_single(so);	/* Set SOLOCKED */
1269 
1270 	if (backlog < 0)
1271 		backlog = 0;
1272 	/*
1273 	 * Use the same qlimit as in BSD. BSD checks the qlimit
1274 	 * before queuing the next connection implying that a
1275 	 * listen(sock, 0) allows one connection to be queued.
1276 	 * BSD also uses 1.5 times the requested backlog.
1277 	 *
1278 	 * XNS Issue 4 required a strict interpretation of the backlog.
1279 	 * This has been waived subsequently for Issue 4 and the change
1280 	 * incorporated in XNS Issue 5. So we aren't required to do
1281 	 * anything special for XPG apps.
1282 	 */
1283 	if (backlog >= (INT_MAX - 1) / 3)
1284 		backlog = INT_MAX;
1285 	else
1286 		backlog = backlog * 3 / 2 + 1;
1287 
1288 	/*
1289 	 * If the listen doesn't change the backlog we do nothing.
1290 	 * This avoids an EPROTO error from the transport.
1291 	 */
1292 	if ((so->so_state & SS_ACCEPTCONN) &&
1293 	    so->so_backlog == backlog)
1294 		goto done;
1295 
1296 	if (!(so->so_state & SS_ISBOUND)) {
1297 		/*
1298 		 * Must have been explicitly bound in the UNIX domain.
1299 		 */
1300 		if (so->so_family == AF_UNIX) {
1301 			error = EINVAL;
1302 			goto done;
1303 		}
1304 		error = sotpi_bindlisten(so, NULL, 0, backlog,
1305 		    _SOBIND_UNSPEC|_SOBIND_LOCK_HELD|_SOBIND_LISTEN);
1306 	} else if (backlog > 0) {
1307 		/*
1308 		 * AF_INET{,6} hack to avoid losing the port.
1309 		 * Assumes that all AF_INET{,6} transports can handle a
1310 		 * O_T_BIND_REQ with a non-zero CONIND_number when the TPI
1311 		 * has already bound thus it is possible to avoid the unbind.
1312 		 */
1313 		if (!((so->so_family == AF_INET || so->so_family == AF_INET6) &&
1314 		    /*CONSTCOND*/
1315 		    !solisten_tpi_tcp)) {
1316 			error = sotpi_unbind(so, _SOUNBIND_REBIND);
1317 			if (error)
1318 				goto done;
1319 		}
1320 		error = sotpi_bindlisten(so, NULL, 0, backlog,
1321 		    _SOBIND_REBIND|_SOBIND_LOCK_HELD|_SOBIND_LISTEN);
1322 	} else {
1323 		so->so_state |= SS_ACCEPTCONN;
1324 		so->so_backlog = backlog;
1325 	}
1326 	if (error)
1327 		goto done;
1328 	ASSERT(so->so_state & SS_ACCEPTCONN);
1329 done:
1330 	so_unlock_single(so, SOLOCKED);
1331 	mutex_exit(&so->so_lock);
1332 	return (error);
1333 }
1334 
1335 /*
1336  * Disconnect either a specified seqno or all (-1).
1337  * The former is used on listening sockets only.
1338  *
1339  * When seqno == -1 sodisconnect could call sotpi_unbind. However,
1340  * the current use of sodisconnect(seqno == -1) is only for shutdown
1341  * so there is no point (and potentially incorrect) to unbind.
1342  */
1343 int
1344 sodisconnect(struct sonode *so, t_scalar_t seqno, int flags)
1345 {
1346 	struct T_discon_req	discon_req;
1347 	int			error = 0;
1348 	mblk_t			*mp;
1349 
1350 	dprintso(so, 1, ("sodisconnect(%p, %d, 0x%x) %s\n",
1351 	    so, seqno, flags, pr_state(so->so_state, so->so_mode)));
1352 
1353 	if (!(flags & _SODISCONNECT_LOCK_HELD)) {
1354 		mutex_enter(&so->so_lock);
1355 		so_lock_single(so);	/* Set SOLOCKED */
1356 	} else {
1357 		ASSERT(MUTEX_HELD(&so->so_lock));
1358 		ASSERT(so->so_flag & SOLOCKED);
1359 	}
1360 
1361 	if (!(so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING|SS_ACCEPTCONN))) {
1362 		error = EINVAL;
1363 		eprintsoline(so, error);
1364 		goto done;
1365 	}
1366 
1367 	mutex_exit(&so->so_lock);
1368 	/*
1369 	 * Flush the write side (unless this is a listener)
1370 	 * and then send down a T_DISCON_REQ.
1371 	 * (Don't flush on listener since it could flush {O_}T_CONN_RES
1372 	 * and other messages.)
1373 	 */
1374 	if (!(so->so_state & SS_ACCEPTCONN))
1375 		(void) putnextctl1(strvp2wq(SOTOV(so)), M_FLUSH, FLUSHW);
1376 
1377 	discon_req.PRIM_type = T_DISCON_REQ;
1378 	discon_req.SEQ_number = seqno;
1379 	mp = soallocproto1(&discon_req, sizeof (discon_req),
1380 	    0, _ALLOC_SLEEP);
1381 	error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0,
1382 	    MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0);
1383 	mutex_enter(&so->so_lock);
1384 	if (error) {
1385 		eprintsoline(so, error);
1386 		goto done;
1387 	}
1388 
1389 	error = sowaitokack(so, T_DISCON_REQ);
1390 	if (error) {
1391 		eprintsoline(so, error);
1392 		goto done;
1393 	}
1394 	/*
1395 	 * Even if some TPI message (e.g. T_DISCON_IND) was received in
1396 	 * strsock_proto while the lock was dropped above, the disconnect
1397 	 * is allowed to complete. However, it is not possible to
1398 	 * assert that SS_ISCONNECTED|SS_ISCONNECTING are set.
1399 	 */
1400 	so->so_state &=
1401 	    ~(SS_ISCONNECTED|SS_ISCONNECTING|SS_LADDR_VALID|SS_FADDR_VALID);
1402 done:
1403 	if (!(flags & _SODISCONNECT_LOCK_HELD)) {
1404 		so_unlock_single(so, SOLOCKED);
1405 		mutex_exit(&so->so_lock);
1406 	} else {
1407 		/* If the caller held the lock don't release it here */
1408 		ASSERT(MUTEX_HELD(&so->so_lock));
1409 		ASSERT(so->so_flag & SOLOCKED);
1410 	}
1411 	return (error);
1412 }
1413 
1414 int
1415 sotpi_accept(struct sonode *so, int fflag, struct sonode **nsop)
1416 {
1417 	struct T_conn_ind	*conn_ind;
1418 	struct T_conn_res	*conn_res;
1419 	int			error = 0;
1420 	mblk_t			*mp, *ctxmp, *ack_mp;
1421 	struct sonode		*nso;
1422 	vnode_t			*nvp;
1423 	void			*src;
1424 	t_uscalar_t		srclen;
1425 	void			*opt;
1426 	t_uscalar_t		optlen;
1427 	t_scalar_t		PRIM_type;
1428 	t_scalar_t		SEQ_number;
1429 	size_t			sinlen;
1430 
1431 	dprintso(so, 1, ("sotpi_accept(%p, 0x%x, %p) %s\n",
1432 	    so, fflag, nsop, pr_state(so->so_state, so->so_mode)));
1433 
1434 	/*
1435 	 * Defer single-threading the accepting socket until
1436 	 * the T_CONN_IND has been received and parsed and the
1437 	 * new sonode has been opened.
1438 	 */
1439 
1440 	/* Check that we are not already connected */
1441 	if ((so->so_state & SS_ACCEPTCONN) == 0)
1442 		goto conn_bad;
1443 again:
1444 	if ((error = sowaitconnind(so, fflag, &mp)) != 0)
1445 		goto e_bad;
1446 
1447 	ASSERT(mp);
1448 	conn_ind = (struct T_conn_ind *)mp->b_rptr;
1449 	ctxmp = mp->b_cont;
1450 
1451 	/*
1452 	 * Save SEQ_number for error paths.
1453 	 */
1454 	SEQ_number = conn_ind->SEQ_number;
1455 
1456 	srclen = conn_ind->SRC_length;
1457 	src = sogetoff(mp, conn_ind->SRC_offset, srclen, 1);
1458 	if (src == NULL) {
1459 		error = EPROTO;
1460 		freemsg(mp);
1461 		eprintsoline(so, error);
1462 		goto disconnect_unlocked;
1463 	}
1464 	optlen = conn_ind->OPT_length;
1465 	switch (so->so_family) {
1466 	case AF_INET:
1467 	case AF_INET6:
1468 		if ((optlen == sizeof (intptr_t)) &&
1469 		    ((so->so_state & SS_DIRECT) != 0)) {
1470 			bcopy(mp->b_rptr + conn_ind->OPT_offset,
1471 			    &opt, conn_ind->OPT_length);
1472 		} else {
1473 			/*
1474 			 * The transport (in this case TCP) hasn't sent up
1475 			 * a pointer to an instance for the accept fast-path.
1476 			 * Disable fast-path completely because the call to
1477 			 * sotpi_create() below would otherwise create an
1478 			 * incomplete TCP instance, which would lead to
1479 			 * problems when sockfs sends a normal T_CONN_RES
1480 			 * message down the new stream.
1481 			 */
1482 			if (so->so_state & SS_DIRECT) {
1483 				int rval;
1484 				/*
1485 				 * For consistency we inform tcp to disable
1486 				 * direct interface on the listener, though
1487 				 * we can certainly live without doing this
1488 				 * because no data will ever travel upstream
1489 				 * on the listening socket.
1490 				 */
1491 				so->so_state &= ~SS_DIRECT;
1492 				(void) strioctl(SOTOV(so), _SIOCSOCKFALLBACK,
1493 				    0, 0, K_TO_K, CRED(), &rval);
1494 			}
1495 			opt = NULL;
1496 			optlen = 0;
1497 		}
1498 		break;
1499 	case AF_UNIX:
1500 	default:
1501 		if (optlen != 0) {
1502 			opt = sogetoff(mp, conn_ind->OPT_offset, optlen,
1503 			    __TPI_ALIGN_SIZE);
1504 			if (opt == NULL) {
1505 				error = EPROTO;
1506 				freemsg(mp);
1507 				eprintsoline(so, error);
1508 				goto disconnect_unlocked;
1509 			}
1510 		}
1511 		if (so->so_family == AF_UNIX) {
1512 			if (!(so->so_state & SS_FADDR_NOXLATE)) {
1513 				src = NULL;
1514 				srclen = 0;
1515 			}
1516 			/* Extract src address from options */
1517 			if (optlen != 0)
1518 				so_getopt_srcaddr(opt, optlen, &src, &srclen);
1519 		}
1520 		break;
1521 	}
1522 
1523 	/*
1524 	 * Create the new socket.
1525 	 */
1526 	VN_HOLD(so->so_accessvp);
1527 	nso = sotpi_create(so->so_accessvp, so->so_family, so->so_type,
1528 	    so->so_protocol, so->so_version, so, &error);
1529 	if (nso == NULL) {
1530 		ASSERT(error != 0);
1531 		/*
1532 		 * Accept can not fail with ENOBUFS. sotpi_create
1533 		 * sleeps waiting for memory until a signal is caught
1534 		 * so return EINTR.
1535 		 */
1536 		freemsg(mp);
1537 		if (error == ENOBUFS)
1538 			error = EINTR;
1539 		goto e_disc_unl;
1540 	}
1541 	nvp = SOTOV(nso);
1542 
1543 	/*
1544 	 * If the transport sent up an SSL connection context, then attach
1545 	 * it the new socket, and set the (sd_wputdatafunc)() and
1546 	 * (sd_rputdatafunc)() stream head hooks to intercept and process
1547 	 * SSL records.
1548 	 */
1549 	if (ctxmp != NULL) {
1550 		/*
1551 		 * This kssl_ctx_t is already held for us by the transport.
1552 		 * So, we don't need to do a kssl_hold_ctx() here.
1553 		 */
1554 		nso->so_kssl_ctx = *((kssl_ctx_t *)ctxmp->b_rptr);
1555 		freemsg(ctxmp);
1556 		mp->b_cont = NULL;
1557 		strsetrwputdatahooks(nvp, strsock_kssl_input,
1558 		    strsock_kssl_output);
1559 	}
1560 #ifdef DEBUG
1561 	/*
1562 	 * SO_DEBUG is used to trigger the dprint* and eprint* macros thus
1563 	 * it's inherited early to allow debugging of the accept code itself.
1564 	 */
1565 	nso->so_options |= so->so_options & SO_DEBUG;
1566 #endif /* DEBUG */
1567 
1568 	/*
1569 	 * Save the SRC address from the T_CONN_IND
1570 	 * for getpeername to work on AF_UNIX and on transports that do not
1571 	 * support TI_GETPEERNAME.
1572 	 *
1573 	 * NOTE: AF_UNIX NUL termination is ensured by the sender's
1574 	 * copyin_name().
1575 	 */
1576 	if (srclen > (t_uscalar_t)nso->so_faddr_maxlen) {
1577 		error = EINVAL;
1578 		freemsg(mp);
1579 		eprintsoline(so, error);
1580 		goto disconnect_vp_unlocked;
1581 	}
1582 	nso->so_faddr_len = (socklen_t)srclen;
1583 	ASSERT(so->so_faddr_len <= so->so_faddr_maxlen);
1584 	bcopy(src, nso->so_faddr_sa, srclen);
1585 	nso->so_state |= SS_FADDR_VALID;
1586 
1587 	if ((DB_REF(mp) > 1) || MBLKSIZE(mp) <
1588 	    (sizeof (struct T_conn_res) + sizeof (intptr_t))) {
1589 		cred_t *cr;
1590 
1591 		if ((cr = DB_CRED(mp)) != NULL) {
1592 			crhold(cr);
1593 			nso->so_peercred = cr;
1594 			nso->so_cpid = DB_CPID(mp);
1595 		}
1596 		freemsg(mp);
1597 
1598 		mp = soallocproto1(NULL, sizeof (struct T_conn_res) +
1599 		    sizeof (intptr_t), 0, _ALLOC_INTR);
1600 		if (mp == NULL) {
1601 			/*
1602 			 * Accept can not fail with ENOBUFS.
1603 			 * A signal was caught so return EINTR.
1604 			 */
1605 			error = EINTR;
1606 			eprintsoline(so, error);
1607 			goto disconnect_vp_unlocked;
1608 		}
1609 		conn_res = (struct T_conn_res *)mp->b_rptr;
1610 	} else {
1611 		nso->so_peercred = DB_CRED(mp);
1612 		nso->so_cpid = DB_CPID(mp);
1613 		DB_CRED(mp) = NULL;
1614 
1615 		mp->b_rptr = DB_BASE(mp);
1616 		conn_res = (struct T_conn_res *)mp->b_rptr;
1617 		mp->b_wptr = mp->b_rptr + sizeof (struct T_conn_res);
1618 	}
1619 
1620 	/*
1621 	 * New socket must be bound at least in sockfs and, except for AF_INET,
1622 	 * (or AF_INET6) it also has to be bound in the transport provider.
1623 	 * We set the local address in the sonode from the T_OK_ACK of the
1624 	 * T_CONN_RES. For this reason the address we bind to here isn't
1625 	 * important.
1626 	 */
1627 	if ((nso->so_family == AF_INET || nso->so_family == AF_INET6) &&
1628 	    /*CONSTCOND*/
1629 	    nso->so_type == SOCK_STREAM && !soaccept_tpi_tcp) {
1630 		/*
1631 		 * Optimization for AF_INET{,6} transports
1632 		 * that can handle a T_CONN_RES without being bound.
1633 		 */
1634 		mutex_enter(&nso->so_lock);
1635 		so_automatic_bind(nso);
1636 		mutex_exit(&nso->so_lock);
1637 	} else {
1638 		/* Perform NULL bind with the transport provider. */
1639 		if ((error = sotpi_bind(nso, NULL, 0, _SOBIND_UNSPEC)) != 0) {
1640 			ASSERT(error != ENOBUFS);
1641 			freemsg(mp);
1642 			eprintsoline(nso, error);
1643 			goto disconnect_vp_unlocked;
1644 		}
1645 	}
1646 
1647 	/*
1648 	 * Inherit SIOCSPGRP, SS_ASYNC before we send the {O_}T_CONN_RES
1649 	 * so that any data arriving on the new socket will cause the
1650 	 * appropriate signals to be delivered for the new socket.
1651 	 *
1652 	 * No other thread (except strsock_proto and strsock_misc)
1653 	 * can access the new socket thus we relax the locking.
1654 	 */
1655 	nso->so_pgrp = so->so_pgrp;
1656 	nso->so_state |= so->so_state & (SS_ASYNC|SS_FADDR_NOXLATE);
1657 
1658 	if (nso->so_pgrp != 0) {
1659 		if ((error = so_set_events(nso, nvp, CRED())) != 0) {
1660 			eprintsoline(nso, error);
1661 			error = 0;
1662 			nso->so_pgrp = 0;
1663 		}
1664 	}
1665 
1666 	/*
1667 	 * Make note of the socket level options. TCP and IP level options
1668 	 * are already inherited. We could do all this after accept is
1669 	 * successful but doing it here simplifies code and no harm done
1670 	 * for error case.
1671 	 */
1672 	nso->so_options = so->so_options & (SO_DEBUG|SO_REUSEADDR|SO_KEEPALIVE|
1673 	    SO_DONTROUTE|SO_BROADCAST|SO_USELOOPBACK|
1674 	    SO_OOBINLINE|SO_DGRAM_ERRIND|SO_LINGER);
1675 	nso->so_sndbuf = so->so_sndbuf;
1676 	nso->so_rcvbuf = so->so_rcvbuf;
1677 	if (nso->so_options & SO_LINGER)
1678 		nso->so_linger = so->so_linger;
1679 
1680 	if ((so->so_state & SS_DIRECT) != 0) {
1681 
1682 		ASSERT(opt != NULL);
1683 
1684 		conn_res->OPT_length = optlen;
1685 		conn_res->OPT_offset = MBLKL(mp);
1686 		bcopy(&opt, mp->b_wptr, optlen);
1687 		mp->b_wptr += optlen;
1688 		conn_res->PRIM_type = T_CONN_RES;
1689 		conn_res->ACCEPTOR_id = 0;
1690 		PRIM_type = T_CONN_RES;
1691 
1692 		/* Send down the T_CONN_RES on acceptor STREAM */
1693 		error = kstrputmsg(SOTOV(nso), mp, NULL,
1694 		    0, 0, MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0);
1695 		if (error) {
1696 			mutex_enter(&so->so_lock);
1697 			so_lock_single(so);
1698 			eprintsoline(so, error);
1699 			goto disconnect_vp;
1700 		}
1701 		mutex_enter(&nso->so_lock);
1702 		error = sowaitprim(nso, T_CONN_RES, T_OK_ACK,
1703 		    (t_uscalar_t)sizeof (struct T_ok_ack), &ack_mp, 0);
1704 		if (error) {
1705 			mutex_exit(&nso->so_lock);
1706 			mutex_enter(&so->so_lock);
1707 			so_lock_single(so);
1708 			eprintsoline(so, error);
1709 			goto disconnect_vp;
1710 		}
1711 		if (nso->so_family == AF_INET) {
1712 			sin_t *sin;
1713 
1714 			sin = (sin_t *)(ack_mp->b_rptr +
1715 			    sizeof (struct T_ok_ack));
1716 			bcopy(sin, nso->so_laddr_sa, sizeof (sin_t));
1717 			nso->so_laddr_len = sizeof (sin_t);
1718 		} else {
1719 			sin6_t *sin6;
1720 
1721 			sin6 = (sin6_t *)(ack_mp->b_rptr +
1722 			    sizeof (struct T_ok_ack));
1723 			bcopy(sin6, nso->so_laddr_sa, sizeof (sin6_t));
1724 			nso->so_laddr_len = sizeof (sin6_t);
1725 		}
1726 		freemsg(ack_mp);
1727 
1728 		nso->so_state |= SS_ISCONNECTED | SS_LADDR_VALID;
1729 		nso->so_priv = opt;
1730 
1731 		if (so->so_nl7c_flags & NL7C_ENABLED) {
1732 			/*
1733 			 * A NL7C marked listen()er so the new socket
1734 			 * inherits the listen()er's NL7C state, except
1735 			 * for NL7C_POLLIN.
1736 			 *
1737 			 * Only call NL7C to process the new socket if
1738 			 * the listen socket allows blocking i/o.
1739 			 */
1740 			nso->so_nl7c_flags = so->so_nl7c_flags & (~NL7C_POLLIN);
1741 			if (so->so_state & (SS_NONBLOCK|SS_NDELAY)) {
1742 				/*
1743 				 * Nonblocking accept() just make it
1744 				 * persist to defer processing to the
1745 				 * read-side syscall (e.g. read).
1746 				 */
1747 				nso->so_nl7c_flags |= NL7C_SOPERSIST;
1748 			} else if (nl7c_process(nso, B_FALSE)) {
1749 				/*
1750 				 * NL7C has completed processing on the
1751 				 * socket, close the socket and back to
1752 				 * the top to await the next T_CONN_IND.
1753 				 */
1754 				mutex_exit(&nso->so_lock);
1755 				(void) VOP_CLOSE(nvp, 0, 1, (offset_t)0,
1756 				    CRED(), NULL);
1757 				VN_RELE(nvp);
1758 				goto again;
1759 			}
1760 			/* Pass the new socket out */
1761 		}
1762 
1763 		mutex_exit(&nso->so_lock);
1764 
1765 		/*
1766 		 * It's possible, through the use of autopush for example,
1767 		 * that the acceptor stream may not support SS_DIRECT
1768 		 * semantics. If the new socket does not support SS_DIRECT
1769 		 * we issue a _SIOCSOCKFALLBACK to inform the transport
1770 		 * as we would in the I_PUSH case.
1771 		 */
1772 		if (!(nso->so_state & SS_DIRECT)) {
1773 			int	rval;
1774 
1775 			if ((error = strioctl(SOTOV(nso), _SIOCSOCKFALLBACK,
1776 			    0, 0, K_TO_K, CRED(), &rval)) != 0) {
1777 				mutex_enter(&so->so_lock);
1778 				so_lock_single(so);
1779 				eprintsoline(so, error);
1780 				goto disconnect_vp;
1781 			}
1782 		}
1783 
1784 		/*
1785 		 * Pass out new socket.
1786 		 */
1787 		if (nsop != NULL)
1788 			*nsop = nso;
1789 
1790 		return (0);
1791 	}
1792 
1793 	/*
1794 	 * This is the non-performance case for sockets (e.g. AF_UNIX sockets)
1795 	 * which don't support the FireEngine accept fast-path. It is also
1796 	 * used when the virtual "sockmod" has been I_POP'd and I_PUSH'd
1797 	 * again. Neither sockfs nor TCP attempt to find out if some other
1798 	 * random module has been inserted in between (in which case we
1799 	 * should follow TLI accept behaviour). We blindly assume the worst
1800 	 * case and revert back to old behaviour i.e. TCP will not send us
1801 	 * any option (eager) and the accept should happen on the listener
1802 	 * queue. Any queued T_conn_ind have already got their options removed
1803 	 * by so_sock2_stream() when "sockmod" was I_POP'd.
1804 	 */
1805 	/*
1806 	 * Fill in the {O_}T_CONN_RES before getting SOLOCKED.
1807 	 */
1808 	if ((nso->so_mode & SM_ACCEPTOR_ID) == 0) {
1809 #ifdef	_ILP32
1810 		queue_t	*q;
1811 
1812 		/*
1813 		 * Find read queue in driver
1814 		 * Can safely do this since we "own" nso/nvp.
1815 		 */
1816 		q = strvp2wq(nvp)->q_next;
1817 		while (SAMESTR(q))
1818 			q = q->q_next;
1819 		q = RD(q);
1820 		conn_res->ACCEPTOR_id = (t_uscalar_t)q;
1821 #else
1822 		conn_res->ACCEPTOR_id = (t_uscalar_t)getminor(nvp->v_rdev);
1823 #endif	/* _ILP32 */
1824 		conn_res->PRIM_type = O_T_CONN_RES;
1825 		PRIM_type = O_T_CONN_RES;
1826 	} else {
1827 		conn_res->ACCEPTOR_id = nso->so_acceptor_id;
1828 		conn_res->PRIM_type = T_CONN_RES;
1829 		PRIM_type = T_CONN_RES;
1830 	}
1831 	conn_res->SEQ_number = SEQ_number;
1832 	conn_res->OPT_length = 0;
1833 	conn_res->OPT_offset = 0;
1834 
1835 	mutex_enter(&so->so_lock);
1836 	so_lock_single(so);	/* Set SOLOCKED */
1837 	mutex_exit(&so->so_lock);
1838 
1839 	error = kstrputmsg(SOTOV(so), mp, NULL,
1840 	    0, 0, MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0);
1841 	mutex_enter(&so->so_lock);
1842 	if (error) {
1843 		eprintsoline(so, error);
1844 		goto disconnect_vp;
1845 	}
1846 	error = sowaitprim(so, PRIM_type, T_OK_ACK,
1847 	    (t_uscalar_t)sizeof (struct T_ok_ack), &ack_mp, 0);
1848 	if (error) {
1849 		eprintsoline(so, error);
1850 		goto disconnect_vp;
1851 	}
1852 	/*
1853 	 * If there is a sin/sin6 appended onto the T_OK_ACK use
1854 	 * that to set the local address. If this is not present
1855 	 * then we zero out the address and don't set the
1856 	 * SS_LADDR_VALID bit. For AF_UNIX endpoints we copy over
1857 	 * the pathname from the listening socket.
1858 	 */
1859 	sinlen = (nso->so_family == AF_INET) ? sizeof (sin_t) : sizeof (sin6_t);
1860 	if ((nso->so_family == AF_INET) || (nso->so_family == AF_INET6) &&
1861 	    MBLKL(ack_mp) == (sizeof (struct T_ok_ack) + sinlen)) {
1862 		ack_mp->b_rptr += sizeof (struct T_ok_ack);
1863 		bcopy(ack_mp->b_rptr, nso->so_laddr_sa, sinlen);
1864 		nso->so_laddr_len = sinlen;
1865 		nso->so_state |= SS_LADDR_VALID;
1866 	} else if (nso->so_family == AF_UNIX) {
1867 		ASSERT(so->so_family == AF_UNIX);
1868 		nso->so_laddr_len = so->so_laddr_len;
1869 		ASSERT(nso->so_laddr_len <= nso->so_laddr_maxlen);
1870 		bcopy(so->so_laddr_sa, nso->so_laddr_sa, nso->so_laddr_len);
1871 		nso->so_state |= SS_LADDR_VALID;
1872 	} else {
1873 		nso->so_laddr_len = so->so_laddr_len;
1874 		ASSERT(nso->so_laddr_len <= nso->so_laddr_maxlen);
1875 		bzero(nso->so_laddr_sa, nso->so_addr_size);
1876 		nso->so_laddr_sa->sa_family = nso->so_family;
1877 	}
1878 	freemsg(ack_mp);
1879 
1880 	so_unlock_single(so, SOLOCKED);
1881 	mutex_exit(&so->so_lock);
1882 
1883 	nso->so_state |= SS_ISCONNECTED;
1884 
1885 	/*
1886 	 * Pass out new socket.
1887 	 */
1888 	if (nsop != NULL)
1889 		*nsop = nso;
1890 
1891 	return (0);
1892 
1893 
1894 eproto_disc_unl:
1895 	error = EPROTO;
1896 e_disc_unl:
1897 	eprintsoline(so, error);
1898 	goto disconnect_unlocked;
1899 
1900 pr_disc_vp_unl:
1901 	eprintsoline(so, error);
1902 disconnect_vp_unlocked:
1903 	(void) VOP_CLOSE(nvp, 0, 1, 0, CRED(), NULL);
1904 	VN_RELE(nvp);
1905 disconnect_unlocked:
1906 	(void) sodisconnect(so, SEQ_number, 0);
1907 	return (error);
1908 
1909 pr_disc_vp:
1910 	eprintsoline(so, error);
1911 disconnect_vp:
1912 	(void) sodisconnect(so, SEQ_number, _SODISCONNECT_LOCK_HELD);
1913 	so_unlock_single(so, SOLOCKED);
1914 	mutex_exit(&so->so_lock);
1915 	(void) VOP_CLOSE(nvp, 0, 1, 0, CRED(), NULL);
1916 	VN_RELE(nvp);
1917 	return (error);
1918 
1919 conn_bad:	/* Note: SunOS 4/BSD unconditionally returns EINVAL here */
1920 	error = (so->so_type == SOCK_DGRAM || so->so_type == SOCK_RAW)
1921 	    ? EOPNOTSUPP : EINVAL;
1922 e_bad:
1923 	eprintsoline(so, error);
1924 	return (error);
1925 }
1926 
1927 /*
1928  * connect a socket.
1929  *
1930  * Allow SOCK_DGRAM sockets to reconnect (by specifying a new address) and to
1931  * unconnect (by specifying a null address).
1932  */
1933 int
1934 sotpi_connect(struct sonode *so,
1935 	const struct sockaddr *name,
1936 	socklen_t namelen,
1937 	int fflag,
1938 	int flags)
1939 {
1940 	struct T_conn_req	conn_req;
1941 	int			error = 0;
1942 	mblk_t			*mp;
1943 	void			*src;
1944 	socklen_t		srclen;
1945 	void			*addr;
1946 	socklen_t		addrlen;
1947 	boolean_t		need_unlock;
1948 
1949 	dprintso(so, 1, ("sotpi_connect(%p, %p, %d, 0x%x, 0x%x) %s\n",
1950 	    so, name, namelen, fflag, flags,
1951 	    pr_state(so->so_state, so->so_mode)));
1952 
1953 	/*
1954 	 * Preallocate the T_CONN_REQ mblk before grabbing SOLOCKED to
1955 	 * avoid sleeping for memory with SOLOCKED held.
1956 	 * We know that the T_CONN_REQ can't be larger than 2 * so_faddr_maxlen
1957 	 * + sizeof (struct T_opthdr).
1958 	 * (the AF_UNIX so_ux_addr_xlate() does not make the address
1959 	 * exceed so_faddr_maxlen).
1960 	 */
1961 	mp = soallocproto(sizeof (struct T_conn_req) +
1962 	    2 * so->so_faddr_maxlen + sizeof (struct T_opthdr), _ALLOC_INTR);
1963 	if (mp == NULL) {
1964 		/*
1965 		 * Connect can not fail with ENOBUFS. A signal was
1966 		 * caught so return EINTR.
1967 		 */
1968 		error = EINTR;
1969 		eprintsoline(so, error);
1970 		return (error);
1971 	}
1972 
1973 	mutex_enter(&so->so_lock);
1974 	/*
1975 	 * Make sure that there is a preallocated unbind_req
1976 	 * message before any binding. This message allocated when
1977 	 * the socket is created  but it might be have been
1978 	 * consumed.
1979 	 */
1980 	if (so->so_unbind_mp == NULL) {
1981 		dprintso(so, 1, ("sotpi_connect: allocating unbind_req\n"));
1982 		/* NOTE: holding so_lock while sleeping */
1983 		so->so_unbind_mp =
1984 		    soallocproto(sizeof (struct T_unbind_req), _ALLOC_INTR);
1985 		if (so->so_unbind_mp == NULL) {
1986 			error = EINTR;
1987 			need_unlock = B_FALSE;
1988 			goto done;
1989 		}
1990 	}
1991 
1992 	so_lock_single(so);	/* Set SOLOCKED */
1993 	need_unlock = B_TRUE;
1994 
1995 	/*
1996 	 * Can't have done a listen before connecting.
1997 	 */
1998 	if (so->so_state & SS_ACCEPTCONN) {
1999 		error = EOPNOTSUPP;
2000 		goto done;
2001 	}
2002 
2003 	/*
2004 	 * Must be bound with the transport
2005 	 */
2006 	if (!(so->so_state & SS_ISBOUND)) {
2007 		if ((so->so_family == AF_INET || so->so_family == AF_INET6) &&
2008 		    /*CONSTCOND*/
2009 		    so->so_type == SOCK_STREAM && !soconnect_tpi_tcp) {
2010 			/*
2011 			 * Optimization for AF_INET{,6} transports
2012 			 * that can handle a T_CONN_REQ without being bound.
2013 			 */
2014 			so_automatic_bind(so);
2015 		} else {
2016 			error = sotpi_bind(so, NULL, 0,
2017 			    _SOBIND_UNSPEC|_SOBIND_LOCK_HELD);
2018 			if (error)
2019 				goto done;
2020 		}
2021 		ASSERT(so->so_state & SS_ISBOUND);
2022 		flags |= _SOCONNECT_DID_BIND;
2023 	}
2024 
2025 	/*
2026 	 * Handle a connect to a name parameter of type AF_UNSPEC like a
2027 	 * connect to a null address. This is the portable method to
2028 	 * unconnect a socket.
2029 	 */
2030 	if ((namelen >= sizeof (sa_family_t)) &&
2031 	    (name->sa_family == AF_UNSPEC)) {
2032 		name = NULL;
2033 		namelen = 0;
2034 	}
2035 
2036 	/*
2037 	 * Check that we are not already connected.
2038 	 * A connection-oriented socket cannot be reconnected.
2039 	 * A connected connection-less socket can be
2040 	 * - connected to a different address by a subsequent connect
2041 	 * - "unconnected" by a connect to the NULL address
2042 	 */
2043 	if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) {
2044 		ASSERT(!(flags & _SOCONNECT_DID_BIND));
2045 		if (so->so_mode & SM_CONNREQUIRED) {
2046 			/* Connection-oriented socket */
2047 			error = so->so_state & SS_ISCONNECTED ?
2048 			    EISCONN : EALREADY;
2049 			goto done;
2050 		}
2051 		/* Connection-less socket */
2052 		if (name == NULL) {
2053 			/*
2054 			 * Remove the connected state and clear SO_DGRAM_ERRIND
2055 			 * since it was set when the socket was connected.
2056 			 * If this is UDP also send down a T_DISCON_REQ.
2057 			 */
2058 			int val;
2059 
2060 			if ((so->so_family == AF_INET ||
2061 			    so->so_family == AF_INET6) &&
2062 			    (so->so_type == SOCK_DGRAM ||
2063 			    so->so_type == SOCK_RAW) &&
2064 			    /*CONSTCOND*/
2065 			    !soconnect_tpi_udp) {
2066 				/* XXX What about implicitly unbinding here? */
2067 				error = sodisconnect(so, -1,
2068 				    _SODISCONNECT_LOCK_HELD);
2069 			} else {
2070 				so->so_state &=
2071 				    ~(SS_ISCONNECTED | SS_ISCONNECTING |
2072 				    SS_FADDR_VALID);
2073 				so->so_faddr_len = 0;
2074 			}
2075 
2076 			so_unlock_single(so, SOLOCKED);
2077 			mutex_exit(&so->so_lock);
2078 
2079 			val = 0;
2080 			(void) sotpi_setsockopt(so, SOL_SOCKET, SO_DGRAM_ERRIND,
2081 			    &val, (t_uscalar_t)sizeof (val));
2082 
2083 			mutex_enter(&so->so_lock);
2084 			so_lock_single(so);	/* Set SOLOCKED */
2085 			goto done;
2086 		}
2087 	}
2088 	ASSERT(so->so_state & SS_ISBOUND);
2089 
2090 	if (name == NULL || namelen == 0) {
2091 		error = EINVAL;
2092 		goto done;
2093 	}
2094 	/*
2095 	 * Mark the socket if so_faddr_sa represents the transport level
2096 	 * address.
2097 	 */
2098 	if (flags & _SOCONNECT_NOXLATE) {
2099 		struct sockaddr_ux	*soaddr_ux;
2100 
2101 		ASSERT(so->so_family == AF_UNIX);
2102 		if (namelen != sizeof (struct sockaddr_ux)) {
2103 			error = EINVAL;
2104 			goto done;
2105 		}
2106 		soaddr_ux = (struct sockaddr_ux *)name;
2107 		name = (struct sockaddr *)&soaddr_ux->sou_addr;
2108 		namelen = sizeof (soaddr_ux->sou_addr);
2109 		so->so_state |= SS_FADDR_NOXLATE;
2110 	}
2111 
2112 	/*
2113 	 * Length and family checks.
2114 	 */
2115 	error = so_addr_verify(so, name, namelen);
2116 	if (error)
2117 		goto bad;
2118 
2119 	/*
2120 	 * Save foreign address. Needed for AF_UNIX as well as
2121 	 * transport providers that do not support TI_GETPEERNAME.
2122 	 * Also used for cached foreign address for TCP and UDP.
2123 	 */
2124 	if (namelen > (t_uscalar_t)so->so_faddr_maxlen) {
2125 		error = EINVAL;
2126 		goto done;
2127 	}
2128 	so->so_faddr_len = (socklen_t)namelen;
2129 	ASSERT(so->so_faddr_len <= so->so_faddr_maxlen);
2130 	bcopy(name, so->so_faddr_sa, namelen);
2131 	so->so_state |= SS_FADDR_VALID;
2132 
2133 	if (so->so_family == AF_UNIX) {
2134 		if (so->so_state & SS_FADDR_NOXLATE) {
2135 			/*
2136 			 * Already have a transport internal address. Do not
2137 			 * pass any (transport internal) source address.
2138 			 */
2139 			addr = so->so_faddr_sa;
2140 			addrlen = (t_uscalar_t)so->so_faddr_len;
2141 			src = NULL;
2142 			srclen = 0;
2143 		} else {
2144 			/*
2145 			 * Pass the sockaddr_un source address as an option
2146 			 * and translate the remote address.
2147 			 * Holding so_lock thus so_laddr_sa can not change.
2148 			 */
2149 			src = so->so_laddr_sa;
2150 			srclen = (t_uscalar_t)so->so_laddr_len;
2151 			dprintso(so, 1,
2152 			    ("sotpi_connect UNIX: srclen %d, src %p\n",
2153 			    srclen, src));
2154 			error = so_ux_addr_xlate(so,
2155 			    so->so_faddr_sa, (socklen_t)so->so_faddr_len,
2156 			    (flags & _SOCONNECT_XPG4_2),
2157 			    &addr, &addrlen);
2158 			if (error)
2159 				goto bad;
2160 		}
2161 	} else {
2162 		addr = so->so_faddr_sa;
2163 		addrlen = (t_uscalar_t)so->so_faddr_len;
2164 		src = NULL;
2165 		srclen = 0;
2166 	}
2167 	/*
2168 	 * When connecting a datagram socket we issue the SO_DGRAM_ERRIND
2169 	 * option which asks the transport provider to send T_UDERR_IND
2170 	 * messages. These T_UDERR_IND messages are used to return connected
2171 	 * style errors (e.g. ECONNRESET) for connected datagram sockets.
2172 	 *
2173 	 * In addition, for UDP (and SOCK_RAW AF_INET{,6} sockets)
2174 	 * we send down a T_CONN_REQ. This is needed to let the
2175 	 * transport assign a local address that is consistent with
2176 	 * the remote address. Applications depend on a getsockname()
2177 	 * after a connect() to retrieve the "source" IP address for
2178 	 * the connected socket.  Invalidate the cached local address
2179 	 * to force getsockname() to enquire of the transport.
2180 	 */
2181 	if (!(so->so_mode & SM_CONNREQUIRED)) {
2182 		/*
2183 		 * Datagram socket.
2184 		 */
2185 		int32_t val;
2186 
2187 		so_unlock_single(so, SOLOCKED);
2188 		mutex_exit(&so->so_lock);
2189 
2190 		val = 1;
2191 		(void) sotpi_setsockopt(so, SOL_SOCKET, SO_DGRAM_ERRIND,
2192 		    &val, (t_uscalar_t)sizeof (val));
2193 
2194 		mutex_enter(&so->so_lock);
2195 		so_lock_single(so);	/* Set SOLOCKED */
2196 		if ((so->so_family != AF_INET && so->so_family != AF_INET6) ||
2197 		    (so->so_type != SOCK_DGRAM && so->so_type != SOCK_RAW) ||
2198 		    soconnect_tpi_udp) {
2199 			soisconnected(so);
2200 			goto done;
2201 		}
2202 		/*
2203 		 * Send down T_CONN_REQ etc.
2204 		 * Clear fflag to avoid returning EWOULDBLOCK.
2205 		 */
2206 		fflag = 0;
2207 		ASSERT(so->so_family != AF_UNIX);
2208 		so->so_state &= ~SS_LADDR_VALID;
2209 	} else if (so->so_laddr_len != 0) {
2210 		/*
2211 		 * If the local address or port was "any" then it may be
2212 		 * changed by the transport as a result of the
2213 		 * connect.  Invalidate the cached version if we have one.
2214 		 */
2215 		switch (so->so_family) {
2216 		case AF_INET:
2217 			ASSERT(so->so_laddr_len == (socklen_t)sizeof (sin_t));
2218 			if (((sin_t *)so->so_laddr_sa)->sin_addr.s_addr ==
2219 			    INADDR_ANY ||
2220 			    ((sin_t *)so->so_laddr_sa)->sin_port == 0)
2221 				so->so_state &= ~SS_LADDR_VALID;
2222 			break;
2223 
2224 		case AF_INET6:
2225 			ASSERT(so->so_laddr_len == (socklen_t)sizeof (sin6_t));
2226 			if (IN6_IS_ADDR_UNSPECIFIED(
2227 			    &((sin6_t *)so->so_laddr_sa) ->sin6_addr) ||
2228 			    IN6_IS_ADDR_V4MAPPED_ANY(
2229 			    &((sin6_t *)so->so_laddr_sa)->sin6_addr) ||
2230 			    ((sin6_t *)so->so_laddr_sa)->sin6_port == 0)
2231 				so->so_state &= ~SS_LADDR_VALID;
2232 			break;
2233 
2234 		default:
2235 			break;
2236 		}
2237 	}
2238 
2239 	/*
2240 	 * Check for failure of an earlier call
2241 	 */
2242 	if (so->so_error != 0)
2243 		goto so_bad;
2244 
2245 	/*
2246 	 * Send down T_CONN_REQ. Message was allocated above.
2247 	 */
2248 	conn_req.PRIM_type = T_CONN_REQ;
2249 	conn_req.DEST_length = addrlen;
2250 	conn_req.DEST_offset = (t_scalar_t)sizeof (conn_req);
2251 	if (srclen == 0) {
2252 		conn_req.OPT_length = 0;
2253 		conn_req.OPT_offset = 0;
2254 		soappendmsg(mp, &conn_req, sizeof (conn_req));
2255 		soappendmsg(mp, addr, addrlen);
2256 	} else {
2257 		/*
2258 		 * There is a AF_UNIX sockaddr_un to include as a source
2259 		 * address option.
2260 		 */
2261 		struct T_opthdr toh;
2262 
2263 		toh.level = SOL_SOCKET;
2264 		toh.name = SO_SRCADDR;
2265 		toh.len = (t_uscalar_t)(srclen + sizeof (struct T_opthdr));
2266 		toh.status = 0;
2267 		conn_req.OPT_length =
2268 		    (t_scalar_t)(sizeof (toh) + _TPI_ALIGN_TOPT(srclen));
2269 		conn_req.OPT_offset = (t_scalar_t)(sizeof (conn_req) +
2270 		    _TPI_ALIGN_TOPT(addrlen));
2271 
2272 		soappendmsg(mp, &conn_req, sizeof (conn_req));
2273 		soappendmsg(mp, addr, addrlen);
2274 		mp->b_wptr += _TPI_ALIGN_TOPT(addrlen) - addrlen;
2275 		soappendmsg(mp, &toh, sizeof (toh));
2276 		soappendmsg(mp, src, srclen);
2277 		mp->b_wptr += _TPI_ALIGN_TOPT(srclen) - srclen;
2278 		ASSERT(mp->b_wptr <= mp->b_datap->db_lim);
2279 	}
2280 	/*
2281 	 * Set SS_ISCONNECTING before sending down the T_CONN_REQ
2282 	 * in order to have the right state when the T_CONN_CON shows up.
2283 	 */
2284 	soisconnecting(so);
2285 	mutex_exit(&so->so_lock);
2286 
2287 #ifdef C2_AUDIT
2288 	if (audit_active)
2289 		audit_sock(T_CONN_REQ, strvp2wq(SOTOV(so)), mp, 0);
2290 #endif /* C2_AUDIT */
2291 
2292 	error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0,
2293 	    MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0);
2294 	mp = NULL;
2295 	mutex_enter(&so->so_lock);
2296 	if (error != 0)
2297 		goto bad;
2298 
2299 	if ((error = sowaitokack(so, T_CONN_REQ)) != 0)
2300 		goto bad;
2301 
2302 	/* Allow other threads to access the socket */
2303 	so_unlock_single(so, SOLOCKED);
2304 	need_unlock = B_FALSE;
2305 
2306 	/*
2307 	 * Wait until we get a T_CONN_CON or an error
2308 	 */
2309 	if ((error = sowaitconnected(so, fflag, 0)) != 0) {
2310 		so_lock_single(so);	/* Set SOLOCKED */
2311 		need_unlock = B_TRUE;
2312 	}
2313 
2314 done:
2315 	freemsg(mp);
2316 	switch (error) {
2317 	case EINPROGRESS:
2318 	case EALREADY:
2319 	case EISCONN:
2320 	case EINTR:
2321 		/* Non-fatal errors */
2322 		so->so_state &= ~SS_LADDR_VALID;
2323 		/* FALLTHRU */
2324 	case 0:
2325 		break;
2326 
2327 	case EHOSTUNREACH:
2328 		if (flags & _SOCONNECT_XPG4_2) {
2329 			/*
2330 			 * X/Open specification contains a requirement that
2331 			 * ENETUNREACH be returned but does not require
2332 			 * EHOSTUNREACH. In order to keep the test suite
2333 			 * happy we mess with the errno here.
2334 			 */
2335 			error = ENETUNREACH;
2336 		}
2337 		/* FALLTHRU */
2338 
2339 	default:
2340 		ASSERT(need_unlock);
2341 		/*
2342 		 * Fatal errors: clear SS_ISCONNECTING in case it was set,
2343 		 * and invalidate local-address cache
2344 		 */
2345 		so->so_state &= ~(SS_ISCONNECTING | SS_LADDR_VALID);
2346 		/* A discon_ind might have already unbound us */
2347 		if ((flags & _SOCONNECT_DID_BIND) &&
2348 		    (so->so_state & SS_ISBOUND)) {
2349 			int err;
2350 
2351 			err = sotpi_unbind(so, 0);
2352 			/* LINTED - statement has no conseq */
2353 			if (err) {
2354 				eprintsoline(so, err);
2355 			}
2356 		}
2357 		break;
2358 	}
2359 	if (need_unlock)
2360 		so_unlock_single(so, SOLOCKED);
2361 	mutex_exit(&so->so_lock);
2362 	return (error);
2363 
2364 so_bad:	error = sogeterr(so);
2365 bad:	eprintsoline(so, error);
2366 	goto done;
2367 }
2368 
2369 int
2370 sotpi_shutdown(struct sonode *so, int how)
2371 {
2372 	struct T_ordrel_req	ordrel_req;
2373 	mblk_t			*mp;
2374 	uint_t			old_state, state_change;
2375 	int			error = 0;
2376 
2377 	dprintso(so, 1, ("sotpi_shutdown(%p, %d) %s\n",
2378 	    so, how, pr_state(so->so_state, so->so_mode)));
2379 
2380 	mutex_enter(&so->so_lock);
2381 	so_lock_single(so);	/* Set SOLOCKED */
2382 
2383 	/*
2384 	 * SunOS 4.X has no check for datagram sockets.
2385 	 * 5.X checks that it is connected (ENOTCONN)
2386 	 * X/Open requires that we check the connected state.
2387 	 */
2388 	if (!(so->so_state & SS_ISCONNECTED)) {
2389 		if (!xnet_skip_checks) {
2390 			error = ENOTCONN;
2391 			if (xnet_check_print) {
2392 				printf("sockfs: X/Open shutdown check "
2393 				    "caused ENOTCONN\n");
2394 			}
2395 		}
2396 		goto done;
2397 	}
2398 	/*
2399 	 * Record the current state and then perform any state changes.
2400 	 * Then use the difference between the old and new states to
2401 	 * determine which messages need to be sent.
2402 	 * This prevents e.g. duplicate T_ORDREL_REQ when there are
2403 	 * duplicate calls to shutdown().
2404 	 */
2405 	old_state = so->so_state;
2406 
2407 	switch (how) {
2408 	case 0:
2409 		socantrcvmore(so);
2410 		break;
2411 	case 1:
2412 		socantsendmore(so);
2413 		break;
2414 	case 2:
2415 		socantsendmore(so);
2416 		socantrcvmore(so);
2417 		break;
2418 	default:
2419 		error = EINVAL;
2420 		goto done;
2421 	}
2422 
2423 	/*
2424 	 * Assumes that the SS_CANT* flags are never cleared in the above code.
2425 	 */
2426 	state_change = (so->so_state & (SS_CANTRCVMORE|SS_CANTSENDMORE)) -
2427 	    (old_state & (SS_CANTRCVMORE|SS_CANTSENDMORE));
2428 	ASSERT((state_change & ~(SS_CANTRCVMORE|SS_CANTSENDMORE)) == 0);
2429 
2430 	switch (state_change) {
2431 	case 0:
2432 		dprintso(so, 1,
2433 		    ("sotpi_shutdown: nothing to send in state 0x%x\n",
2434 		    so->so_state));
2435 		goto done;
2436 
2437 	case SS_CANTRCVMORE:
2438 		mutex_exit(&so->so_lock);
2439 		strseteof(SOTOV(so), 1);
2440 		/*
2441 		 * strseteof takes care of read side wakeups,
2442 		 * pollwakeups, and signals.
2443 		 */
2444 		/*
2445 		 * Get the read lock before flushing data to avoid problems
2446 		 * with the T_EXDATA_IND MSG_PEEK code in sotpi_recvmsg.
2447 		 */
2448 		mutex_enter(&so->so_lock);
2449 		(void) so_lock_read(so, 0);	/* Set SOREADLOCKED */
2450 		mutex_exit(&so->so_lock);
2451 
2452 		/* Flush read side queue */
2453 		strflushrq(SOTOV(so), FLUSHALL);
2454 
2455 		mutex_enter(&so->so_lock);
2456 		so_unlock_read(so);		/* Clear SOREADLOCKED */
2457 		break;
2458 
2459 	case SS_CANTSENDMORE:
2460 		mutex_exit(&so->so_lock);
2461 		strsetwerror(SOTOV(so), 0, 0, sogetwrerr);
2462 		mutex_enter(&so->so_lock);
2463 		break;
2464 
2465 	case SS_CANTSENDMORE|SS_CANTRCVMORE:
2466 		mutex_exit(&so->so_lock);
2467 		strsetwerror(SOTOV(so), 0, 0, sogetwrerr);
2468 		strseteof(SOTOV(so), 1);
2469 		/*
2470 		 * strseteof takes care of read side wakeups,
2471 		 * pollwakeups, and signals.
2472 		 */
2473 		/*
2474 		 * Get the read lock before flushing data to avoid problems
2475 		 * with the T_EXDATA_IND MSG_PEEK code in sotpi_recvmsg.
2476 		 */
2477 		mutex_enter(&so->so_lock);
2478 		(void) so_lock_read(so, 0);	/* Set SOREADLOCKED */
2479 		mutex_exit(&so->so_lock);
2480 
2481 		/* Flush read side queue */
2482 		strflushrq(SOTOV(so), FLUSHALL);
2483 
2484 		mutex_enter(&so->so_lock);
2485 		so_unlock_read(so);		/* Clear SOREADLOCKED */
2486 		break;
2487 	}
2488 
2489 	ASSERT(MUTEX_HELD(&so->so_lock));
2490 
2491 	/*
2492 	 * If either SS_CANTSENDMORE or SS_CANTRCVMORE or both of them
2493 	 * was set due to this call and the new state has both of them set:
2494 	 *	Send the AF_UNIX close indication
2495 	 *	For T_COTS send a discon_ind
2496 	 *
2497 	 * If cantsend was set due to this call:
2498 	 *	For T_COTSORD send an ordrel_ind
2499 	 *
2500 	 * Note that for T_CLTS there is no message sent here.
2501 	 */
2502 	if ((so->so_state & (SS_CANTRCVMORE|SS_CANTSENDMORE)) ==
2503 	    (SS_CANTRCVMORE|SS_CANTSENDMORE)) {
2504 		/*
2505 		 * For SunOS 4.X compatibility we tell the other end
2506 		 * that we are unable to receive at this point.
2507 		 */
2508 		if (so->so_family == AF_UNIX && so->so_serv_type != T_CLTS)
2509 			so_unix_close(so);
2510 
2511 		if (so->so_serv_type == T_COTS)
2512 			error = sodisconnect(so, -1, _SODISCONNECT_LOCK_HELD);
2513 	}
2514 	if ((state_change & SS_CANTSENDMORE) &&
2515 	    (so->so_serv_type == T_COTS_ORD)) {
2516 		/* Send an orderly release */
2517 		ordrel_req.PRIM_type = T_ORDREL_REQ;
2518 
2519 		mutex_exit(&so->so_lock);
2520 		mp = soallocproto1(&ordrel_req, sizeof (ordrel_req),
2521 		    0, _ALLOC_SLEEP);
2522 		/*
2523 		 * Send down the T_ORDREL_REQ even if there is flow control.
2524 		 * This prevents shutdown from blocking.
2525 		 * Note that there is no T_OK_ACK for ordrel_req.
2526 		 */
2527 		error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0,
2528 		    MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR|MSG_IGNFLOW, 0);
2529 		mutex_enter(&so->so_lock);
2530 		if (error) {
2531 			eprintsoline(so, error);
2532 			goto done;
2533 		}
2534 	}
2535 
2536 done:
2537 	so_unlock_single(so, SOLOCKED);
2538 	mutex_exit(&so->so_lock);
2539 	return (error);
2540 }
2541 
2542 /*
2543  * For any connected SOCK_STREAM/SOCK_SEQPACKET AF_UNIX socket we send
2544  * a zero-length T_OPTDATA_REQ with the SO_UNIX_CLOSE option to inform the peer
2545  * that we have closed.
2546  * Also, for connected AF_UNIX SOCK_DGRAM sockets we send a zero-length
2547  * T_UNITDATA_REQ containing the same option.
2548  *
2549  * For SOCK_DGRAM half-connections (somebody connected to this end
2550  * but this end is not connect) we don't know where to send any
2551  * SO_UNIX_CLOSE.
2552  *
2553  * We have to ignore stream head errors just in case there has been
2554  * a shutdown(output).
2555  * Ignore any flow control to try to get the message more quickly to the peer.
2556  * While locally ignoring flow control solves the problem when there
2557  * is only the loopback transport on the stream it would not provide
2558  * the correct AF_UNIX socket semantics when one or more modules have
2559  * been pushed.
2560  */
2561 void
2562 so_unix_close(struct sonode *so)
2563 {
2564 	int		error;
2565 	struct T_opthdr	toh;
2566 	mblk_t		*mp;
2567 
2568 	ASSERT(MUTEX_HELD(&so->so_lock));
2569 
2570 	ASSERT(so->so_family == AF_UNIX);
2571 
2572 	if ((so->so_state & (SS_ISCONNECTED|SS_ISBOUND)) !=
2573 	    (SS_ISCONNECTED|SS_ISBOUND))
2574 		return;
2575 
2576 	dprintso(so, 1, ("so_unix_close(%p) %s\n",
2577 	    so, pr_state(so->so_state, so->so_mode)));
2578 
2579 	toh.level = SOL_SOCKET;
2580 	toh.name = SO_UNIX_CLOSE;
2581 
2582 	/* zero length + header */
2583 	toh.len = (t_uscalar_t)sizeof (struct T_opthdr);
2584 	toh.status = 0;
2585 
2586 	if (so->so_type == SOCK_STREAM || so->so_type == SOCK_SEQPACKET) {
2587 		struct T_optdata_req tdr;
2588 
2589 		tdr.PRIM_type = T_OPTDATA_REQ;
2590 		tdr.DATA_flag = 0;
2591 
2592 		tdr.OPT_length = (t_scalar_t)sizeof (toh);
2593 		tdr.OPT_offset = (t_scalar_t)sizeof (tdr);
2594 
2595 		/* NOTE: holding so_lock while sleeping */
2596 		mp = soallocproto2(&tdr, sizeof (tdr),
2597 		    &toh, sizeof (toh), 0, _ALLOC_SLEEP);
2598 	} else {
2599 		struct T_unitdata_req	tudr;
2600 		void			*addr;
2601 		socklen_t		addrlen;
2602 		void			*src;
2603 		socklen_t		srclen;
2604 		struct T_opthdr		toh2;
2605 		t_scalar_t		size;
2606 
2607 		/* Connecteded DGRAM socket */
2608 
2609 		/*
2610 		 * For AF_UNIX the destination address is translated to
2611 		 * an internal name and the source address is passed as
2612 		 * an option.
2613 		 */
2614 		/*
2615 		 * Length and family checks.
2616 		 */
2617 		error = so_addr_verify(so, so->so_faddr_sa,
2618 		    (t_uscalar_t)so->so_faddr_len);
2619 		if (error) {
2620 			eprintsoline(so, error);
2621 			return;
2622 		}
2623 		if (so->so_state & SS_FADDR_NOXLATE) {
2624 			/*
2625 			 * Already have a transport internal address. Do not
2626 			 * pass any (transport internal) source address.
2627 			 */
2628 			addr = so->so_faddr_sa;
2629 			addrlen = (t_uscalar_t)so->so_faddr_len;
2630 			src = NULL;
2631 			srclen = 0;
2632 		} else {
2633 			/*
2634 			 * Pass the sockaddr_un source address as an option
2635 			 * and translate the remote address.
2636 			 * Holding so_lock thus so_laddr_sa can not change.
2637 			 */
2638 			src = so->so_laddr_sa;
2639 			srclen = (socklen_t)so->so_laddr_len;
2640 			dprintso(so, 1,
2641 			    ("so_ux_close: srclen %d, src %p\n",
2642 			    srclen, src));
2643 			error = so_ux_addr_xlate(so,
2644 			    so->so_faddr_sa,
2645 			    (socklen_t)so->so_faddr_len, 0,
2646 			    &addr, &addrlen);
2647 			if (error) {
2648 				eprintsoline(so, error);
2649 				return;
2650 			}
2651 		}
2652 		tudr.PRIM_type = T_UNITDATA_REQ;
2653 		tudr.DEST_length = addrlen;
2654 		tudr.DEST_offset = (t_scalar_t)sizeof (tudr);
2655 		if (srclen == 0) {
2656 			tudr.OPT_length = (t_scalar_t)sizeof (toh);
2657 			tudr.OPT_offset = (t_scalar_t)(sizeof (tudr) +
2658 			    _TPI_ALIGN_TOPT(addrlen));
2659 
2660 			size = tudr.OPT_offset + tudr.OPT_length;
2661 			/* NOTE: holding so_lock while sleeping */
2662 			mp = soallocproto2(&tudr, sizeof (tudr),
2663 			    addr, addrlen, size, _ALLOC_SLEEP);
2664 			mp->b_wptr += (_TPI_ALIGN_TOPT(addrlen) - addrlen);
2665 			soappendmsg(mp, &toh, sizeof (toh));
2666 		} else {
2667 			/*
2668 			 * There is a AF_UNIX sockaddr_un to include as a
2669 			 * source address option.
2670 			 */
2671 			tudr.OPT_length = (t_scalar_t)(2 * sizeof (toh) +
2672 			    _TPI_ALIGN_TOPT(srclen));
2673 			tudr.OPT_offset = (t_scalar_t)(sizeof (tudr) +
2674 			    _TPI_ALIGN_TOPT(addrlen));
2675 
2676 			toh2.level = SOL_SOCKET;
2677 			toh2.name = SO_SRCADDR;
2678 			toh2.len = (t_uscalar_t)(srclen +
2679 			    sizeof (struct T_opthdr));
2680 			toh2.status = 0;
2681 
2682 			size = tudr.OPT_offset + tudr.OPT_length;
2683 
2684 			/* NOTE: holding so_lock while sleeping */
2685 			mp = soallocproto2(&tudr, sizeof (tudr),
2686 			    addr, addrlen, size, _ALLOC_SLEEP);
2687 			mp->b_wptr += _TPI_ALIGN_TOPT(addrlen) - addrlen;
2688 			soappendmsg(mp, &toh, sizeof (toh));
2689 			soappendmsg(mp, &toh2, sizeof (toh2));
2690 			soappendmsg(mp, src, srclen);
2691 			mp->b_wptr += _TPI_ALIGN_TOPT(srclen) - srclen;
2692 		}
2693 		ASSERT(mp->b_wptr <= mp->b_datap->db_lim);
2694 	}
2695 	mutex_exit(&so->so_lock);
2696 	error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0,
2697 	    MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR|MSG_IGNFLOW, 0);
2698 	mutex_enter(&so->so_lock);
2699 }
2700 
2701 /*
2702  * Handle recv* calls that set MSG_OOB or MSG_OOB together with MSG_PEEK.
2703  */
2704 int
2705 sorecvoob(struct sonode *so, struct nmsghdr *msg, struct uio *uiop, int flags)
2706 {
2707 	mblk_t		*mp, *nmp;
2708 	int		error;
2709 
2710 	dprintso(so, 1, ("sorecvoob(%p, %p, 0x%x)\n", so, msg, flags));
2711 
2712 	/*
2713 	 * There is never any oob data with addresses or control since
2714 	 * the T_EXDATA_IND does not carry any options.
2715 	 */
2716 	msg->msg_controllen = 0;
2717 	msg->msg_namelen = 0;
2718 
2719 	mutex_enter(&so->so_lock);
2720 	ASSERT(so_verify_oobstate(so));
2721 	if ((so->so_options & SO_OOBINLINE) ||
2722 	    (so->so_state & (SS_OOBPEND|SS_HADOOBDATA)) != SS_OOBPEND) {
2723 		dprintso(so, 1, ("sorecvoob: inline or data consumed\n"));
2724 		mutex_exit(&so->so_lock);
2725 		return (EINVAL);
2726 	}
2727 	if (!(so->so_state & SS_HAVEOOBDATA)) {
2728 		dprintso(so, 1, ("sorecvoob: no data yet\n"));
2729 		mutex_exit(&so->so_lock);
2730 		return (EWOULDBLOCK);
2731 	}
2732 	ASSERT(so->so_oobmsg != NULL);
2733 	mp = so->so_oobmsg;
2734 	if (flags & MSG_PEEK) {
2735 		/*
2736 		 * Since recv* can not return ENOBUFS we can not use dupmsg.
2737 		 * Instead we revert to the consolidation private
2738 		 * allocb_wait plus bcopy.
2739 		 */
2740 		mblk_t *mp1;
2741 
2742 		mp1 = allocb_wait(msgdsize(mp), BPRI_MED, STR_NOSIG, NULL);
2743 		ASSERT(mp1);
2744 
2745 		while (mp != NULL) {
2746 			ssize_t size;
2747 
2748 			size = MBLKL(mp);
2749 			bcopy(mp->b_rptr, mp1->b_wptr, size);
2750 			mp1->b_wptr += size;
2751 			ASSERT(mp1->b_wptr <= mp1->b_datap->db_lim);
2752 			mp = mp->b_cont;
2753 		}
2754 		mp = mp1;
2755 	} else {
2756 		/*
2757 		 * Update the state indicating that the data has been consumed.
2758 		 * Keep SS_OOBPEND set until data is consumed past the mark.
2759 		 */
2760 		so->so_oobmsg = NULL;
2761 		so->so_state ^= SS_HAVEOOBDATA|SS_HADOOBDATA;
2762 	}
2763 	dprintso(so, 1,
2764 	    ("after recvoob(%p): counts %d/%d state %s\n",
2765 	    so, so->so_oobsigcnt,
2766 	    so->so_oobcnt, pr_state(so->so_state, so->so_mode)));
2767 	ASSERT(so_verify_oobstate(so));
2768 	mutex_exit(&so->so_lock);
2769 
2770 	error = 0;
2771 	nmp = mp;
2772 	while (nmp != NULL && uiop->uio_resid > 0) {
2773 		ssize_t n = MBLKL(nmp);
2774 
2775 		n = MIN(n, uiop->uio_resid);
2776 		if (n > 0)
2777 			error = uiomove(nmp->b_rptr, n,
2778 			    UIO_READ, uiop);
2779 		if (error)
2780 			break;
2781 		nmp = nmp->b_cont;
2782 	}
2783 	freemsg(mp);
2784 	return (error);
2785 }
2786 
2787 /*
2788  * Called by sotpi_recvmsg when reading a non-zero amount of data.
2789  * In addition, the caller typically verifies that there is some
2790  * potential state to clear by checking
2791  *	if (so->so_state & (SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK))
2792  * before calling this routine.
2793  * Note that such a check can be made without holding so_lock since
2794  * sotpi_recvmsg is single-threaded (using SOREADLOCKED) and only sotpi_recvmsg
2795  * decrements so_oobsigcnt.
2796  *
2797  * When data is read *after* the point that all pending
2798  * oob data has been consumed the oob indication is cleared.
2799  *
2800  * This logic keeps select/poll returning POLLRDBAND and
2801  * SIOCATMARK returning true until we have read past
2802  * the mark.
2803  */
2804 static void
2805 sorecv_update_oobstate(struct sonode *so)
2806 {
2807 	mutex_enter(&so->so_lock);
2808 	ASSERT(so_verify_oobstate(so));
2809 	dprintso(so, 1,
2810 	    ("sorecv_update_oobstate: counts %d/%d state %s\n",
2811 	    so->so_oobsigcnt,
2812 	    so->so_oobcnt, pr_state(so->so_state, so->so_mode)));
2813 	if (so->so_oobsigcnt == 0) {
2814 		/* No more pending oob indications */
2815 		so->so_state &= ~(SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK);
2816 		freemsg(so->so_oobmsg);
2817 		so->so_oobmsg = NULL;
2818 	}
2819 	ASSERT(so_verify_oobstate(so));
2820 	mutex_exit(&so->so_lock);
2821 }
2822 
2823 /*
2824  * Handle recv* calls for an so which has NL7C saved recv mblk_t(s).
2825  */
2826 static int
2827 nl7c_sorecv(struct sonode *so, mblk_t **rmp, uio_t *uiop, rval_t *rp)
2828 {
2829 	int	error = 0;
2830 	mblk_t *tmp = NULL;
2831 	mblk_t *pmp = NULL;
2832 	mblk_t *nmp = so->so_nl7c_rcv_mp;
2833 
2834 	ASSERT(nmp != NULL);
2835 
2836 	while (nmp != NULL && uiop->uio_resid > 0) {
2837 		ssize_t n;
2838 
2839 		if (DB_TYPE(nmp) == M_DATA) {
2840 			/*
2841 			 * We have some data, uiomove up to resid bytes.
2842 			 */
2843 			n = MIN(MBLKL(nmp), uiop->uio_resid);
2844 			if (n > 0)
2845 				error = uiomove(nmp->b_rptr, n, UIO_READ, uiop);
2846 			nmp->b_rptr += n;
2847 			if (nmp->b_rptr == nmp->b_wptr) {
2848 				pmp = nmp;
2849 				nmp = nmp->b_cont;
2850 			}
2851 			if (error)
2852 				break;
2853 		} else {
2854 			/*
2855 			 * We only handle data, save for caller to handle.
2856 			 */
2857 			if (pmp != NULL) {
2858 				pmp->b_cont = nmp->b_cont;
2859 			}
2860 			nmp->b_cont = NULL;
2861 			if (*rmp == NULL) {
2862 				*rmp = nmp;
2863 			} else {
2864 				tmp->b_cont = nmp;
2865 			}
2866 			nmp = nmp->b_cont;
2867 			tmp = nmp;
2868 		}
2869 	}
2870 	if (pmp != NULL) {
2871 		/* Free any mblk_t(s) which we have consumed */
2872 		pmp->b_cont = NULL;
2873 		freemsg(so->so_nl7c_rcv_mp);
2874 	}
2875 	if ((so->so_nl7c_rcv_mp = nmp) == NULL) {
2876 		/* Last mblk_t so return the saved kstrgetmsg() rval/error */
2877 		if (error == 0) {
2878 			rval_t	*p = (rval_t *)&so->so_nl7c_rcv_rval;
2879 
2880 			error = p->r_v.r_v2;
2881 			p->r_v.r_v2 = 0;
2882 		}
2883 		rp->r_vals = so->so_nl7c_rcv_rval;
2884 		so->so_nl7c_rcv_rval = 0;
2885 	} else {
2886 		/* More mblk_t(s) to process so no rval to return */
2887 		rp->r_vals = 0;
2888 	}
2889 	return (error);
2890 }
2891 
2892 /*
2893  * Receive the next message on the queue.
2894  * If msg_controllen is non-zero when called the caller is interested in
2895  * any received control info (options).
2896  * If msg_namelen is non-zero when called the caller is interested in
2897  * any received source address.
2898  * The routine returns with msg_control and msg_name pointing to
2899  * kmem_alloc'ed memory which the caller has to free.
2900  */
2901 int
2902 sotpi_recvmsg(struct sonode *so, struct nmsghdr *msg, struct uio *uiop)
2903 {
2904 	union T_primitives	*tpr;
2905 	mblk_t			*mp;
2906 	uchar_t			pri;
2907 	int			pflag, opflag;
2908 	void			*control;
2909 	t_uscalar_t		controllen;
2910 	t_uscalar_t		namelen;
2911 	int			so_state = so->so_state; /* Snapshot */
2912 	ssize_t			saved_resid;
2913 	int			error;
2914 	rval_t			rval;
2915 	int			flags;
2916 	clock_t			timout;
2917 	int			first;
2918 
2919 	flags = msg->msg_flags;
2920 	msg->msg_flags = 0;
2921 
2922 	dprintso(so, 1, ("sotpi_recvmsg(%p, %p, 0x%x) state %s err %d\n",
2923 	    so, msg, flags,
2924 	    pr_state(so->so_state, so->so_mode), so->so_error));
2925 
2926 	/*
2927 	 * If we are not connected because we have never been connected
2928 	 * we return ENOTCONN. If we have been connected (but are no longer
2929 	 * connected) then SS_CANTRCVMORE is set and we let kstrgetmsg return
2930 	 * the EOF.
2931 	 *
2932 	 * An alternative would be to post an ENOTCONN error in stream head
2933 	 * (read+write) and clear it when we're connected. However, that error
2934 	 * would cause incorrect poll/select behavior!
2935 	 */
2936 	if ((so_state & (SS_ISCONNECTED|SS_CANTRCVMORE)) == 0 &&
2937 	    (so->so_mode & SM_CONNREQUIRED)) {
2938 		return (ENOTCONN);
2939 	}
2940 
2941 	/*
2942 	 * Note: SunOS 4.X checks uio_resid == 0 before going to sleep (but
2943 	 * after checking that the read queue is empty) and returns zero.
2944 	 * This implementation will sleep (in kstrgetmsg) even if uio_resid
2945 	 * is zero.
2946 	 */
2947 
2948 	if (flags & MSG_OOB) {
2949 		/* Check that the transport supports OOB */
2950 		if (!(so->so_mode & SM_EXDATA))
2951 			return (EOPNOTSUPP);
2952 		return (sorecvoob(so, msg, uiop, flags));
2953 	}
2954 
2955 	/*
2956 	 * Set msg_controllen and msg_namelen to zero here to make it
2957 	 * simpler in the cases that no control or name is returned.
2958 	 */
2959 	controllen = msg->msg_controllen;
2960 	namelen = msg->msg_namelen;
2961 	msg->msg_controllen = 0;
2962 	msg->msg_namelen = 0;
2963 
2964 	dprintso(so, 1, ("sotpi_recvmsg: namelen %d controllen %d\n",
2965 	    namelen, controllen));
2966 
2967 	mutex_enter(&so->so_lock);
2968 	/*
2969 	 * If an NL7C enabled socket and not waiting for write data.
2970 	 */
2971 	if ((so->so_nl7c_flags & (NL7C_ENABLED | NL7C_WAITWRITE)) ==
2972 	    NL7C_ENABLED) {
2973 		if (so->so_nl7c_uri) {
2974 			/* Close uri processing for a previous request */
2975 			nl7c_close(so);
2976 		}
2977 		if ((so_state & SS_CANTRCVMORE) && so->so_nl7c_rcv_mp == NULL) {
2978 			/* Nothing to process, EOF */
2979 			mutex_exit(&so->so_lock);
2980 			return (0);
2981 		} else if (so->so_nl7c_flags & NL7C_SOPERSIST) {
2982 			/* Persistent NL7C socket, try to process request */
2983 			boolean_t ret;
2984 
2985 			ret = nl7c_process(so,
2986 			    (so->so_state & (SS_NONBLOCK|SS_NDELAY)));
2987 			rval.r_vals = so->so_nl7c_rcv_rval;
2988 			error = rval.r_v.r_v2;
2989 			if (error) {
2990 				/* Error of some sort, return it */
2991 				mutex_exit(&so->so_lock);
2992 				return (error);
2993 			}
2994 			if (so->so_nl7c_flags &&
2995 			    ! (so->so_nl7c_flags & NL7C_WAITWRITE)) {
2996 				/*
2997 				 * Still an NL7C socket and no data
2998 				 * to pass up to the caller.
2999 				 */
3000 				mutex_exit(&so->so_lock);
3001 				if (ret) {
3002 					/* EOF */
3003 					return (0);
3004 				} else {
3005 					/* Need more data */
3006 					return (EAGAIN);
3007 				}
3008 			}
3009 		} else {
3010 			/*
3011 			 * Not persistent so no further NL7C processing.
3012 			 */
3013 			so->so_nl7c_flags = 0;
3014 		}
3015 	}
3016 	/*
3017 	 * Only one reader is allowed at any given time. This is needed
3018 	 * for T_EXDATA handling and, in the future, MSG_WAITALL.
3019 	 *
3020 	 * This is slightly different that BSD behavior in that it fails with
3021 	 * EWOULDBLOCK when using nonblocking io. In BSD the read queue access
3022 	 * is single-threaded using sblock(), which is dropped while waiting
3023 	 * for data to appear. The difference shows up e.g. if one
3024 	 * file descriptor does not have O_NONBLOCK but a dup'ed file descriptor
3025 	 * does use nonblocking io and different threads are reading each
3026 	 * file descriptor. In BSD there would never be an EWOULDBLOCK error
3027 	 * in this case as long as the read queue doesn't get empty.
3028 	 * In this implementation the thread using nonblocking io can
3029 	 * get an EWOULDBLOCK error due to the blocking thread executing
3030 	 * e.g. in the uiomove in kstrgetmsg.
3031 	 * This difference is not believed to be significant.
3032 	 */
3033 	/* Set SOREADLOCKED */
3034 	error = so_lock_read_intr(so,
3035 	    uiop->uio_fmode | ((flags & MSG_DONTWAIT) ? FNONBLOCK : 0));
3036 	mutex_exit(&so->so_lock);
3037 	if (error)
3038 		return (error);
3039 
3040 	/*
3041 	 * Tell kstrgetmsg to not inspect the stream head errors until all
3042 	 * queued data has been consumed.
3043 	 * Use a timeout=-1 to wait forever unless MSG_DONTWAIT is set.
3044 	 * Also, If uio_fmode indicates nonblocking kstrgetmsg will not block.
3045 	 *
3046 	 * MSG_WAITALL only applies to M_DATA and T_DATA_IND messages and
3047 	 * to T_OPTDATA_IND that do not contain any user-visible control msg.
3048 	 * Note that MSG_WAITALL set with MSG_PEEK is a noop.
3049 	 */
3050 	pflag = MSG_ANY | MSG_DELAYERROR;
3051 	if (flags & MSG_PEEK) {
3052 		pflag |= MSG_IPEEK;
3053 		flags &= ~MSG_WAITALL;
3054 	}
3055 	if (so->so_mode & SM_ATOMIC)
3056 		pflag |= MSG_DISCARDTAIL;
3057 
3058 	if (flags & MSG_DONTWAIT)
3059 		timout = 0;
3060 	else
3061 		timout = -1;
3062 	opflag = pflag;
3063 	first = 1;
3064 
3065 retry:
3066 	saved_resid = uiop->uio_resid;
3067 	pri = 0;
3068 	mp = NULL;
3069 	if (so->so_nl7c_rcv_mp != NULL) {
3070 		/* Already kstrgetmsg()ed saved mblk(s) from NL7C */
3071 		error = nl7c_sorecv(so, &mp, uiop, &rval);
3072 	} else {
3073 		error = kstrgetmsg(SOTOV(so), &mp, uiop, &pri, &pflag,
3074 		    timout, &rval);
3075 	}
3076 	if (error) {
3077 		switch (error) {
3078 		case EINTR:
3079 		case EWOULDBLOCK:
3080 			if (!first)
3081 				error = 0;
3082 			break;
3083 		case ETIME:
3084 			/* Returned from kstrgetmsg when timeout expires */
3085 			if (!first)
3086 				error = 0;
3087 			else
3088 				error = EWOULDBLOCK;
3089 			break;
3090 		default:
3091 			eprintsoline(so, error);
3092 			break;
3093 		}
3094 		mutex_enter(&so->so_lock);
3095 		so_unlock_read(so);	/* Clear SOREADLOCKED */
3096 		mutex_exit(&so->so_lock);
3097 		return (error);
3098 	}
3099 	/*
3100 	 * For datagrams the MOREDATA flag is used to set MSG_TRUNC.
3101 	 * For non-datagrams MOREDATA is used to set MSG_EOR.
3102 	 */
3103 	ASSERT(!(rval.r_val1 & MORECTL));
3104 	if ((rval.r_val1 & MOREDATA) && (so->so_mode & SM_ATOMIC))
3105 		msg->msg_flags |= MSG_TRUNC;
3106 
3107 	if (mp == NULL) {
3108 		dprintso(so, 1, ("sotpi_recvmsg: got M_DATA\n"));
3109 		/*
3110 		 * 4.3BSD and 4.4BSD clears the mark when peeking across it.
3111 		 * The draft Posix socket spec states that the mark should
3112 		 * not be cleared when peeking. We follow the latter.
3113 		 */
3114 		if ((so->so_state &
3115 		    (SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK)) &&
3116 		    (uiop->uio_resid != saved_resid) &&
3117 		    !(flags & MSG_PEEK)) {
3118 			sorecv_update_oobstate(so);
3119 		}
3120 
3121 		mutex_enter(&so->so_lock);
3122 		/* Set MSG_EOR based on MOREDATA */
3123 		if (!(rval.r_val1 & MOREDATA)) {
3124 			if (so->so_state & SS_SAVEDEOR) {
3125 				msg->msg_flags |= MSG_EOR;
3126 				so->so_state &= ~SS_SAVEDEOR;
3127 			}
3128 		}
3129 		/*
3130 		 * If some data was received (i.e. not EOF) and the
3131 		 * read/recv* has not been satisfied wait for some more.
3132 		 */
3133 		if ((flags & MSG_WAITALL) && !(msg->msg_flags & MSG_EOR) &&
3134 		    uiop->uio_resid != saved_resid && uiop->uio_resid > 0) {
3135 			mutex_exit(&so->so_lock);
3136 			first = 0;
3137 			pflag = opflag | MSG_NOMARK;
3138 			goto retry;
3139 		}
3140 		so_unlock_read(so);	/* Clear SOREADLOCKED */
3141 		mutex_exit(&so->so_lock);
3142 		return (0);
3143 	}
3144 
3145 	/* strsock_proto has already verified length and alignment */
3146 	tpr = (union T_primitives *)mp->b_rptr;
3147 	dprintso(so, 1, ("sotpi_recvmsg: type %d\n", tpr->type));
3148 
3149 	switch (tpr->type) {
3150 	case T_DATA_IND: {
3151 		if ((so->so_state &
3152 		    (SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK)) &&
3153 		    (uiop->uio_resid != saved_resid) &&
3154 		    !(flags & MSG_PEEK)) {
3155 			sorecv_update_oobstate(so);
3156 		}
3157 
3158 		/*
3159 		 * Set msg_flags to MSG_EOR based on
3160 		 * MORE_flag and MOREDATA.
3161 		 */
3162 		mutex_enter(&so->so_lock);
3163 		so->so_state &= ~SS_SAVEDEOR;
3164 		if (!(tpr->data_ind.MORE_flag & 1)) {
3165 			if (!(rval.r_val1 & MOREDATA))
3166 				msg->msg_flags |= MSG_EOR;
3167 			else
3168 				so->so_state |= SS_SAVEDEOR;
3169 		}
3170 		freemsg(mp);
3171 		/*
3172 		 * If some data was received (i.e. not EOF) and the
3173 		 * read/recv* has not been satisfied wait for some more.
3174 		 */
3175 		if ((flags & MSG_WAITALL) && !(msg->msg_flags & MSG_EOR) &&
3176 		    uiop->uio_resid != saved_resid && uiop->uio_resid > 0) {
3177 			mutex_exit(&so->so_lock);
3178 			first = 0;
3179 			pflag = opflag | MSG_NOMARK;
3180 			goto retry;
3181 		}
3182 		so_unlock_read(so);	/* Clear SOREADLOCKED */
3183 		mutex_exit(&so->so_lock);
3184 		return (0);
3185 	}
3186 	case T_UNITDATA_IND: {
3187 		void *addr;
3188 		t_uscalar_t addrlen;
3189 		void *abuf;
3190 		t_uscalar_t optlen;
3191 		void *opt;
3192 
3193 		if ((so->so_state &
3194 		    (SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK)) &&
3195 		    (uiop->uio_resid != saved_resid) &&
3196 		    !(flags & MSG_PEEK)) {
3197 			sorecv_update_oobstate(so);
3198 		}
3199 
3200 		if (namelen != 0) {
3201 			/* Caller wants source address */
3202 			addrlen = tpr->unitdata_ind.SRC_length;
3203 			addr = sogetoff(mp,
3204 			    tpr->unitdata_ind.SRC_offset,
3205 			    addrlen, 1);
3206 			if (addr == NULL) {
3207 				freemsg(mp);
3208 				error = EPROTO;
3209 				eprintsoline(so, error);
3210 				goto err;
3211 			}
3212 			if (so->so_family == AF_UNIX) {
3213 				/*
3214 				 * Can not use the transport level address.
3215 				 * If there is a SO_SRCADDR option carrying
3216 				 * the socket level address it will be
3217 				 * extracted below.
3218 				 */
3219 				addr = NULL;
3220 				addrlen = 0;
3221 			}
3222 		}
3223 		optlen = tpr->unitdata_ind.OPT_length;
3224 		if (optlen != 0) {
3225 			t_uscalar_t ncontrollen;
3226 
3227 			/*
3228 			 * Extract any source address option.
3229 			 * Determine how large cmsg buffer is needed.
3230 			 */
3231 			opt = sogetoff(mp,
3232 			    tpr->unitdata_ind.OPT_offset,
3233 			    optlen, __TPI_ALIGN_SIZE);
3234 
3235 			if (opt == NULL) {
3236 				freemsg(mp);
3237 				error = EPROTO;
3238 				eprintsoline(so, error);
3239 				goto err;
3240 			}
3241 			if (so->so_family == AF_UNIX)
3242 				so_getopt_srcaddr(opt, optlen, &addr, &addrlen);
3243 			ncontrollen = so_cmsglen(mp, opt, optlen,
3244 			    !(flags & MSG_XPG4_2));
3245 			if (controllen != 0)
3246 				controllen = ncontrollen;
3247 			else if (ncontrollen != 0)
3248 				msg->msg_flags |= MSG_CTRUNC;
3249 		} else {
3250 			controllen = 0;
3251 		}
3252 
3253 		if (namelen != 0) {
3254 			/*
3255 			 * Return address to caller.
3256 			 * Caller handles truncation if length
3257 			 * exceeds msg_namelen.
3258 			 * NOTE: AF_UNIX NUL termination is ensured by
3259 			 * the sender's copyin_name().
3260 			 */
3261 			abuf = kmem_alloc(addrlen, KM_SLEEP);
3262 
3263 			bcopy(addr, abuf, addrlen);
3264 			msg->msg_name = abuf;
3265 			msg->msg_namelen = addrlen;
3266 		}
3267 
3268 		if (controllen != 0) {
3269 			/*
3270 			 * Return control msg to caller.
3271 			 * Caller handles truncation if length
3272 			 * exceeds msg_controllen.
3273 			 */
3274 			control = kmem_zalloc(controllen, KM_SLEEP);
3275 
3276 			error = so_opt2cmsg(mp, opt, optlen,
3277 			    !(flags & MSG_XPG4_2),
3278 			    control, controllen);
3279 			if (error) {
3280 				freemsg(mp);
3281 				if (msg->msg_namelen != 0)
3282 					kmem_free(msg->msg_name,
3283 					    msg->msg_namelen);
3284 				kmem_free(control, controllen);
3285 				eprintsoline(so, error);
3286 				goto err;
3287 			}
3288 			msg->msg_control = control;
3289 			msg->msg_controllen = controllen;
3290 		}
3291 
3292 		freemsg(mp);
3293 		mutex_enter(&so->so_lock);
3294 		so_unlock_read(so);	/* Clear SOREADLOCKED */
3295 		mutex_exit(&so->so_lock);
3296 		return (0);
3297 	}
3298 	case T_OPTDATA_IND: {
3299 		struct T_optdata_req *tdr;
3300 		void *opt;
3301 		t_uscalar_t optlen;
3302 
3303 		if ((so->so_state &
3304 		    (SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK)) &&
3305 		    (uiop->uio_resid != saved_resid) &&
3306 		    !(flags & MSG_PEEK)) {
3307 			sorecv_update_oobstate(so);
3308 		}
3309 
3310 		tdr = (struct T_optdata_req *)mp->b_rptr;
3311 		optlen = tdr->OPT_length;
3312 		if (optlen != 0) {
3313 			t_uscalar_t ncontrollen;
3314 			/*
3315 			 * Determine how large cmsg buffer is needed.
3316 			 */
3317 			opt = sogetoff(mp,
3318 			    tpr->optdata_ind.OPT_offset,
3319 			    optlen, __TPI_ALIGN_SIZE);
3320 
3321 			if (opt == NULL) {
3322 				freemsg(mp);
3323 				error = EPROTO;
3324 				eprintsoline(so, error);
3325 				goto err;
3326 			}
3327 
3328 			ncontrollen = so_cmsglen(mp, opt, optlen,
3329 			    !(flags & MSG_XPG4_2));
3330 			if (controllen != 0)
3331 				controllen = ncontrollen;
3332 			else if (ncontrollen != 0)
3333 				msg->msg_flags |= MSG_CTRUNC;
3334 		} else {
3335 			controllen = 0;
3336 		}
3337 
3338 		if (controllen != 0) {
3339 			/*
3340 			 * Return control msg to caller.
3341 			 * Caller handles truncation if length
3342 			 * exceeds msg_controllen.
3343 			 */
3344 			control = kmem_zalloc(controllen, KM_SLEEP);
3345 
3346 			error = so_opt2cmsg(mp, opt, optlen,
3347 			    !(flags & MSG_XPG4_2),
3348 			    control, controllen);
3349 			if (error) {
3350 				freemsg(mp);
3351 				kmem_free(control, controllen);
3352 				eprintsoline(so, error);
3353 				goto err;
3354 			}
3355 			msg->msg_control = control;
3356 			msg->msg_controllen = controllen;
3357 		}
3358 
3359 		/*
3360 		 * Set msg_flags to MSG_EOR based on
3361 		 * DATA_flag and MOREDATA.
3362 		 */
3363 		mutex_enter(&so->so_lock);
3364 		so->so_state &= ~SS_SAVEDEOR;
3365 		if (!(tpr->data_ind.MORE_flag & 1)) {
3366 			if (!(rval.r_val1 & MOREDATA))
3367 				msg->msg_flags |= MSG_EOR;
3368 			else
3369 				so->so_state |= SS_SAVEDEOR;
3370 		}
3371 		freemsg(mp);
3372 		/*
3373 		 * If some data was received (i.e. not EOF) and the
3374 		 * read/recv* has not been satisfied wait for some more.
3375 		 * Not possible to wait if control info was received.
3376 		 */
3377 		if ((flags & MSG_WAITALL) && !(msg->msg_flags & MSG_EOR) &&
3378 		    controllen == 0 &&
3379 		    uiop->uio_resid != saved_resid && uiop->uio_resid > 0) {
3380 			mutex_exit(&so->so_lock);
3381 			first = 0;
3382 			pflag = opflag | MSG_NOMARK;
3383 			goto retry;
3384 		}
3385 		so_unlock_read(so);	/* Clear SOREADLOCKED */
3386 		mutex_exit(&so->so_lock);
3387 		return (0);
3388 	}
3389 	case T_EXDATA_IND: {
3390 		dprintso(so, 1,
3391 		    ("sotpi_recvmsg: EXDATA_IND counts %d/%d consumed %ld "
3392 		    "state %s\n",
3393 		    so->so_oobsigcnt, so->so_oobcnt,
3394 		    saved_resid - uiop->uio_resid,
3395 		    pr_state(so->so_state, so->so_mode)));
3396 		/*
3397 		 * kstrgetmsg handles MSGMARK so there is nothing to
3398 		 * inspect in the T_EXDATA_IND.
3399 		 * strsock_proto makes the stream head queue the T_EXDATA_IND
3400 		 * as a separate message with no M_DATA component. Furthermore,
3401 		 * the stream head does not consolidate M_DATA messages onto
3402 		 * an MSGMARK'ed message ensuring that the T_EXDATA_IND
3403 		 * remains a message by itself. This is needed since MSGMARK
3404 		 * marks both the whole message as well as the last byte
3405 		 * of the message.
3406 		 */
3407 		freemsg(mp);
3408 		ASSERT(uiop->uio_resid == saved_resid);	/* No data */
3409 		if (flags & MSG_PEEK) {
3410 			/*
3411 			 * Even though we are peeking we consume the
3412 			 * T_EXDATA_IND thereby moving the mark information
3413 			 * to SS_RCVATMARK. Then the oob code below will
3414 			 * retry the peeking kstrgetmsg.
3415 			 * Note that the stream head read queue is
3416 			 * never flushed without holding SOREADLOCKED
3417 			 * thus the T_EXDATA_IND can not disappear
3418 			 * underneath us.
3419 			 */
3420 			dprintso(so, 1,
3421 			    ("sotpi_recvmsg: consume EXDATA_IND "
3422 			    "counts %d/%d state %s\n",
3423 			    so->so_oobsigcnt,
3424 			    so->so_oobcnt,
3425 			    pr_state(so->so_state, so->so_mode)));
3426 
3427 			pflag = MSG_ANY | MSG_DELAYERROR;
3428 			if (so->so_mode & SM_ATOMIC)
3429 				pflag |= MSG_DISCARDTAIL;
3430 
3431 			pri = 0;
3432 			mp = NULL;
3433 
3434 			error = kstrgetmsg(SOTOV(so), &mp, uiop,
3435 			    &pri, &pflag, (clock_t)-1, &rval);
3436 			ASSERT(uiop->uio_resid == saved_resid);
3437 
3438 			if (error) {
3439 #ifdef SOCK_DEBUG
3440 				if (error != EWOULDBLOCK && error != EINTR) {
3441 					eprintsoline(so, error);
3442 				}
3443 #endif /* SOCK_DEBUG */
3444 				mutex_enter(&so->so_lock);
3445 				so_unlock_read(so);	/* Clear SOREADLOCKED */
3446 				mutex_exit(&so->so_lock);
3447 				return (error);
3448 			}
3449 			ASSERT(mp);
3450 			tpr = (union T_primitives *)mp->b_rptr;
3451 			ASSERT(tpr->type == T_EXDATA_IND);
3452 			freemsg(mp);
3453 		} /* end "if (flags & MSG_PEEK)" */
3454 
3455 		/*
3456 		 * Decrement the number of queued and pending oob.
3457 		 *
3458 		 * SS_RCVATMARK is cleared when we read past a mark.
3459 		 * SS_HAVEOOBDATA is cleared when we've read past the
3460 		 * last mark.
3461 		 * SS_OOBPEND is cleared if we've read past the last
3462 		 * mark and no (new) SIGURG has been posted.
3463 		 */
3464 		mutex_enter(&so->so_lock);
3465 		ASSERT(so_verify_oobstate(so));
3466 		ASSERT(so->so_oobsigcnt >= so->so_oobcnt);
3467 		ASSERT(so->so_oobsigcnt > 0);
3468 		so->so_oobsigcnt--;
3469 		ASSERT(so->so_oobcnt > 0);
3470 		so->so_oobcnt--;
3471 		/*
3472 		 * Since the T_EXDATA_IND has been removed from the stream
3473 		 * head, but we have not read data past the mark,
3474 		 * sockfs needs to track that the socket is still at the mark.
3475 		 *
3476 		 * Since no data was received call kstrgetmsg again to wait
3477 		 * for data.
3478 		 */
3479 		so->so_state |= SS_RCVATMARK;
3480 		mutex_exit(&so->so_lock);
3481 		dprintso(so, 1,
3482 		    ("sotpi_recvmsg: retry EXDATA_IND counts %d/%d state %s\n",
3483 		    so->so_oobsigcnt, so->so_oobcnt,
3484 		    pr_state(so->so_state, so->so_mode)));
3485 		pflag = opflag;
3486 		goto retry;
3487 	}
3488 	default:
3489 		ASSERT(0);
3490 		freemsg(mp);
3491 		error = EPROTO;
3492 		eprintsoline(so, error);
3493 		goto err;
3494 	}
3495 	/* NOTREACHED */
3496 err:
3497 	mutex_enter(&so->so_lock);
3498 	so_unlock_read(so);	/* Clear SOREADLOCKED */
3499 	mutex_exit(&so->so_lock);
3500 	return (error);
3501 }
3502 
3503 /*
3504  * Sending data with options on a datagram socket.
3505  * Assumes caller has verified that SS_ISBOUND etc. are set.
3506  */
3507 static int
3508 sosend_dgramcmsg(struct sonode *so, struct sockaddr *name, socklen_t namelen,
3509     struct uio *uiop, void *control, t_uscalar_t controllen, int flags)
3510 {
3511 	struct T_unitdata_req	tudr;
3512 	mblk_t			*mp;
3513 	int			error;
3514 	void			*addr;
3515 	socklen_t		addrlen;
3516 	void			*src;
3517 	socklen_t		srclen;
3518 	ssize_t			len;
3519 	int			size;
3520 	struct T_opthdr		toh;
3521 	struct fdbuf		*fdbuf;
3522 	t_uscalar_t		optlen;
3523 	void			*fds;
3524 	int			fdlen;
3525 
3526 	ASSERT(name && namelen);
3527 	ASSERT(control && controllen);
3528 
3529 	len = uiop->uio_resid;
3530 	if (len > (ssize_t)so->so_tidu_size) {
3531 		return (EMSGSIZE);
3532 	}
3533 
3534 	/*
3535 	 * For AF_UNIX the destination address is translated to an internal
3536 	 * name and the source address is passed as an option.
3537 	 * Also, file descriptors are passed as file pointers in an
3538 	 * option.
3539 	 */
3540 
3541 	/*
3542 	 * Length and family checks.
3543 	 */
3544 	error = so_addr_verify(so, name, namelen);
3545 	if (error) {
3546 		eprintsoline(so, error);
3547 		return (error);
3548 	}
3549 	if (so->so_family == AF_UNIX) {
3550 		if (so->so_state & SS_FADDR_NOXLATE) {
3551 			/*
3552 			 * Already have a transport internal address. Do not
3553 			 * pass any (transport internal) source address.
3554 			 */
3555 			addr = name;
3556 			addrlen = namelen;
3557 			src = NULL;
3558 			srclen = 0;
3559 		} else {
3560 			/*
3561 			 * Pass the sockaddr_un source address as an option
3562 			 * and translate the remote address.
3563 			 *
3564 			 * Note that this code does not prevent so_laddr_sa
3565 			 * from changing while it is being used. Thus
3566 			 * if an unbind+bind occurs concurrently with this
3567 			 * send the peer might see a partially new and a
3568 			 * partially old "from" address.
3569 			 */
3570 			src = so->so_laddr_sa;
3571 			srclen = (t_uscalar_t)so->so_laddr_len;
3572 			dprintso(so, 1,
3573 			    ("sosend_dgramcmsg UNIX: srclen %d, src %p\n",
3574 			    srclen, src));
3575 			error = so_ux_addr_xlate(so, name, namelen,
3576 			    (flags & MSG_XPG4_2),
3577 			    &addr, &addrlen);
3578 			if (error) {
3579 				eprintsoline(so, error);
3580 				return (error);
3581 			}
3582 		}
3583 	} else {
3584 		addr = name;
3585 		addrlen = namelen;
3586 		src = NULL;
3587 		srclen = 0;
3588 	}
3589 	optlen = so_optlen(control, controllen,
3590 	    !(flags & MSG_XPG4_2));
3591 	tudr.PRIM_type = T_UNITDATA_REQ;
3592 	tudr.DEST_length = addrlen;
3593 	tudr.DEST_offset = (t_scalar_t)sizeof (tudr);
3594 	if (srclen != 0)
3595 		tudr.OPT_length = (t_scalar_t)(optlen + sizeof (toh) +
3596 		    _TPI_ALIGN_TOPT(srclen));
3597 	else
3598 		tudr.OPT_length = optlen;
3599 	tudr.OPT_offset = (t_scalar_t)(sizeof (tudr) +
3600 	    _TPI_ALIGN_TOPT(addrlen));
3601 
3602 	size = tudr.OPT_offset + tudr.OPT_length;
3603 
3604 	/*
3605 	 * File descriptors only when SM_FDPASSING set.
3606 	 */
3607 	error = so_getfdopt(control, controllen,
3608 	    !(flags & MSG_XPG4_2), &fds, &fdlen);
3609 	if (error)
3610 		return (error);
3611 	if (fdlen != -1) {
3612 		if (!(so->so_mode & SM_FDPASSING))
3613 			return (EOPNOTSUPP);
3614 
3615 		error = fdbuf_create(fds, fdlen, &fdbuf);
3616 		if (error)
3617 			return (error);
3618 		mp = fdbuf_allocmsg(size, fdbuf);
3619 	} else {
3620 		mp = soallocproto(size, _ALLOC_INTR);
3621 		if (mp == NULL) {
3622 			/*
3623 			 * Caught a signal waiting for memory.
3624 			 * Let send* return EINTR.
3625 			 */
3626 			return (EINTR);
3627 		}
3628 	}
3629 	soappendmsg(mp, &tudr, sizeof (tudr));
3630 	soappendmsg(mp, addr, addrlen);
3631 	mp->b_wptr += _TPI_ALIGN_TOPT(addrlen) - addrlen;
3632 
3633 	if (fdlen != -1) {
3634 		ASSERT(fdbuf != NULL);
3635 		toh.level = SOL_SOCKET;
3636 		toh.name = SO_FILEP;
3637 		toh.len = fdbuf->fd_size +
3638 		    (t_uscalar_t)sizeof (struct T_opthdr);
3639 		toh.status = 0;
3640 		soappendmsg(mp, &toh, sizeof (toh));
3641 		soappendmsg(mp, fdbuf, fdbuf->fd_size);
3642 		ASSERT(__TPI_TOPT_ISALIGNED(mp->b_wptr));
3643 	}
3644 	if (srclen != 0) {
3645 		/*
3646 		 * There is a AF_UNIX sockaddr_un to include as a source
3647 		 * address option.
3648 		 */
3649 		toh.level = SOL_SOCKET;
3650 		toh.name = SO_SRCADDR;
3651 		toh.len = (t_uscalar_t)(srclen + sizeof (struct T_opthdr));
3652 		toh.status = 0;
3653 		soappendmsg(mp, &toh, sizeof (toh));
3654 		soappendmsg(mp, src, srclen);
3655 		mp->b_wptr += _TPI_ALIGN_TOPT(srclen) - srclen;
3656 		ASSERT(__TPI_TOPT_ISALIGNED(mp->b_wptr));
3657 	}
3658 	ASSERT(mp->b_wptr <= mp->b_datap->db_lim);
3659 	so_cmsg2opt(control, controllen, !(flags & MSG_XPG4_2), mp);
3660 	/* At most 3 bytes left in the message */
3661 	ASSERT(MBLKL(mp) > (ssize_t)(size - __TPI_ALIGN_SIZE));
3662 	ASSERT(MBLKL(mp) <= (ssize_t)size);
3663 
3664 	ASSERT(mp->b_wptr <= mp->b_datap->db_lim);
3665 #ifdef C2_AUDIT
3666 	if (audit_active)
3667 		audit_sock(T_UNITDATA_REQ, strvp2wq(SOTOV(so)), mp, 0);
3668 #endif /* C2_AUDIT */
3669 
3670 	error = kstrputmsg(SOTOV(so), mp, uiop, len, 0, MSG_BAND, 0);
3671 #ifdef SOCK_DEBUG
3672 	if (error) {
3673 		eprintsoline(so, error);
3674 	}
3675 #endif /* SOCK_DEBUG */
3676 	return (error);
3677 }
3678 
3679 /*
3680  * Sending data with options on a connected stream socket.
3681  * Assumes caller has verified that SS_ISCONNECTED is set.
3682  */
3683 static int
3684 sosend_svccmsg(struct sonode *so,
3685 		struct uio *uiop,
3686 		int more,
3687 		void *control,
3688 		t_uscalar_t controllen,
3689 		int flags)
3690 {
3691 	struct T_optdata_req	tdr;
3692 	mblk_t			*mp;
3693 	int			error;
3694 	ssize_t			iosize;
3695 	int			first = 1;
3696 	int			size;
3697 	struct fdbuf		*fdbuf;
3698 	t_uscalar_t		optlen;
3699 	void			*fds;
3700 	int			fdlen;
3701 	struct T_opthdr		toh;
3702 
3703 	dprintso(so, 1,
3704 	    ("sosend_svccmsg: resid %ld bytes\n", uiop->uio_resid));
3705 
3706 	/*
3707 	 * Has to be bound and connected. However, since no locks are
3708 	 * held the state could have changed after sotpi_sendmsg checked it
3709 	 * thus it is not possible to ASSERT on the state.
3710 	 */
3711 
3712 	/* Options on connection-oriented only when SM_OPTDATA set. */
3713 	if (!(so->so_mode & SM_OPTDATA))
3714 		return (EOPNOTSUPP);
3715 
3716 	do {
3717 		/*
3718 		 * Set the MORE flag if uio_resid does not fit in this
3719 		 * message or if the caller passed in "more".
3720 		 * Error for transports with zero tidu_size.
3721 		 */
3722 		tdr.PRIM_type = T_OPTDATA_REQ;
3723 		iosize = so->so_tidu_size;
3724 		if (iosize <= 0)
3725 			return (EMSGSIZE);
3726 		if (uiop->uio_resid > iosize) {
3727 			tdr.DATA_flag = 1;
3728 		} else {
3729 			if (more)
3730 				tdr.DATA_flag = 1;
3731 			else
3732 				tdr.DATA_flag = 0;
3733 			iosize = uiop->uio_resid;
3734 		}
3735 		dprintso(so, 1, ("sosend_svccmsg: sending %d, %ld bytes\n",
3736 		    tdr.DATA_flag, iosize));
3737 
3738 		optlen = so_optlen(control, controllen, !(flags & MSG_XPG4_2));
3739 		tdr.OPT_length = optlen;
3740 		tdr.OPT_offset = (t_scalar_t)sizeof (tdr);
3741 
3742 		size = (int)sizeof (tdr) + optlen;
3743 		/*
3744 		 * File descriptors only when SM_FDPASSING set.
3745 		 */
3746 		error = so_getfdopt(control, controllen,
3747 		    !(flags & MSG_XPG4_2), &fds, &fdlen);
3748 		if (error)
3749 			return (error);
3750 		if (fdlen != -1) {
3751 			if (!(so->so_mode & SM_FDPASSING))
3752 				return (EOPNOTSUPP);
3753 
3754 			error = fdbuf_create(fds, fdlen, &fdbuf);
3755 			if (error)
3756 				return (error);
3757 			mp = fdbuf_allocmsg(size, fdbuf);
3758 		} else {
3759 			mp = soallocproto(size, _ALLOC_INTR);
3760 			if (mp == NULL) {
3761 				/*
3762 				 * Caught a signal waiting for memory.
3763 				 * Let send* return EINTR.
3764 				 */
3765 				return (first ? EINTR : 0);
3766 			}
3767 		}
3768 		soappendmsg(mp, &tdr, sizeof (tdr));
3769 
3770 		if (fdlen != -1) {
3771 			ASSERT(fdbuf != NULL);
3772 			toh.level = SOL_SOCKET;
3773 			toh.name = SO_FILEP;
3774 			toh.len = fdbuf->fd_size +
3775 			    (t_uscalar_t)sizeof (struct T_opthdr);
3776 			toh.status = 0;
3777 			soappendmsg(mp, &toh, sizeof (toh));
3778 			soappendmsg(mp, fdbuf, fdbuf->fd_size);
3779 			ASSERT(__TPI_TOPT_ISALIGNED(mp->b_wptr));
3780 		}
3781 		so_cmsg2opt(control, controllen, !(flags & MSG_XPG4_2), mp);
3782 		/* At most 3 bytes left in the message */
3783 		ASSERT(MBLKL(mp) > (ssize_t)(size - __TPI_ALIGN_SIZE));
3784 		ASSERT(MBLKL(mp) <= (ssize_t)size);
3785 
3786 		ASSERT(mp->b_wptr <= mp->b_datap->db_lim);
3787 
3788 		error = kstrputmsg(SOTOV(so), mp, uiop, iosize,
3789 		    0, MSG_BAND, 0);
3790 		if (error) {
3791 			if (!first && error == EWOULDBLOCK)
3792 				return (0);
3793 			eprintsoline(so, error);
3794 			return (error);
3795 		}
3796 		control = NULL;
3797 		first = 0;
3798 		if (uiop->uio_resid > 0) {
3799 			/*
3800 			 * Recheck for fatal errors. Fail write even though
3801 			 * some data have been written. This is consistent
3802 			 * with strwrite semantics and BSD sockets semantics.
3803 			 */
3804 			if (so->so_state & SS_CANTSENDMORE) {
3805 				tsignal(curthread, SIGPIPE);
3806 				eprintsoline(so, error);
3807 				return (EPIPE);
3808 			}
3809 			if (so->so_error != 0) {
3810 				mutex_enter(&so->so_lock);
3811 				error = sogeterr(so);
3812 				mutex_exit(&so->so_lock);
3813 				if (error != 0) {
3814 					eprintsoline(so, error);
3815 					return (error);
3816 				}
3817 			}
3818 		}
3819 	} while (uiop->uio_resid > 0);
3820 	return (0);
3821 }
3822 
3823 /*
3824  * Sending data on a datagram socket.
3825  * Assumes caller has verified that SS_ISBOUND etc. are set.
3826  *
3827  * For AF_UNIX the destination address is translated to an internal
3828  * name and the source address is passed as an option.
3829  */
3830 int
3831 sosend_dgram(struct sonode *so, struct sockaddr	*name, socklen_t namelen,
3832     struct uio *uiop, int flags)
3833 {
3834 	struct T_unitdata_req	tudr;
3835 	mblk_t			*mp;
3836 	int			error;
3837 	void			*addr;
3838 	socklen_t		addrlen;
3839 	void			*src;
3840 	socklen_t		srclen;
3841 	ssize_t			len;
3842 
3843 	ASSERT(name != NULL && namelen != 0);
3844 
3845 	len = uiop->uio_resid;
3846 	if (len > so->so_tidu_size) {
3847 		error = EMSGSIZE;
3848 		goto done;
3849 	}
3850 
3851 	/* Length and family checks */
3852 	error = so_addr_verify(so, name, namelen);
3853 	if (error != 0)
3854 		goto done;
3855 
3856 	if (so->so_state & SS_DIRECT)
3857 		return (sodgram_direct(so, name, namelen, uiop, flags));
3858 
3859 	if (so->so_family == AF_UNIX) {
3860 		if (so->so_state & SS_FADDR_NOXLATE) {
3861 			/*
3862 			 * Already have a transport internal address. Do not
3863 			 * pass any (transport internal) source address.
3864 			 */
3865 			addr = name;
3866 			addrlen = namelen;
3867 			src = NULL;
3868 			srclen = 0;
3869 		} else {
3870 			/*
3871 			 * Pass the sockaddr_un source address as an option
3872 			 * and translate the remote address.
3873 			 *
3874 			 * Note that this code does not prevent so_laddr_sa
3875 			 * from changing while it is being used. Thus
3876 			 * if an unbind+bind occurs concurrently with this
3877 			 * send the peer might see a partially new and a
3878 			 * partially old "from" address.
3879 			 */
3880 			src = so->so_laddr_sa;
3881 			srclen = (socklen_t)so->so_laddr_len;
3882 			dprintso(so, 1,
3883 			    ("sosend_dgram UNIX: srclen %d, src %p\n",
3884 			    srclen, src));
3885 			error = so_ux_addr_xlate(so, name, namelen,
3886 			    (flags & MSG_XPG4_2),
3887 			    &addr, &addrlen);
3888 			if (error) {
3889 				eprintsoline(so, error);
3890 				goto done;
3891 			}
3892 		}
3893 	} else {
3894 		addr = name;
3895 		addrlen = namelen;
3896 		src = NULL;
3897 		srclen = 0;
3898 	}
3899 	tudr.PRIM_type = T_UNITDATA_REQ;
3900 	tudr.DEST_length = addrlen;
3901 	tudr.DEST_offset = (t_scalar_t)sizeof (tudr);
3902 	if (srclen == 0) {
3903 		tudr.OPT_length = 0;
3904 		tudr.OPT_offset = 0;
3905 
3906 		mp = soallocproto2(&tudr, sizeof (tudr),
3907 		    addr, addrlen, 0, _ALLOC_INTR);
3908 		if (mp == NULL) {
3909 			/*
3910 			 * Caught a signal waiting for memory.
3911 			 * Let send* return EINTR.
3912 			 */
3913 			error = EINTR;
3914 			goto done;
3915 		}
3916 	} else {
3917 		/*
3918 		 * There is a AF_UNIX sockaddr_un to include as a source
3919 		 * address option.
3920 		 */
3921 		struct T_opthdr toh;
3922 		ssize_t size;
3923 
3924 		tudr.OPT_length = (t_scalar_t)(sizeof (toh) +
3925 		    _TPI_ALIGN_TOPT(srclen));
3926 		tudr.OPT_offset = (t_scalar_t)(sizeof (tudr) +
3927 		    _TPI_ALIGN_TOPT(addrlen));
3928 
3929 		toh.level = SOL_SOCKET;
3930 		toh.name = SO_SRCADDR;
3931 		toh.len = (t_uscalar_t)(srclen + sizeof (struct T_opthdr));
3932 		toh.status = 0;
3933 
3934 		size = tudr.OPT_offset + tudr.OPT_length;
3935 		mp = soallocproto2(&tudr, sizeof (tudr),
3936 		    addr, addrlen, size, _ALLOC_INTR);
3937 		if (mp == NULL) {
3938 			/*
3939 			 * Caught a signal waiting for memory.
3940 			 * Let send* return EINTR.
3941 			 */
3942 			error = EINTR;
3943 			goto done;
3944 		}
3945 		mp->b_wptr += _TPI_ALIGN_TOPT(addrlen) - addrlen;
3946 		soappendmsg(mp, &toh, sizeof (toh));
3947 		soappendmsg(mp, src, srclen);
3948 		mp->b_wptr += _TPI_ALIGN_TOPT(srclen) - srclen;
3949 		ASSERT(mp->b_wptr <= mp->b_datap->db_lim);
3950 	}
3951 
3952 #ifdef C2_AUDIT
3953 	if (audit_active)
3954 		audit_sock(T_UNITDATA_REQ, strvp2wq(SOTOV(so)), mp, 0);
3955 #endif /* C2_AUDIT */
3956 
3957 	error = kstrputmsg(SOTOV(so), mp, uiop, len, 0, MSG_BAND, 0);
3958 done:
3959 #ifdef SOCK_DEBUG
3960 	if (error) {
3961 		eprintsoline(so, error);
3962 	}
3963 #endif /* SOCK_DEBUG */
3964 	return (error);
3965 }
3966 
3967 /*
3968  * Sending data on a connected stream socket.
3969  * Assumes caller has verified that SS_ISCONNECTED is set.
3970  */
3971 int
3972 sosend_svc(struct sonode *so,
3973 	struct uio *uiop,
3974 	t_scalar_t prim,
3975 	int more,
3976 	int sflag)
3977 {
3978 	struct T_data_req	tdr;
3979 	mblk_t			*mp;
3980 	int			error;
3981 	ssize_t			iosize;
3982 	int			first = 1;
3983 
3984 	dprintso(so, 1,
3985 	    ("sosend_svc: %p, resid %ld bytes, prim %d, sflag 0x%x\n",
3986 	    so, uiop->uio_resid, prim, sflag));
3987 
3988 	/*
3989 	 * Has to be bound and connected. However, since no locks are
3990 	 * held the state could have changed after sotpi_sendmsg checked it
3991 	 * thus it is not possible to ASSERT on the state.
3992 	 */
3993 
3994 	do {
3995 		/*
3996 		 * Set the MORE flag if uio_resid does not fit in this
3997 		 * message or if the caller passed in "more".
3998 		 * Error for transports with zero tidu_size.
3999 		 */
4000 		tdr.PRIM_type = prim;
4001 		iosize = so->so_tidu_size;
4002 		if (iosize <= 0)
4003 			return (EMSGSIZE);
4004 		if (uiop->uio_resid > iosize) {
4005 			tdr.MORE_flag = 1;
4006 		} else {
4007 			if (more)
4008 				tdr.MORE_flag = 1;
4009 			else
4010 				tdr.MORE_flag = 0;
4011 			iosize = uiop->uio_resid;
4012 		}
4013 		dprintso(so, 1, ("sosend_svc: sending 0x%x %d, %ld bytes\n",
4014 		    prim, tdr.MORE_flag, iosize));
4015 		mp = soallocproto1(&tdr, sizeof (tdr), 0, _ALLOC_INTR);
4016 		if (mp == NULL) {
4017 			/*
4018 			 * Caught a signal waiting for memory.
4019 			 * Let send* return EINTR.
4020 			 */
4021 			if (first)
4022 				return (EINTR);
4023 			else
4024 				return (0);
4025 		}
4026 
4027 		error = kstrputmsg(SOTOV(so), mp, uiop, iosize,
4028 		    0, sflag | MSG_BAND, 0);
4029 		if (error) {
4030 			if (!first && error == EWOULDBLOCK)
4031 				return (0);
4032 			eprintsoline(so, error);
4033 			return (error);
4034 		}
4035 		first = 0;
4036 		if (uiop->uio_resid > 0) {
4037 			/*
4038 			 * Recheck for fatal errors. Fail write even though
4039 			 * some data have been written. This is consistent
4040 			 * with strwrite semantics and BSD sockets semantics.
4041 			 */
4042 			if (so->so_state & SS_CANTSENDMORE) {
4043 				tsignal(curthread, SIGPIPE);
4044 				eprintsoline(so, error);
4045 				return (EPIPE);
4046 			}
4047 			if (so->so_error != 0) {
4048 				mutex_enter(&so->so_lock);
4049 				error = sogeterr(so);
4050 				mutex_exit(&so->so_lock);
4051 				if (error != 0) {
4052 					eprintsoline(so, error);
4053 					return (error);
4054 				}
4055 			}
4056 		}
4057 	} while (uiop->uio_resid > 0);
4058 	return (0);
4059 }
4060 
4061 /*
4062  * Check the state for errors and call the appropriate send function.
4063  *
4064  * If MSG_DONTROUTE is set (and SO_DONTROUTE isn't already set)
4065  * this function issues a setsockopt to toggle SO_DONTROUTE before and
4066  * after sending the message.
4067  */
4068 static int
4069 sotpi_sendmsg(struct sonode *so, struct nmsghdr *msg, struct uio *uiop)
4070 {
4071 	int		so_state;
4072 	int		so_mode;
4073 	int		error;
4074 	struct sockaddr *name;
4075 	t_uscalar_t	namelen;
4076 	int		dontroute;
4077 	int		flags;
4078 
4079 	dprintso(so, 1, ("sotpi_sendmsg(%p, %p, 0x%x) state %s, error %d\n",
4080 	    so, msg, msg->msg_flags,
4081 	    pr_state(so->so_state, so->so_mode), so->so_error));
4082 
4083 	mutex_enter(&so->so_lock);
4084 	so_state = so->so_state;
4085 
4086 	if (so_state & SS_CANTSENDMORE) {
4087 		mutex_exit(&so->so_lock);
4088 		tsignal(curthread, SIGPIPE);
4089 		return (EPIPE);
4090 	}
4091 
4092 	if (so->so_error != 0) {
4093 		error = sogeterr(so);
4094 		if (error != 0) {
4095 			mutex_exit(&so->so_lock);
4096 			return (error);
4097 		}
4098 	}
4099 
4100 	name = (struct sockaddr *)msg->msg_name;
4101 	namelen = msg->msg_namelen;
4102 
4103 	so_mode = so->so_mode;
4104 
4105 	if (name == NULL) {
4106 		if (!(so_state & SS_ISCONNECTED)) {
4107 			mutex_exit(&so->so_lock);
4108 			if (so_mode & SM_CONNREQUIRED)
4109 				return (ENOTCONN);
4110 			else
4111 				return (EDESTADDRREQ);
4112 		}
4113 		if (so_mode & SM_CONNREQUIRED) {
4114 			name = NULL;
4115 			namelen = 0;
4116 		} else {
4117 			/*
4118 			 * Note that this code does not prevent so_faddr_sa
4119 			 * from changing while it is being used. Thus
4120 			 * if an "unconnect"+connect occurs concurrently with
4121 			 * this send the datagram might be delivered to a
4122 			 * garbaled address.
4123 			 */
4124 			ASSERT(so->so_faddr_sa);
4125 			name = so->so_faddr_sa;
4126 			namelen = (t_uscalar_t)so->so_faddr_len;
4127 		}
4128 	} else {
4129 		if (!(so_state & SS_ISCONNECTED) &&
4130 		    (so_mode & SM_CONNREQUIRED)) {
4131 			/* Required but not connected */
4132 			mutex_exit(&so->so_lock);
4133 			return (ENOTCONN);
4134 		}
4135 		/*
4136 		 * Ignore the address on connection-oriented sockets.
4137 		 * Just like BSD this code does not generate an error for
4138 		 * TCP (a CONNREQUIRED socket) when sending to an address
4139 		 * passed in with sendto/sendmsg. Instead the data is
4140 		 * delivered on the connection as if no address had been
4141 		 * supplied.
4142 		 */
4143 		if ((so_state & SS_ISCONNECTED) &&
4144 		    !(so_mode & SM_CONNREQUIRED)) {
4145 			mutex_exit(&so->so_lock);
4146 			return (EISCONN);
4147 		}
4148 		if (!(so_state & SS_ISBOUND)) {
4149 			so_lock_single(so);	/* Set SOLOCKED */
4150 			error = sotpi_bind(so, NULL, 0,
4151 			    _SOBIND_UNSPEC|_SOBIND_LOCK_HELD);
4152 			so_unlock_single(so, SOLOCKED);
4153 			if (error) {
4154 				mutex_exit(&so->so_lock);
4155 				eprintsoline(so, error);
4156 				return (error);
4157 			}
4158 		}
4159 		/*
4160 		 * Handle delayed datagram errors. These are only queued
4161 		 * when the application sets SO_DGRAM_ERRIND.
4162 		 * Return the error if we are sending to the address
4163 		 * that was returned in the last T_UDERROR_IND.
4164 		 * If sending to some other address discard the delayed
4165 		 * error indication.
4166 		 */
4167 		if (so->so_delayed_error) {
4168 			struct T_uderror_ind	*tudi;
4169 			void			*addr;
4170 			t_uscalar_t		addrlen;
4171 			boolean_t		match = B_FALSE;
4172 
4173 			ASSERT(so->so_eaddr_mp);
4174 			error = so->so_delayed_error;
4175 			so->so_delayed_error = 0;
4176 			tudi = (struct T_uderror_ind *)so->so_eaddr_mp->b_rptr;
4177 			addrlen = tudi->DEST_length;
4178 			addr = sogetoff(so->so_eaddr_mp,
4179 			    tudi->DEST_offset,
4180 			    addrlen, 1);
4181 			ASSERT(addr);	/* Checked by strsock_proto */
4182 			switch (so->so_family) {
4183 			case AF_INET: {
4184 				/* Compare just IP address and port */
4185 				sin_t *sin1 = (sin_t *)name;
4186 				sin_t *sin2 = (sin_t *)addr;
4187 
4188 				if (addrlen == sizeof (sin_t) &&
4189 				    namelen == addrlen &&
4190 				    sin1->sin_port == sin2->sin_port &&
4191 				    sin1->sin_addr.s_addr ==
4192 				    sin2->sin_addr.s_addr)
4193 					match = B_TRUE;
4194 				break;
4195 			}
4196 			case AF_INET6: {
4197 				/* Compare just IP address and port. Not flow */
4198 				sin6_t *sin1 = (sin6_t *)name;
4199 				sin6_t *sin2 = (sin6_t *)addr;
4200 
4201 				if (addrlen == sizeof (sin6_t) &&
4202 				    namelen == addrlen &&
4203 				    sin1->sin6_port == sin2->sin6_port &&
4204 				    IN6_ARE_ADDR_EQUAL(&sin1->sin6_addr,
4205 				    &sin2->sin6_addr))
4206 					match = B_TRUE;
4207 				break;
4208 			}
4209 			case AF_UNIX:
4210 			default:
4211 				if (namelen == addrlen &&
4212 				    bcmp(name, addr, namelen) == 0)
4213 					match = B_TRUE;
4214 			}
4215 			if (match) {
4216 				freemsg(so->so_eaddr_mp);
4217 				so->so_eaddr_mp = NULL;
4218 				mutex_exit(&so->so_lock);
4219 #ifdef DEBUG
4220 				dprintso(so, 0,
4221 				    ("sockfs delayed error %d for %s\n",
4222 				    error,
4223 				    pr_addr(so->so_family, name, namelen)));
4224 #endif /* DEBUG */
4225 				return (error);
4226 			}
4227 			freemsg(so->so_eaddr_mp);
4228 			so->so_eaddr_mp = NULL;
4229 		}
4230 	}
4231 	mutex_exit(&so->so_lock);
4232 
4233 	flags = msg->msg_flags;
4234 	dontroute = 0;
4235 	if ((flags & MSG_DONTROUTE) && !(so->so_options & SO_DONTROUTE)) {
4236 		uint32_t	val;
4237 
4238 		val = 1;
4239 		error = sotpi_setsockopt(so, SOL_SOCKET, SO_DONTROUTE,
4240 		    &val, (t_uscalar_t)sizeof (val));
4241 		if (error)
4242 			return (error);
4243 		dontroute = 1;
4244 	}
4245 
4246 	if ((flags & MSG_OOB) && !(so_mode & SM_EXDATA)) {
4247 		error = EOPNOTSUPP;
4248 		goto done;
4249 	}
4250 	if (msg->msg_controllen != 0) {
4251 		if (!(so_mode & SM_CONNREQUIRED)) {
4252 			error = sosend_dgramcmsg(so, name, namelen, uiop,
4253 			    msg->msg_control, msg->msg_controllen, flags);
4254 		} else {
4255 			if (flags & MSG_OOB) {
4256 				/* Can't generate T_EXDATA_REQ with options */
4257 				error = EOPNOTSUPP;
4258 				goto done;
4259 			}
4260 			error = sosend_svccmsg(so, uiop,
4261 			    !(flags & MSG_EOR),
4262 			    msg->msg_control, msg->msg_controllen,
4263 			    flags);
4264 		}
4265 		goto done;
4266 	}
4267 
4268 	if (!(so_mode & SM_CONNREQUIRED)) {
4269 		/*
4270 		 * If there is no SO_DONTROUTE to turn off return immediately
4271 		 * from send_dgram. This can allow tail-call optimizations.
4272 		 */
4273 		if (!dontroute) {
4274 			return (sosend_dgram(so, name, namelen, uiop, flags));
4275 		}
4276 		error = sosend_dgram(so, name, namelen, uiop, flags);
4277 	} else {
4278 		t_scalar_t prim;
4279 		int sflag;
4280 
4281 		/* Ignore msg_name in the connected state */
4282 		if (flags & MSG_OOB) {
4283 			prim = T_EXDATA_REQ;
4284 			/*
4285 			 * Send down T_EXDATA_REQ even if there is flow
4286 			 * control for data.
4287 			 */
4288 			sflag = MSG_IGNFLOW;
4289 		} else {
4290 			if (so_mode & SM_BYTESTREAM) {
4291 				/* Byte stream transport - use write */
4292 
4293 				dprintso(so, 1, ("sotpi_sendmsg: write\n"));
4294 				/*
4295 				 * If there is no SO_DONTROUTE to turn off,
4296 				 * SS_DIRECT is on, and there is no flow
4297 				 * control, we can take the fast path.
4298 				 */
4299 				if (!dontroute &&
4300 				    (so_state & SS_DIRECT) &&
4301 				    canputnext(SOTOV(so)->v_stream->sd_wrq)) {
4302 					return (sostream_direct(so, uiop,
4303 					    NULL, CRED()));
4304 				}
4305 				error = strwrite(SOTOV(so), uiop, CRED());
4306 				goto done;
4307 			}
4308 			prim = T_DATA_REQ;
4309 			sflag = 0;
4310 		}
4311 		/*
4312 		 * If there is no SO_DONTROUTE to turn off return immediately
4313 		 * from sosend_svc. This can allow tail-call optimizations.
4314 		 */
4315 		if (!dontroute)
4316 			return (sosend_svc(so, uiop, prim,
4317 			    !(flags & MSG_EOR), sflag));
4318 		error = sosend_svc(so, uiop, prim,
4319 		    !(flags & MSG_EOR), sflag);
4320 	}
4321 	ASSERT(dontroute);
4322 done:
4323 	if (dontroute) {
4324 		uint32_t	val;
4325 
4326 		val = 0;
4327 		(void) sotpi_setsockopt(so, SOL_SOCKET, SO_DONTROUTE,
4328 		    &val, (t_uscalar_t)sizeof (val));
4329 	}
4330 	return (error);
4331 }
4332 
4333 /*
4334  * Sending data on a datagram socket.
4335  * Assumes caller has verified that SS_ISBOUND etc. are set.
4336  */
4337 /* ARGSUSED */
4338 static int
4339 sodgram_direct(struct sonode *so, struct sockaddr *name,
4340     socklen_t namelen, struct uio *uiop, int flags)
4341 {
4342 	struct T_unitdata_req	tudr;
4343 	mblk_t			*mp = NULL;
4344 	int			error = 0;
4345 	void			*addr;
4346 	socklen_t		addrlen;
4347 	ssize_t			len;
4348 	struct stdata		*stp = SOTOV(so)->v_stream;
4349 	int			so_state;
4350 	queue_t			*udp_wq;
4351 	boolean_t		connected;
4352 	mblk_t			*mpdata = NULL;
4353 
4354 	ASSERT(name != NULL && namelen != 0);
4355 	ASSERT(!(so->so_mode & SM_CONNREQUIRED));
4356 	ASSERT(!(so->so_mode & SM_EXDATA));
4357 	ASSERT(so->so_family == AF_INET || so->so_family == AF_INET6);
4358 	ASSERT(SOTOV(so)->v_type == VSOCK);
4359 
4360 	/* Caller checked for proper length */
4361 	len = uiop->uio_resid;
4362 	ASSERT(len <= so->so_tidu_size);
4363 
4364 	/* Length and family checks have been done by caller */
4365 	ASSERT(name->sa_family == so->so_family);
4366 	ASSERT(so->so_family == AF_INET ||
4367 	    (namelen == (socklen_t)sizeof (struct sockaddr_in6)));
4368 	ASSERT(so->so_family == AF_INET6 ||
4369 	    (namelen == (socklen_t)sizeof (struct sockaddr_in)));
4370 
4371 	addr = name;
4372 	addrlen = namelen;
4373 
4374 	if (stp->sd_sidp != NULL &&
4375 	    (error = straccess(stp, JCWRITE)) != 0)
4376 		goto done;
4377 
4378 	so_state = so->so_state;
4379 
4380 	connected = so_state & SS_ISCONNECTED;
4381 	if (!connected) {
4382 		tudr.PRIM_type = T_UNITDATA_REQ;
4383 		tudr.DEST_length = addrlen;
4384 		tudr.DEST_offset = (t_scalar_t)sizeof (tudr);
4385 		tudr.OPT_length = 0;
4386 		tudr.OPT_offset = 0;
4387 
4388 		mp = soallocproto2(&tudr, sizeof (tudr), addr, addrlen, 0,
4389 		    _ALLOC_INTR);
4390 		if (mp == NULL) {
4391 			/*
4392 			 * Caught a signal waiting for memory.
4393 			 * Let send* return EINTR.
4394 			 */
4395 			error = EINTR;
4396 			goto done;
4397 		}
4398 	}
4399 
4400 	/*
4401 	 * For UDP we don't break up the copyin into smaller pieces
4402 	 * as in the TCP case.  That means if ENOMEM is returned by
4403 	 * mcopyinuio() then the uio vector has not been modified at
4404 	 * all and we fallback to either strwrite() or kstrputmsg()
4405 	 * below.  Note also that we never generate priority messages
4406 	 * from here.
4407 	 */
4408 	udp_wq = stp->sd_wrq->q_next;
4409 	if (canput(udp_wq) &&
4410 	    (mpdata = mcopyinuio(stp, uiop, -1, -1, &error)) != NULL) {
4411 		ASSERT(DB_TYPE(mpdata) == M_DATA);
4412 		ASSERT(uiop->uio_resid == 0);
4413 		if (!connected)
4414 			linkb(mp, mpdata);
4415 		else
4416 			mp = mpdata;
4417 #ifdef C2_AUDIT
4418 		if (audit_active)
4419 			audit_sock(T_UNITDATA_REQ, strvp2wq(SOTOV(so)), mp, 0);
4420 #endif /* C2_AUDIT */
4421 
4422 		udp_wput(udp_wq, mp);
4423 		return (0);
4424 	}
4425 
4426 	ASSERT(mpdata == NULL);
4427 	if (error != 0 && error != ENOMEM) {
4428 		freemsg(mp);
4429 		return (error);
4430 	}
4431 
4432 	/*
4433 	 * For connected, let strwrite() handle the blocking case.
4434 	 * Otherwise we fall thru and use kstrputmsg().
4435 	 */
4436 	if (connected)
4437 		return (strwrite(SOTOV(so), uiop, CRED()));
4438 
4439 #ifdef C2_AUDIT
4440 	if (audit_active)
4441 		audit_sock(T_UNITDATA_REQ, strvp2wq(SOTOV(so)), mp, 0);
4442 #endif /* C2_AUDIT */
4443 
4444 	error = kstrputmsg(SOTOV(so), mp, uiop, len, 0, MSG_BAND, 0);
4445 done:
4446 #ifdef SOCK_DEBUG
4447 	if (error != 0) {
4448 		eprintsoline(so, error);
4449 	}
4450 #endif /* SOCK_DEBUG */
4451 	return (error);
4452 }
4453 
4454 int
4455 sostream_direct(struct sonode *so, struct uio *uiop, mblk_t *mp, cred_t *cr)
4456 {
4457 	struct stdata *stp = SOTOV(so)->v_stream;
4458 	ssize_t iosize, rmax, maxblk;
4459 	queue_t *tcp_wq = stp->sd_wrq->q_next;
4460 	mblk_t *newmp;
4461 	int error = 0, wflag = 0;
4462 
4463 	ASSERT(so->so_mode & SM_BYTESTREAM);
4464 	ASSERT(SOTOV(so)->v_type == VSOCK);
4465 
4466 	if (stp->sd_sidp != NULL &&
4467 	    (error = straccess(stp, JCWRITE)) != 0)
4468 		return (error);
4469 
4470 	if (uiop == NULL) {
4471 		/*
4472 		 * kstrwritemp() should have checked sd_flag and
4473 		 * flow-control before coming here.  If we end up
4474 		 * here it means that we can simply pass down the
4475 		 * data to tcp.
4476 		 */
4477 		ASSERT(mp != NULL);
4478 		if (stp->sd_wputdatafunc != NULL) {
4479 			newmp = (stp->sd_wputdatafunc)(SOTOV(so), mp, NULL,
4480 			    NULL, NULL, NULL);
4481 			if (newmp == NULL) {
4482 				/* The caller will free mp */
4483 				return (ECOMM);
4484 			}
4485 			mp = newmp;
4486 		}
4487 		tcp_wput(tcp_wq, mp);
4488 		return (0);
4489 	}
4490 
4491 	/* Fallback to strwrite() to do proper error handling */
4492 	if (stp->sd_flag & (STWRERR|STRHUP|STPLEX|STRDELIM|OLDNDELAY))
4493 		return (strwrite(SOTOV(so), uiop, cr));
4494 
4495 	rmax = stp->sd_qn_maxpsz;
4496 	ASSERT(rmax >= 0 || rmax == INFPSZ);
4497 	if (rmax == 0 || uiop->uio_resid <= 0)
4498 		return (0);
4499 
4500 	if (rmax == INFPSZ)
4501 		rmax = uiop->uio_resid;
4502 
4503 	maxblk = stp->sd_maxblk;
4504 
4505 	for (;;) {
4506 		iosize = MIN(uiop->uio_resid, rmax);
4507 
4508 		mp = mcopyinuio(stp, uiop, iosize, maxblk, &error);
4509 		if (mp == NULL) {
4510 			/*
4511 			 * Fallback to strwrite() for ENOMEM; if this
4512 			 * is our first time in this routine and the uio
4513 			 * vector has not been modified, we will end up
4514 			 * calling strwrite() without any flag set.
4515 			 */
4516 			if (error == ENOMEM)
4517 				goto slow_send;
4518 			else
4519 				return (error);
4520 		}
4521 		ASSERT(uiop->uio_resid >= 0);
4522 		/*
4523 		 * If mp is non-NULL and ENOMEM is set, it means that
4524 		 * mcopyinuio() was able to break down some of the user
4525 		 * data into one or more mblks.  Send the partial data
4526 		 * to tcp and let the rest be handled in strwrite().
4527 		 */
4528 		ASSERT(error == 0 || error == ENOMEM);
4529 		if (stp->sd_wputdatafunc != NULL) {
4530 			newmp = (stp->sd_wputdatafunc)(SOTOV(so), mp, NULL,
4531 			    NULL, NULL, NULL);
4532 			if (newmp == NULL) {
4533 				/* The caller will free mp */
4534 				return (ECOMM);
4535 			}
4536 			mp = newmp;
4537 		}
4538 		tcp_wput(tcp_wq, mp);
4539 
4540 		wflag |= NOINTR;
4541 
4542 		if (uiop->uio_resid == 0) {	/* No more data; we're done */
4543 			ASSERT(error == 0);
4544 			break;
4545 		} else if (error == ENOMEM || !canput(tcp_wq) || (stp->sd_flag &
4546 		    (STWRERR|STRHUP|STPLEX|STRDELIM|OLDNDELAY))) {
4547 slow_send:
4548 			/*
4549 			 * We were able to send down partial data using
4550 			 * the direct call interface, but are now relying
4551 			 * on strwrite() to handle the non-fastpath cases.
4552 			 * If the socket is blocking we will sleep in
4553 			 * strwaitq() until write is permitted, otherwise,
4554 			 * we will need to return the amount of bytes
4555 			 * written so far back to the app.  This is the
4556 			 * reason why we pass NOINTR flag to strwrite()
4557 			 * for non-blocking socket, because we don't want
4558 			 * to return EAGAIN when portion of the user data
4559 			 * has actually been sent down.
4560 			 */
4561 			return (strwrite_common(SOTOV(so), uiop, cr, wflag));
4562 		}
4563 	}
4564 	return (0);
4565 }
4566 
4567 /*
4568  * Update so_faddr by asking the transport (unless AF_UNIX).
4569  */
4570 int
4571 sotpi_getpeername(struct sonode *so)
4572 {
4573 	struct strbuf	strbuf;
4574 	int		error = 0, res;
4575 	void		*addr;
4576 	t_uscalar_t	addrlen;
4577 	k_sigset_t	smask;
4578 
4579 	dprintso(so, 1, ("sotpi_getpeername(%p) %s\n",
4580 	    so, pr_state(so->so_state, so->so_mode)));
4581 
4582 	mutex_enter(&so->so_lock);
4583 	so_lock_single(so);	/* Set SOLOCKED */
4584 	if (!(so->so_state & SS_ISCONNECTED)) {
4585 		error = ENOTCONN;
4586 		goto done;
4587 	}
4588 	/* Added this check for X/Open */
4589 	if ((so->so_state & SS_CANTSENDMORE) && !xnet_skip_checks) {
4590 		error = EINVAL;
4591 		if (xnet_check_print) {
4592 			printf("sockfs: X/Open getpeername check => EINVAL\n");
4593 		}
4594 		goto done;
4595 	}
4596 #ifdef DEBUG
4597 	dprintso(so, 1, ("sotpi_getpeername (local): %s\n",
4598 	    pr_addr(so->so_family, so->so_faddr_sa,
4599 	    (t_uscalar_t)so->so_faddr_len)));
4600 #endif /* DEBUG */
4601 
4602 	if (so->so_family == AF_UNIX) {
4603 		/* Transport has different name space - return local info */
4604 		error = 0;
4605 		goto done;
4606 	}
4607 
4608 	ASSERT(so->so_faddr_sa);
4609 	/* Allocate local buffer to use with ioctl */
4610 	addrlen = (t_uscalar_t)so->so_faddr_maxlen;
4611 	mutex_exit(&so->so_lock);
4612 	addr = kmem_alloc(addrlen, KM_SLEEP);
4613 
4614 	/*
4615 	 * Issue TI_GETPEERNAME with signals masked.
4616 	 * Put the result in so_faddr_sa so that getpeername works after
4617 	 * a shutdown(output).
4618 	 * If the ioctl fails (e.g. due to a ECONNRESET) the error is reposted
4619 	 * back to the socket.
4620 	 */
4621 	strbuf.buf = addr;
4622 	strbuf.maxlen = addrlen;
4623 	strbuf.len = 0;
4624 
4625 	sigintr(&smask, 0);
4626 	res = 0;
4627 	ASSERT(CRED());
4628 	error = strioctl(SOTOV(so), TI_GETPEERNAME, (intptr_t)&strbuf,
4629 	    0, K_TO_K, CRED(), &res);
4630 	sigunintr(&smask);
4631 
4632 	mutex_enter(&so->so_lock);
4633 	/*
4634 	 * If there is an error record the error in so_error put don't fail
4635 	 * the getpeername. Instead fallback on the recorded
4636 	 * so->so_faddr_sa.
4637 	 */
4638 	if (error) {
4639 		/*
4640 		 * Various stream head errors can be returned to the ioctl.
4641 		 * However, it is impossible to determine which ones of
4642 		 * these are really socket level errors that were incorrectly
4643 		 * consumed by the ioctl. Thus this code silently ignores the
4644 		 * error - to code explicitly does not reinstate the error
4645 		 * using soseterror().
4646 		 * Experiments have shows that at least this set of
4647 		 * errors are reported and should not be reinstated on the
4648 		 * socket:
4649 		 *	EINVAL	E.g. if an I_LINK was in effect when
4650 		 *		getpeername was called.
4651 		 *	EPIPE	The ioctl error semantics prefer the write
4652 		 *		side error over the read side error.
4653 		 *	ENOTCONN The transport just got disconnected but
4654 		 *		sockfs had not yet seen the T_DISCON_IND
4655 		 *		when issuing the ioctl.
4656 		 */
4657 		error = 0;
4658 	} else if (res == 0 && strbuf.len > 0 &&
4659 	    (so->so_state & SS_ISCONNECTED)) {
4660 		ASSERT(strbuf.len <= (int)so->so_faddr_maxlen);
4661 		so->so_faddr_len = (socklen_t)strbuf.len;
4662 		bcopy(addr, so->so_faddr_sa, so->so_faddr_len);
4663 		so->so_state |= SS_FADDR_VALID;
4664 	}
4665 	kmem_free(addr, addrlen);
4666 #ifdef DEBUG
4667 	dprintso(so, 1, ("sotpi_getpeername (tp): %s\n",
4668 	    pr_addr(so->so_family, so->so_faddr_sa,
4669 	    (t_uscalar_t)so->so_faddr_len)));
4670 #endif /* DEBUG */
4671 done:
4672 	so_unlock_single(so, SOLOCKED);
4673 	mutex_exit(&so->so_lock);
4674 	return (error);
4675 }
4676 
4677 /*
4678  * Update so_laddr by asking the transport (unless AF_UNIX).
4679  */
4680 int
4681 sotpi_getsockname(struct sonode *so)
4682 {
4683 	struct strbuf	strbuf;
4684 	int		error = 0, res;
4685 	void		*addr;
4686 	t_uscalar_t	addrlen;
4687 	k_sigset_t	smask;
4688 
4689 	dprintso(so, 1, ("sotpi_getsockname(%p) %s\n",
4690 	    so, pr_state(so->so_state, so->so_mode)));
4691 
4692 	mutex_enter(&so->so_lock);
4693 	so_lock_single(so);	/* Set SOLOCKED */
4694 	if (!(so->so_state & SS_ISBOUND) && so->so_family != AF_UNIX) {
4695 		/* Return an all zero address except for the family */
4696 		if (so->so_family == AF_INET)
4697 			so->so_laddr_len = (socklen_t)sizeof (sin_t);
4698 		else if (so->so_family == AF_INET6)
4699 			so->so_laddr_len = (socklen_t)sizeof (sin6_t);
4700 		ASSERT(so->so_laddr_len <= so->so_laddr_maxlen);
4701 		bzero(so->so_laddr_sa, so->so_laddr_len);
4702 		/*
4703 		 * Can not assume there is a sa_family for all
4704 		 * protocol families.
4705 		 */
4706 		if (so->so_family == AF_INET || so->so_family == AF_INET6)
4707 			so->so_laddr_sa->sa_family = so->so_family;
4708 	}
4709 #ifdef DEBUG
4710 	dprintso(so, 1, ("sotpi_getsockname (local): %s\n",
4711 	    pr_addr(so->so_family, so->so_laddr_sa,
4712 	    (t_uscalar_t)so->so_laddr_len)));
4713 #endif /* DEBUG */
4714 	if (so->so_family == AF_UNIX) {
4715 		/* Transport has different name space - return local info */
4716 		error = 0;
4717 		goto done;
4718 	}
4719 	if (!(so->so_state & SS_ISBOUND)) {
4720 		/* If not bound, then nothing to return. */
4721 		error = 0;
4722 		goto done;
4723 	}
4724 	/* Allocate local buffer to use with ioctl */
4725 	addrlen = (t_uscalar_t)so->so_laddr_maxlen;
4726 	mutex_exit(&so->so_lock);
4727 	addr = kmem_alloc(addrlen, KM_SLEEP);
4728 
4729 	/*
4730 	 * Issue TI_GETMYNAME with signals masked.
4731 	 * Put the result in so_laddr_sa so that getsockname works after
4732 	 * a shutdown(output).
4733 	 * If the ioctl fails (e.g. due to a ECONNRESET) the error is reposted
4734 	 * back to the socket.
4735 	 */
4736 	strbuf.buf = addr;
4737 	strbuf.maxlen = addrlen;
4738 	strbuf.len = 0;
4739 
4740 	sigintr(&smask, 0);
4741 	res = 0;
4742 	ASSERT(CRED());
4743 	error = strioctl(SOTOV(so), TI_GETMYNAME, (intptr_t)&strbuf,
4744 	    0, K_TO_K, CRED(), &res);
4745 	sigunintr(&smask);
4746 
4747 	mutex_enter(&so->so_lock);
4748 	/*
4749 	 * If there is an error record the error in so_error put don't fail
4750 	 * the getsockname. Instead fallback on the recorded
4751 	 * so->so_laddr_sa.
4752 	 */
4753 	if (error) {
4754 		/*
4755 		 * Various stream head errors can be returned to the ioctl.
4756 		 * However, it is impossible to determine which ones of
4757 		 * these are really socket level errors that were incorrectly
4758 		 * consumed by the ioctl. Thus this code silently ignores the
4759 		 * error - to code explicitly does not reinstate the error
4760 		 * using soseterror().
4761 		 * Experiments have shows that at least this set of
4762 		 * errors are reported and should not be reinstated on the
4763 		 * socket:
4764 		 *	EINVAL	E.g. if an I_LINK was in effect when
4765 		 *		getsockname was called.
4766 		 *	EPIPE	The ioctl error semantics prefer the write
4767 		 *		side error over the read side error.
4768 		 */
4769 		error = 0;
4770 	} else if (res == 0 && strbuf.len > 0 &&
4771 	    (so->so_state & SS_ISBOUND)) {
4772 		ASSERT(strbuf.len <= (int)so->so_laddr_maxlen);
4773 		so->so_laddr_len = (socklen_t)strbuf.len;
4774 		bcopy(addr, so->so_laddr_sa, so->so_laddr_len);
4775 		so->so_state |= SS_LADDR_VALID;
4776 	}
4777 	kmem_free(addr, addrlen);
4778 #ifdef DEBUG
4779 	dprintso(so, 1, ("sotpi_getsockname (tp): %s\n",
4780 	    pr_addr(so->so_family, so->so_laddr_sa,
4781 	    (t_uscalar_t)so->so_laddr_len)));
4782 #endif /* DEBUG */
4783 done:
4784 	so_unlock_single(so, SOLOCKED);
4785 	mutex_exit(&so->so_lock);
4786 	return (error);
4787 }
4788 
4789 /*
4790  * Get socket options. For SOL_SOCKET options some options are handled
4791  * by the sockfs while others use the value recorded in the sonode as a
4792  * fallback should the T_SVR4_OPTMGMT_REQ fail.
4793  *
4794  * On the return most *optlenp bytes are copied to optval.
4795  */
4796 int
4797 sotpi_getsockopt(struct sonode *so, int level, int option_name,
4798 		void *optval, socklen_t *optlenp, int flags)
4799 {
4800 	struct T_optmgmt_req	optmgmt_req;
4801 	struct T_optmgmt_ack	*optmgmt_ack;
4802 	struct opthdr		oh;
4803 	struct opthdr		*opt_res;
4804 	mblk_t			*mp = NULL;
4805 	int			error = 0;
4806 	void			*option = NULL;	/* Set if fallback value */
4807 	t_uscalar_t		maxlen = *optlenp;
4808 	t_uscalar_t		len;
4809 	uint32_t		value;
4810 
4811 	dprintso(so, 1, ("sotpi_getsockopt(%p, 0x%x, 0x%x, %p, %p) %s\n",
4812 	    so, level, option_name, optval, optlenp,
4813 	    pr_state(so->so_state, so->so_mode)));
4814 
4815 	mutex_enter(&so->so_lock);
4816 	so_lock_single(so);	/* Set SOLOCKED */
4817 
4818 	/*
4819 	 * Check for SOL_SOCKET options.
4820 	 * Certain SOL_SOCKET options are returned directly whereas
4821 	 * others only provide a default (fallback) value should
4822 	 * the T_SVR4_OPTMGMT_REQ fail.
4823 	 */
4824 	if (level == SOL_SOCKET) {
4825 		/* Check parameters */
4826 		switch (option_name) {
4827 		case SO_TYPE:
4828 		case SO_ERROR:
4829 		case SO_DEBUG:
4830 		case SO_ACCEPTCONN:
4831 		case SO_REUSEADDR:
4832 		case SO_KEEPALIVE:
4833 		case SO_DONTROUTE:
4834 		case SO_BROADCAST:
4835 		case SO_USELOOPBACK:
4836 		case SO_OOBINLINE:
4837 		case SO_SNDBUF:
4838 		case SO_RCVBUF:
4839 #ifdef notyet
4840 		case SO_SNDLOWAT:
4841 		case SO_RCVLOWAT:
4842 		case SO_SNDTIMEO:
4843 		case SO_RCVTIMEO:
4844 #endif /* notyet */
4845 		case SO_DOMAIN:
4846 		case SO_DGRAM_ERRIND:
4847 			if (maxlen < (t_uscalar_t)sizeof (int32_t)) {
4848 				error = EINVAL;
4849 				eprintsoline(so, error);
4850 				goto done2;
4851 			}
4852 			break;
4853 		case SO_LINGER:
4854 			if (maxlen < (t_uscalar_t)sizeof (struct linger)) {
4855 				error = EINVAL;
4856 				eprintsoline(so, error);
4857 				goto done2;
4858 			}
4859 			break;
4860 		}
4861 
4862 		len = (t_uscalar_t)sizeof (uint32_t);	/* Default */
4863 
4864 		switch (option_name) {
4865 		case SO_TYPE:
4866 			value = so->so_type;
4867 			option = &value;
4868 			goto copyout; /* No need to issue T_SVR4_OPTMGMT_REQ */
4869 
4870 		case SO_ERROR:
4871 			value = sogeterr(so);
4872 			option = &value;
4873 			goto copyout; /* No need to issue T_SVR4_OPTMGMT_REQ */
4874 
4875 		case SO_ACCEPTCONN:
4876 			if (so->so_state & SS_ACCEPTCONN)
4877 				value = SO_ACCEPTCONN;
4878 			else
4879 				value = 0;
4880 #ifdef DEBUG
4881 			if (value) {
4882 				dprintso(so, 1,
4883 				    ("sotpi_getsockopt: 0x%x is set\n",
4884 				    option_name));
4885 			} else {
4886 				dprintso(so, 1,
4887 				    ("sotpi_getsockopt: 0x%x not set\n",
4888 				    option_name));
4889 			}
4890 #endif /* DEBUG */
4891 			option = &value;
4892 			goto copyout; /* No need to issue T_SVR4_OPTMGMT_REQ */
4893 
4894 		case SO_DEBUG:
4895 		case SO_REUSEADDR:
4896 		case SO_KEEPALIVE:
4897 		case SO_DONTROUTE:
4898 		case SO_BROADCAST:
4899 		case SO_USELOOPBACK:
4900 		case SO_OOBINLINE:
4901 		case SO_DGRAM_ERRIND:
4902 			value = (so->so_options & option_name);
4903 #ifdef DEBUG
4904 			if (value) {
4905 				dprintso(so, 1,
4906 				    ("sotpi_getsockopt: 0x%x is set\n",
4907 				    option_name));
4908 			} else {
4909 				dprintso(so, 1,
4910 				    ("sotpi_getsockopt: 0x%x not set\n",
4911 				    option_name));
4912 			}
4913 #endif /* DEBUG */
4914 			option = &value;
4915 			goto copyout; /* No need to issue T_SVR4_OPTMGMT_REQ */
4916 
4917 		/*
4918 		 * The following options are only returned by sockfs when the
4919 		 * T_SVR4_OPTMGMT_REQ fails.
4920 		 */
4921 		case SO_LINGER:
4922 			option = &so->so_linger;
4923 			len = (t_uscalar_t)sizeof (struct linger);
4924 			break;
4925 		case SO_SNDBUF: {
4926 			ssize_t lvalue;
4927 
4928 			/*
4929 			 * If the option has not been set then get a default
4930 			 * value from the read queue. This value is
4931 			 * returned if the transport fails
4932 			 * the T_SVR4_OPTMGMT_REQ.
4933 			 */
4934 			lvalue = so->so_sndbuf;
4935 			if (lvalue == 0) {
4936 				mutex_exit(&so->so_lock);
4937 				(void) strqget(strvp2wq(SOTOV(so))->q_next,
4938 				    QHIWAT, 0, &lvalue);
4939 				mutex_enter(&so->so_lock);
4940 				dprintso(so, 1,
4941 				    ("got SO_SNDBUF %ld from q\n", lvalue));
4942 			}
4943 			value = (int)lvalue;
4944 			option = &value;
4945 			len = (t_uscalar_t)sizeof (so->so_sndbuf);
4946 			break;
4947 		}
4948 		case SO_RCVBUF: {
4949 			ssize_t lvalue;
4950 
4951 			/*
4952 			 * If the option has not been set then get a default
4953 			 * value from the read queue. This value is
4954 			 * returned if the transport fails
4955 			 * the T_SVR4_OPTMGMT_REQ.
4956 			 *
4957 			 * XXX If SO_RCVBUF has been set and this is an
4958 			 * XPG 4.2 application then do not ask the transport
4959 			 * since the transport might adjust the value and not
4960 			 * return exactly what was set by the application.
4961 			 * For non-XPG 4.2 application we return the value
4962 			 * that the transport is actually using.
4963 			 */
4964 			lvalue = so->so_rcvbuf;
4965 			if (lvalue == 0) {
4966 				mutex_exit(&so->so_lock);
4967 				(void) strqget(RD(strvp2wq(SOTOV(so))),
4968 				    QHIWAT, 0, &lvalue);
4969 				mutex_enter(&so->so_lock);
4970 				dprintso(so, 1,
4971 				    ("got SO_RCVBUF %ld from q\n", lvalue));
4972 			} else if (flags & _SOGETSOCKOPT_XPG4_2) {
4973 				value = (int)lvalue;
4974 				option = &value;
4975 				goto copyout;	/* skip asking transport */
4976 			}
4977 			value = (int)lvalue;
4978 			option = &value;
4979 			len = (t_uscalar_t)sizeof (so->so_rcvbuf);
4980 			break;
4981 		}
4982 		case SO_DOMAIN:
4983 			value = so->so_family;
4984 			option = &value;
4985 			goto copyout; /* No need to issue T_SVR4_OPTMGMT_REQ */
4986 
4987 #ifdef notyet
4988 		/*
4989 		 * We do not implement the semantics of these options
4990 		 * thus we shouldn't implement the options either.
4991 		 */
4992 		case SO_SNDLOWAT:
4993 			value = so->so_sndlowat;
4994 			option = &value;
4995 			break;
4996 		case SO_RCVLOWAT:
4997 			value = so->so_rcvlowat;
4998 			option = &value;
4999 			break;
5000 		case SO_SNDTIMEO:
5001 			value = so->so_sndtimeo;
5002 			option = &value;
5003 			break;
5004 		case SO_RCVTIMEO:
5005 			value = so->so_rcvtimeo;
5006 			option = &value;
5007 			break;
5008 #endif /* notyet */
5009 		}
5010 	}
5011 
5012 	mutex_exit(&so->so_lock);
5013 
5014 	/* Send request */
5015 	optmgmt_req.PRIM_type = T_SVR4_OPTMGMT_REQ;
5016 	optmgmt_req.MGMT_flags = T_CHECK;
5017 	optmgmt_req.OPT_length = (t_scalar_t)(sizeof (oh) + maxlen);
5018 	optmgmt_req.OPT_offset = (t_scalar_t)sizeof (optmgmt_req);
5019 
5020 	oh.level = level;
5021 	oh.name = option_name;
5022 	oh.len = maxlen;
5023 
5024 	mp = soallocproto3(&optmgmt_req, sizeof (optmgmt_req),
5025 	    &oh, sizeof (oh), NULL, maxlen, 0, _ALLOC_SLEEP);
5026 	/* Let option management work in the presence of data flow control */
5027 	error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0,
5028 	    MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR|MSG_IGNFLOW, 0);
5029 	mp = NULL;
5030 	mutex_enter(&so->so_lock);
5031 	if (error) {
5032 		eprintsoline(so, error);
5033 		goto done2;
5034 	}
5035 	error = sowaitprim(so, T_SVR4_OPTMGMT_REQ, T_OPTMGMT_ACK,
5036 	    (t_uscalar_t)(sizeof (*optmgmt_ack) + sizeof (*opt_res)), &mp, 0);
5037 	if (error) {
5038 		if (option != NULL) {
5039 			/* We have a fallback value */
5040 			error = 0;
5041 			goto copyout;
5042 		}
5043 		eprintsoline(so, error);
5044 		goto done2;
5045 	}
5046 	ASSERT(mp);
5047 	optmgmt_ack = (struct T_optmgmt_ack *)mp->b_rptr;
5048 	opt_res = (struct opthdr *)sogetoff(mp, optmgmt_ack->OPT_offset,
5049 	    optmgmt_ack->OPT_length, __TPI_ALIGN_SIZE);
5050 	if (opt_res == NULL) {
5051 		if (option != NULL) {
5052 			/* We have a fallback value */
5053 			error = 0;
5054 			goto copyout;
5055 		}
5056 		error = EPROTO;
5057 		eprintsoline(so, error);
5058 		goto done;
5059 	}
5060 	option = &opt_res[1];
5061 
5062 	/* check to ensure that the option is within bounds */
5063 	if (((uintptr_t)option + opt_res->len < (uintptr_t)option) ||
5064 	    (uintptr_t)option + opt_res->len > (uintptr_t)mp->b_wptr) {
5065 		if (option != NULL) {
5066 			/* We have a fallback value */
5067 			error = 0;
5068 			goto copyout;
5069 		}
5070 		error = EPROTO;
5071 		eprintsoline(so, error);
5072 		goto done;
5073 	}
5074 
5075 	len = opt_res->len;
5076 
5077 copyout: {
5078 		t_uscalar_t size = MIN(len, maxlen);
5079 		bcopy(option, optval, size);
5080 		bcopy(&size, optlenp, sizeof (size));
5081 	}
5082 done:
5083 	freemsg(mp);
5084 done2:
5085 	so_unlock_single(so, SOLOCKED);
5086 	mutex_exit(&so->so_lock);
5087 	return (error);
5088 }
5089 
5090 /*
5091  * Set socket options. All options are passed down in a T_SVR4_OPTMGMT_REQ.
5092  * SOL_SOCKET options are also recorded in the sonode. A setsockopt for
5093  * SOL_SOCKET options will not fail just because the T_SVR4_OPTMGMT_REQ fails -
5094  * setsockopt has to work even if the transport does not support the option.
5095  */
5096 int
5097 sotpi_setsockopt(struct sonode *so, int level, int option_name,
5098 	const void *optval, t_uscalar_t optlen)
5099 {
5100 	struct T_optmgmt_req	optmgmt_req;
5101 	struct opthdr		oh;
5102 	mblk_t			*mp;
5103 	int			error = 0;
5104 	boolean_t		handled = B_FALSE;
5105 
5106 	dprintso(so, 1, ("sotpi_setsockopt(%p, 0x%x, 0x%x, %p, %d) %s\n",
5107 	    so, level, option_name, optval, optlen,
5108 	    pr_state(so->so_state, so->so_mode)));
5109 
5110 
5111 	/* X/Open requires this check */
5112 	if ((so->so_state & SS_CANTSENDMORE) && !xnet_skip_checks) {
5113 		if (xnet_check_print)
5114 			printf("sockfs: X/Open setsockopt check => EINVAL\n");
5115 		return (EINVAL);
5116 	}
5117 
5118 	/* Caller allocates aligned optval, or passes null */
5119 	ASSERT(((uintptr_t)optval & (sizeof (t_scalar_t) - 1)) == 0);
5120 	/* If optval is null optlen is 0, and vice-versa */
5121 	ASSERT(optval != NULL || optlen == 0);
5122 	ASSERT(optlen != 0 || optval == NULL);
5123 
5124 	mutex_enter(&so->so_lock);
5125 	so_lock_single(so);	/* Set SOLOCKED */
5126 	mutex_exit(&so->so_lock);
5127 
5128 	/*
5129 	 * For SOCKET or TCP level options, try to set it here itself
5130 	 * provided socket has not been popped and we know the tcp
5131 	 * structure (stored in so_priv).
5132 	 */
5133 	if ((level == SOL_SOCKET || level == IPPROTO_TCP) &&
5134 	    (so->so_family == AF_INET || so->so_family == AF_INET6) &&
5135 	    (so->so_version == SOV_SOCKSTREAM) && (so->so_priv != NULL)) {
5136 		tcp_t		*tcp = so->so_priv;
5137 		boolean_t	onoff;
5138 
5139 #define	intvalue	(*(int32_t *)optval)
5140 
5141 		switch (level) {
5142 		case SOL_SOCKET:
5143 			switch (option_name) {		/* Check length param */
5144 			case SO_DEBUG:
5145 			case SO_REUSEADDR:
5146 			case SO_DONTROUTE:
5147 			case SO_BROADCAST:
5148 			case SO_USELOOPBACK:
5149 			case SO_OOBINLINE:
5150 			case SO_DGRAM_ERRIND:
5151 				if (optlen != (t_uscalar_t)sizeof (int32_t)) {
5152 					error = EINVAL;
5153 					eprintsoline(so, error);
5154 					mutex_enter(&so->so_lock);
5155 					goto done2;
5156 				}
5157 				ASSERT(optval);
5158 				onoff = intvalue != 0;
5159 				handled = B_TRUE;
5160 				break;
5161 			case SO_LINGER:
5162 				if (optlen !=
5163 				    (t_uscalar_t)sizeof (struct linger)) {
5164 					error = EINVAL;
5165 					eprintsoline(so, error);
5166 					mutex_enter(&so->so_lock);
5167 					goto done2;
5168 				}
5169 				ASSERT(optval);
5170 				handled = B_TRUE;
5171 				break;
5172 			}
5173 
5174 			switch (option_name) {			/* Do actions */
5175 			case SO_LINGER: {
5176 				struct linger *lgr = (struct linger *)optval;
5177 
5178 				if (lgr->l_onoff) {
5179 					tcp->tcp_linger = 1;
5180 					tcp->tcp_lingertime = lgr->l_linger;
5181 					so->so_linger.l_onoff = SO_LINGER;
5182 					so->so_options |= SO_LINGER;
5183 				} else {
5184 					tcp->tcp_linger = 0;
5185 					tcp->tcp_lingertime = 0;
5186 					so->so_linger.l_onoff = 0;
5187 					so->so_options &= ~SO_LINGER;
5188 				}
5189 				so->so_linger.l_linger = lgr->l_linger;
5190 				handled = B_TRUE;
5191 				break;
5192 			}
5193 			case SO_DEBUG:
5194 				tcp->tcp_debug = onoff;
5195 #ifdef SOCK_TEST
5196 				if (intvalue & 2)
5197 					sock_test_timelimit = 10 * hz;
5198 				else
5199 					sock_test_timelimit = 0;
5200 
5201 				if (intvalue & 4)
5202 					do_useracc = 0;
5203 				else
5204 					do_useracc = 1;
5205 #endif /* SOCK_TEST */
5206 				break;
5207 			case SO_DONTROUTE:
5208 				/*
5209 				 * SO_DONTROUTE, SO_USELOOPBACK and
5210 				 * SO_BROADCAST are only of interest to IP.
5211 				 * We track them here only so
5212 				 * that we can report their current value.
5213 				 */
5214 				tcp->tcp_dontroute = onoff;
5215 				if (onoff)
5216 					so->so_options |= option_name;
5217 				else
5218 					so->so_options &= ~option_name;
5219 				break;
5220 			case SO_USELOOPBACK:
5221 				tcp->tcp_useloopback = onoff;
5222 				if (onoff)
5223 					so->so_options |= option_name;
5224 				else
5225 					so->so_options &= ~option_name;
5226 				break;
5227 			case SO_BROADCAST:
5228 				tcp->tcp_broadcast = onoff;
5229 				if (onoff)
5230 					so->so_options |= option_name;
5231 				else
5232 					so->so_options &= ~option_name;
5233 				break;
5234 			case SO_REUSEADDR:
5235 				tcp->tcp_reuseaddr = onoff;
5236 				if (onoff)
5237 					so->so_options |= option_name;
5238 				else
5239 					so->so_options &= ~option_name;
5240 				break;
5241 			case SO_OOBINLINE:
5242 				tcp->tcp_oobinline = onoff;
5243 				if (onoff)
5244 					so->so_options |= option_name;
5245 				else
5246 					so->so_options &= ~option_name;
5247 				break;
5248 			case SO_DGRAM_ERRIND:
5249 				tcp->tcp_dgram_errind = onoff;
5250 				if (onoff)
5251 					so->so_options |= option_name;
5252 				else
5253 					so->so_options &= ~option_name;
5254 				break;
5255 			}
5256 			break;
5257 		case IPPROTO_TCP:
5258 			switch (option_name) {
5259 			case TCP_NODELAY:
5260 				if (optlen != (t_uscalar_t)sizeof (int32_t)) {
5261 					error = EINVAL;
5262 					eprintsoline(so, error);
5263 					mutex_enter(&so->so_lock);
5264 					goto done2;
5265 				}
5266 				ASSERT(optval);
5267 				tcp->tcp_naglim = intvalue ? 1 : tcp->tcp_mss;
5268 				handled = B_TRUE;
5269 				break;
5270 			}
5271 			break;
5272 		default:
5273 			handled = B_FALSE;
5274 			break;
5275 		}
5276 	}
5277 
5278 	if (handled) {
5279 		mutex_enter(&so->so_lock);
5280 		goto done2;
5281 	}
5282 
5283 	optmgmt_req.PRIM_type = T_SVR4_OPTMGMT_REQ;
5284 	optmgmt_req.MGMT_flags = T_NEGOTIATE;
5285 	optmgmt_req.OPT_length = (t_scalar_t)sizeof (oh) + optlen;
5286 	optmgmt_req.OPT_offset = (t_scalar_t)sizeof (optmgmt_req);
5287 
5288 	oh.level = level;
5289 	oh.name = option_name;
5290 	oh.len = optlen;
5291 
5292 	mp = soallocproto3(&optmgmt_req, sizeof (optmgmt_req),
5293 	    &oh, sizeof (oh), optval, optlen, 0, _ALLOC_SLEEP);
5294 	/* Let option management work in the presence of data flow control */
5295 	error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0,
5296 	    MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR|MSG_IGNFLOW, 0);
5297 	mp = NULL;
5298 	mutex_enter(&so->so_lock);
5299 	if (error) {
5300 		eprintsoline(so, error);
5301 		goto done;
5302 	}
5303 	error = sowaitprim(so, T_SVR4_OPTMGMT_REQ, T_OPTMGMT_ACK,
5304 	    (t_uscalar_t)sizeof (struct T_optmgmt_ack), &mp, 0);
5305 	if (error) {
5306 		eprintsoline(so, error);
5307 		goto done;
5308 	}
5309 	ASSERT(mp);
5310 	/* No need to verify T_optmgmt_ack */
5311 	freemsg(mp);
5312 done:
5313 	/*
5314 	 * Check for SOL_SOCKET options and record their values.
5315 	 * If we know about a SOL_SOCKET parameter and the transport
5316 	 * failed it with TBADOPT or TOUTSTATE (i.e. ENOPROTOOPT or
5317 	 * EPROTO) we let the setsockopt succeed.
5318 	 */
5319 	if (level == SOL_SOCKET) {
5320 		/* Check parameters */
5321 		switch (option_name) {
5322 		case SO_DEBUG:
5323 		case SO_REUSEADDR:
5324 		case SO_KEEPALIVE:
5325 		case SO_DONTROUTE:
5326 		case SO_BROADCAST:
5327 		case SO_USELOOPBACK:
5328 		case SO_OOBINLINE:
5329 		case SO_SNDBUF:
5330 		case SO_RCVBUF:
5331 #ifdef notyet
5332 		case SO_SNDLOWAT:
5333 		case SO_RCVLOWAT:
5334 		case SO_SNDTIMEO:
5335 		case SO_RCVTIMEO:
5336 #endif /* notyet */
5337 		case SO_DGRAM_ERRIND:
5338 			if (optlen != (t_uscalar_t)sizeof (int32_t)) {
5339 				error = EINVAL;
5340 				eprintsoline(so, error);
5341 				goto done2;
5342 			}
5343 			ASSERT(optval);
5344 			handled = B_TRUE;
5345 			break;
5346 		case SO_LINGER:
5347 			if (optlen != (t_uscalar_t)sizeof (struct linger)) {
5348 				error = EINVAL;
5349 				eprintsoline(so, error);
5350 				goto done2;
5351 			}
5352 			ASSERT(optval);
5353 			handled = B_TRUE;
5354 			break;
5355 		}
5356 
5357 #define	intvalue	(*(int32_t *)optval)
5358 
5359 		switch (option_name) {
5360 		case SO_TYPE:
5361 		case SO_ERROR:
5362 		case SO_ACCEPTCONN:
5363 			/* Can't be set */
5364 			error = ENOPROTOOPT;
5365 			goto done2;
5366 		case SO_LINGER: {
5367 			struct linger *l = (struct linger *)optval;
5368 
5369 			so->so_linger.l_linger = l->l_linger;
5370 			if (l->l_onoff) {
5371 				so->so_linger.l_onoff = SO_LINGER;
5372 				so->so_options |= SO_LINGER;
5373 			} else {
5374 				so->so_linger.l_onoff = 0;
5375 				so->so_options &= ~SO_LINGER;
5376 			}
5377 			break;
5378 		}
5379 
5380 		case SO_DEBUG:
5381 #ifdef SOCK_TEST
5382 			if (intvalue & 2)
5383 				sock_test_timelimit = 10 * hz;
5384 			else
5385 				sock_test_timelimit = 0;
5386 
5387 			if (intvalue & 4)
5388 				do_useracc = 0;
5389 			else
5390 				do_useracc = 1;
5391 #endif /* SOCK_TEST */
5392 			/* FALLTHRU */
5393 		case SO_REUSEADDR:
5394 		case SO_KEEPALIVE:
5395 		case SO_DONTROUTE:
5396 		case SO_BROADCAST:
5397 		case SO_USELOOPBACK:
5398 		case SO_OOBINLINE:
5399 		case SO_DGRAM_ERRIND:
5400 			if (intvalue != 0) {
5401 				dprintso(so, 1,
5402 				    ("sotpi_setsockopt: setting 0x%x\n",
5403 				    option_name));
5404 				so->so_options |= option_name;
5405 			} else {
5406 				dprintso(so, 1,
5407 				    ("sotpi_setsockopt: clearing 0x%x\n",
5408 				    option_name));
5409 				so->so_options &= ~option_name;
5410 			}
5411 			break;
5412 		/*
5413 		 * The following options are only returned by us when the
5414 		 * T_SVR4_OPTMGMT_REQ fails.
5415 		 * XXX XPG 4.2 applications retrieve SO_RCVBUF from sockfs
5416 		 * since the transport might adjust the value and not
5417 		 * return exactly what was set by the application.
5418 		 */
5419 		case SO_SNDBUF:
5420 			so->so_sndbuf = intvalue;
5421 			break;
5422 		case SO_RCVBUF:
5423 			so->so_rcvbuf = intvalue;
5424 			break;
5425 #ifdef notyet
5426 		/*
5427 		 * We do not implement the semantics of these options
5428 		 * thus we shouldn't implement the options either.
5429 		 */
5430 		case SO_SNDLOWAT:
5431 			so->so_sndlowat = intvalue;
5432 			break;
5433 		case SO_RCVLOWAT:
5434 			so->so_rcvlowat = intvalue;
5435 			break;
5436 		case SO_SNDTIMEO:
5437 			so->so_sndtimeo = intvalue;
5438 			break;
5439 		case SO_RCVTIMEO:
5440 			so->so_rcvtimeo = intvalue;
5441 			break;
5442 #endif /* notyet */
5443 		}
5444 #undef	intvalue
5445 
5446 		if (error) {
5447 			if ((error == ENOPROTOOPT || error == EPROTO ||
5448 			    error == EINVAL) && handled) {
5449 				dprintso(so, 1,
5450 				    ("setsockopt: ignoring error %d for 0x%x\n",
5451 				    error, option_name));
5452 				error = 0;
5453 			}
5454 		}
5455 	}
5456 done2:
5457 ret:
5458 	so_unlock_single(so, SOLOCKED);
5459 	mutex_exit(&so->so_lock);
5460 	return (error);
5461 }
5462