xref: /titanic_44/usr/src/uts/common/fs/sockfs/socktpi.c (revision a776e90690d98f629ab57f6493ff5949af9d65bd)
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 there is a preallocated T_unbind_req message
1976 	 * before any binding. This message is allocated when the
1977 	 * socket is created. Since another thread can consume
1978 	 * so_unbind_mp by the time we return from so_lock_single(),
1979 	 * we should check the availability of so_unbind_mp after
1980 	 * we return from so_lock_single().
1981 	 */
1982 
1983 	so_lock_single(so);	/* Set SOLOCKED */
1984 	need_unlock = B_TRUE;
1985 
1986 	if (so->so_unbind_mp == NULL) {
1987 		dprintso(so, 1, ("sotpi_connect: allocating unbind_req\n"));
1988 		/* NOTE: holding so_lock while sleeping */
1989 		so->so_unbind_mp =
1990 		    soallocproto(sizeof (struct T_unbind_req), _ALLOC_INTR);
1991 		if (so->so_unbind_mp == NULL) {
1992 			error = EINTR;
1993 			goto done;
1994 		}
1995 	}
1996 
1997 	/*
1998 	 * Can't have done a listen before connecting.
1999 	 */
2000 	if (so->so_state & SS_ACCEPTCONN) {
2001 		error = EOPNOTSUPP;
2002 		goto done;
2003 	}
2004 
2005 	/*
2006 	 * Must be bound with the transport
2007 	 */
2008 	if (!(so->so_state & SS_ISBOUND)) {
2009 		if ((so->so_family == AF_INET || so->so_family == AF_INET6) &&
2010 		    /*CONSTCOND*/
2011 		    so->so_type == SOCK_STREAM && !soconnect_tpi_tcp) {
2012 			/*
2013 			 * Optimization for AF_INET{,6} transports
2014 			 * that can handle a T_CONN_REQ without being bound.
2015 			 */
2016 			so_automatic_bind(so);
2017 		} else {
2018 			error = sotpi_bind(so, NULL, 0,
2019 			    _SOBIND_UNSPEC|_SOBIND_LOCK_HELD);
2020 			if (error)
2021 				goto done;
2022 		}
2023 		ASSERT(so->so_state & SS_ISBOUND);
2024 		flags |= _SOCONNECT_DID_BIND;
2025 	}
2026 
2027 	/*
2028 	 * Handle a connect to a name parameter of type AF_UNSPEC like a
2029 	 * connect to a null address. This is the portable method to
2030 	 * unconnect a socket.
2031 	 */
2032 	if ((namelen >= sizeof (sa_family_t)) &&
2033 	    (name->sa_family == AF_UNSPEC)) {
2034 		name = NULL;
2035 		namelen = 0;
2036 	}
2037 
2038 	/*
2039 	 * Check that we are not already connected.
2040 	 * A connection-oriented socket cannot be reconnected.
2041 	 * A connected connection-less socket can be
2042 	 * - connected to a different address by a subsequent connect
2043 	 * - "unconnected" by a connect to the NULL address
2044 	 */
2045 	if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) {
2046 		ASSERT(!(flags & _SOCONNECT_DID_BIND));
2047 		if (so->so_mode & SM_CONNREQUIRED) {
2048 			/* Connection-oriented socket */
2049 			error = so->so_state & SS_ISCONNECTED ?
2050 			    EISCONN : EALREADY;
2051 			goto done;
2052 		}
2053 		/* Connection-less socket */
2054 		if (name == NULL) {
2055 			/*
2056 			 * Remove the connected state and clear SO_DGRAM_ERRIND
2057 			 * since it was set when the socket was connected.
2058 			 * If this is UDP also send down a T_DISCON_REQ.
2059 			 */
2060 			int val;
2061 
2062 			if ((so->so_family == AF_INET ||
2063 			    so->so_family == AF_INET6) &&
2064 			    (so->so_type == SOCK_DGRAM ||
2065 			    so->so_type == SOCK_RAW) &&
2066 			    /*CONSTCOND*/
2067 			    !soconnect_tpi_udp) {
2068 				/* XXX What about implicitly unbinding here? */
2069 				error = sodisconnect(so, -1,
2070 				    _SODISCONNECT_LOCK_HELD);
2071 			} else {
2072 				so->so_state &=
2073 				    ~(SS_ISCONNECTED | SS_ISCONNECTING |
2074 				    SS_FADDR_VALID);
2075 				so->so_faddr_len = 0;
2076 			}
2077 
2078 			so_unlock_single(so, SOLOCKED);
2079 			mutex_exit(&so->so_lock);
2080 
2081 			val = 0;
2082 			(void) sotpi_setsockopt(so, SOL_SOCKET, SO_DGRAM_ERRIND,
2083 			    &val, (t_uscalar_t)sizeof (val));
2084 
2085 			mutex_enter(&so->so_lock);
2086 			so_lock_single(so);	/* Set SOLOCKED */
2087 			goto done;
2088 		}
2089 	}
2090 	ASSERT(so->so_state & SS_ISBOUND);
2091 
2092 	if (name == NULL || namelen == 0) {
2093 		error = EINVAL;
2094 		goto done;
2095 	}
2096 	/*
2097 	 * Mark the socket if so_faddr_sa represents the transport level
2098 	 * address.
2099 	 */
2100 	if (flags & _SOCONNECT_NOXLATE) {
2101 		struct sockaddr_ux	*soaddr_ux;
2102 
2103 		ASSERT(so->so_family == AF_UNIX);
2104 		if (namelen != sizeof (struct sockaddr_ux)) {
2105 			error = EINVAL;
2106 			goto done;
2107 		}
2108 		soaddr_ux = (struct sockaddr_ux *)name;
2109 		name = (struct sockaddr *)&soaddr_ux->sou_addr;
2110 		namelen = sizeof (soaddr_ux->sou_addr);
2111 		so->so_state |= SS_FADDR_NOXLATE;
2112 	}
2113 
2114 	/*
2115 	 * Length and family checks.
2116 	 */
2117 	error = so_addr_verify(so, name, namelen);
2118 	if (error)
2119 		goto bad;
2120 
2121 	/*
2122 	 * Save foreign address. Needed for AF_UNIX as well as
2123 	 * transport providers that do not support TI_GETPEERNAME.
2124 	 * Also used for cached foreign address for TCP and UDP.
2125 	 */
2126 	if (namelen > (t_uscalar_t)so->so_faddr_maxlen) {
2127 		error = EINVAL;
2128 		goto done;
2129 	}
2130 	so->so_faddr_len = (socklen_t)namelen;
2131 	ASSERT(so->so_faddr_len <= so->so_faddr_maxlen);
2132 	bcopy(name, so->so_faddr_sa, namelen);
2133 	so->so_state |= SS_FADDR_VALID;
2134 
2135 	if (so->so_family == AF_UNIX) {
2136 		if (so->so_state & SS_FADDR_NOXLATE) {
2137 			/*
2138 			 * Already have a transport internal address. Do not
2139 			 * pass any (transport internal) source address.
2140 			 */
2141 			addr = so->so_faddr_sa;
2142 			addrlen = (t_uscalar_t)so->so_faddr_len;
2143 			src = NULL;
2144 			srclen = 0;
2145 		} else {
2146 			/*
2147 			 * Pass the sockaddr_un source address as an option
2148 			 * and translate the remote address.
2149 			 * Holding so_lock thus so_laddr_sa can not change.
2150 			 */
2151 			src = so->so_laddr_sa;
2152 			srclen = (t_uscalar_t)so->so_laddr_len;
2153 			dprintso(so, 1,
2154 			    ("sotpi_connect UNIX: srclen %d, src %p\n",
2155 			    srclen, src));
2156 			error = so_ux_addr_xlate(so,
2157 			    so->so_faddr_sa, (socklen_t)so->so_faddr_len,
2158 			    (flags & _SOCONNECT_XPG4_2),
2159 			    &addr, &addrlen);
2160 			if (error)
2161 				goto bad;
2162 		}
2163 	} else {
2164 		addr = so->so_faddr_sa;
2165 		addrlen = (t_uscalar_t)so->so_faddr_len;
2166 		src = NULL;
2167 		srclen = 0;
2168 	}
2169 	/*
2170 	 * When connecting a datagram socket we issue the SO_DGRAM_ERRIND
2171 	 * option which asks the transport provider to send T_UDERR_IND
2172 	 * messages. These T_UDERR_IND messages are used to return connected
2173 	 * style errors (e.g. ECONNRESET) for connected datagram sockets.
2174 	 *
2175 	 * In addition, for UDP (and SOCK_RAW AF_INET{,6} sockets)
2176 	 * we send down a T_CONN_REQ. This is needed to let the
2177 	 * transport assign a local address that is consistent with
2178 	 * the remote address. Applications depend on a getsockname()
2179 	 * after a connect() to retrieve the "source" IP address for
2180 	 * the connected socket.  Invalidate the cached local address
2181 	 * to force getsockname() to enquire of the transport.
2182 	 */
2183 	if (!(so->so_mode & SM_CONNREQUIRED)) {
2184 		/*
2185 		 * Datagram socket.
2186 		 */
2187 		int32_t val;
2188 
2189 		so_unlock_single(so, SOLOCKED);
2190 		mutex_exit(&so->so_lock);
2191 
2192 		val = 1;
2193 		(void) sotpi_setsockopt(so, SOL_SOCKET, SO_DGRAM_ERRIND,
2194 		    &val, (t_uscalar_t)sizeof (val));
2195 
2196 		mutex_enter(&so->so_lock);
2197 		so_lock_single(so);	/* Set SOLOCKED */
2198 		if ((so->so_family != AF_INET && so->so_family != AF_INET6) ||
2199 		    (so->so_type != SOCK_DGRAM && so->so_type != SOCK_RAW) ||
2200 		    soconnect_tpi_udp) {
2201 			soisconnected(so);
2202 			goto done;
2203 		}
2204 		/*
2205 		 * Send down T_CONN_REQ etc.
2206 		 * Clear fflag to avoid returning EWOULDBLOCK.
2207 		 */
2208 		fflag = 0;
2209 		ASSERT(so->so_family != AF_UNIX);
2210 		so->so_state &= ~SS_LADDR_VALID;
2211 	} else if (so->so_laddr_len != 0) {
2212 		/*
2213 		 * If the local address or port was "any" then it may be
2214 		 * changed by the transport as a result of the
2215 		 * connect.  Invalidate the cached version if we have one.
2216 		 */
2217 		switch (so->so_family) {
2218 		case AF_INET:
2219 			ASSERT(so->so_laddr_len == (socklen_t)sizeof (sin_t));
2220 			if (((sin_t *)so->so_laddr_sa)->sin_addr.s_addr ==
2221 			    INADDR_ANY ||
2222 			    ((sin_t *)so->so_laddr_sa)->sin_port == 0)
2223 				so->so_state &= ~SS_LADDR_VALID;
2224 			break;
2225 
2226 		case AF_INET6:
2227 			ASSERT(so->so_laddr_len == (socklen_t)sizeof (sin6_t));
2228 			if (IN6_IS_ADDR_UNSPECIFIED(
2229 			    &((sin6_t *)so->so_laddr_sa) ->sin6_addr) ||
2230 			    IN6_IS_ADDR_V4MAPPED_ANY(
2231 			    &((sin6_t *)so->so_laddr_sa)->sin6_addr) ||
2232 			    ((sin6_t *)so->so_laddr_sa)->sin6_port == 0)
2233 				so->so_state &= ~SS_LADDR_VALID;
2234 			break;
2235 
2236 		default:
2237 			break;
2238 		}
2239 	}
2240 
2241 	/*
2242 	 * Check for failure of an earlier call
2243 	 */
2244 	if (so->so_error != 0)
2245 		goto so_bad;
2246 
2247 	/*
2248 	 * Send down T_CONN_REQ. Message was allocated above.
2249 	 */
2250 	conn_req.PRIM_type = T_CONN_REQ;
2251 	conn_req.DEST_length = addrlen;
2252 	conn_req.DEST_offset = (t_scalar_t)sizeof (conn_req);
2253 	if (srclen == 0) {
2254 		conn_req.OPT_length = 0;
2255 		conn_req.OPT_offset = 0;
2256 		soappendmsg(mp, &conn_req, sizeof (conn_req));
2257 		soappendmsg(mp, addr, addrlen);
2258 	} else {
2259 		/*
2260 		 * There is a AF_UNIX sockaddr_un to include as a source
2261 		 * address option.
2262 		 */
2263 		struct T_opthdr toh;
2264 
2265 		toh.level = SOL_SOCKET;
2266 		toh.name = SO_SRCADDR;
2267 		toh.len = (t_uscalar_t)(srclen + sizeof (struct T_opthdr));
2268 		toh.status = 0;
2269 		conn_req.OPT_length =
2270 		    (t_scalar_t)(sizeof (toh) + _TPI_ALIGN_TOPT(srclen));
2271 		conn_req.OPT_offset = (t_scalar_t)(sizeof (conn_req) +
2272 		    _TPI_ALIGN_TOPT(addrlen));
2273 
2274 		soappendmsg(mp, &conn_req, sizeof (conn_req));
2275 		soappendmsg(mp, addr, addrlen);
2276 		mp->b_wptr += _TPI_ALIGN_TOPT(addrlen) - addrlen;
2277 		soappendmsg(mp, &toh, sizeof (toh));
2278 		soappendmsg(mp, src, srclen);
2279 		mp->b_wptr += _TPI_ALIGN_TOPT(srclen) - srclen;
2280 		ASSERT(mp->b_wptr <= mp->b_datap->db_lim);
2281 	}
2282 	/*
2283 	 * Set SS_ISCONNECTING before sending down the T_CONN_REQ
2284 	 * in order to have the right state when the T_CONN_CON shows up.
2285 	 */
2286 	soisconnecting(so);
2287 	mutex_exit(&so->so_lock);
2288 
2289 	if (audit_active)
2290 		audit_sock(T_CONN_REQ, strvp2wq(SOTOV(so)), mp, 0);
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 	if (audit_active)
3666 		audit_sock(T_UNITDATA_REQ, strvp2wq(SOTOV(so)), mp, 0);
3667 
3668 	error = kstrputmsg(SOTOV(so), mp, uiop, len, 0, MSG_BAND, 0);
3669 #ifdef SOCK_DEBUG
3670 	if (error) {
3671 		eprintsoline(so, error);
3672 	}
3673 #endif /* SOCK_DEBUG */
3674 	return (error);
3675 }
3676 
3677 /*
3678  * Sending data with options on a connected stream socket.
3679  * Assumes caller has verified that SS_ISCONNECTED is set.
3680  */
3681 static int
3682 sosend_svccmsg(struct sonode *so,
3683 		struct uio *uiop,
3684 		int more,
3685 		void *control,
3686 		t_uscalar_t controllen,
3687 		int flags)
3688 {
3689 	struct T_optdata_req	tdr;
3690 	mblk_t			*mp;
3691 	int			error;
3692 	ssize_t			iosize;
3693 	int			first = 1;
3694 	int			size;
3695 	struct fdbuf		*fdbuf;
3696 	t_uscalar_t		optlen;
3697 	void			*fds;
3698 	int			fdlen;
3699 	struct T_opthdr		toh;
3700 
3701 	dprintso(so, 1,
3702 	    ("sosend_svccmsg: resid %ld bytes\n", uiop->uio_resid));
3703 
3704 	/*
3705 	 * Has to be bound and connected. However, since no locks are
3706 	 * held the state could have changed after sotpi_sendmsg checked it
3707 	 * thus it is not possible to ASSERT on the state.
3708 	 */
3709 
3710 	/* Options on connection-oriented only when SM_OPTDATA set. */
3711 	if (!(so->so_mode & SM_OPTDATA))
3712 		return (EOPNOTSUPP);
3713 
3714 	do {
3715 		/*
3716 		 * Set the MORE flag if uio_resid does not fit in this
3717 		 * message or if the caller passed in "more".
3718 		 * Error for transports with zero tidu_size.
3719 		 */
3720 		tdr.PRIM_type = T_OPTDATA_REQ;
3721 		iosize = so->so_tidu_size;
3722 		if (iosize <= 0)
3723 			return (EMSGSIZE);
3724 		if (uiop->uio_resid > iosize) {
3725 			tdr.DATA_flag = 1;
3726 		} else {
3727 			if (more)
3728 				tdr.DATA_flag = 1;
3729 			else
3730 				tdr.DATA_flag = 0;
3731 			iosize = uiop->uio_resid;
3732 		}
3733 		dprintso(so, 1, ("sosend_svccmsg: sending %d, %ld bytes\n",
3734 		    tdr.DATA_flag, iosize));
3735 
3736 		optlen = so_optlen(control, controllen, !(flags & MSG_XPG4_2));
3737 		tdr.OPT_length = optlen;
3738 		tdr.OPT_offset = (t_scalar_t)sizeof (tdr);
3739 
3740 		size = (int)sizeof (tdr) + optlen;
3741 		/*
3742 		 * File descriptors only when SM_FDPASSING set.
3743 		 */
3744 		error = so_getfdopt(control, controllen,
3745 		    !(flags & MSG_XPG4_2), &fds, &fdlen);
3746 		if (error)
3747 			return (error);
3748 		if (fdlen != -1) {
3749 			if (!(so->so_mode & SM_FDPASSING))
3750 				return (EOPNOTSUPP);
3751 
3752 			error = fdbuf_create(fds, fdlen, &fdbuf);
3753 			if (error)
3754 				return (error);
3755 			mp = fdbuf_allocmsg(size, fdbuf);
3756 		} else {
3757 			mp = soallocproto(size, _ALLOC_INTR);
3758 			if (mp == NULL) {
3759 				/*
3760 				 * Caught a signal waiting for memory.
3761 				 * Let send* return EINTR.
3762 				 */
3763 				return (first ? EINTR : 0);
3764 			}
3765 		}
3766 		soappendmsg(mp, &tdr, sizeof (tdr));
3767 
3768 		if (fdlen != -1) {
3769 			ASSERT(fdbuf != NULL);
3770 			toh.level = SOL_SOCKET;
3771 			toh.name = SO_FILEP;
3772 			toh.len = fdbuf->fd_size +
3773 			    (t_uscalar_t)sizeof (struct T_opthdr);
3774 			toh.status = 0;
3775 			soappendmsg(mp, &toh, sizeof (toh));
3776 			soappendmsg(mp, fdbuf, fdbuf->fd_size);
3777 			ASSERT(__TPI_TOPT_ISALIGNED(mp->b_wptr));
3778 		}
3779 		so_cmsg2opt(control, controllen, !(flags & MSG_XPG4_2), mp);
3780 		/* At most 3 bytes left in the message */
3781 		ASSERT(MBLKL(mp) > (ssize_t)(size - __TPI_ALIGN_SIZE));
3782 		ASSERT(MBLKL(mp) <= (ssize_t)size);
3783 
3784 		ASSERT(mp->b_wptr <= mp->b_datap->db_lim);
3785 
3786 		error = kstrputmsg(SOTOV(so), mp, uiop, iosize,
3787 		    0, MSG_BAND, 0);
3788 		if (error) {
3789 			if (!first && error == EWOULDBLOCK)
3790 				return (0);
3791 			eprintsoline(so, error);
3792 			return (error);
3793 		}
3794 		control = NULL;
3795 		first = 0;
3796 		if (uiop->uio_resid > 0) {
3797 			/*
3798 			 * Recheck for fatal errors. Fail write even though
3799 			 * some data have been written. This is consistent
3800 			 * with strwrite semantics and BSD sockets semantics.
3801 			 */
3802 			if (so->so_state & SS_CANTSENDMORE) {
3803 				tsignal(curthread, SIGPIPE);
3804 				eprintsoline(so, error);
3805 				return (EPIPE);
3806 			}
3807 			if (so->so_error != 0) {
3808 				mutex_enter(&so->so_lock);
3809 				error = sogeterr(so);
3810 				mutex_exit(&so->so_lock);
3811 				if (error != 0) {
3812 					eprintsoline(so, error);
3813 					return (error);
3814 				}
3815 			}
3816 		}
3817 	} while (uiop->uio_resid > 0);
3818 	return (0);
3819 }
3820 
3821 /*
3822  * Sending data on a datagram socket.
3823  * Assumes caller has verified that SS_ISBOUND etc. are set.
3824  *
3825  * For AF_UNIX the destination address is translated to an internal
3826  * name and the source address is passed as an option.
3827  */
3828 int
3829 sosend_dgram(struct sonode *so, struct sockaddr	*name, socklen_t namelen,
3830     struct uio *uiop, int flags)
3831 {
3832 	struct T_unitdata_req	tudr;
3833 	mblk_t			*mp;
3834 	int			error;
3835 	void			*addr;
3836 	socklen_t		addrlen;
3837 	void			*src;
3838 	socklen_t		srclen;
3839 	ssize_t			len;
3840 
3841 	ASSERT(name != NULL && namelen != 0);
3842 
3843 	len = uiop->uio_resid;
3844 	if (len > so->so_tidu_size) {
3845 		error = EMSGSIZE;
3846 		goto done;
3847 	}
3848 
3849 	/* Length and family checks */
3850 	error = so_addr_verify(so, name, namelen);
3851 	if (error != 0)
3852 		goto done;
3853 
3854 	if (so->so_state & SS_DIRECT)
3855 		return (sodgram_direct(so, name, namelen, uiop, flags));
3856 
3857 	if (so->so_family == AF_UNIX) {
3858 		if (so->so_state & SS_FADDR_NOXLATE) {
3859 			/*
3860 			 * Already have a transport internal address. Do not
3861 			 * pass any (transport internal) source address.
3862 			 */
3863 			addr = name;
3864 			addrlen = namelen;
3865 			src = NULL;
3866 			srclen = 0;
3867 		} else {
3868 			/*
3869 			 * Pass the sockaddr_un source address as an option
3870 			 * and translate the remote address.
3871 			 *
3872 			 * Note that this code does not prevent so_laddr_sa
3873 			 * from changing while it is being used. Thus
3874 			 * if an unbind+bind occurs concurrently with this
3875 			 * send the peer might see a partially new and a
3876 			 * partially old "from" address.
3877 			 */
3878 			src = so->so_laddr_sa;
3879 			srclen = (socklen_t)so->so_laddr_len;
3880 			dprintso(so, 1,
3881 			    ("sosend_dgram UNIX: srclen %d, src %p\n",
3882 			    srclen, src));
3883 			error = so_ux_addr_xlate(so, name, namelen,
3884 			    (flags & MSG_XPG4_2),
3885 			    &addr, &addrlen);
3886 			if (error) {
3887 				eprintsoline(so, error);
3888 				goto done;
3889 			}
3890 		}
3891 	} else {
3892 		addr = name;
3893 		addrlen = namelen;
3894 		src = NULL;
3895 		srclen = 0;
3896 	}
3897 	tudr.PRIM_type = T_UNITDATA_REQ;
3898 	tudr.DEST_length = addrlen;
3899 	tudr.DEST_offset = (t_scalar_t)sizeof (tudr);
3900 	if (srclen == 0) {
3901 		tudr.OPT_length = 0;
3902 		tudr.OPT_offset = 0;
3903 
3904 		mp = soallocproto2(&tudr, sizeof (tudr),
3905 		    addr, addrlen, 0, _ALLOC_INTR);
3906 		if (mp == NULL) {
3907 			/*
3908 			 * Caught a signal waiting for memory.
3909 			 * Let send* return EINTR.
3910 			 */
3911 			error = EINTR;
3912 			goto done;
3913 		}
3914 	} else {
3915 		/*
3916 		 * There is a AF_UNIX sockaddr_un to include as a source
3917 		 * address option.
3918 		 */
3919 		struct T_opthdr toh;
3920 		ssize_t size;
3921 
3922 		tudr.OPT_length = (t_scalar_t)(sizeof (toh) +
3923 		    _TPI_ALIGN_TOPT(srclen));
3924 		tudr.OPT_offset = (t_scalar_t)(sizeof (tudr) +
3925 		    _TPI_ALIGN_TOPT(addrlen));
3926 
3927 		toh.level = SOL_SOCKET;
3928 		toh.name = SO_SRCADDR;
3929 		toh.len = (t_uscalar_t)(srclen + sizeof (struct T_opthdr));
3930 		toh.status = 0;
3931 
3932 		size = tudr.OPT_offset + tudr.OPT_length;
3933 		mp = soallocproto2(&tudr, sizeof (tudr),
3934 		    addr, addrlen, size, _ALLOC_INTR);
3935 		if (mp == NULL) {
3936 			/*
3937 			 * Caught a signal waiting for memory.
3938 			 * Let send* return EINTR.
3939 			 */
3940 			error = EINTR;
3941 			goto done;
3942 		}
3943 		mp->b_wptr += _TPI_ALIGN_TOPT(addrlen) - addrlen;
3944 		soappendmsg(mp, &toh, sizeof (toh));
3945 		soappendmsg(mp, src, srclen);
3946 		mp->b_wptr += _TPI_ALIGN_TOPT(srclen) - srclen;
3947 		ASSERT(mp->b_wptr <= mp->b_datap->db_lim);
3948 	}
3949 
3950 	if (audit_active)
3951 		audit_sock(T_UNITDATA_REQ, strvp2wq(SOTOV(so)), mp, 0);
3952 
3953 	error = kstrputmsg(SOTOV(so), mp, uiop, len, 0, MSG_BAND, 0);
3954 done:
3955 #ifdef SOCK_DEBUG
3956 	if (error) {
3957 		eprintsoline(so, error);
3958 	}
3959 #endif /* SOCK_DEBUG */
3960 	return (error);
3961 }
3962 
3963 /*
3964  * Sending data on a connected stream socket.
3965  * Assumes caller has verified that SS_ISCONNECTED is set.
3966  */
3967 int
3968 sosend_svc(struct sonode *so,
3969 	struct uio *uiop,
3970 	t_scalar_t prim,
3971 	int more,
3972 	int sflag)
3973 {
3974 	struct T_data_req	tdr;
3975 	mblk_t			*mp;
3976 	int			error;
3977 	ssize_t			iosize;
3978 	int			first = 1;
3979 
3980 	dprintso(so, 1,
3981 	    ("sosend_svc: %p, resid %ld bytes, prim %d, sflag 0x%x\n",
3982 	    so, uiop->uio_resid, prim, sflag));
3983 
3984 	/*
3985 	 * Has to be bound and connected. However, since no locks are
3986 	 * held the state could have changed after sotpi_sendmsg checked it
3987 	 * thus it is not possible to ASSERT on the state.
3988 	 */
3989 
3990 	do {
3991 		/*
3992 		 * Set the MORE flag if uio_resid does not fit in this
3993 		 * message or if the caller passed in "more".
3994 		 * Error for transports with zero tidu_size.
3995 		 */
3996 		tdr.PRIM_type = prim;
3997 		iosize = so->so_tidu_size;
3998 		if (iosize <= 0)
3999 			return (EMSGSIZE);
4000 		if (uiop->uio_resid > iosize) {
4001 			tdr.MORE_flag = 1;
4002 		} else {
4003 			if (more)
4004 				tdr.MORE_flag = 1;
4005 			else
4006 				tdr.MORE_flag = 0;
4007 			iosize = uiop->uio_resid;
4008 		}
4009 		dprintso(so, 1, ("sosend_svc: sending 0x%x %d, %ld bytes\n",
4010 		    prim, tdr.MORE_flag, iosize));
4011 		mp = soallocproto1(&tdr, sizeof (tdr), 0, _ALLOC_INTR);
4012 		if (mp == NULL) {
4013 			/*
4014 			 * Caught a signal waiting for memory.
4015 			 * Let send* return EINTR.
4016 			 */
4017 			if (first)
4018 				return (EINTR);
4019 			else
4020 				return (0);
4021 		}
4022 
4023 		error = kstrputmsg(SOTOV(so), mp, uiop, iosize,
4024 		    0, sflag | MSG_BAND, 0);
4025 		if (error) {
4026 			if (!first && error == EWOULDBLOCK)
4027 				return (0);
4028 			eprintsoline(so, error);
4029 			return (error);
4030 		}
4031 		first = 0;
4032 		if (uiop->uio_resid > 0) {
4033 			/*
4034 			 * Recheck for fatal errors. Fail write even though
4035 			 * some data have been written. This is consistent
4036 			 * with strwrite semantics and BSD sockets semantics.
4037 			 */
4038 			if (so->so_state & SS_CANTSENDMORE) {
4039 				tsignal(curthread, SIGPIPE);
4040 				eprintsoline(so, error);
4041 				return (EPIPE);
4042 			}
4043 			if (so->so_error != 0) {
4044 				mutex_enter(&so->so_lock);
4045 				error = sogeterr(so);
4046 				mutex_exit(&so->so_lock);
4047 				if (error != 0) {
4048 					eprintsoline(so, error);
4049 					return (error);
4050 				}
4051 			}
4052 		}
4053 	} while (uiop->uio_resid > 0);
4054 	return (0);
4055 }
4056 
4057 /*
4058  * Check the state for errors and call the appropriate send function.
4059  *
4060  * If MSG_DONTROUTE is set (and SO_DONTROUTE isn't already set)
4061  * this function issues a setsockopt to toggle SO_DONTROUTE before and
4062  * after sending the message.
4063  */
4064 static int
4065 sotpi_sendmsg(struct sonode *so, struct nmsghdr *msg, struct uio *uiop)
4066 {
4067 	int		so_state;
4068 	int		so_mode;
4069 	int		error;
4070 	struct sockaddr *name;
4071 	t_uscalar_t	namelen;
4072 	int		dontroute;
4073 	int		flags;
4074 
4075 	dprintso(so, 1, ("sotpi_sendmsg(%p, %p, 0x%x) state %s, error %d\n",
4076 	    so, msg, msg->msg_flags,
4077 	    pr_state(so->so_state, so->so_mode), so->so_error));
4078 
4079 	mutex_enter(&so->so_lock);
4080 	so_state = so->so_state;
4081 
4082 	if (so_state & SS_CANTSENDMORE) {
4083 		mutex_exit(&so->so_lock);
4084 		tsignal(curthread, SIGPIPE);
4085 		return (EPIPE);
4086 	}
4087 
4088 	if (so->so_error != 0) {
4089 		error = sogeterr(so);
4090 		if (error != 0) {
4091 			mutex_exit(&so->so_lock);
4092 			return (error);
4093 		}
4094 	}
4095 
4096 	name = (struct sockaddr *)msg->msg_name;
4097 	namelen = msg->msg_namelen;
4098 
4099 	so_mode = so->so_mode;
4100 
4101 	if (name == NULL) {
4102 		if (!(so_state & SS_ISCONNECTED)) {
4103 			mutex_exit(&so->so_lock);
4104 			if (so_mode & SM_CONNREQUIRED)
4105 				return (ENOTCONN);
4106 			else
4107 				return (EDESTADDRREQ);
4108 		}
4109 		if (so_mode & SM_CONNREQUIRED) {
4110 			name = NULL;
4111 			namelen = 0;
4112 		} else {
4113 			/*
4114 			 * Note that this code does not prevent so_faddr_sa
4115 			 * from changing while it is being used. Thus
4116 			 * if an "unconnect"+connect occurs concurrently with
4117 			 * this send the datagram might be delivered to a
4118 			 * garbaled address.
4119 			 */
4120 			ASSERT(so->so_faddr_sa);
4121 			name = so->so_faddr_sa;
4122 			namelen = (t_uscalar_t)so->so_faddr_len;
4123 		}
4124 	} else {
4125 		if (!(so_state & SS_ISCONNECTED) &&
4126 		    (so_mode & SM_CONNREQUIRED)) {
4127 			/* Required but not connected */
4128 			mutex_exit(&so->so_lock);
4129 			return (ENOTCONN);
4130 		}
4131 		/*
4132 		 * Ignore the address on connection-oriented sockets.
4133 		 * Just like BSD this code does not generate an error for
4134 		 * TCP (a CONNREQUIRED socket) when sending to an address
4135 		 * passed in with sendto/sendmsg. Instead the data is
4136 		 * delivered on the connection as if no address had been
4137 		 * supplied.
4138 		 */
4139 		if ((so_state & SS_ISCONNECTED) &&
4140 		    !(so_mode & SM_CONNREQUIRED)) {
4141 			mutex_exit(&so->so_lock);
4142 			return (EISCONN);
4143 		}
4144 		if (!(so_state & SS_ISBOUND)) {
4145 			so_lock_single(so);	/* Set SOLOCKED */
4146 			error = sotpi_bind(so, NULL, 0,
4147 			    _SOBIND_UNSPEC|_SOBIND_LOCK_HELD);
4148 			so_unlock_single(so, SOLOCKED);
4149 			if (error) {
4150 				mutex_exit(&so->so_lock);
4151 				eprintsoline(so, error);
4152 				return (error);
4153 			}
4154 		}
4155 		/*
4156 		 * Handle delayed datagram errors. These are only queued
4157 		 * when the application sets SO_DGRAM_ERRIND.
4158 		 * Return the error if we are sending to the address
4159 		 * that was returned in the last T_UDERROR_IND.
4160 		 * If sending to some other address discard the delayed
4161 		 * error indication.
4162 		 */
4163 		if (so->so_delayed_error) {
4164 			struct T_uderror_ind	*tudi;
4165 			void			*addr;
4166 			t_uscalar_t		addrlen;
4167 			boolean_t		match = B_FALSE;
4168 
4169 			ASSERT(so->so_eaddr_mp);
4170 			error = so->so_delayed_error;
4171 			so->so_delayed_error = 0;
4172 			tudi = (struct T_uderror_ind *)so->so_eaddr_mp->b_rptr;
4173 			addrlen = tudi->DEST_length;
4174 			addr = sogetoff(so->so_eaddr_mp,
4175 			    tudi->DEST_offset,
4176 			    addrlen, 1);
4177 			ASSERT(addr);	/* Checked by strsock_proto */
4178 			switch (so->so_family) {
4179 			case AF_INET: {
4180 				/* Compare just IP address and port */
4181 				sin_t *sin1 = (sin_t *)name;
4182 				sin_t *sin2 = (sin_t *)addr;
4183 
4184 				if (addrlen == sizeof (sin_t) &&
4185 				    namelen == addrlen &&
4186 				    sin1->sin_port == sin2->sin_port &&
4187 				    sin1->sin_addr.s_addr ==
4188 				    sin2->sin_addr.s_addr)
4189 					match = B_TRUE;
4190 				break;
4191 			}
4192 			case AF_INET6: {
4193 				/* Compare just IP address and port. Not flow */
4194 				sin6_t *sin1 = (sin6_t *)name;
4195 				sin6_t *sin2 = (sin6_t *)addr;
4196 
4197 				if (addrlen == sizeof (sin6_t) &&
4198 				    namelen == addrlen &&
4199 				    sin1->sin6_port == sin2->sin6_port &&
4200 				    IN6_ARE_ADDR_EQUAL(&sin1->sin6_addr,
4201 				    &sin2->sin6_addr))
4202 					match = B_TRUE;
4203 				break;
4204 			}
4205 			case AF_UNIX:
4206 			default:
4207 				if (namelen == addrlen &&
4208 				    bcmp(name, addr, namelen) == 0)
4209 					match = B_TRUE;
4210 			}
4211 			if (match) {
4212 				freemsg(so->so_eaddr_mp);
4213 				so->so_eaddr_mp = NULL;
4214 				mutex_exit(&so->so_lock);
4215 #ifdef DEBUG
4216 				dprintso(so, 0,
4217 				    ("sockfs delayed error %d for %s\n",
4218 				    error,
4219 				    pr_addr(so->so_family, name, namelen)));
4220 #endif /* DEBUG */
4221 				return (error);
4222 			}
4223 			freemsg(so->so_eaddr_mp);
4224 			so->so_eaddr_mp = NULL;
4225 		}
4226 	}
4227 	mutex_exit(&so->so_lock);
4228 
4229 	flags = msg->msg_flags;
4230 	dontroute = 0;
4231 	if ((flags & MSG_DONTROUTE) && !(so->so_options & SO_DONTROUTE)) {
4232 		uint32_t	val;
4233 
4234 		val = 1;
4235 		error = sotpi_setsockopt(so, SOL_SOCKET, SO_DONTROUTE,
4236 		    &val, (t_uscalar_t)sizeof (val));
4237 		if (error)
4238 			return (error);
4239 		dontroute = 1;
4240 	}
4241 
4242 	if ((flags & MSG_OOB) && !(so_mode & SM_EXDATA)) {
4243 		error = EOPNOTSUPP;
4244 		goto done;
4245 	}
4246 	if (msg->msg_controllen != 0) {
4247 		if (!(so_mode & SM_CONNREQUIRED)) {
4248 			error = sosend_dgramcmsg(so, name, namelen, uiop,
4249 			    msg->msg_control, msg->msg_controllen, flags);
4250 		} else {
4251 			if (flags & MSG_OOB) {
4252 				/* Can't generate T_EXDATA_REQ with options */
4253 				error = EOPNOTSUPP;
4254 				goto done;
4255 			}
4256 			error = sosend_svccmsg(so, uiop,
4257 			    !(flags & MSG_EOR),
4258 			    msg->msg_control, msg->msg_controllen,
4259 			    flags);
4260 		}
4261 		goto done;
4262 	}
4263 
4264 	if (!(so_mode & SM_CONNREQUIRED)) {
4265 		/*
4266 		 * If there is no SO_DONTROUTE to turn off return immediately
4267 		 * from send_dgram. This can allow tail-call optimizations.
4268 		 */
4269 		if (!dontroute) {
4270 			return (sosend_dgram(so, name, namelen, uiop, flags));
4271 		}
4272 		error = sosend_dgram(so, name, namelen, uiop, flags);
4273 	} else {
4274 		t_scalar_t prim;
4275 		int sflag;
4276 
4277 		/* Ignore msg_name in the connected state */
4278 		if (flags & MSG_OOB) {
4279 			prim = T_EXDATA_REQ;
4280 			/*
4281 			 * Send down T_EXDATA_REQ even if there is flow
4282 			 * control for data.
4283 			 */
4284 			sflag = MSG_IGNFLOW;
4285 		} else {
4286 			if (so_mode & SM_BYTESTREAM) {
4287 				/* Byte stream transport - use write */
4288 
4289 				dprintso(so, 1, ("sotpi_sendmsg: write\n"));
4290 				/*
4291 				 * If there is no SO_DONTROUTE to turn off,
4292 				 * SS_DIRECT is on, and there is no flow
4293 				 * control, we can take the fast path.
4294 				 */
4295 				if (!dontroute &&
4296 				    (so_state & SS_DIRECT) &&
4297 				    canputnext(SOTOV(so)->v_stream->sd_wrq)) {
4298 					return (sostream_direct(so, uiop,
4299 					    NULL, CRED()));
4300 				}
4301 				error = strwrite(SOTOV(so), uiop, CRED());
4302 				goto done;
4303 			}
4304 			prim = T_DATA_REQ;
4305 			sflag = 0;
4306 		}
4307 		/*
4308 		 * If there is no SO_DONTROUTE to turn off return immediately
4309 		 * from sosend_svc. This can allow tail-call optimizations.
4310 		 */
4311 		if (!dontroute)
4312 			return (sosend_svc(so, uiop, prim,
4313 			    !(flags & MSG_EOR), sflag));
4314 		error = sosend_svc(so, uiop, prim,
4315 		    !(flags & MSG_EOR), sflag);
4316 	}
4317 	ASSERT(dontroute);
4318 done:
4319 	if (dontroute) {
4320 		uint32_t	val;
4321 
4322 		val = 0;
4323 		(void) sotpi_setsockopt(so, SOL_SOCKET, SO_DONTROUTE,
4324 		    &val, (t_uscalar_t)sizeof (val));
4325 	}
4326 	return (error);
4327 }
4328 
4329 /*
4330  * Sending data on a datagram socket.
4331  * Assumes caller has verified that SS_ISBOUND etc. are set.
4332  */
4333 /* ARGSUSED */
4334 static int
4335 sodgram_direct(struct sonode *so, struct sockaddr *name,
4336     socklen_t namelen, struct uio *uiop, int flags)
4337 {
4338 	struct T_unitdata_req	tudr;
4339 	mblk_t			*mp = NULL;
4340 	int			error = 0;
4341 	void			*addr;
4342 	socklen_t		addrlen;
4343 	ssize_t			len;
4344 	struct stdata		*stp = SOTOV(so)->v_stream;
4345 	int			so_state;
4346 	queue_t			*udp_wq;
4347 	boolean_t		connected;
4348 	mblk_t			*mpdata = NULL;
4349 
4350 	ASSERT(name != NULL && namelen != 0);
4351 	ASSERT(!(so->so_mode & SM_CONNREQUIRED));
4352 	ASSERT(!(so->so_mode & SM_EXDATA));
4353 	ASSERT(so->so_family == AF_INET || so->so_family == AF_INET6);
4354 	ASSERT(SOTOV(so)->v_type == VSOCK);
4355 
4356 	/* Caller checked for proper length */
4357 	len = uiop->uio_resid;
4358 	ASSERT(len <= so->so_tidu_size);
4359 
4360 	/* Length and family checks have been done by caller */
4361 	ASSERT(name->sa_family == so->so_family);
4362 	ASSERT(so->so_family == AF_INET ||
4363 	    (namelen == (socklen_t)sizeof (struct sockaddr_in6)));
4364 	ASSERT(so->so_family == AF_INET6 ||
4365 	    (namelen == (socklen_t)sizeof (struct sockaddr_in)));
4366 
4367 	addr = name;
4368 	addrlen = namelen;
4369 
4370 	if (stp->sd_sidp != NULL &&
4371 	    (error = straccess(stp, JCWRITE)) != 0)
4372 		goto done;
4373 
4374 	so_state = so->so_state;
4375 
4376 	connected = so_state & SS_ISCONNECTED;
4377 	if (!connected) {
4378 		tudr.PRIM_type = T_UNITDATA_REQ;
4379 		tudr.DEST_length = addrlen;
4380 		tudr.DEST_offset = (t_scalar_t)sizeof (tudr);
4381 		tudr.OPT_length = 0;
4382 		tudr.OPT_offset = 0;
4383 
4384 		mp = soallocproto2(&tudr, sizeof (tudr), addr, addrlen, 0,
4385 		    _ALLOC_INTR);
4386 		if (mp == NULL) {
4387 			/*
4388 			 * Caught a signal waiting for memory.
4389 			 * Let send* return EINTR.
4390 			 */
4391 			error = EINTR;
4392 			goto done;
4393 		}
4394 	}
4395 
4396 	/*
4397 	 * For UDP we don't break up the copyin into smaller pieces
4398 	 * as in the TCP case.  That means if ENOMEM is returned by
4399 	 * mcopyinuio() then the uio vector has not been modified at
4400 	 * all and we fallback to either strwrite() or kstrputmsg()
4401 	 * below.  Note also that we never generate priority messages
4402 	 * from here.
4403 	 */
4404 	udp_wq = stp->sd_wrq->q_next;
4405 	if (canput(udp_wq) &&
4406 	    (mpdata = mcopyinuio(stp, uiop, -1, -1, &error)) != NULL) {
4407 		ASSERT(DB_TYPE(mpdata) == M_DATA);
4408 		ASSERT(uiop->uio_resid == 0);
4409 		if (!connected)
4410 			linkb(mp, mpdata);
4411 		else
4412 			mp = mpdata;
4413 		if (audit_active)
4414 			audit_sock(T_UNITDATA_REQ, strvp2wq(SOTOV(so)), mp, 0);
4415 
4416 		udp_wput(udp_wq, mp);
4417 		return (0);
4418 	}
4419 
4420 	ASSERT(mpdata == NULL);
4421 	if (error != 0 && error != ENOMEM) {
4422 		freemsg(mp);
4423 		return (error);
4424 	}
4425 
4426 	/*
4427 	 * For connected, let strwrite() handle the blocking case.
4428 	 * Otherwise we fall thru and use kstrputmsg().
4429 	 */
4430 	if (connected)
4431 		return (strwrite(SOTOV(so), uiop, CRED()));
4432 
4433 	if (audit_active)
4434 		audit_sock(T_UNITDATA_REQ, strvp2wq(SOTOV(so)), mp, 0);
4435 
4436 	error = kstrputmsg(SOTOV(so), mp, uiop, len, 0, MSG_BAND, 0);
4437 done:
4438 #ifdef SOCK_DEBUG
4439 	if (error != 0) {
4440 		eprintsoline(so, error);
4441 	}
4442 #endif /* SOCK_DEBUG */
4443 	return (error);
4444 }
4445 
4446 int
4447 sostream_direct(struct sonode *so, struct uio *uiop, mblk_t *mp, cred_t *cr)
4448 {
4449 	struct stdata *stp = SOTOV(so)->v_stream;
4450 	ssize_t iosize, rmax, maxblk;
4451 	queue_t *tcp_wq = stp->sd_wrq->q_next;
4452 	mblk_t *newmp;
4453 	int error = 0, wflag = 0;
4454 
4455 	ASSERT(so->so_mode & SM_BYTESTREAM);
4456 	ASSERT(SOTOV(so)->v_type == VSOCK);
4457 
4458 	if (stp->sd_sidp != NULL &&
4459 	    (error = straccess(stp, JCWRITE)) != 0)
4460 		return (error);
4461 
4462 	if (uiop == NULL) {
4463 		/*
4464 		 * kstrwritemp() should have checked sd_flag and
4465 		 * flow-control before coming here.  If we end up
4466 		 * here it means that we can simply pass down the
4467 		 * data to tcp.
4468 		 */
4469 		ASSERT(mp != NULL);
4470 		if (stp->sd_wputdatafunc != NULL) {
4471 			newmp = (stp->sd_wputdatafunc)(SOTOV(so), mp, NULL,
4472 			    NULL, NULL, NULL);
4473 			if (newmp == NULL) {
4474 				/* The caller will free mp */
4475 				return (ECOMM);
4476 			}
4477 			mp = newmp;
4478 		}
4479 		tcp_wput(tcp_wq, mp);
4480 		return (0);
4481 	}
4482 
4483 	/* Fallback to strwrite() to do proper error handling */
4484 	if (stp->sd_flag & (STWRERR|STRHUP|STPLEX|STRDELIM|OLDNDELAY))
4485 		return (strwrite(SOTOV(so), uiop, cr));
4486 
4487 	rmax = stp->sd_qn_maxpsz;
4488 	ASSERT(rmax >= 0 || rmax == INFPSZ);
4489 	if (rmax == 0 || uiop->uio_resid <= 0)
4490 		return (0);
4491 
4492 	if (rmax == INFPSZ)
4493 		rmax = uiop->uio_resid;
4494 
4495 	maxblk = stp->sd_maxblk;
4496 
4497 	for (;;) {
4498 		iosize = MIN(uiop->uio_resid, rmax);
4499 
4500 		mp = mcopyinuio(stp, uiop, iosize, maxblk, &error);
4501 		if (mp == NULL) {
4502 			/*
4503 			 * Fallback to strwrite() for ENOMEM; if this
4504 			 * is our first time in this routine and the uio
4505 			 * vector has not been modified, we will end up
4506 			 * calling strwrite() without any flag set.
4507 			 */
4508 			if (error == ENOMEM)
4509 				goto slow_send;
4510 			else
4511 				return (error);
4512 		}
4513 		ASSERT(uiop->uio_resid >= 0);
4514 		/*
4515 		 * If mp is non-NULL and ENOMEM is set, it means that
4516 		 * mcopyinuio() was able to break down some of the user
4517 		 * data into one or more mblks.  Send the partial data
4518 		 * to tcp and let the rest be handled in strwrite().
4519 		 */
4520 		ASSERT(error == 0 || error == ENOMEM);
4521 		if (stp->sd_wputdatafunc != NULL) {
4522 			newmp = (stp->sd_wputdatafunc)(SOTOV(so), mp, NULL,
4523 			    NULL, NULL, NULL);
4524 			if (newmp == NULL) {
4525 				/* The caller will free mp */
4526 				return (ECOMM);
4527 			}
4528 			mp = newmp;
4529 		}
4530 		tcp_wput(tcp_wq, mp);
4531 
4532 		wflag |= NOINTR;
4533 
4534 		if (uiop->uio_resid == 0) {	/* No more data; we're done */
4535 			ASSERT(error == 0);
4536 			break;
4537 		} else if (error == ENOMEM || !canput(tcp_wq) || (stp->sd_flag &
4538 		    (STWRERR|STRHUP|STPLEX|STRDELIM|OLDNDELAY))) {
4539 slow_send:
4540 			/*
4541 			 * We were able to send down partial data using
4542 			 * the direct call interface, but are now relying
4543 			 * on strwrite() to handle the non-fastpath cases.
4544 			 * If the socket is blocking we will sleep in
4545 			 * strwaitq() until write is permitted, otherwise,
4546 			 * we will need to return the amount of bytes
4547 			 * written so far back to the app.  This is the
4548 			 * reason why we pass NOINTR flag to strwrite()
4549 			 * for non-blocking socket, because we don't want
4550 			 * to return EAGAIN when portion of the user data
4551 			 * has actually been sent down.
4552 			 */
4553 			return (strwrite_common(SOTOV(so), uiop, cr, wflag));
4554 		}
4555 	}
4556 	return (0);
4557 }
4558 
4559 /*
4560  * Update so_faddr by asking the transport (unless AF_UNIX).
4561  */
4562 int
4563 sotpi_getpeername(struct sonode *so)
4564 {
4565 	struct strbuf	strbuf;
4566 	int		error = 0, res;
4567 	void		*addr;
4568 	t_uscalar_t	addrlen;
4569 	k_sigset_t	smask;
4570 
4571 	dprintso(so, 1, ("sotpi_getpeername(%p) %s\n",
4572 	    so, pr_state(so->so_state, so->so_mode)));
4573 
4574 	mutex_enter(&so->so_lock);
4575 	so_lock_single(so);	/* Set SOLOCKED */
4576 	if (!(so->so_state & SS_ISCONNECTED)) {
4577 		error = ENOTCONN;
4578 		goto done;
4579 	}
4580 	/* Added this check for X/Open */
4581 	if ((so->so_state & SS_CANTSENDMORE) && !xnet_skip_checks) {
4582 		error = EINVAL;
4583 		if (xnet_check_print) {
4584 			printf("sockfs: X/Open getpeername check => EINVAL\n");
4585 		}
4586 		goto done;
4587 	}
4588 #ifdef DEBUG
4589 	dprintso(so, 1, ("sotpi_getpeername (local): %s\n",
4590 	    pr_addr(so->so_family, so->so_faddr_sa,
4591 	    (t_uscalar_t)so->so_faddr_len)));
4592 #endif /* DEBUG */
4593 
4594 	if (so->so_family == AF_UNIX) {
4595 		/* Transport has different name space - return local info */
4596 		error = 0;
4597 		goto done;
4598 	}
4599 
4600 	ASSERT(so->so_faddr_sa);
4601 	/* Allocate local buffer to use with ioctl */
4602 	addrlen = (t_uscalar_t)so->so_faddr_maxlen;
4603 	mutex_exit(&so->so_lock);
4604 	addr = kmem_alloc(addrlen, KM_SLEEP);
4605 
4606 	/*
4607 	 * Issue TI_GETPEERNAME with signals masked.
4608 	 * Put the result in so_faddr_sa so that getpeername works after
4609 	 * a shutdown(output).
4610 	 * If the ioctl fails (e.g. due to a ECONNRESET) the error is reposted
4611 	 * back to the socket.
4612 	 */
4613 	strbuf.buf = addr;
4614 	strbuf.maxlen = addrlen;
4615 	strbuf.len = 0;
4616 
4617 	sigintr(&smask, 0);
4618 	res = 0;
4619 	ASSERT(CRED());
4620 	error = strioctl(SOTOV(so), TI_GETPEERNAME, (intptr_t)&strbuf,
4621 	    0, K_TO_K, CRED(), &res);
4622 	sigunintr(&smask);
4623 
4624 	mutex_enter(&so->so_lock);
4625 	/*
4626 	 * If there is an error record the error in so_error put don't fail
4627 	 * the getpeername. Instead fallback on the recorded
4628 	 * so->so_faddr_sa.
4629 	 */
4630 	if (error) {
4631 		/*
4632 		 * Various stream head errors can be returned to the ioctl.
4633 		 * However, it is impossible to determine which ones of
4634 		 * these are really socket level errors that were incorrectly
4635 		 * consumed by the ioctl. Thus this code silently ignores the
4636 		 * error - to code explicitly does not reinstate the error
4637 		 * using soseterror().
4638 		 * Experiments have shows that at least this set of
4639 		 * errors are reported and should not be reinstated on the
4640 		 * socket:
4641 		 *	EINVAL	E.g. if an I_LINK was in effect when
4642 		 *		getpeername was called.
4643 		 *	EPIPE	The ioctl error semantics prefer the write
4644 		 *		side error over the read side error.
4645 		 *	ENOTCONN The transport just got disconnected but
4646 		 *		sockfs had not yet seen the T_DISCON_IND
4647 		 *		when issuing the ioctl.
4648 		 */
4649 		error = 0;
4650 	} else if (res == 0 && strbuf.len > 0 &&
4651 	    (so->so_state & SS_ISCONNECTED)) {
4652 		ASSERT(strbuf.len <= (int)so->so_faddr_maxlen);
4653 		so->so_faddr_len = (socklen_t)strbuf.len;
4654 		bcopy(addr, so->so_faddr_sa, so->so_faddr_len);
4655 		so->so_state |= SS_FADDR_VALID;
4656 	}
4657 	kmem_free(addr, addrlen);
4658 #ifdef DEBUG
4659 	dprintso(so, 1, ("sotpi_getpeername (tp): %s\n",
4660 	    pr_addr(so->so_family, so->so_faddr_sa,
4661 	    (t_uscalar_t)so->so_faddr_len)));
4662 #endif /* DEBUG */
4663 done:
4664 	so_unlock_single(so, SOLOCKED);
4665 	mutex_exit(&so->so_lock);
4666 	return (error);
4667 }
4668 
4669 /*
4670  * Update so_laddr by asking the transport (unless AF_UNIX).
4671  */
4672 int
4673 sotpi_getsockname(struct sonode *so)
4674 {
4675 	struct strbuf	strbuf;
4676 	int		error = 0, res;
4677 	void		*addr;
4678 	t_uscalar_t	addrlen;
4679 	k_sigset_t	smask;
4680 
4681 	dprintso(so, 1, ("sotpi_getsockname(%p) %s\n",
4682 	    so, pr_state(so->so_state, so->so_mode)));
4683 
4684 	mutex_enter(&so->so_lock);
4685 	so_lock_single(so);	/* Set SOLOCKED */
4686 	if (!(so->so_state & SS_ISBOUND) && so->so_family != AF_UNIX) {
4687 		/* Return an all zero address except for the family */
4688 		if (so->so_family == AF_INET)
4689 			so->so_laddr_len = (socklen_t)sizeof (sin_t);
4690 		else if (so->so_family == AF_INET6)
4691 			so->so_laddr_len = (socklen_t)sizeof (sin6_t);
4692 		ASSERT(so->so_laddr_len <= so->so_laddr_maxlen);
4693 		bzero(so->so_laddr_sa, so->so_laddr_len);
4694 		/*
4695 		 * Can not assume there is a sa_family for all
4696 		 * protocol families.
4697 		 */
4698 		if (so->so_family == AF_INET || so->so_family == AF_INET6)
4699 			so->so_laddr_sa->sa_family = so->so_family;
4700 	}
4701 #ifdef DEBUG
4702 	dprintso(so, 1, ("sotpi_getsockname (local): %s\n",
4703 	    pr_addr(so->so_family, so->so_laddr_sa,
4704 	    (t_uscalar_t)so->so_laddr_len)));
4705 #endif /* DEBUG */
4706 	if (so->so_family == AF_UNIX) {
4707 		/* Transport has different name space - return local info */
4708 		error = 0;
4709 		goto done;
4710 	}
4711 	if (!(so->so_state & SS_ISBOUND)) {
4712 		/* If not bound, then nothing to return. */
4713 		error = 0;
4714 		goto done;
4715 	}
4716 	/* Allocate local buffer to use with ioctl */
4717 	addrlen = (t_uscalar_t)so->so_laddr_maxlen;
4718 	mutex_exit(&so->so_lock);
4719 	addr = kmem_alloc(addrlen, KM_SLEEP);
4720 
4721 	/*
4722 	 * Issue TI_GETMYNAME with signals masked.
4723 	 * Put the result in so_laddr_sa so that getsockname works after
4724 	 * a shutdown(output).
4725 	 * If the ioctl fails (e.g. due to a ECONNRESET) the error is reposted
4726 	 * back to the socket.
4727 	 */
4728 	strbuf.buf = addr;
4729 	strbuf.maxlen = addrlen;
4730 	strbuf.len = 0;
4731 
4732 	sigintr(&smask, 0);
4733 	res = 0;
4734 	ASSERT(CRED());
4735 	error = strioctl(SOTOV(so), TI_GETMYNAME, (intptr_t)&strbuf,
4736 	    0, K_TO_K, CRED(), &res);
4737 	sigunintr(&smask);
4738 
4739 	mutex_enter(&so->so_lock);
4740 	/*
4741 	 * If there is an error record the error in so_error put don't fail
4742 	 * the getsockname. Instead fallback on the recorded
4743 	 * so->so_laddr_sa.
4744 	 */
4745 	if (error) {
4746 		/*
4747 		 * Various stream head errors can be returned to the ioctl.
4748 		 * However, it is impossible to determine which ones of
4749 		 * these are really socket level errors that were incorrectly
4750 		 * consumed by the ioctl. Thus this code silently ignores the
4751 		 * error - to code explicitly does not reinstate the error
4752 		 * using soseterror().
4753 		 * Experiments have shows that at least this set of
4754 		 * errors are reported and should not be reinstated on the
4755 		 * socket:
4756 		 *	EINVAL	E.g. if an I_LINK was in effect when
4757 		 *		getsockname was called.
4758 		 *	EPIPE	The ioctl error semantics prefer the write
4759 		 *		side error over the read side error.
4760 		 */
4761 		error = 0;
4762 	} else if (res == 0 && strbuf.len > 0 &&
4763 	    (so->so_state & SS_ISBOUND)) {
4764 		ASSERT(strbuf.len <= (int)so->so_laddr_maxlen);
4765 		so->so_laddr_len = (socklen_t)strbuf.len;
4766 		bcopy(addr, so->so_laddr_sa, so->so_laddr_len);
4767 		so->so_state |= SS_LADDR_VALID;
4768 	}
4769 	kmem_free(addr, addrlen);
4770 #ifdef DEBUG
4771 	dprintso(so, 1, ("sotpi_getsockname (tp): %s\n",
4772 	    pr_addr(so->so_family, so->so_laddr_sa,
4773 	    (t_uscalar_t)so->so_laddr_len)));
4774 #endif /* DEBUG */
4775 done:
4776 	so_unlock_single(so, SOLOCKED);
4777 	mutex_exit(&so->so_lock);
4778 	return (error);
4779 }
4780 
4781 /*
4782  * Get socket options. For SOL_SOCKET options some options are handled
4783  * by the sockfs while others use the value recorded in the sonode as a
4784  * fallback should the T_SVR4_OPTMGMT_REQ fail.
4785  *
4786  * On the return most *optlenp bytes are copied to optval.
4787  */
4788 int
4789 sotpi_getsockopt(struct sonode *so, int level, int option_name,
4790 		void *optval, socklen_t *optlenp, int flags)
4791 {
4792 	struct T_optmgmt_req	optmgmt_req;
4793 	struct T_optmgmt_ack	*optmgmt_ack;
4794 	struct opthdr		oh;
4795 	struct opthdr		*opt_res;
4796 	mblk_t			*mp = NULL;
4797 	int			error = 0;
4798 	void			*option = NULL;	/* Set if fallback value */
4799 	t_uscalar_t		maxlen = *optlenp;
4800 	t_uscalar_t		len;
4801 	uint32_t		value;
4802 
4803 	dprintso(so, 1, ("sotpi_getsockopt(%p, 0x%x, 0x%x, %p, %p) %s\n",
4804 	    so, level, option_name, optval, optlenp,
4805 	    pr_state(so->so_state, so->so_mode)));
4806 
4807 	mutex_enter(&so->so_lock);
4808 	so_lock_single(so);	/* Set SOLOCKED */
4809 
4810 	/*
4811 	 * Check for SOL_SOCKET options.
4812 	 * Certain SOL_SOCKET options are returned directly whereas
4813 	 * others only provide a default (fallback) value should
4814 	 * the T_SVR4_OPTMGMT_REQ fail.
4815 	 */
4816 	if (level == SOL_SOCKET) {
4817 		/* Check parameters */
4818 		switch (option_name) {
4819 		case SO_TYPE:
4820 		case SO_ERROR:
4821 		case SO_DEBUG:
4822 		case SO_ACCEPTCONN:
4823 		case SO_REUSEADDR:
4824 		case SO_KEEPALIVE:
4825 		case SO_DONTROUTE:
4826 		case SO_BROADCAST:
4827 		case SO_USELOOPBACK:
4828 		case SO_OOBINLINE:
4829 		case SO_SNDBUF:
4830 		case SO_RCVBUF:
4831 #ifdef notyet
4832 		case SO_SNDLOWAT:
4833 		case SO_RCVLOWAT:
4834 		case SO_SNDTIMEO:
4835 		case SO_RCVTIMEO:
4836 #endif /* notyet */
4837 		case SO_DOMAIN:
4838 		case SO_DGRAM_ERRIND:
4839 			if (maxlen < (t_uscalar_t)sizeof (int32_t)) {
4840 				error = EINVAL;
4841 				eprintsoline(so, error);
4842 				goto done2;
4843 			}
4844 			break;
4845 		case SO_LINGER:
4846 			if (maxlen < (t_uscalar_t)sizeof (struct linger)) {
4847 				error = EINVAL;
4848 				eprintsoline(so, error);
4849 				goto done2;
4850 			}
4851 			break;
4852 		}
4853 
4854 		len = (t_uscalar_t)sizeof (uint32_t);	/* Default */
4855 
4856 		switch (option_name) {
4857 		case SO_TYPE:
4858 			value = so->so_type;
4859 			option = &value;
4860 			goto copyout; /* No need to issue T_SVR4_OPTMGMT_REQ */
4861 
4862 		case SO_ERROR:
4863 			value = sogeterr(so);
4864 			option = &value;
4865 			goto copyout; /* No need to issue T_SVR4_OPTMGMT_REQ */
4866 
4867 		case SO_ACCEPTCONN:
4868 			if (so->so_state & SS_ACCEPTCONN)
4869 				value = SO_ACCEPTCONN;
4870 			else
4871 				value = 0;
4872 #ifdef DEBUG
4873 			if (value) {
4874 				dprintso(so, 1,
4875 				    ("sotpi_getsockopt: 0x%x is set\n",
4876 				    option_name));
4877 			} else {
4878 				dprintso(so, 1,
4879 				    ("sotpi_getsockopt: 0x%x not set\n",
4880 				    option_name));
4881 			}
4882 #endif /* DEBUG */
4883 			option = &value;
4884 			goto copyout; /* No need to issue T_SVR4_OPTMGMT_REQ */
4885 
4886 		case SO_DEBUG:
4887 		case SO_REUSEADDR:
4888 		case SO_KEEPALIVE:
4889 		case SO_DONTROUTE:
4890 		case SO_BROADCAST:
4891 		case SO_USELOOPBACK:
4892 		case SO_OOBINLINE:
4893 		case SO_DGRAM_ERRIND:
4894 			value = (so->so_options & option_name);
4895 #ifdef DEBUG
4896 			if (value) {
4897 				dprintso(so, 1,
4898 				    ("sotpi_getsockopt: 0x%x is set\n",
4899 				    option_name));
4900 			} else {
4901 				dprintso(so, 1,
4902 				    ("sotpi_getsockopt: 0x%x not set\n",
4903 				    option_name));
4904 			}
4905 #endif /* DEBUG */
4906 			option = &value;
4907 			goto copyout; /* No need to issue T_SVR4_OPTMGMT_REQ */
4908 
4909 		/*
4910 		 * The following options are only returned by sockfs when the
4911 		 * T_SVR4_OPTMGMT_REQ fails.
4912 		 */
4913 		case SO_LINGER:
4914 			option = &so->so_linger;
4915 			len = (t_uscalar_t)sizeof (struct linger);
4916 			break;
4917 		case SO_SNDBUF: {
4918 			ssize_t lvalue;
4919 
4920 			/*
4921 			 * If the option has not been set then get a default
4922 			 * value from the read queue. This value is
4923 			 * returned if the transport fails
4924 			 * the T_SVR4_OPTMGMT_REQ.
4925 			 */
4926 			lvalue = so->so_sndbuf;
4927 			if (lvalue == 0) {
4928 				mutex_exit(&so->so_lock);
4929 				(void) strqget(strvp2wq(SOTOV(so))->q_next,
4930 				    QHIWAT, 0, &lvalue);
4931 				mutex_enter(&so->so_lock);
4932 				dprintso(so, 1,
4933 				    ("got SO_SNDBUF %ld from q\n", lvalue));
4934 			}
4935 			value = (int)lvalue;
4936 			option = &value;
4937 			len = (t_uscalar_t)sizeof (so->so_sndbuf);
4938 			break;
4939 		}
4940 		case SO_RCVBUF: {
4941 			ssize_t lvalue;
4942 
4943 			/*
4944 			 * If the option has not been set then get a default
4945 			 * value from the read queue. This value is
4946 			 * returned if the transport fails
4947 			 * the T_SVR4_OPTMGMT_REQ.
4948 			 *
4949 			 * XXX If SO_RCVBUF has been set and this is an
4950 			 * XPG 4.2 application then do not ask the transport
4951 			 * since the transport might adjust the value and not
4952 			 * return exactly what was set by the application.
4953 			 * For non-XPG 4.2 application we return the value
4954 			 * that the transport is actually using.
4955 			 */
4956 			lvalue = so->so_rcvbuf;
4957 			if (lvalue == 0) {
4958 				mutex_exit(&so->so_lock);
4959 				(void) strqget(RD(strvp2wq(SOTOV(so))),
4960 				    QHIWAT, 0, &lvalue);
4961 				mutex_enter(&so->so_lock);
4962 				dprintso(so, 1,
4963 				    ("got SO_RCVBUF %ld from q\n", lvalue));
4964 			} else if (flags & _SOGETSOCKOPT_XPG4_2) {
4965 				value = (int)lvalue;
4966 				option = &value;
4967 				goto copyout;	/* skip asking transport */
4968 			}
4969 			value = (int)lvalue;
4970 			option = &value;
4971 			len = (t_uscalar_t)sizeof (so->so_rcvbuf);
4972 			break;
4973 		}
4974 		case SO_DOMAIN:
4975 			value = so->so_family;
4976 			option = &value;
4977 			goto copyout; /* No need to issue T_SVR4_OPTMGMT_REQ */
4978 
4979 #ifdef notyet
4980 		/*
4981 		 * We do not implement the semantics of these options
4982 		 * thus we shouldn't implement the options either.
4983 		 */
4984 		case SO_SNDLOWAT:
4985 			value = so->so_sndlowat;
4986 			option = &value;
4987 			break;
4988 		case SO_RCVLOWAT:
4989 			value = so->so_rcvlowat;
4990 			option = &value;
4991 			break;
4992 		case SO_SNDTIMEO:
4993 			value = so->so_sndtimeo;
4994 			option = &value;
4995 			break;
4996 		case SO_RCVTIMEO:
4997 			value = so->so_rcvtimeo;
4998 			option = &value;
4999 			break;
5000 #endif /* notyet */
5001 		}
5002 	}
5003 
5004 	mutex_exit(&so->so_lock);
5005 
5006 	/* Send request */
5007 	optmgmt_req.PRIM_type = T_SVR4_OPTMGMT_REQ;
5008 	optmgmt_req.MGMT_flags = T_CHECK;
5009 	optmgmt_req.OPT_length = (t_scalar_t)(sizeof (oh) + maxlen);
5010 	optmgmt_req.OPT_offset = (t_scalar_t)sizeof (optmgmt_req);
5011 
5012 	oh.level = level;
5013 	oh.name = option_name;
5014 	oh.len = maxlen;
5015 
5016 	mp = soallocproto3(&optmgmt_req, sizeof (optmgmt_req),
5017 	    &oh, sizeof (oh), NULL, maxlen, 0, _ALLOC_SLEEP);
5018 	/* Let option management work in the presence of data flow control */
5019 	error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0,
5020 	    MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR|MSG_IGNFLOW, 0);
5021 	mp = NULL;
5022 	mutex_enter(&so->so_lock);
5023 	if (error) {
5024 		eprintsoline(so, error);
5025 		goto done2;
5026 	}
5027 	error = sowaitprim(so, T_SVR4_OPTMGMT_REQ, T_OPTMGMT_ACK,
5028 	    (t_uscalar_t)(sizeof (*optmgmt_ack) + sizeof (*opt_res)), &mp, 0);
5029 	if (error) {
5030 		if (option != NULL) {
5031 			/* We have a fallback value */
5032 			error = 0;
5033 			goto copyout;
5034 		}
5035 		eprintsoline(so, error);
5036 		goto done2;
5037 	}
5038 	ASSERT(mp);
5039 	optmgmt_ack = (struct T_optmgmt_ack *)mp->b_rptr;
5040 	opt_res = (struct opthdr *)sogetoff(mp, optmgmt_ack->OPT_offset,
5041 	    optmgmt_ack->OPT_length, __TPI_ALIGN_SIZE);
5042 	if (opt_res == NULL) {
5043 		if (option != NULL) {
5044 			/* We have a fallback value */
5045 			error = 0;
5046 			goto copyout;
5047 		}
5048 		error = EPROTO;
5049 		eprintsoline(so, error);
5050 		goto done;
5051 	}
5052 	option = &opt_res[1];
5053 
5054 	/* check to ensure that the option is within bounds */
5055 	if (((uintptr_t)option + opt_res->len < (uintptr_t)option) ||
5056 	    (uintptr_t)option + opt_res->len > (uintptr_t)mp->b_wptr) {
5057 		if (option != NULL) {
5058 			/* We have a fallback value */
5059 			error = 0;
5060 			goto copyout;
5061 		}
5062 		error = EPROTO;
5063 		eprintsoline(so, error);
5064 		goto done;
5065 	}
5066 
5067 	len = opt_res->len;
5068 
5069 copyout: {
5070 		t_uscalar_t size = MIN(len, maxlen);
5071 		bcopy(option, optval, size);
5072 		bcopy(&size, optlenp, sizeof (size));
5073 	}
5074 done:
5075 	freemsg(mp);
5076 done2:
5077 	so_unlock_single(so, SOLOCKED);
5078 	mutex_exit(&so->so_lock);
5079 	return (error);
5080 }
5081 
5082 /*
5083  * Set socket options. All options are passed down in a T_SVR4_OPTMGMT_REQ.
5084  * SOL_SOCKET options are also recorded in the sonode. A setsockopt for
5085  * SOL_SOCKET options will not fail just because the T_SVR4_OPTMGMT_REQ fails -
5086  * setsockopt has to work even if the transport does not support the option.
5087  */
5088 int
5089 sotpi_setsockopt(struct sonode *so, int level, int option_name,
5090 	const void *optval, t_uscalar_t optlen)
5091 {
5092 	struct T_optmgmt_req	optmgmt_req;
5093 	struct opthdr		oh;
5094 	mblk_t			*mp;
5095 	int			error = 0;
5096 	boolean_t		handled = B_FALSE;
5097 
5098 	dprintso(so, 1, ("sotpi_setsockopt(%p, 0x%x, 0x%x, %p, %d) %s\n",
5099 	    so, level, option_name, optval, optlen,
5100 	    pr_state(so->so_state, so->so_mode)));
5101 
5102 
5103 	/* X/Open requires this check */
5104 	if ((so->so_state & SS_CANTSENDMORE) && !xnet_skip_checks) {
5105 		if (xnet_check_print)
5106 			printf("sockfs: X/Open setsockopt check => EINVAL\n");
5107 		return (EINVAL);
5108 	}
5109 
5110 	/* Caller allocates aligned optval, or passes null */
5111 	ASSERT(((uintptr_t)optval & (sizeof (t_scalar_t) - 1)) == 0);
5112 	/* If optval is null optlen is 0, and vice-versa */
5113 	ASSERT(optval != NULL || optlen == 0);
5114 	ASSERT(optlen != 0 || optval == NULL);
5115 
5116 	mutex_enter(&so->so_lock);
5117 	so_lock_single(so);	/* Set SOLOCKED */
5118 	mutex_exit(&so->so_lock);
5119 
5120 	/*
5121 	 * For SOCKET or TCP level options, try to set it here itself
5122 	 * provided socket has not been popped and we know the tcp
5123 	 * structure (stored in so_priv).
5124 	 */
5125 	if ((level == SOL_SOCKET || level == IPPROTO_TCP) &&
5126 	    (so->so_family == AF_INET || so->so_family == AF_INET6) &&
5127 	    (so->so_version == SOV_SOCKSTREAM) && (so->so_priv != NULL)) {
5128 		tcp_t		*tcp = so->so_priv;
5129 		boolean_t	onoff;
5130 
5131 #define	intvalue	(*(int32_t *)optval)
5132 
5133 		switch (level) {
5134 		case SOL_SOCKET:
5135 			switch (option_name) {		/* Check length param */
5136 			case SO_DEBUG:
5137 			case SO_REUSEADDR:
5138 			case SO_DONTROUTE:
5139 			case SO_BROADCAST:
5140 			case SO_USELOOPBACK:
5141 			case SO_OOBINLINE:
5142 			case SO_DGRAM_ERRIND:
5143 				if (optlen != (t_uscalar_t)sizeof (int32_t)) {
5144 					error = EINVAL;
5145 					eprintsoline(so, error);
5146 					mutex_enter(&so->so_lock);
5147 					goto done2;
5148 				}
5149 				ASSERT(optval);
5150 				onoff = intvalue != 0;
5151 				handled = B_TRUE;
5152 				break;
5153 			case SO_LINGER:
5154 				if (optlen !=
5155 				    (t_uscalar_t)sizeof (struct linger)) {
5156 					error = EINVAL;
5157 					eprintsoline(so, error);
5158 					mutex_enter(&so->so_lock);
5159 					goto done2;
5160 				}
5161 				ASSERT(optval);
5162 				handled = B_TRUE;
5163 				break;
5164 			}
5165 
5166 			switch (option_name) {			/* Do actions */
5167 			case SO_LINGER: {
5168 				struct linger *lgr = (struct linger *)optval;
5169 
5170 				if (lgr->l_onoff) {
5171 					tcp->tcp_linger = 1;
5172 					tcp->tcp_lingertime = lgr->l_linger;
5173 					so->so_linger.l_onoff = SO_LINGER;
5174 					so->so_options |= SO_LINGER;
5175 				} else {
5176 					tcp->tcp_linger = 0;
5177 					tcp->tcp_lingertime = 0;
5178 					so->so_linger.l_onoff = 0;
5179 					so->so_options &= ~SO_LINGER;
5180 				}
5181 				so->so_linger.l_linger = lgr->l_linger;
5182 				handled = B_TRUE;
5183 				break;
5184 			}
5185 			case SO_DEBUG:
5186 				tcp->tcp_debug = onoff;
5187 #ifdef SOCK_TEST
5188 				if (intvalue & 2)
5189 					sock_test_timelimit = 10 * hz;
5190 				else
5191 					sock_test_timelimit = 0;
5192 
5193 				if (intvalue & 4)
5194 					do_useracc = 0;
5195 				else
5196 					do_useracc = 1;
5197 #endif /* SOCK_TEST */
5198 				break;
5199 			case SO_DONTROUTE:
5200 				/*
5201 				 * SO_DONTROUTE, SO_USELOOPBACK and
5202 				 * SO_BROADCAST are only of interest to IP.
5203 				 * We track them here only so
5204 				 * that we can report their current value.
5205 				 */
5206 				tcp->tcp_dontroute = onoff;
5207 				if (onoff)
5208 					so->so_options |= option_name;
5209 				else
5210 					so->so_options &= ~option_name;
5211 				break;
5212 			case SO_USELOOPBACK:
5213 				tcp->tcp_useloopback = onoff;
5214 				if (onoff)
5215 					so->so_options |= option_name;
5216 				else
5217 					so->so_options &= ~option_name;
5218 				break;
5219 			case SO_BROADCAST:
5220 				tcp->tcp_broadcast = onoff;
5221 				if (onoff)
5222 					so->so_options |= option_name;
5223 				else
5224 					so->so_options &= ~option_name;
5225 				break;
5226 			case SO_REUSEADDR:
5227 				tcp->tcp_reuseaddr = onoff;
5228 				if (onoff)
5229 					so->so_options |= option_name;
5230 				else
5231 					so->so_options &= ~option_name;
5232 				break;
5233 			case SO_OOBINLINE:
5234 				tcp->tcp_oobinline = onoff;
5235 				if (onoff)
5236 					so->so_options |= option_name;
5237 				else
5238 					so->so_options &= ~option_name;
5239 				break;
5240 			case SO_DGRAM_ERRIND:
5241 				tcp->tcp_dgram_errind = onoff;
5242 				if (onoff)
5243 					so->so_options |= option_name;
5244 				else
5245 					so->so_options &= ~option_name;
5246 				break;
5247 			}
5248 			break;
5249 		case IPPROTO_TCP:
5250 			switch (option_name) {
5251 			case TCP_NODELAY:
5252 				if (optlen != (t_uscalar_t)sizeof (int32_t)) {
5253 					error = EINVAL;
5254 					eprintsoline(so, error);
5255 					mutex_enter(&so->so_lock);
5256 					goto done2;
5257 				}
5258 				ASSERT(optval);
5259 				tcp->tcp_naglim = intvalue ? 1 : tcp->tcp_mss;
5260 				handled = B_TRUE;
5261 				break;
5262 			}
5263 			break;
5264 		default:
5265 			handled = B_FALSE;
5266 			break;
5267 		}
5268 	}
5269 
5270 	if (handled) {
5271 		mutex_enter(&so->so_lock);
5272 		goto done2;
5273 	}
5274 
5275 	optmgmt_req.PRIM_type = T_SVR4_OPTMGMT_REQ;
5276 	optmgmt_req.MGMT_flags = T_NEGOTIATE;
5277 	optmgmt_req.OPT_length = (t_scalar_t)sizeof (oh) + optlen;
5278 	optmgmt_req.OPT_offset = (t_scalar_t)sizeof (optmgmt_req);
5279 
5280 	oh.level = level;
5281 	oh.name = option_name;
5282 	oh.len = optlen;
5283 
5284 	mp = soallocproto3(&optmgmt_req, sizeof (optmgmt_req),
5285 	    &oh, sizeof (oh), optval, optlen, 0, _ALLOC_SLEEP);
5286 	/* Let option management work in the presence of data flow control */
5287 	error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0,
5288 	    MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR|MSG_IGNFLOW, 0);
5289 	mp = NULL;
5290 	mutex_enter(&so->so_lock);
5291 	if (error) {
5292 		eprintsoline(so, error);
5293 		goto done;
5294 	}
5295 	error = sowaitprim(so, T_SVR4_OPTMGMT_REQ, T_OPTMGMT_ACK,
5296 	    (t_uscalar_t)sizeof (struct T_optmgmt_ack), &mp, 0);
5297 	if (error) {
5298 		eprintsoline(so, error);
5299 		goto done;
5300 	}
5301 	ASSERT(mp);
5302 	/* No need to verify T_optmgmt_ack */
5303 	freemsg(mp);
5304 done:
5305 	/*
5306 	 * Check for SOL_SOCKET options and record their values.
5307 	 * If we know about a SOL_SOCKET parameter and the transport
5308 	 * failed it with TBADOPT or TOUTSTATE (i.e. ENOPROTOOPT or
5309 	 * EPROTO) we let the setsockopt succeed.
5310 	 */
5311 	if (level == SOL_SOCKET) {
5312 		/* Check parameters */
5313 		switch (option_name) {
5314 		case SO_DEBUG:
5315 		case SO_REUSEADDR:
5316 		case SO_KEEPALIVE:
5317 		case SO_DONTROUTE:
5318 		case SO_BROADCAST:
5319 		case SO_USELOOPBACK:
5320 		case SO_OOBINLINE:
5321 		case SO_SNDBUF:
5322 		case SO_RCVBUF:
5323 #ifdef notyet
5324 		case SO_SNDLOWAT:
5325 		case SO_RCVLOWAT:
5326 		case SO_SNDTIMEO:
5327 		case SO_RCVTIMEO:
5328 #endif /* notyet */
5329 		case SO_DGRAM_ERRIND:
5330 			if (optlen != (t_uscalar_t)sizeof (int32_t)) {
5331 				error = EINVAL;
5332 				eprintsoline(so, error);
5333 				goto done2;
5334 			}
5335 			ASSERT(optval);
5336 			handled = B_TRUE;
5337 			break;
5338 		case SO_LINGER:
5339 			if (optlen != (t_uscalar_t)sizeof (struct linger)) {
5340 				error = EINVAL;
5341 				eprintsoline(so, error);
5342 				goto done2;
5343 			}
5344 			ASSERT(optval);
5345 			handled = B_TRUE;
5346 			break;
5347 		}
5348 
5349 #define	intvalue	(*(int32_t *)optval)
5350 
5351 		switch (option_name) {
5352 		case SO_TYPE:
5353 		case SO_ERROR:
5354 		case SO_ACCEPTCONN:
5355 			/* Can't be set */
5356 			error = ENOPROTOOPT;
5357 			goto done2;
5358 		case SO_LINGER: {
5359 			struct linger *l = (struct linger *)optval;
5360 
5361 			so->so_linger.l_linger = l->l_linger;
5362 			if (l->l_onoff) {
5363 				so->so_linger.l_onoff = SO_LINGER;
5364 				so->so_options |= SO_LINGER;
5365 			} else {
5366 				so->so_linger.l_onoff = 0;
5367 				so->so_options &= ~SO_LINGER;
5368 			}
5369 			break;
5370 		}
5371 
5372 		case SO_DEBUG:
5373 #ifdef SOCK_TEST
5374 			if (intvalue & 2)
5375 				sock_test_timelimit = 10 * hz;
5376 			else
5377 				sock_test_timelimit = 0;
5378 
5379 			if (intvalue & 4)
5380 				do_useracc = 0;
5381 			else
5382 				do_useracc = 1;
5383 #endif /* SOCK_TEST */
5384 			/* FALLTHRU */
5385 		case SO_REUSEADDR:
5386 		case SO_KEEPALIVE:
5387 		case SO_DONTROUTE:
5388 		case SO_BROADCAST:
5389 		case SO_USELOOPBACK:
5390 		case SO_OOBINLINE:
5391 		case SO_DGRAM_ERRIND:
5392 			if (intvalue != 0) {
5393 				dprintso(so, 1,
5394 				    ("sotpi_setsockopt: setting 0x%x\n",
5395 				    option_name));
5396 				so->so_options |= option_name;
5397 			} else {
5398 				dprintso(so, 1,
5399 				    ("sotpi_setsockopt: clearing 0x%x\n",
5400 				    option_name));
5401 				so->so_options &= ~option_name;
5402 			}
5403 			break;
5404 		/*
5405 		 * The following options are only returned by us when the
5406 		 * T_SVR4_OPTMGMT_REQ fails.
5407 		 * XXX XPG 4.2 applications retrieve SO_RCVBUF from sockfs
5408 		 * since the transport might adjust the value and not
5409 		 * return exactly what was set by the application.
5410 		 */
5411 		case SO_SNDBUF:
5412 			so->so_sndbuf = intvalue;
5413 			break;
5414 		case SO_RCVBUF:
5415 			so->so_rcvbuf = intvalue;
5416 			break;
5417 #ifdef notyet
5418 		/*
5419 		 * We do not implement the semantics of these options
5420 		 * thus we shouldn't implement the options either.
5421 		 */
5422 		case SO_SNDLOWAT:
5423 			so->so_sndlowat = intvalue;
5424 			break;
5425 		case SO_RCVLOWAT:
5426 			so->so_rcvlowat = intvalue;
5427 			break;
5428 		case SO_SNDTIMEO:
5429 			so->so_sndtimeo = intvalue;
5430 			break;
5431 		case SO_RCVTIMEO:
5432 			so->so_rcvtimeo = intvalue;
5433 			break;
5434 #endif /* notyet */
5435 		}
5436 #undef	intvalue
5437 
5438 		if (error) {
5439 			if ((error == ENOPROTOOPT || error == EPROTO ||
5440 			    error == EINVAL) && handled) {
5441 				dprintso(so, 1,
5442 				    ("setsockopt: ignoring error %d for 0x%x\n",
5443 				    error, option_name));
5444 				error = 0;
5445 			}
5446 		}
5447 	}
5448 done2:
5449 ret:
5450 	so_unlock_single(so, SOLOCKED);
5451 	mutex_exit(&so->so_lock);
5452 	return (error);
5453 }
5454