xref: /illumos-gate/usr/src/uts/common/fs/sockfs/socktpi.c (revision 52aec5b9)
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 (c) 1995, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright 2015, Joyent, Inc.
25  * Copyright 2016 Nexenta Systems, Inc.  All rights reserved.
26  */
27 
28 #include <sys/types.h>
29 #include <sys/t_lock.h>
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/buf.h>
33 #include <sys/conf.h>
34 #include <sys/cred.h>
35 #include <sys/kmem.h>
36 #include <sys/kmem_impl.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/suntpi.h>
51 #include <sys/ddi.h>
52 #include <sys/esunddi.h>
53 #include <sys/flock.h>
54 #include <sys/modctl.h>
55 #include <sys/vtrace.h>
56 #include <sys/cmn_err.h>
57 #include <sys/pathname.h>
58 
59 #include <sys/socket.h>
60 #include <sys/socketvar.h>
61 #include <sys/sockio.h>
62 #include <netinet/in.h>
63 #include <sys/un.h>
64 #include <sys/strsun.h>
65 
66 #include <sys/tiuser.h>
67 #define	_SUN_TPI_VERSION	2
68 #include <sys/tihdr.h>
69 #include <sys/timod.h>		/* TI_GETMYNAME, TI_GETPEERNAME */
70 
71 #include <c2/audit.h>
72 
73 #include <inet/common.h>
74 #include <inet/ip.h>
75 #include <inet/ip6.h>
76 #include <inet/tcp.h>
77 #include <inet/udp_impl.h>
78 
79 #include <sys/zone.h>
80 
81 #include <fs/sockfs/nl7c.h>
82 #include <fs/sockfs/nl7curi.h>
83 
84 #include <fs/sockfs/sockcommon.h>
85 #include <fs/sockfs/socktpi.h>
86 #include <fs/sockfs/socktpi_impl.h>
87 
88 /*
89  * Possible failures when memory can't be allocated. The documented behavior:
90  *
91  *		5.5:			4.X:		XNET:
92  * accept:	ENOMEM/ENOSR/EINTR	- (EINTR)	ENOMEM/ENOBUFS/ENOSR/
93  *							EINTR
94  *	(4.X does not document EINTR but returns it)
95  * bind:	ENOSR			-		ENOBUFS/ENOSR
96  * connect:	EINTR			EINTR		ENOBUFS/ENOSR/EINTR
97  * getpeername:	ENOMEM/ENOSR		ENOBUFS (-)	ENOBUFS/ENOSR
98  * getsockname:	ENOMEM/ENOSR		ENOBUFS (-)	ENOBUFS/ENOSR
99  *	(4.X getpeername and getsockname do not fail in practice)
100  * getsockopt:	ENOMEM/ENOSR		-		ENOBUFS/ENOSR
101  * listen:	-			-		ENOBUFS
102  * recv:	ENOMEM/ENOSR/EINTR	EINTR		ENOBUFS/ENOMEM/ENOSR/
103  *							EINTR
104  * send:	ENOMEM/ENOSR/EINTR	ENOBUFS/EINTR	ENOBUFS/ENOMEM/ENOSR/
105  *							EINTR
106  * setsockopt:	ENOMEM/ENOSR		-		ENOBUFS/ENOMEM/ENOSR
107  * shutdown:	ENOMEM/ENOSR		-		ENOBUFS/ENOSR
108  * socket:	ENOMEM/ENOSR		ENOBUFS		ENOBUFS/ENOMEM/ENOSR
109  * socketpair:	ENOMEM/ENOSR		-		ENOBUFS/ENOMEM/ENOSR
110  *
111  * Resolution. When allocation fails:
112  *	recv: return EINTR
113  *	send: return EINTR
114  *	connect, accept: EINTR
115  *	bind, listen, shutdown (unbind, unix_close, disconnect): sleep
116  *	socket, socketpair: ENOBUFS
117  *	getpeername, getsockname: sleep
118  *	getsockopt, setsockopt: sleep
119  */
120 
121 #ifdef SOCK_TEST
122 /*
123  * Variables that make sockfs do something other than the standard TPI
124  * for the AF_INET transports.
125  *
126  * solisten_tpi_tcp:
127  *	TCP can handle a O_T_BIND_REQ with an increased backlog even though
128  *	the transport is already bound. This is needed to avoid loosing the
129  *	port number should listen() do a T_UNBIND_REQ followed by a
130  *	O_T_BIND_REQ.
131  *
132  * soconnect_tpi_udp:
133  *	UDP and ICMP can handle a T_CONN_REQ.
134  *	This is needed to make the sequence of connect(), getsockname()
135  *	return the local IP address used to send packets to the connected to
136  *	destination.
137  *
138  * soconnect_tpi_tcp:
139  *	TCP can handle a T_CONN_REQ without seeing a O_T_BIND_REQ.
140  *	Set this to non-zero to send TPI conformant messages to TCP in this
141  *	respect. This is a performance optimization.
142  *
143  * soaccept_tpi_tcp:
144  *	TCP can handle a T_CONN_REQ without the acceptor being bound.
145  *	This is a performance optimization that has been picked up in XTI.
146  *
147  * soaccept_tpi_multioptions:
148  *	When inheriting SOL_SOCKET options from the listener to the accepting
149  *	socket send them as a single message for AF_INET{,6}.
150  */
151 int solisten_tpi_tcp = 0;
152 int soconnect_tpi_udp = 0;
153 int soconnect_tpi_tcp = 0;
154 int soaccept_tpi_tcp = 0;
155 int soaccept_tpi_multioptions = 1;
156 #else /* SOCK_TEST */
157 #define	soconnect_tpi_tcp	0
158 #define	soconnect_tpi_udp	0
159 #define	solisten_tpi_tcp	0
160 #define	soaccept_tpi_tcp	0
161 #define	soaccept_tpi_multioptions	1
162 #endif /* SOCK_TEST */
163 
164 #ifdef SOCK_TEST
165 extern int do_useracc;
166 extern clock_t sock_test_timelimit;
167 #endif /* SOCK_TEST */
168 
169 extern uint32_t ucredsize;
170 
171 /*
172  * Some X/Open added checks might have to be backed out to keep SunOS 4.X
173  * applications working. Turn on this flag to disable these checks.
174  */
175 int xnet_skip_checks = 0;
176 int xnet_check_print = 0;
177 int xnet_truncate_print = 0;
178 
179 static void sotpi_destroy(struct sonode *);
180 static struct sonode *sotpi_create(struct sockparams *, int, int, int, int,
181     int, int *, cred_t *cr);
182 
183 static boolean_t	sotpi_info_create(struct sonode *, int);
184 static void		sotpi_info_init(struct sonode *);
185 static void		sotpi_info_fini(struct sonode *);
186 static void		sotpi_info_destroy(struct sonode *);
187 
188 /*
189  * Do direct function call to the transport layer below; this would
190  * also allow the transport to utilize read-side synchronous stream
191  * interface if necessary.  This is a /etc/system tunable that must
192  * not be modified on a running system.  By default this is enabled
193  * for performance reasons and may be disabled for debugging purposes.
194  */
195 boolean_t socktpi_direct = B_TRUE;
196 
197 static struct kmem_cache *socktpi_cache, *socktpi_unix_cache;
198 
199 extern	void sigintr(k_sigset_t *, int);
200 extern	void sigunintr(k_sigset_t *);
201 
202 static int	sotpi_unbind(struct sonode *, int);
203 
204 /* TPI sockfs sonode operations */
205 int		sotpi_init(struct sonode *, struct sonode *, struct cred *,
206 		    int);
207 static int	sotpi_accept(struct sonode *, int, struct cred *,
208 		    struct sonode **);
209 static int	sotpi_bind(struct sonode *, struct sockaddr *, socklen_t,
210 		    int, struct cred *);
211 static int	sotpi_listen(struct sonode *, int, struct cred *);
212 static int	sotpi_connect(struct sonode *, struct sockaddr *,
213 		    socklen_t, int, int, struct cred *);
214 extern int	sotpi_recvmsg(struct sonode *, struct nmsghdr *,
215 		    struct uio *, struct cred *);
216 static int	sotpi_sendmsg(struct sonode *, struct nmsghdr *,
217 		    struct uio *, struct cred *);
218 static int	sotpi_sendmblk(struct sonode *, struct nmsghdr *, int,
219 		    struct cred *, mblk_t **);
220 static int	sosend_dgramcmsg(struct sonode *, struct sockaddr *, socklen_t,
221 		    struct uio *, void *, t_uscalar_t, int);
222 static int	sodgram_direct(struct sonode *, struct sockaddr *,
223 		    socklen_t, struct uio *, int);
224 extern int	sotpi_getpeername(struct sonode *, struct sockaddr *,
225 		    socklen_t *, boolean_t, struct cred *);
226 static int	sotpi_getsockname(struct sonode *, struct sockaddr *,
227 		    socklen_t *, struct cred *);
228 static int	sotpi_shutdown(struct sonode *, int, struct cred *);
229 extern int	sotpi_getsockopt(struct sonode *, int, int, void *,
230 		    socklen_t *, int, struct cred *);
231 extern int	sotpi_setsockopt(struct sonode *, int, int, const void *,
232 		    socklen_t, struct cred *);
233 static int	sotpi_ioctl(struct sonode *, int, intptr_t, int, struct cred *,
234 		    int32_t *);
235 static int	socktpi_plumbioctl(struct vnode *, int, intptr_t, int,
236 		    struct cred *, int32_t *);
237 static int	sotpi_poll(struct sonode *, short, int, short *,
238 		    struct pollhead **);
239 static int	sotpi_close(struct sonode *, int, struct cred *);
240 
241 static int	i_sotpi_info_constructor(sotpi_info_t *);
242 static void	i_sotpi_info_destructor(sotpi_info_t *);
243 
244 sonodeops_t sotpi_sonodeops = {
245 	sotpi_init,		/* sop_init		*/
246 	sotpi_accept,		/* sop_accept		*/
247 	sotpi_bind,		/* sop_bind		*/
248 	sotpi_listen,		/* sop_listen		*/
249 	sotpi_connect,		/* sop_connect		*/
250 	sotpi_recvmsg,		/* sop_recvmsg		*/
251 	sotpi_sendmsg,		/* sop_sendmsg		*/
252 	sotpi_sendmblk,		/* sop_sendmblk		*/
253 	sotpi_getpeername,	/* sop_getpeername	*/
254 	sotpi_getsockname,	/* sop_getsockname	*/
255 	sotpi_shutdown,		/* sop_shutdown		*/
256 	sotpi_getsockopt,	/* sop_getsockopt	*/
257 	sotpi_setsockopt,	/* sop_setsockopt	*/
258 	sotpi_ioctl,		/* sop_ioctl		*/
259 	sotpi_poll,		/* sop_poll		*/
260 	sotpi_close,		/* sop_close		*/
261 };
262 
263 /*
264  * Return a TPI socket vnode.
265  *
266  * Note that sockets assume that the driver will clone (either itself
267  * or by using the clone driver) i.e. a socket() call will always
268  * result in a new vnode being created.
269  */
270 
271 /*
272  * Common create code for socket and accept. If tso is set the values
273  * from that node is used instead of issuing a T_INFO_REQ.
274  */
275 
276 /* ARGSUSED */
277 static struct sonode *
sotpi_create(struct sockparams * sp,int family,int type,int protocol,int version,int sflags,int * errorp,cred_t * cr)278 sotpi_create(struct sockparams *sp, int family, int type, int protocol,
279     int version, int sflags, int *errorp, cred_t *cr)
280 {
281 	struct sonode	*so;
282 	kmem_cache_t	*cp;
283 	int		sfamily = family;
284 
285 	ASSERT(sp->sp_sdev_info.sd_vnode != NULL);
286 
287 	if (family == AF_NCA) {
288 		/*
289 		 * The request is for an NCA socket so for NL7C use the
290 		 * INET domain instead and mark NL7C_AF_NCA below.
291 		 */
292 		family = AF_INET;
293 		/*
294 		 * NL7C is not supported in the non-global zone,
295 		 * we enforce this restriction here.
296 		 */
297 		if (getzoneid() != GLOBAL_ZONEID) {
298 			*errorp = ENOTSUP;
299 			return (NULL);
300 		}
301 	}
302 
303 	/*
304 	 * to be compatible with old tpi socket implementation ignore
305 	 * sleep flag (sflags) passed in
306 	 */
307 	cp = (family == AF_UNIX) ? socktpi_unix_cache : socktpi_cache;
308 	so = kmem_cache_alloc(cp, KM_SLEEP);
309 	if (so == NULL) {
310 		*errorp = ENOMEM;
311 		return (NULL);
312 	}
313 
314 	sonode_init(so, sp, family, type, protocol, &sotpi_sonodeops);
315 	sotpi_info_init(so);
316 
317 	if (sfamily == AF_NCA) {
318 		SOTOTPI(so)->sti_nl7c_flags = NL7C_AF_NCA;
319 	}
320 
321 	if (version == SOV_DEFAULT)
322 		version = so_default_version;
323 
324 	so->so_version = (short)version;
325 	*errorp = 0;
326 
327 	return (so);
328 }
329 
330 static void
sotpi_destroy(struct sonode * so)331 sotpi_destroy(struct sonode *so)
332 {
333 	kmem_cache_t *cp;
334 	struct sockparams *origsp;
335 
336 	/*
337 	 * If there is a new dealloc function (ie. smod_destroy_func),
338 	 * then it should check the correctness of the ops.
339 	 */
340 
341 	ASSERT(so->so_ops == &sotpi_sonodeops);
342 
343 	origsp = SOTOTPI(so)->sti_orig_sp;
344 
345 	sotpi_info_fini(so);
346 
347 	if (so->so_state & SS_FALLBACK_COMP) {
348 		/*
349 		 * A fallback happend, which means that a sotpi_info_t struct
350 		 * was allocated (as opposed to being allocated from the TPI
351 		 * sonode cache. Therefore we explicitly free the struct
352 		 * here.
353 		 */
354 		sotpi_info_destroy(so);
355 		ASSERT(origsp != NULL);
356 
357 		origsp->sp_smod_info->smod_sock_destroy_func(so);
358 		SOCKPARAMS_DEC_REF(origsp);
359 	} else {
360 		sonode_fini(so);
361 		cp = (so->so_family == AF_UNIX) ? socktpi_unix_cache :
362 		    socktpi_cache;
363 		kmem_cache_free(cp, so);
364 	}
365 }
366 
367 /* ARGSUSED1 */
368 int
sotpi_init(struct sonode * so,struct sonode * tso,struct cred * cr,int flags)369 sotpi_init(struct sonode *so, struct sonode *tso, struct cred *cr, int flags)
370 {
371 	major_t maj;
372 	dev_t newdev;
373 	struct vnode *vp;
374 	int error = 0;
375 	struct stdata *stp;
376 
377 	sotpi_info_t *sti = SOTOTPI(so);
378 
379 	dprint(1, ("sotpi_init()\n"));
380 
381 	/*
382 	 * over write the sleep flag passed in but that is ok
383 	 * as tpi socket does not honor sleep flag.
384 	 */
385 	flags |= FREAD|FWRITE;
386 
387 	/*
388 	 * Record in so_flag that it is a clone.
389 	 */
390 	if (getmajor(sti->sti_dev) == clone_major)
391 		so->so_flag |= SOCLONE;
392 
393 	if ((so->so_type == SOCK_STREAM || so->so_type == SOCK_DGRAM) &&
394 	    (so->so_family == AF_INET || so->so_family == AF_INET6) &&
395 	    (so->so_protocol == IPPROTO_TCP || so->so_protocol == IPPROTO_UDP ||
396 	    so->so_protocol == IPPROTO_IP)) {
397 		/* Tell tcp or udp that it's talking to sockets */
398 		flags |= SO_SOCKSTR;
399 
400 		/*
401 		 * Here we indicate to socktpi_open() our attempt to
402 		 * make direct calls between sockfs and transport.
403 		 * The final decision is left to socktpi_open().
404 		 */
405 		sti->sti_direct = 1;
406 
407 		ASSERT(so->so_type != SOCK_DGRAM || tso == NULL);
408 		if (so->so_type == SOCK_STREAM && tso != NULL) {
409 			if (SOTOTPI(tso)->sti_direct) {
410 				/*
411 				 * Inherit sti_direct from listener and pass
412 				 * SO_ACCEPTOR open flag to tcp, indicating
413 				 * that this is an accept fast-path instance.
414 				 */
415 				flags |= SO_ACCEPTOR;
416 			} else {
417 				/*
418 				 * sti_direct is not set on listener, meaning
419 				 * that the listener has been converted from
420 				 * a socket to a stream.  Ensure that the
421 				 * acceptor inherits these settings.
422 				 */
423 				sti->sti_direct = 0;
424 				flags &= ~SO_SOCKSTR;
425 			}
426 		}
427 	}
428 
429 	/*
430 	 * Tell local transport that it is talking to sockets.
431 	 */
432 	if (so->so_family == AF_UNIX) {
433 		flags |= SO_SOCKSTR;
434 	}
435 
436 	vp = SOTOV(so);
437 	newdev = vp->v_rdev;
438 	maj = getmajor(newdev);
439 	ASSERT(STREAMSTAB(maj));
440 
441 	error = stropen(vp, &newdev, flags, cr);
442 
443 	stp = vp->v_stream;
444 	if (error == 0) {
445 		if (so->so_flag & SOCLONE)
446 			ASSERT(newdev != vp->v_rdev);
447 		mutex_enter(&so->so_lock);
448 		sti->sti_dev = newdev;
449 		vp->v_rdev = newdev;
450 		mutex_exit(&so->so_lock);
451 
452 		if (stp->sd_flag & STRISTTY) {
453 			/*
454 			 * this is a post SVR4 tty driver - a socket can not
455 			 * be a controlling terminal. Fail the open.
456 			 */
457 			(void) sotpi_close(so, flags, cr);
458 			return (ENOTTY);	/* XXX */
459 		}
460 
461 		ASSERT(stp->sd_wrq != NULL);
462 		sti->sti_provinfo = tpi_findprov(stp->sd_wrq);
463 
464 		/*
465 		 * If caller is interested in doing direct function call
466 		 * interface to/from transport module, probe the module
467 		 * directly beneath the streamhead to see if it qualifies.
468 		 *
469 		 * We turn off the direct interface when qualifications fail.
470 		 * In the acceptor case, we simply turn off the sti_direct
471 		 * flag on the socket. We do the fallback after the accept
472 		 * has completed, before the new socket is returned to the
473 		 * application.
474 		 */
475 		if (sti->sti_direct) {
476 			queue_t *tq = stp->sd_wrq->q_next;
477 
478 			/*
479 			 * sti_direct is currently supported and tested
480 			 * only for tcp/udp; this is the main reason to
481 			 * have the following assertions.
482 			 */
483 			ASSERT(so->so_family == AF_INET ||
484 			    so->so_family == AF_INET6);
485 			ASSERT(so->so_protocol == IPPROTO_UDP ||
486 			    so->so_protocol == IPPROTO_TCP ||
487 			    so->so_protocol == IPPROTO_IP);
488 			ASSERT(so->so_type == SOCK_DGRAM ||
489 			    so->so_type == SOCK_STREAM);
490 
491 			/*
492 			 * Abort direct call interface if the module directly
493 			 * underneath the stream head is not defined with the
494 			 * _D_DIRECT flag.  This could happen in the tcp or
495 			 * udp case, when some other module is autopushed
496 			 * above it, or for some reasons the expected module
497 			 * isn't purely D_MP (which is the main requirement).
498 			 */
499 			if (!socktpi_direct || !(tq->q_flag & _QDIRECT) ||
500 			    !(_OTHERQ(tq)->q_flag & _QDIRECT)) {
501 				int rval;
502 
503 				/* Continue on without direct calls */
504 				sti->sti_direct = 0;
505 
506 				/*
507 				 * Cannot issue ioctl on fallback socket since
508 				 * there is no conn associated with the queue.
509 				 * The fallback downcall will notify the proto
510 				 * of the change.
511 				 */
512 				if (!(flags & SO_ACCEPTOR) &&
513 				    !(flags & SO_FALLBACK)) {
514 					if ((error = strioctl(vp,
515 					    _SIOCSOCKFALLBACK, 0, 0, K_TO_K,
516 					    cr, &rval)) != 0) {
517 						(void) sotpi_close(so, flags,
518 						    cr);
519 						return (error);
520 					}
521 				}
522 			}
523 		}
524 
525 		if (flags & SO_FALLBACK) {
526 			/*
527 			 * The stream created does not have a conn.
528 			 * do stream set up after conn has been assigned
529 			 */
530 			return (error);
531 		}
532 		if (error = so_strinit(so, tso)) {
533 			(void) sotpi_close(so, flags, cr);
534 			return (error);
535 		}
536 
537 		/* Enable sendfile() on AF_UNIX streams */
538 		if (so->so_family == AF_UNIX && so->so_type == SOCK_STREAM) {
539 			mutex_enter(&so->so_lock);
540 			so->so_mode |= SM_SENDFILESUPP;
541 			mutex_exit(&so->so_lock);
542 		}
543 
544 		/* Wildcard */
545 		if (so->so_protocol != so->so_sockparams->sp_protocol) {
546 			int protocol = so->so_protocol;
547 			/*
548 			 * Issue SO_PROTOTYPE setsockopt.
549 			 */
550 			error = sotpi_setsockopt(so, SOL_SOCKET, SO_PROTOTYPE,
551 			    &protocol, (t_uscalar_t)sizeof (protocol), cr);
552 			if (error != 0) {
553 				(void) sotpi_close(so, flags, cr);
554 				/*
555 				 * Setsockopt often fails with ENOPROTOOPT but
556 				 * socket() should fail with
557 				 * EPROTONOSUPPORT/EPROTOTYPE.
558 				 */
559 				return (EPROTONOSUPPORT);
560 			}
561 		}
562 
563 	} else {
564 		/*
565 		 * While the same socket can not be reopened (unlike specfs)
566 		 * the stream head sets STREOPENFAIL when the autopush fails.
567 		 */
568 		if ((stp != NULL) &&
569 		    (stp->sd_flag & STREOPENFAIL)) {
570 			/*
571 			 * Open failed part way through.
572 			 */
573 			mutex_enter(&stp->sd_lock);
574 			stp->sd_flag &= ~STREOPENFAIL;
575 			mutex_exit(&stp->sd_lock);
576 			(void) sotpi_close(so, flags, cr);
577 			return (error);
578 			/*NOTREACHED*/
579 		}
580 		ASSERT(stp == NULL);
581 	}
582 	TRACE_4(TR_FAC_SOCKFS, TR_SOCKFS_OPEN,
583 	    "sockfs open:maj %d vp %p so %p error %d",
584 	    maj, vp, so, error);
585 	return (error);
586 }
587 
588 /*
589  * Bind the socket to an unspecified address in sockfs only.
590  * Used for TCP/UDP transports where we know that the O_T_BIND_REQ isn't
591  * required in all cases.
592  */
593 static void
so_automatic_bind(struct sonode * so)594 so_automatic_bind(struct sonode *so)
595 {
596 	sotpi_info_t *sti = SOTOTPI(so);
597 	ASSERT(so->so_family == AF_INET || so->so_family == AF_INET6);
598 
599 	ASSERT(MUTEX_HELD(&so->so_lock));
600 	ASSERT(!(so->so_state & SS_ISBOUND));
601 	ASSERT(sti->sti_unbind_mp);
602 
603 	ASSERT(sti->sti_laddr_len <= sti->sti_laddr_maxlen);
604 	bzero(sti->sti_laddr_sa, sti->sti_laddr_len);
605 	sti->sti_laddr_sa->sa_family = so->so_family;
606 	so->so_state |= SS_ISBOUND;
607 }
608 
609 
610 /*
611  * bind the socket.
612  *
613  * If the socket is already bound and none of _SOBIND_SOCKBSD or _SOBIND_XPG4_2
614  * are passed in we allow rebinding. Note that for backwards compatibility
615  * even "svr4" sockets pass in _SOBIND_SOCKBSD/SOV_SOCKBSD to sobind/bind.
616  * Thus the rebinding code is currently not executed.
617  *
618  * The constraints for rebinding are:
619  * - it is a SOCK_DGRAM, or
620  * - it is a SOCK_STREAM/SOCK_SEQPACKET that has not been connected
621  *   and no listen() has been done.
622  * This rebinding code was added based on some language in the XNET book
623  * about not returning EINVAL it the protocol allows rebinding. However,
624  * this language is not present in the Posix socket draft. Thus maybe the
625  * rebinding logic should be deleted from the source.
626  *
627  * A null "name" can be used to unbind the socket if:
628  * - it is a SOCK_DGRAM, or
629  * - it is a SOCK_STREAM/SOCK_SEQPACKET that has not been connected
630  *   and no listen() has been done.
631  */
632 /* ARGSUSED */
633 static int
sotpi_bindlisten(struct sonode * so,struct sockaddr * name,socklen_t namelen,int backlog,int flags,struct cred * cr)634 sotpi_bindlisten(struct sonode *so, struct sockaddr *name,
635     socklen_t namelen, int backlog, int flags, struct cred *cr)
636 {
637 	struct T_bind_req	bind_req;
638 	struct T_bind_ack	*bind_ack;
639 	int			error = 0;
640 	mblk_t			*mp;
641 	void			*addr;
642 	t_uscalar_t		addrlen;
643 	int			unbind_on_err = 1;
644 	boolean_t		clear_acceptconn_on_err = B_FALSE;
645 	boolean_t		restore_backlog_on_err = B_FALSE;
646 	int			save_so_backlog;
647 	t_scalar_t		PRIM_type = O_T_BIND_REQ;
648 	boolean_t		tcp_udp_xport;
649 	void			*nl7c = NULL;
650 	sotpi_info_t		*sti = SOTOTPI(so);
651 
652 	dprintso(so, 1, ("sotpi_bindlisten(%p, %p, %d, %d, 0x%x) %s\n",
653 	    (void *)so, (void *)name, namelen, backlog, flags,
654 	    pr_state(so->so_state, so->so_mode)));
655 
656 	tcp_udp_xport = so->so_type == SOCK_STREAM || so->so_type == SOCK_DGRAM;
657 
658 	if (!(flags & _SOBIND_LOCK_HELD)) {
659 		mutex_enter(&so->so_lock);
660 		so_lock_single(so);	/* Set SOLOCKED */
661 	} else {
662 		ASSERT(MUTEX_HELD(&so->so_lock));
663 		ASSERT(so->so_flag & SOLOCKED);
664 	}
665 
666 	/*
667 	 * Make sure that there is a preallocated unbind_req message
668 	 * before binding. This message allocated when the socket is
669 	 * created  but it might be have been consumed.
670 	 */
671 	if (sti->sti_unbind_mp == NULL) {
672 		dprintso(so, 1, ("sobind: allocating unbind_req\n"));
673 		/* NOTE: holding so_lock while sleeping */
674 		sti->sti_unbind_mp =
675 		    soallocproto(sizeof (struct T_unbind_req), _ALLOC_SLEEP,
676 		    cr);
677 	}
678 
679 	if (flags & _SOBIND_REBIND) {
680 		/*
681 		 * Called from solisten after doing an sotpi_unbind() or
682 		 * potentially without the unbind (latter for AF_INET{,6}).
683 		 */
684 		ASSERT(name == NULL && namelen == 0);
685 
686 		if (so->so_family == AF_UNIX) {
687 			ASSERT(sti->sti_ux_bound_vp);
688 			addr = &sti->sti_ux_laddr;
689 			addrlen = (t_uscalar_t)sizeof (sti->sti_ux_laddr);
690 			dprintso(so, 1, ("sobind rebind UNIX: addrlen %d, "
691 			    "addr 0x%p, vp %p\n",
692 			    addrlen,
693 			    (void *)((struct so_ux_addr *)addr)->soua_vp,
694 			    (void *)sti->sti_ux_bound_vp));
695 		} else {
696 			addr = sti->sti_laddr_sa;
697 			addrlen = (t_uscalar_t)sti->sti_laddr_len;
698 		}
699 	} else if (flags & _SOBIND_UNSPEC) {
700 		ASSERT(name == NULL && namelen == 0);
701 
702 		/*
703 		 * The caller checked SS_ISBOUND but not necessarily
704 		 * under so_lock
705 		 */
706 		if (so->so_state & SS_ISBOUND) {
707 			/* No error */
708 			goto done;
709 		}
710 
711 		/* Set an initial local address */
712 		switch (so->so_family) {
713 		case AF_UNIX:
714 			/*
715 			 * Use an address with same size as struct sockaddr
716 			 * just like BSD.
717 			 */
718 			sti->sti_laddr_len =
719 			    (socklen_t)sizeof (struct sockaddr);
720 			ASSERT(sti->sti_laddr_len <= sti->sti_laddr_maxlen);
721 			bzero(sti->sti_laddr_sa, sti->sti_laddr_len);
722 			sti->sti_laddr_sa->sa_family = so->so_family;
723 
724 			/*
725 			 * Pass down an address with the implicit bind
726 			 * magic number and the rest all zeros.
727 			 * The transport will return a unique address.
728 			 */
729 			sti->sti_ux_laddr.soua_vp = NULL;
730 			sti->sti_ux_laddr.soua_magic = SOU_MAGIC_IMPLICIT;
731 			addr = &sti->sti_ux_laddr;
732 			addrlen = (t_uscalar_t)sizeof (sti->sti_ux_laddr);
733 			break;
734 
735 		case AF_INET:
736 		case AF_INET6:
737 			/*
738 			 * An unspecified bind in TPI has a NULL address.
739 			 * Set the address in sockfs to have the sa_family.
740 			 */
741 			sti->sti_laddr_len = (so->so_family == AF_INET) ?
742 			    (socklen_t)sizeof (sin_t) :
743 			    (socklen_t)sizeof (sin6_t);
744 			ASSERT(sti->sti_laddr_len <= sti->sti_laddr_maxlen);
745 			bzero(sti->sti_laddr_sa, sti->sti_laddr_len);
746 			sti->sti_laddr_sa->sa_family = so->so_family;
747 			addr = NULL;
748 			addrlen = 0;
749 			break;
750 
751 		default:
752 			/*
753 			 * An unspecified bind in TPI has a NULL address.
754 			 * Set the address in sockfs to be zero length.
755 			 *
756 			 * Can not assume there is a sa_family for all
757 			 * protocol families. For example, AF_X25 does not
758 			 * have a family field.
759 			 */
760 			bzero(sti->sti_laddr_sa, sti->sti_laddr_len);
761 			sti->sti_laddr_len = 0;	/* XXX correct? */
762 			addr = NULL;
763 			addrlen = 0;
764 			break;
765 		}
766 
767 	} else {
768 		if (so->so_state & SS_ISBOUND) {
769 			/*
770 			 * If it is ok to rebind the socket, first unbind
771 			 * with the transport. A rebind to the NULL address
772 			 * is interpreted as an unbind.
773 			 * Note that a bind to NULL in BSD does unbind the
774 			 * socket but it fails with EINVAL.
775 			 * Note that regular sockets set SOV_SOCKBSD i.e.
776 			 * _SOBIND_SOCKBSD gets set here hence no type of
777 			 * socket does currently allow rebinding.
778 			 *
779 			 * If the name is NULL just do an unbind.
780 			 */
781 			if (flags & (_SOBIND_SOCKBSD|_SOBIND_XPG4_2) &&
782 			    name != NULL) {
783 				error = EINVAL;
784 				unbind_on_err = 0;
785 				eprintsoline(so, error);
786 				goto done;
787 			}
788 			if ((so->so_mode & SM_CONNREQUIRED) &&
789 			    (so->so_state & SS_CANTREBIND)) {
790 				error = EINVAL;
791 				unbind_on_err = 0;
792 				eprintsoline(so, error);
793 				goto done;
794 			}
795 			error = sotpi_unbind(so, 0);
796 			if (error) {
797 				eprintsoline(so, error);
798 				goto done;
799 			}
800 			ASSERT(!(so->so_state & SS_ISBOUND));
801 			if (name == NULL) {
802 				so->so_state &=
803 				    ~(SS_ISCONNECTED|SS_ISCONNECTING);
804 				goto done;
805 			}
806 		}
807 
808 		/* X/Open requires this check */
809 		if ((so->so_state & SS_CANTSENDMORE) && !xnet_skip_checks) {
810 			if (xnet_check_print) {
811 				printf("sockfs: X/Open bind state check "
812 				    "caused EINVAL\n");
813 			}
814 			error = EINVAL;
815 			goto done;
816 		}
817 
818 		switch (so->so_family) {
819 		case AF_UNIX:
820 			/*
821 			 * All AF_UNIX addresses are nul terminated
822 			 * when copied (copyin_name) in so the minimum
823 			 * length is 3 bytes.
824 			 */
825 			if (name == NULL ||
826 			    (ssize_t)namelen <= sizeof (short) + 1) {
827 				error = EISDIR;
828 				eprintsoline(so, error);
829 				goto done;
830 			}
831 			/*
832 			 * Verify so_family matches the bound family.
833 			 * BSD does not check this for AF_UNIX resulting
834 			 * in funny mknods.
835 			 */
836 			if (name->sa_family != so->so_family) {
837 				error = EAFNOSUPPORT;
838 				goto done;
839 			}
840 			break;
841 		case AF_INET:
842 			if (name == NULL) {
843 				error = EINVAL;
844 				eprintsoline(so, error);
845 				goto done;
846 			}
847 			if ((size_t)namelen != sizeof (sin_t)) {
848 				error = name->sa_family != so->so_family ?
849 				    EAFNOSUPPORT : EINVAL;
850 				eprintsoline(so, error);
851 				goto done;
852 			}
853 			if ((flags & _SOBIND_XPG4_2) &&
854 			    (name->sa_family != so->so_family)) {
855 				/*
856 				 * This check has to be made for X/Open
857 				 * sockets however application failures have
858 				 * been observed when it is applied to
859 				 * all sockets.
860 				 */
861 				error = EAFNOSUPPORT;
862 				eprintsoline(so, error);
863 				goto done;
864 			}
865 			/*
866 			 * Force a zero sa_family to match so_family.
867 			 *
868 			 * Some programs like inetd(1M) don't set the
869 			 * family field. Other programs leave
870 			 * sin_family set to garbage - SunOS 4.X does
871 			 * not check the family field on a bind.
872 			 * We use the family field that
873 			 * was passed in to the socket() call.
874 			 */
875 			name->sa_family = so->so_family;
876 			break;
877 
878 		case AF_INET6: {
879 #ifdef DEBUG
880 			sin6_t *sin6 = (sin6_t *)name;
881 #endif /* DEBUG */
882 
883 			if (name == NULL) {
884 				error = EINVAL;
885 				eprintsoline(so, error);
886 				goto done;
887 			}
888 			if ((size_t)namelen != sizeof (sin6_t)) {
889 				error = name->sa_family != so->so_family ?
890 				    EAFNOSUPPORT : EINVAL;
891 				eprintsoline(so, error);
892 				goto done;
893 			}
894 			if (name->sa_family != so->so_family) {
895 				/*
896 				 * With IPv6 we require the family to match
897 				 * unlike in IPv4.
898 				 */
899 				error = EAFNOSUPPORT;
900 				eprintsoline(so, error);
901 				goto done;
902 			}
903 #ifdef DEBUG
904 			/*
905 			 * Verify that apps don't forget to clear
906 			 * sin6_scope_id etc
907 			 */
908 			if (sin6->sin6_scope_id != 0 &&
909 			    !IN6_IS_ADDR_LINKSCOPE(&sin6->sin6_addr)) {
910 				zcmn_err(getzoneid(), CE_WARN,
911 				    "bind with uninitialized sin6_scope_id "
912 				    "(%d) on socket. Pid = %d\n",
913 				    (int)sin6->sin6_scope_id,
914 				    (int)curproc->p_pid);
915 			}
916 			if (sin6->__sin6_src_id != 0) {
917 				zcmn_err(getzoneid(), CE_WARN,
918 				    "bind with uninitialized __sin6_src_id "
919 				    "(%d) on socket. Pid = %d\n",
920 				    (int)sin6->__sin6_src_id,
921 				    (int)curproc->p_pid);
922 			}
923 #endif /* DEBUG */
924 			break;
925 		}
926 		default:
927 			/*
928 			 * Don't do any length or sa_family check to allow
929 			 * non-sockaddr style addresses.
930 			 */
931 			if (name == NULL) {
932 				error = EINVAL;
933 				eprintsoline(so, error);
934 				goto done;
935 			}
936 			break;
937 		}
938 
939 		if (namelen > (t_uscalar_t)sti->sti_laddr_maxlen) {
940 			error = ENAMETOOLONG;
941 			eprintsoline(so, error);
942 			goto done;
943 		}
944 		/*
945 		 * Save local address.
946 		 */
947 		sti->sti_laddr_len = (socklen_t)namelen;
948 		ASSERT(sti->sti_laddr_len <= sti->sti_laddr_maxlen);
949 		bcopy(name, sti->sti_laddr_sa, namelen);
950 
951 		addr = sti->sti_laddr_sa;
952 		addrlen = (t_uscalar_t)sti->sti_laddr_len;
953 		switch (so->so_family) {
954 		case AF_INET6:
955 		case AF_INET:
956 			break;
957 		case AF_UNIX: {
958 			struct sockaddr_un *soun =
959 			    (struct sockaddr_un *)sti->sti_laddr_sa;
960 			struct vnode *vp, *rvp;
961 			struct vattr vattr;
962 
963 			ASSERT(sti->sti_ux_bound_vp == NULL);
964 			/*
965 			 * Create vnode for the specified path name.
966 			 * Keep vnode held with a reference in sti_ux_bound_vp.
967 			 * Use the vnode pointer as the address used in the
968 			 * bind with the transport.
969 			 *
970 			 * Use the same mode as in BSD. In particular this does
971 			 * not observe the umask.
972 			 */
973 			/* MAXPATHLEN + soun_family + nul termination */
974 			if (sti->sti_laddr_len >
975 			    (socklen_t)(MAXPATHLEN + sizeof (short) + 1)) {
976 				error = ENAMETOOLONG;
977 				eprintsoline(so, error);
978 				goto done;
979 			}
980 			vattr.va_type = VSOCK;
981 			vattr.va_mode = 0777 & ~PTOU(curproc)->u_cmask;
982 			vattr.va_mask = AT_TYPE|AT_MODE;
983 			/* NOTE: holding so_lock */
984 			error = vn_create(soun->sun_path, UIO_SYSSPACE, &vattr,
985 			    EXCL, 0, &vp, CRMKNOD, 0, 0);
986 			if (error) {
987 				if (error == EEXIST)
988 					error = EADDRINUSE;
989 				eprintsoline(so, error);
990 				goto done;
991 			}
992 			/*
993 			 * Establish pointer from the underlying filesystem
994 			 * vnode to the socket node.
995 			 * sti_ux_bound_vp and v_stream->sd_vnode form the
996 			 * cross-linkage between the underlying filesystem
997 			 * node and the socket node.
998 			 */
999 
1000 			if ((VOP_REALVP(vp, &rvp, NULL) == 0) && (vp != rvp)) {
1001 				VN_HOLD(rvp);
1002 				VN_RELE(vp);
1003 				vp = rvp;
1004 			}
1005 
1006 			ASSERT(SOTOV(so)->v_stream);
1007 			mutex_enter(&vp->v_lock);
1008 			vp->v_stream = SOTOV(so)->v_stream;
1009 			sti->sti_ux_bound_vp = vp;
1010 			mutex_exit(&vp->v_lock);
1011 
1012 			/*
1013 			 * Use the vnode pointer value as a unique address
1014 			 * (together with the magic number to avoid conflicts
1015 			 * with implicit binds) in the transport provider.
1016 			 */
1017 			sti->sti_ux_laddr.soua_vp =
1018 			    (void *)sti->sti_ux_bound_vp;
1019 			sti->sti_ux_laddr.soua_magic = SOU_MAGIC_EXPLICIT;
1020 			addr = &sti->sti_ux_laddr;
1021 			addrlen = (t_uscalar_t)sizeof (sti->sti_ux_laddr);
1022 			dprintso(so, 1, ("sobind UNIX: addrlen %d, addr %p\n",
1023 			    addrlen,
1024 			    (void *)((struct so_ux_addr *)addr)->soua_vp));
1025 			break;
1026 		}
1027 		} /* end switch (so->so_family) */
1028 	}
1029 
1030 	/*
1031 	 * set SS_ACCEPTCONN before sending down O_T_BIND_REQ since
1032 	 * the transport can start passing up T_CONN_IND messages
1033 	 * as soon as it receives the bind req and strsock_proto()
1034 	 * insists that SS_ACCEPTCONN is set when processing T_CONN_INDs.
1035 	 */
1036 	if (flags & _SOBIND_LISTEN) {
1037 		if ((so->so_state & SS_ACCEPTCONN) == 0)
1038 			clear_acceptconn_on_err = B_TRUE;
1039 		save_so_backlog = so->so_backlog;
1040 		restore_backlog_on_err = B_TRUE;
1041 		so->so_state |= SS_ACCEPTCONN;
1042 		so->so_backlog = backlog;
1043 	}
1044 
1045 	/*
1046 	 * If NL7C addr(s) have been configured check for addr/port match,
1047 	 * or if an implicit NL7C socket via AF_NCA mark socket as NL7C.
1048 	 *
1049 	 * NL7C supports the TCP transport only so check AF_INET and AF_INET6
1050 	 * family sockets only. If match mark as such.
1051 	 */
1052 	if (nl7c_enabled && ((addr != NULL &&
1053 	    (so->so_family == AF_INET || so->so_family == AF_INET6) &&
1054 	    (nl7c = nl7c_lookup_addr(addr, addrlen))) ||
1055 	    sti->sti_nl7c_flags == NL7C_AF_NCA)) {
1056 		/*
1057 		 * NL7C is not supported in non-global zones,
1058 		 * we enforce this restriction here.
1059 		 */
1060 		if (so->so_zoneid == GLOBAL_ZONEID) {
1061 			/* An NL7C socket, mark it */
1062 			sti->sti_nl7c_flags |= NL7C_ENABLED;
1063 			if (nl7c == NULL) {
1064 				/*
1065 				 * Was an AF_NCA bind() so add it to the
1066 				 * addr list for reporting purposes.
1067 				 */
1068 				nl7c = nl7c_add_addr(addr, addrlen);
1069 			}
1070 		} else
1071 			nl7c = NULL;
1072 	}
1073 
1074 	/*
1075 	 * We send a T_BIND_REQ for TCP/UDP since we know it supports it,
1076 	 * for other transports we will send in a O_T_BIND_REQ.
1077 	 */
1078 	if (tcp_udp_xport &&
1079 	    (so->so_family == AF_INET || so->so_family == AF_INET6))
1080 		PRIM_type = T_BIND_REQ;
1081 
1082 	bind_req.PRIM_type = PRIM_type;
1083 	bind_req.ADDR_length = addrlen;
1084 	bind_req.ADDR_offset = (t_scalar_t)sizeof (bind_req);
1085 	bind_req.CONIND_number = backlog;
1086 	/* NOTE: holding so_lock while sleeping */
1087 	mp = soallocproto2(&bind_req, sizeof (bind_req),
1088 	    addr, addrlen, 0, _ALLOC_SLEEP, cr);
1089 	sti->sti_laddr_valid = 0;
1090 
1091 	/* Done using sti_laddr_sa - can drop the lock */
1092 	mutex_exit(&so->so_lock);
1093 
1094 	error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0,
1095 	    MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0);
1096 	if (error) {
1097 		eprintsoline(so, error);
1098 		mutex_enter(&so->so_lock);
1099 		goto done;
1100 	}
1101 
1102 	mutex_enter(&so->so_lock);
1103 	error = sowaitprim(so, PRIM_type, T_BIND_ACK,
1104 	    (t_uscalar_t)sizeof (*bind_ack), &mp, 0);
1105 	if (error) {
1106 		eprintsoline(so, error);
1107 		goto done;
1108 	}
1109 	ASSERT(mp);
1110 	/*
1111 	 * Even if some TPI message (e.g. T_DISCON_IND) was received in
1112 	 * strsock_proto while the lock was dropped above, the bind
1113 	 * is allowed to complete.
1114 	 */
1115 
1116 	/* Mark as bound. This will be undone if we detect errors below. */
1117 	if (flags & _SOBIND_NOXLATE) {
1118 		ASSERT(so->so_family == AF_UNIX);
1119 		sti->sti_faddr_noxlate = 1;
1120 	}
1121 	ASSERT(!(so->so_state & SS_ISBOUND) || (flags & _SOBIND_REBIND));
1122 	so->so_state |= SS_ISBOUND;
1123 	ASSERT(sti->sti_unbind_mp);
1124 
1125 	/* note that we've already set SS_ACCEPTCONN above */
1126 
1127 	/*
1128 	 * Recompute addrlen - an unspecied bind sent down an
1129 	 * address of length zero but we expect the appropriate length
1130 	 * in return.
1131 	 */
1132 	addrlen = (t_uscalar_t)(so->so_family == AF_UNIX ?
1133 	    sizeof (sti->sti_ux_laddr) : sti->sti_laddr_len);
1134 
1135 	bind_ack = (struct T_bind_ack *)mp->b_rptr;
1136 	/*
1137 	 * The alignment restriction is really too strict but
1138 	 * we want enough alignment to inspect the fields of
1139 	 * a sockaddr_in.
1140 	 */
1141 	addr = sogetoff(mp, bind_ack->ADDR_offset,
1142 	    bind_ack->ADDR_length,
1143 	    __TPI_ALIGN_SIZE);
1144 	if (addr == NULL) {
1145 		freemsg(mp);
1146 		error = EPROTO;
1147 		eprintsoline(so, error);
1148 		goto done;
1149 	}
1150 	if (!(flags & _SOBIND_UNSPEC)) {
1151 		/*
1152 		 * Verify that the transport didn't return something we
1153 		 * did not want e.g. an address other than what we asked for.
1154 		 *
1155 		 * NOTE: These checks would go away if/when we switch to
1156 		 * using the new TPI (in which the transport would fail
1157 		 * the request instead of assigning a different address).
1158 		 *
1159 		 * NOTE2: For protocols that we don't know (i.e. any
1160 		 * other than AF_INET6, AF_INET and AF_UNIX), we
1161 		 * cannot know if the transport should be expected to
1162 		 * return the same address as that requested.
1163 		 *
1164 		 * NOTE3: For AF_INET and AF_INET6, TCP/UDP, we send
1165 		 * down a T_BIND_REQ. We use O_T_BIND_REQ for others.
1166 		 *
1167 		 * For example, in the case of netatalk it may be
1168 		 * inappropriate for the transport to return the
1169 		 * requested address (as it may have allocated a local
1170 		 * port number in behaviour similar to that of an
1171 		 * AF_INET bind request with a port number of zero).
1172 		 *
1173 		 * Given the definition of O_T_BIND_REQ, where the
1174 		 * transport may bind to an address other than the
1175 		 * requested address, it's not possible to determine
1176 		 * whether a returned address that differs from the
1177 		 * requested address is a reason to fail (because the
1178 		 * requested address was not available) or succeed
1179 		 * (because the transport allocated an appropriate
1180 		 * address and/or port).
1181 		 *
1182 		 * sockfs currently requires that the transport return
1183 		 * the requested address in the T_BIND_ACK, unless
1184 		 * there is code here to allow for any discrepancy.
1185 		 * Such code exists for AF_INET and AF_INET6.
1186 		 *
1187 		 * Netatalk chooses to return the requested address
1188 		 * rather than the (correct) allocated address.  This
1189 		 * means that netatalk violates the TPI specification
1190 		 * (and would not function correctly if used from a
1191 		 * TLI application), but it does mean that it works
1192 		 * with sockfs.
1193 		 *
1194 		 * As noted above, using the newer XTI bind primitive
1195 		 * (T_BIND_REQ) in preference to O_T_BIND_REQ would
1196 		 * allow sockfs to be more sure about whether or not
1197 		 * the bind request had succeeded (as transports are
1198 		 * not permitted to bind to a different address than
1199 		 * that requested - they must return failure).
1200 		 * Unfortunately, support for T_BIND_REQ may not be
1201 		 * present in all transport implementations (netatalk,
1202 		 * for example, doesn't have it), making the
1203 		 * transition difficult.
1204 		 */
1205 		if (bind_ack->ADDR_length != addrlen) {
1206 			/* Assumes that the requested address was in use */
1207 			freemsg(mp);
1208 			error = EADDRINUSE;
1209 			eprintsoline(so, error);
1210 			goto done;
1211 		}
1212 
1213 		switch (so->so_family) {
1214 		case AF_INET6:
1215 		case AF_INET: {
1216 			sin_t *rname, *aname;
1217 
1218 			rname = (sin_t *)addr;
1219 			aname = (sin_t *)sti->sti_laddr_sa;
1220 
1221 			/*
1222 			 * Take advantage of the alignment
1223 			 * of sin_port and sin6_port which fall
1224 			 * in the same place in their data structures.
1225 			 * Just use sin_port for either address family.
1226 			 *
1227 			 * This may become a problem if (heaven forbid)
1228 			 * there's a separate ipv6port_reserved... :-P
1229 			 *
1230 			 * Binding to port 0 has the semantics of letting
1231 			 * the transport bind to any port.
1232 			 *
1233 			 * If the transport is TCP or UDP since we had sent
1234 			 * a T_BIND_REQ we would not get a port other than
1235 			 * what we asked for.
1236 			 */
1237 			if (tcp_udp_xport) {
1238 				/*
1239 				 * Pick up the new port number if we bound to
1240 				 * port 0.
1241 				 */
1242 				if (aname->sin_port == 0)
1243 					aname->sin_port = rname->sin_port;
1244 				sti->sti_laddr_valid = 1;
1245 				break;
1246 			}
1247 			if (aname->sin_port != 0 &&
1248 			    aname->sin_port != rname->sin_port) {
1249 				freemsg(mp);
1250 				error = EADDRINUSE;
1251 				eprintsoline(so, error);
1252 				goto done;
1253 			}
1254 			/*
1255 			 * Pick up the new port number if we bound to port 0.
1256 			 */
1257 			aname->sin_port = rname->sin_port;
1258 
1259 			/*
1260 			 * Unfortunately, addresses aren't _quite_ the same.
1261 			 */
1262 			if (so->so_family == AF_INET) {
1263 				if (aname->sin_addr.s_addr !=
1264 				    rname->sin_addr.s_addr) {
1265 					freemsg(mp);
1266 					error = EADDRNOTAVAIL;
1267 					eprintsoline(so, error);
1268 					goto done;
1269 				}
1270 			} else {
1271 				sin6_t *rname6 = (sin6_t *)rname;
1272 				sin6_t *aname6 = (sin6_t *)aname;
1273 
1274 				if (!IN6_ARE_ADDR_EQUAL(&aname6->sin6_addr,
1275 				    &rname6->sin6_addr)) {
1276 					freemsg(mp);
1277 					error = EADDRNOTAVAIL;
1278 					eprintsoline(so, error);
1279 					goto done;
1280 				}
1281 			}
1282 			break;
1283 		}
1284 		case AF_UNIX:
1285 			if (bcmp(addr, &sti->sti_ux_laddr, addrlen) != 0) {
1286 				freemsg(mp);
1287 				error = EADDRINUSE;
1288 				eprintsoline(so, error);
1289 				eprintso(so,
1290 				    ("addrlen %d, addr 0x%x, vp %p\n",
1291 				    addrlen, *((int *)addr),
1292 				    (void *)sti->sti_ux_bound_vp));
1293 				goto done;
1294 			}
1295 			sti->sti_laddr_valid = 1;
1296 			break;
1297 		default:
1298 			/*
1299 			 * NOTE: This assumes that addresses can be
1300 			 * byte-compared for equivalence.
1301 			 */
1302 			if (bcmp(addr, sti->sti_laddr_sa, addrlen) != 0) {
1303 				freemsg(mp);
1304 				error = EADDRINUSE;
1305 				eprintsoline(so, error);
1306 				goto done;
1307 			}
1308 			/*
1309 			 * Don't mark sti_laddr_valid, as we cannot be
1310 			 * sure that the returned address is the real
1311 			 * bound address when talking to an unknown
1312 			 * transport.
1313 			 */
1314 			break;
1315 		}
1316 	} else {
1317 		/*
1318 		 * Save for returned address for getsockname.
1319 		 * Needed for unspecific bind unless transport supports
1320 		 * the TI_GETMYNAME ioctl.
1321 		 * Do this for AF_INET{,6} even though they do, as
1322 		 * caching info here is much better performance than
1323 		 * a TPI/STREAMS trip to the transport for getsockname.
1324 		 * Any which can't for some reason _must_ _not_ set
1325 		 * sti_laddr_valid here for the caching version of
1326 		 * getsockname to not break;
1327 		 */
1328 		switch (so->so_family) {
1329 		case AF_UNIX:
1330 			/*
1331 			 * Record the address bound with the transport
1332 			 * for use by socketpair.
1333 			 */
1334 			bcopy(addr, &sti->sti_ux_laddr, addrlen);
1335 			sti->sti_laddr_valid = 1;
1336 			break;
1337 		case AF_INET:
1338 		case AF_INET6:
1339 			ASSERT(sti->sti_laddr_len <= sti->sti_laddr_maxlen);
1340 			bcopy(addr, sti->sti_laddr_sa, sti->sti_laddr_len);
1341 			sti->sti_laddr_valid = 1;
1342 			break;
1343 		default:
1344 			/*
1345 			 * Don't mark sti_laddr_valid, as we cannot be
1346 			 * sure that the returned address is the real
1347 			 * bound address when talking to an unknown
1348 			 * transport.
1349 			 */
1350 			break;
1351 		}
1352 	}
1353 
1354 	if (nl7c != NULL) {
1355 		/* Register listen()er sonode pointer with NL7C */
1356 		nl7c_listener_addr(nl7c, so);
1357 	}
1358 
1359 	freemsg(mp);
1360 
1361 done:
1362 	if (error) {
1363 		/* reset state & backlog to values held on entry */
1364 		if (clear_acceptconn_on_err == B_TRUE)
1365 			so->so_state &= ~SS_ACCEPTCONN;
1366 		if (restore_backlog_on_err == B_TRUE)
1367 			so->so_backlog = save_so_backlog;
1368 
1369 		if (unbind_on_err && so->so_state & SS_ISBOUND) {
1370 			int err;
1371 
1372 			err = sotpi_unbind(so, 0);
1373 			/* LINTED - statement has no consequent: if */
1374 			if (err) {
1375 				eprintsoline(so, error);
1376 			} else {
1377 				ASSERT(!(so->so_state & SS_ISBOUND));
1378 			}
1379 		}
1380 	}
1381 	if (!(flags & _SOBIND_LOCK_HELD)) {
1382 		so_unlock_single(so, SOLOCKED);
1383 		mutex_exit(&so->so_lock);
1384 	} else {
1385 		ASSERT(MUTEX_HELD(&so->so_lock));
1386 		ASSERT(so->so_flag & SOLOCKED);
1387 	}
1388 	return (error);
1389 }
1390 
1391 /* bind the socket */
1392 static int
sotpi_bind(struct sonode * so,struct sockaddr * name,socklen_t namelen,int flags,struct cred * cr)1393 sotpi_bind(struct sonode *so, struct sockaddr *name, socklen_t namelen,
1394     int flags, struct cred *cr)
1395 {
1396 	if ((flags & _SOBIND_SOCKETPAIR) == 0)
1397 		return (sotpi_bindlisten(so, name, namelen, 0, flags, cr));
1398 
1399 	flags &= ~_SOBIND_SOCKETPAIR;
1400 	return (sotpi_bindlisten(so, name, namelen, 1, flags, cr));
1401 }
1402 
1403 /*
1404  * Unbind a socket - used when bind() fails, when bind() specifies a NULL
1405  * address, or when listen needs to unbind and bind.
1406  * If the _SOUNBIND_REBIND flag is specified the addresses are retained
1407  * so that a sobind can pick them up.
1408  */
1409 static int
sotpi_unbind(struct sonode * so,int flags)1410 sotpi_unbind(struct sonode *so, int flags)
1411 {
1412 	struct T_unbind_req	unbind_req;
1413 	int			error = 0;
1414 	mblk_t			*mp;
1415 	sotpi_info_t		*sti = SOTOTPI(so);
1416 
1417 	dprintso(so, 1, ("sotpi_unbind(%p, 0x%x) %s\n",
1418 	    (void *)so, flags, pr_state(so->so_state, so->so_mode)));
1419 
1420 	ASSERT(MUTEX_HELD(&so->so_lock));
1421 	ASSERT(so->so_flag & SOLOCKED);
1422 
1423 	if (!(so->so_state & SS_ISBOUND)) {
1424 		error = EINVAL;
1425 		eprintsoline(so, error);
1426 		goto done;
1427 	}
1428 
1429 	mutex_exit(&so->so_lock);
1430 
1431 	/*
1432 	 * Flush the read and write side (except stream head read queue)
1433 	 * and send down T_UNBIND_REQ.
1434 	 */
1435 	(void) putnextctl1(strvp2wq(SOTOV(so)), M_FLUSH, FLUSHRW);
1436 
1437 	unbind_req.PRIM_type = T_UNBIND_REQ;
1438 	mp = soallocproto1(&unbind_req, sizeof (unbind_req),
1439 	    0, _ALLOC_SLEEP, CRED());
1440 	error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0,
1441 	    MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0);
1442 	mutex_enter(&so->so_lock);
1443 	if (error) {
1444 		eprintsoline(so, error);
1445 		goto done;
1446 	}
1447 
1448 	error = sowaitokack(so, T_UNBIND_REQ);
1449 	if (error) {
1450 		eprintsoline(so, error);
1451 		goto done;
1452 	}
1453 
1454 	/*
1455 	 * Even if some TPI message (e.g. T_DISCON_IND) was received in
1456 	 * strsock_proto while the lock was dropped above, the unbind
1457 	 * is allowed to complete.
1458 	 */
1459 	if (!(flags & _SOUNBIND_REBIND)) {
1460 		/*
1461 		 * Clear out bound address.
1462 		 */
1463 		vnode_t *vp;
1464 
1465 		if ((vp = sti->sti_ux_bound_vp) != NULL) {
1466 			sti->sti_ux_bound_vp = NULL;
1467 			vn_rele_stream(vp);
1468 		}
1469 		/* Clear out address */
1470 		sti->sti_laddr_len = 0;
1471 	}
1472 	so->so_state &= ~(SS_ISBOUND|SS_ACCEPTCONN);
1473 	sti->sti_laddr_valid = 0;
1474 
1475 done:
1476 
1477 	/* If the caller held the lock don't release it here */
1478 	ASSERT(MUTEX_HELD(&so->so_lock));
1479 	ASSERT(so->so_flag & SOLOCKED);
1480 
1481 	return (error);
1482 }
1483 
1484 /*
1485  * listen on the socket.
1486  * For TPI conforming transports this has to first unbind with the transport
1487  * and then bind again using the new backlog.
1488  */
1489 /* ARGSUSED */
1490 int
sotpi_listen(struct sonode * so,int backlog,struct cred * cr)1491 sotpi_listen(struct sonode *so, int backlog, struct cred *cr)
1492 {
1493 	int		error = 0;
1494 	sotpi_info_t	*sti = SOTOTPI(so);
1495 
1496 	dprintso(so, 1, ("sotpi_listen(%p, %d) %s\n",
1497 	    (void *)so, backlog, pr_state(so->so_state, so->so_mode)));
1498 
1499 	if (sti->sti_serv_type == T_CLTS)
1500 		return (EOPNOTSUPP);
1501 
1502 	/*
1503 	 * If the socket is ready to accept connections already, then
1504 	 * return without doing anything.  This avoids a problem where
1505 	 * a second listen() call fails if a connection is pending and
1506 	 * leaves the socket unbound. Only when we are not unbinding
1507 	 * with the transport can we safely increase the backlog.
1508 	 */
1509 	if (so->so_state & SS_ACCEPTCONN &&
1510 	    !((so->so_family == AF_INET || so->so_family == AF_INET6) &&
1511 	    /*CONSTCOND*/
1512 	    !solisten_tpi_tcp))
1513 		return (0);
1514 
1515 	if (so->so_state & SS_ISCONNECTED)
1516 		return (EINVAL);
1517 
1518 	mutex_enter(&so->so_lock);
1519 	so_lock_single(so);	/* Set SOLOCKED */
1520 
1521 	/*
1522 	 * If the listen doesn't change the backlog we do nothing.
1523 	 * This avoids an EPROTO error from the transport.
1524 	 */
1525 	if ((so->so_state & SS_ACCEPTCONN) &&
1526 	    so->so_backlog == backlog)
1527 		goto done;
1528 
1529 	if (!(so->so_state & SS_ISBOUND)) {
1530 		/*
1531 		 * Must have been explicitly bound in the UNIX domain.
1532 		 */
1533 		if (so->so_family == AF_UNIX) {
1534 			error = EINVAL;
1535 			goto done;
1536 		}
1537 		error = sotpi_bindlisten(so, NULL, 0, backlog,
1538 		    _SOBIND_UNSPEC|_SOBIND_LOCK_HELD|_SOBIND_LISTEN, cr);
1539 	} else if (backlog > 0) {
1540 		/*
1541 		 * AF_INET{,6} hack to avoid losing the port.
1542 		 * Assumes that all AF_INET{,6} transports can handle a
1543 		 * O_T_BIND_REQ with a non-zero CONIND_number when the TPI
1544 		 * has already bound thus it is possible to avoid the unbind.
1545 		 */
1546 		if (!((so->so_family == AF_INET || so->so_family == AF_INET6) &&
1547 		    /*CONSTCOND*/
1548 		    !solisten_tpi_tcp)) {
1549 			error = sotpi_unbind(so, _SOUNBIND_REBIND);
1550 			if (error)
1551 				goto done;
1552 		}
1553 		error = sotpi_bindlisten(so, NULL, 0, backlog,
1554 		    _SOBIND_REBIND|_SOBIND_LOCK_HELD|_SOBIND_LISTEN, cr);
1555 	} else {
1556 		so->so_state |= SS_ACCEPTCONN;
1557 		so->so_backlog = backlog;
1558 	}
1559 	if (error)
1560 		goto done;
1561 	ASSERT(so->so_state & SS_ACCEPTCONN);
1562 done:
1563 	so_unlock_single(so, SOLOCKED);
1564 	mutex_exit(&so->so_lock);
1565 	return (error);
1566 }
1567 
1568 /*
1569  * Disconnect either a specified seqno or all (-1).
1570  * The former is used on listening sockets only.
1571  *
1572  * When seqno == -1 sodisconnect could call sotpi_unbind. However,
1573  * the current use of sodisconnect(seqno == -1) is only for shutdown
1574  * so there is no point (and potentially incorrect) to unbind.
1575  */
1576 static int
sodisconnect(struct sonode * so,t_scalar_t seqno,int flags)1577 sodisconnect(struct sonode *so, t_scalar_t seqno, int flags)
1578 {
1579 	struct T_discon_req	discon_req;
1580 	int			error = 0;
1581 	mblk_t			*mp;
1582 
1583 	dprintso(so, 1, ("sodisconnect(%p, %d, 0x%x) %s\n",
1584 	    (void *)so, seqno, flags, pr_state(so->so_state, so->so_mode)));
1585 
1586 	if (!(flags & _SODISCONNECT_LOCK_HELD)) {
1587 		mutex_enter(&so->so_lock);
1588 		so_lock_single(so);	/* Set SOLOCKED */
1589 	} else {
1590 		ASSERT(MUTEX_HELD(&so->so_lock));
1591 		ASSERT(so->so_flag & SOLOCKED);
1592 	}
1593 
1594 	if (!(so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING|SS_ACCEPTCONN))) {
1595 		error = EINVAL;
1596 		eprintsoline(so, error);
1597 		goto done;
1598 	}
1599 
1600 	mutex_exit(&so->so_lock);
1601 	/*
1602 	 * Flush the write side (unless this is a listener)
1603 	 * and then send down a T_DISCON_REQ.
1604 	 * (Don't flush on listener since it could flush {O_}T_CONN_RES
1605 	 * and other messages.)
1606 	 */
1607 	if (!(so->so_state & SS_ACCEPTCONN))
1608 		(void) putnextctl1(strvp2wq(SOTOV(so)), M_FLUSH, FLUSHW);
1609 
1610 	discon_req.PRIM_type = T_DISCON_REQ;
1611 	discon_req.SEQ_number = seqno;
1612 	mp = soallocproto1(&discon_req, sizeof (discon_req),
1613 	    0, _ALLOC_SLEEP, CRED());
1614 	error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0,
1615 	    MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0);
1616 	mutex_enter(&so->so_lock);
1617 	if (error) {
1618 		eprintsoline(so, error);
1619 		goto done;
1620 	}
1621 
1622 	error = sowaitokack(so, T_DISCON_REQ);
1623 	if (error) {
1624 		eprintsoline(so, error);
1625 		goto done;
1626 	}
1627 	/*
1628 	 * Even if some TPI message (e.g. T_DISCON_IND) was received in
1629 	 * strsock_proto while the lock was dropped above, the disconnect
1630 	 * is allowed to complete. However, it is not possible to
1631 	 * assert that SS_ISCONNECTED|SS_ISCONNECTING are set.
1632 	 */
1633 	so->so_state &= ~(SS_ISCONNECTED|SS_ISCONNECTING);
1634 	SOTOTPI(so)->sti_laddr_valid = 0;
1635 	SOTOTPI(so)->sti_faddr_valid = 0;
1636 done:
1637 	if (!(flags & _SODISCONNECT_LOCK_HELD)) {
1638 		so_unlock_single(so, SOLOCKED);
1639 		mutex_exit(&so->so_lock);
1640 	} else {
1641 		/* If the caller held the lock don't release it here */
1642 		ASSERT(MUTEX_HELD(&so->so_lock));
1643 		ASSERT(so->so_flag & SOLOCKED);
1644 	}
1645 	return (error);
1646 }
1647 
1648 /* ARGSUSED */
1649 int
sotpi_accept(struct sonode * so,int fflag,struct cred * cr,struct sonode ** nsop)1650 sotpi_accept(struct sonode *so, int fflag, struct cred *cr,
1651     struct sonode **nsop)
1652 {
1653 	struct T_conn_ind	*conn_ind;
1654 	struct T_conn_res	*conn_res;
1655 	int			error = 0;
1656 	mblk_t			*mp, *ack_mp;
1657 	struct sonode		*nso;
1658 	vnode_t			*nvp;
1659 	void			*src;
1660 	t_uscalar_t		srclen;
1661 	void			*opt;
1662 	t_uscalar_t		optlen;
1663 	t_scalar_t		PRIM_type;
1664 	t_scalar_t		SEQ_number;
1665 	size_t			sinlen;
1666 	sotpi_info_t		*sti = SOTOTPI(so);
1667 	sotpi_info_t		*nsti;
1668 
1669 	dprintso(so, 1, ("sotpi_accept(%p, 0x%x, %p) %s\n",
1670 	    (void *)so, fflag, (void *)nsop,
1671 	    pr_state(so->so_state, so->so_mode)));
1672 
1673 	/*
1674 	 * Defer single-threading the accepting socket until
1675 	 * the T_CONN_IND has been received and parsed and the
1676 	 * new sonode has been opened.
1677 	 */
1678 
1679 	/* Check that we are not already connected */
1680 	if ((so->so_state & SS_ACCEPTCONN) == 0)
1681 		goto conn_bad;
1682 again:
1683 	if ((error = sowaitconnind(so, fflag, &mp)) != 0)
1684 		goto e_bad;
1685 
1686 	ASSERT(mp != NULL);
1687 	conn_ind = (struct T_conn_ind *)mp->b_rptr;
1688 
1689 	/*
1690 	 * Save SEQ_number for error paths.
1691 	 */
1692 	SEQ_number = conn_ind->SEQ_number;
1693 
1694 	srclen = conn_ind->SRC_length;
1695 	src = sogetoff(mp, conn_ind->SRC_offset, srclen, 1);
1696 	if (src == NULL) {
1697 		error = EPROTO;
1698 		freemsg(mp);
1699 		eprintsoline(so, error);
1700 		goto disconnect_unlocked;
1701 	}
1702 	optlen = conn_ind->OPT_length;
1703 	switch (so->so_family) {
1704 	case AF_INET:
1705 	case AF_INET6:
1706 		if ((optlen == sizeof (intptr_t)) && (sti->sti_direct != 0)) {
1707 			bcopy(mp->b_rptr + conn_ind->OPT_offset,
1708 			    &opt, conn_ind->OPT_length);
1709 		} else {
1710 			/*
1711 			 * The transport (in this case TCP) hasn't sent up
1712 			 * a pointer to an instance for the accept fast-path.
1713 			 * Disable fast-path completely because the call to
1714 			 * sotpi_create() below would otherwise create an
1715 			 * incomplete TCP instance, which would lead to
1716 			 * problems when sockfs sends a normal T_CONN_RES
1717 			 * message down the new stream.
1718 			 */
1719 			if (sti->sti_direct) {
1720 				int rval;
1721 				/*
1722 				 * For consistency we inform tcp to disable
1723 				 * direct interface on the listener, though
1724 				 * we can certainly live without doing this
1725 				 * because no data will ever travel upstream
1726 				 * on the listening socket.
1727 				 */
1728 				sti->sti_direct = 0;
1729 				(void) strioctl(SOTOV(so), _SIOCSOCKFALLBACK,
1730 				    0, 0, K_TO_K, cr, &rval);
1731 			}
1732 			opt = NULL;
1733 			optlen = 0;
1734 		}
1735 		break;
1736 	case AF_UNIX:
1737 	default:
1738 		if (optlen != 0) {
1739 			opt = sogetoff(mp, conn_ind->OPT_offset, optlen,
1740 			    __TPI_ALIGN_SIZE);
1741 			if (opt == NULL) {
1742 				error = EPROTO;
1743 				freemsg(mp);
1744 				eprintsoline(so, error);
1745 				goto disconnect_unlocked;
1746 			}
1747 		}
1748 		if (so->so_family == AF_UNIX) {
1749 			if (!sti->sti_faddr_noxlate) {
1750 				src = NULL;
1751 				srclen = 0;
1752 			}
1753 			/* Extract src address from options */
1754 			if (optlen != 0)
1755 				so_getopt_srcaddr(opt, optlen, &src, &srclen);
1756 		}
1757 		break;
1758 	}
1759 
1760 	/*
1761 	 * Create the new socket.
1762 	 */
1763 	nso = socket_newconn(so, NULL, NULL, SOCKET_SLEEP, &error);
1764 	if (nso == NULL) {
1765 		ASSERT(error != 0);
1766 		/*
1767 		 * Accept can not fail with ENOBUFS. sotpi_create
1768 		 * sleeps waiting for memory until a signal is caught
1769 		 * so return EINTR.
1770 		 */
1771 		freemsg(mp);
1772 		if (error == ENOBUFS)
1773 			error = EINTR;
1774 		goto e_disc_unl;
1775 	}
1776 	nvp = SOTOV(nso);
1777 	nsti = SOTOTPI(nso);
1778 
1779 #ifdef DEBUG
1780 	/*
1781 	 * SO_DEBUG is used to trigger the dprint* and eprint* macros thus
1782 	 * it's inherited early to allow debugging of the accept code itself.
1783 	 */
1784 	nso->so_options |= so->so_options & SO_DEBUG;
1785 #endif /* DEBUG */
1786 
1787 	/*
1788 	 * Save the SRC address from the T_CONN_IND
1789 	 * for getpeername to work on AF_UNIX and on transports that do not
1790 	 * support TI_GETPEERNAME.
1791 	 *
1792 	 * NOTE: AF_UNIX NUL termination is ensured by the sender's
1793 	 * copyin_name().
1794 	 */
1795 	if (srclen > (t_uscalar_t)nsti->sti_faddr_maxlen) {
1796 		error = EINVAL;
1797 		freemsg(mp);
1798 		eprintsoline(so, error);
1799 		goto disconnect_vp_unlocked;
1800 	}
1801 	nsti->sti_faddr_len = (socklen_t)srclen;
1802 	ASSERT(sti->sti_faddr_len <= sti->sti_faddr_maxlen);
1803 	bcopy(src, nsti->sti_faddr_sa, srclen);
1804 	nsti->sti_faddr_valid = 1;
1805 
1806 	/*
1807 	 * Record so_peercred and so_cpid from a cred in the T_CONN_IND.
1808 	 */
1809 	if ((DB_REF(mp) > 1) || MBLKSIZE(mp) <
1810 	    (sizeof (struct T_conn_res) + sizeof (intptr_t))) {
1811 		cred_t	*cr;
1812 		pid_t	cpid;
1813 
1814 		cr = msg_getcred(mp, &cpid);
1815 		if (cr != NULL) {
1816 			crhold(cr);
1817 			nso->so_peercred = cr;
1818 			nso->so_cpid = cpid;
1819 		}
1820 		freemsg(mp);
1821 
1822 		mp = soallocproto1(NULL, sizeof (struct T_conn_res) +
1823 		    sizeof (intptr_t), 0, _ALLOC_INTR, cr);
1824 		if (mp == NULL) {
1825 			/*
1826 			 * Accept can not fail with ENOBUFS.
1827 			 * A signal was caught so return EINTR.
1828 			 */
1829 			error = EINTR;
1830 			eprintsoline(so, error);
1831 			goto disconnect_vp_unlocked;
1832 		}
1833 		conn_res = (struct T_conn_res *)mp->b_rptr;
1834 	} else {
1835 		/*
1836 		 * For efficency reasons we use msg_extractcred; no crhold
1837 		 * needed since db_credp is cleared (i.e., we move the cred
1838 		 * from the message to so_peercred.
1839 		 */
1840 		nso->so_peercred = msg_extractcred(mp, &nso->so_cpid);
1841 
1842 		mp->b_rptr = DB_BASE(mp);
1843 		conn_res = (struct T_conn_res *)mp->b_rptr;
1844 		mp->b_wptr = mp->b_rptr + sizeof (struct T_conn_res);
1845 
1846 		mblk_setcred(mp, cr, curproc->p_pid);
1847 	}
1848 
1849 	/*
1850 	 * New socket must be bound at least in sockfs and, except for AF_INET,
1851 	 * (or AF_INET6) it also has to be bound in the transport provider.
1852 	 * We set the local address in the sonode from the T_OK_ACK of the
1853 	 * T_CONN_RES. For this reason the address we bind to here isn't
1854 	 * important.
1855 	 */
1856 	if ((nso->so_family == AF_INET || nso->so_family == AF_INET6) &&
1857 	    /*CONSTCOND*/
1858 	    nso->so_type == SOCK_STREAM && !soaccept_tpi_tcp) {
1859 		/*
1860 		 * Optimization for AF_INET{,6} transports
1861 		 * that can handle a T_CONN_RES without being bound.
1862 		 */
1863 		mutex_enter(&nso->so_lock);
1864 		so_automatic_bind(nso);
1865 		mutex_exit(&nso->so_lock);
1866 	} else {
1867 		/* Perform NULL bind with the transport provider. */
1868 		if ((error = sotpi_bind(nso, NULL, 0, _SOBIND_UNSPEC,
1869 		    cr)) != 0) {
1870 			ASSERT(error != ENOBUFS);
1871 			freemsg(mp);
1872 			eprintsoline(nso, error);
1873 			goto disconnect_vp_unlocked;
1874 		}
1875 	}
1876 
1877 	/*
1878 	 * Inherit SIOCSPGRP, SS_ASYNC before we send the {O_}T_CONN_RES
1879 	 * so that any data arriving on the new socket will cause the
1880 	 * appropriate signals to be delivered for the new socket.
1881 	 *
1882 	 * No other thread (except strsock_proto and strsock_misc)
1883 	 * can access the new socket thus we relax the locking.
1884 	 */
1885 	nso->so_pgrp = so->so_pgrp;
1886 	nso->so_state |= so->so_state & SS_ASYNC;
1887 	nsti->sti_faddr_noxlate = sti->sti_faddr_noxlate;
1888 
1889 	if (nso->so_pgrp != 0) {
1890 		if ((error = so_set_events(nso, nvp, cr)) != 0) {
1891 			eprintsoline(nso, error);
1892 			error = 0;
1893 			nso->so_pgrp = 0;
1894 		}
1895 	}
1896 
1897 	/*
1898 	 * Make note of the socket level options. TCP and IP level options
1899 	 * are already inherited. We could do all this after accept is
1900 	 * successful but doing it here simplifies code and no harm done
1901 	 * for error case.
1902 	 */
1903 	nso->so_options = so->so_options & (SO_DEBUG|SO_REUSEADDR|SO_KEEPALIVE|
1904 	    SO_DONTROUTE|SO_BROADCAST|SO_USELOOPBACK|
1905 	    SO_OOBINLINE|SO_DGRAM_ERRIND|SO_LINGER);
1906 	nso->so_sndbuf = so->so_sndbuf;
1907 	nso->so_rcvbuf = so->so_rcvbuf;
1908 	if (nso->so_options & SO_LINGER)
1909 		nso->so_linger = so->so_linger;
1910 
1911 	/*
1912 	 * Note that the following sti_direct code path should be
1913 	 * removed once we are confident that the direct sockets
1914 	 * do not result in any degradation.
1915 	 */
1916 	if (sti->sti_direct) {
1917 
1918 		ASSERT(opt != NULL);
1919 
1920 		conn_res->OPT_length = optlen;
1921 		conn_res->OPT_offset = MBLKL(mp);
1922 		bcopy(&opt, mp->b_wptr, optlen);
1923 		mp->b_wptr += optlen;
1924 		conn_res->PRIM_type = T_CONN_RES;
1925 		conn_res->ACCEPTOR_id = 0;
1926 		PRIM_type = T_CONN_RES;
1927 
1928 		/* Send down the T_CONN_RES on acceptor STREAM */
1929 		error = kstrputmsg(SOTOV(nso), mp, NULL,
1930 		    0, 0, MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0);
1931 		if (error) {
1932 			mutex_enter(&so->so_lock);
1933 			so_lock_single(so);
1934 			eprintsoline(so, error);
1935 			goto disconnect_vp;
1936 		}
1937 		mutex_enter(&nso->so_lock);
1938 		error = sowaitprim(nso, T_CONN_RES, T_OK_ACK,
1939 		    (t_uscalar_t)sizeof (struct T_ok_ack), &ack_mp, 0);
1940 		if (error) {
1941 			mutex_exit(&nso->so_lock);
1942 			mutex_enter(&so->so_lock);
1943 			so_lock_single(so);
1944 			eprintsoline(so, error);
1945 			goto disconnect_vp;
1946 		}
1947 		if (nso->so_family == AF_INET) {
1948 			sin_t *sin;
1949 
1950 			sin = (sin_t *)(ack_mp->b_rptr +
1951 			    sizeof (struct T_ok_ack));
1952 			bcopy(sin, nsti->sti_laddr_sa, sizeof (sin_t));
1953 			nsti->sti_laddr_len = sizeof (sin_t);
1954 		} else {
1955 			sin6_t *sin6;
1956 
1957 			sin6 = (sin6_t *)(ack_mp->b_rptr +
1958 			    sizeof (struct T_ok_ack));
1959 			bcopy(sin6, nsti->sti_laddr_sa, sizeof (sin6_t));
1960 			nsti->sti_laddr_len = sizeof (sin6_t);
1961 		}
1962 		freemsg(ack_mp);
1963 
1964 		nso->so_state |= SS_ISCONNECTED;
1965 		nso->so_proto_handle = (sock_lower_handle_t)opt;
1966 		nsti->sti_laddr_valid = 1;
1967 
1968 		if (sti->sti_nl7c_flags & NL7C_ENABLED) {
1969 			/*
1970 			 * A NL7C marked listen()er so the new socket
1971 			 * inherits the listen()er's NL7C state, except
1972 			 * for NL7C_POLLIN.
1973 			 *
1974 			 * Only call NL7C to process the new socket if
1975 			 * the listen socket allows blocking i/o.
1976 			 */
1977 			nsti->sti_nl7c_flags =
1978 			    sti->sti_nl7c_flags & (~NL7C_POLLIN);
1979 			if (so->so_state & (SS_NONBLOCK|SS_NDELAY)) {
1980 				/*
1981 				 * Nonblocking accept() just make it
1982 				 * persist to defer processing to the
1983 				 * read-side syscall (e.g. read).
1984 				 */
1985 				nsti->sti_nl7c_flags |= NL7C_SOPERSIST;
1986 			} else if (nl7c_process(nso, B_FALSE)) {
1987 				/*
1988 				 * NL7C has completed processing on the
1989 				 * socket, close the socket and back to
1990 				 * the top to await the next T_CONN_IND.
1991 				 */
1992 				mutex_exit(&nso->so_lock);
1993 				(void) VOP_CLOSE(nvp, 0, 1, (offset_t)0,
1994 				    cr, NULL);
1995 				VN_RELE(nvp);
1996 				goto again;
1997 			}
1998 			/* Pass the new socket out */
1999 		}
2000 
2001 		mutex_exit(&nso->so_lock);
2002 
2003 		/*
2004 		 * It's possible, through the use of autopush for example,
2005 		 * that the acceptor stream may not support sti_direct
2006 		 * semantics. If the new socket does not support sti_direct
2007 		 * we issue a _SIOCSOCKFALLBACK to inform the transport
2008 		 * as we would in the I_PUSH case.
2009 		 */
2010 		if (nsti->sti_direct == 0) {
2011 			int	rval;
2012 
2013 			if ((error = strioctl(SOTOV(nso), _SIOCSOCKFALLBACK,
2014 			    0, 0, K_TO_K, cr, &rval)) != 0) {
2015 				mutex_enter(&so->so_lock);
2016 				so_lock_single(so);
2017 				eprintsoline(so, error);
2018 				goto disconnect_vp;
2019 			}
2020 		}
2021 
2022 		/*
2023 		 * Pass out new socket.
2024 		 */
2025 		if (nsop != NULL)
2026 			*nsop = nso;
2027 
2028 		return (0);
2029 	}
2030 
2031 	/*
2032 	 * This is the non-performance case for sockets (e.g. AF_UNIX sockets)
2033 	 * which don't support the FireEngine accept fast-path. It is also
2034 	 * used when the virtual "sockmod" has been I_POP'd and I_PUSH'd
2035 	 * again. Neither sockfs nor TCP attempt to find out if some other
2036 	 * random module has been inserted in between (in which case we
2037 	 * should follow TLI accept behaviour). We blindly assume the worst
2038 	 * case and revert back to old behaviour i.e. TCP will not send us
2039 	 * any option (eager) and the accept should happen on the listener
2040 	 * queue. Any queued T_conn_ind have already got their options removed
2041 	 * by so_sock2_stream() when "sockmod" was I_POP'd.
2042 	 */
2043 	/*
2044 	 * Fill in the {O_}T_CONN_RES before getting SOLOCKED.
2045 	 */
2046 	if ((nso->so_mode & SM_ACCEPTOR_ID) == 0) {
2047 #ifdef	_ILP32
2048 		queue_t	*q;
2049 
2050 		/*
2051 		 * Find read queue in driver
2052 		 * Can safely do this since we "own" nso/nvp.
2053 		 */
2054 		q = strvp2wq(nvp)->q_next;
2055 		while (SAMESTR(q))
2056 			q = q->q_next;
2057 		q = RD(q);
2058 		conn_res->ACCEPTOR_id = (t_uscalar_t)q;
2059 #else
2060 		conn_res->ACCEPTOR_id = (t_uscalar_t)getminor(nvp->v_rdev);
2061 #endif	/* _ILP32 */
2062 		conn_res->PRIM_type = O_T_CONN_RES;
2063 		PRIM_type = O_T_CONN_RES;
2064 	} else {
2065 		conn_res->ACCEPTOR_id = nsti->sti_acceptor_id;
2066 		conn_res->PRIM_type = T_CONN_RES;
2067 		PRIM_type = T_CONN_RES;
2068 	}
2069 	conn_res->SEQ_number = SEQ_number;
2070 	conn_res->OPT_length = 0;
2071 	conn_res->OPT_offset = 0;
2072 
2073 	mutex_enter(&so->so_lock);
2074 	so_lock_single(so);	/* Set SOLOCKED */
2075 	mutex_exit(&so->so_lock);
2076 
2077 	error = kstrputmsg(SOTOV(so), mp, NULL,
2078 	    0, 0, MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0);
2079 	mutex_enter(&so->so_lock);
2080 	if (error) {
2081 		eprintsoline(so, error);
2082 		goto disconnect_vp;
2083 	}
2084 	error = sowaitprim(so, PRIM_type, T_OK_ACK,
2085 	    (t_uscalar_t)sizeof (struct T_ok_ack), &ack_mp, 0);
2086 	if (error) {
2087 		eprintsoline(so, error);
2088 		goto disconnect_vp;
2089 	}
2090 	mutex_exit(&so->so_lock);
2091 	/*
2092 	 * If there is a sin/sin6 appended onto the T_OK_ACK use
2093 	 * that to set the local address. If this is not present
2094 	 * then we zero out the address and don't set the
2095 	 * sti_laddr_valid bit. For AF_UNIX endpoints we copy over
2096 	 * the pathname from the listening socket.
2097 	 * In the case where this is TCP or an AF_UNIX socket the
2098 	 * client side may have queued data or a T_ORDREL in the
2099 	 * transport. Having now sent the T_CONN_RES we may receive
2100 	 * those queued messages at any time. Hold the acceptor
2101 	 * so_lock until its state and laddr are finalized.
2102 	 */
2103 	mutex_enter(&nso->so_lock);
2104 	sinlen = (nso->so_family == AF_INET) ? sizeof (sin_t) : sizeof (sin6_t);
2105 	if ((nso->so_family == AF_INET) || (nso->so_family == AF_INET6) &&
2106 	    MBLKL(ack_mp) == (sizeof (struct T_ok_ack) + sinlen)) {
2107 		ack_mp->b_rptr += sizeof (struct T_ok_ack);
2108 		bcopy(ack_mp->b_rptr, nsti->sti_laddr_sa, sinlen);
2109 		nsti->sti_laddr_len = sinlen;
2110 		nsti->sti_laddr_valid = 1;
2111 	} else if (nso->so_family == AF_UNIX) {
2112 		ASSERT(so->so_family == AF_UNIX);
2113 		nsti->sti_laddr_len = sti->sti_laddr_len;
2114 		ASSERT(nsti->sti_laddr_len <= nsti->sti_laddr_maxlen);
2115 		bcopy(sti->sti_laddr_sa, nsti->sti_laddr_sa,
2116 		    nsti->sti_laddr_len);
2117 		nsti->sti_laddr_valid = 1;
2118 	} else {
2119 		nsti->sti_laddr_len = sti->sti_laddr_len;
2120 		ASSERT(nsti->sti_laddr_len <= nsti->sti_laddr_maxlen);
2121 		bzero(nsti->sti_laddr_sa, nsti->sti_addr_size);
2122 		nsti->sti_laddr_sa->sa_family = nso->so_family;
2123 	}
2124 	nso->so_state |= SS_ISCONNECTED;
2125 	mutex_exit(&nso->so_lock);
2126 
2127 	freemsg(ack_mp);
2128 
2129 	mutex_enter(&so->so_lock);
2130 	so_unlock_single(so, SOLOCKED);
2131 	mutex_exit(&so->so_lock);
2132 
2133 	/*
2134 	 * Pass out new socket.
2135 	 */
2136 	if (nsop != NULL)
2137 		*nsop = nso;
2138 
2139 	return (0);
2140 
2141 
2142 eproto_disc_unl:
2143 	error = EPROTO;
2144 e_disc_unl:
2145 	eprintsoline(so, error);
2146 	goto disconnect_unlocked;
2147 
2148 pr_disc_vp_unl:
2149 	eprintsoline(so, error);
2150 disconnect_vp_unlocked:
2151 	(void) VOP_CLOSE(nvp, 0, 1, 0, cr, NULL);
2152 	VN_RELE(nvp);
2153 disconnect_unlocked:
2154 	(void) sodisconnect(so, SEQ_number, 0);
2155 	return (error);
2156 
2157 pr_disc_vp:
2158 	eprintsoline(so, error);
2159 disconnect_vp:
2160 	(void) sodisconnect(so, SEQ_number, _SODISCONNECT_LOCK_HELD);
2161 	so_unlock_single(so, SOLOCKED);
2162 	mutex_exit(&so->so_lock);
2163 	(void) VOP_CLOSE(nvp, 0, 1, 0, cr, NULL);
2164 	VN_RELE(nvp);
2165 	return (error);
2166 
2167 conn_bad:	/* Note: SunOS 4/BSD unconditionally returns EINVAL here */
2168 	error = (so->so_type == SOCK_DGRAM || so->so_type == SOCK_RAW)
2169 	    ? EOPNOTSUPP : EINVAL;
2170 e_bad:
2171 	eprintsoline(so, error);
2172 	return (error);
2173 }
2174 
2175 /*
2176  * connect a socket.
2177  *
2178  * Allow SOCK_DGRAM sockets to reconnect (by specifying a new address) and to
2179  * unconnect (by specifying a null address).
2180  */
2181 int
sotpi_connect(struct sonode * so,struct sockaddr * name,socklen_t namelen,int fflag,int flags,struct cred * cr)2182 sotpi_connect(struct sonode *so,
2183     struct sockaddr *name,
2184     socklen_t namelen,
2185     int fflag,
2186     int flags,
2187     struct cred *cr)
2188 {
2189 	struct T_conn_req	conn_req;
2190 	int			error = 0;
2191 	mblk_t			*mp;
2192 	void			*src;
2193 	socklen_t		srclen;
2194 	void			*addr;
2195 	socklen_t		addrlen;
2196 	boolean_t		need_unlock;
2197 	sotpi_info_t		*sti = SOTOTPI(so);
2198 
2199 	dprintso(so, 1, ("sotpi_connect(%p, %p, %d, 0x%x, 0x%x) %s\n",
2200 	    (void *)so, (void *)name, namelen, fflag, flags,
2201 	    pr_state(so->so_state, so->so_mode)));
2202 
2203 	/*
2204 	 * Preallocate the T_CONN_REQ mblk before grabbing SOLOCKED to
2205 	 * avoid sleeping for memory with SOLOCKED held.
2206 	 * We know that the T_CONN_REQ can't be larger than 2 * sti_faddr_maxlen
2207 	 * + sizeof (struct T_opthdr).
2208 	 * (the AF_UNIX so_ux_addr_xlate() does not make the address
2209 	 * exceed sti_faddr_maxlen).
2210 	 */
2211 	mp = soallocproto(sizeof (struct T_conn_req) +
2212 	    2 * sti->sti_faddr_maxlen + sizeof (struct T_opthdr), _ALLOC_INTR,
2213 	    cr);
2214 	if (mp == NULL) {
2215 		/*
2216 		 * Connect can not fail with ENOBUFS. A signal was
2217 		 * caught so return EINTR.
2218 		 */
2219 		error = EINTR;
2220 		eprintsoline(so, error);
2221 		return (error);
2222 	}
2223 
2224 	mutex_enter(&so->so_lock);
2225 	/*
2226 	 * Make sure there is a preallocated T_unbind_req message
2227 	 * before any binding. This message is allocated when the
2228 	 * socket is created. Since another thread can consume
2229 	 * so_unbind_mp by the time we return from so_lock_single(),
2230 	 * we should check the availability of so_unbind_mp after
2231 	 * we return from so_lock_single().
2232 	 */
2233 
2234 	so_lock_single(so);	/* Set SOLOCKED */
2235 	need_unlock = B_TRUE;
2236 
2237 	if (sti->sti_unbind_mp == NULL) {
2238 		dprintso(so, 1, ("sotpi_connect: allocating unbind_req\n"));
2239 		/* NOTE: holding so_lock while sleeping */
2240 		sti->sti_unbind_mp =
2241 		    soallocproto(sizeof (struct T_unbind_req), _ALLOC_INTR, cr);
2242 		if (sti->sti_unbind_mp == NULL) {
2243 			error = EINTR;
2244 			goto done;
2245 		}
2246 	}
2247 
2248 	/*
2249 	 * Can't have done a listen before connecting.
2250 	 */
2251 	if (so->so_state & SS_ACCEPTCONN) {
2252 		error = EOPNOTSUPP;
2253 		goto done;
2254 	}
2255 
2256 	/*
2257 	 * Must be bound with the transport
2258 	 */
2259 	if (!(so->so_state & SS_ISBOUND)) {
2260 		if ((so->so_family == AF_INET || so->so_family == AF_INET6) &&
2261 		    /*CONSTCOND*/
2262 		    so->so_type == SOCK_STREAM && !soconnect_tpi_tcp) {
2263 			/*
2264 			 * Optimization for AF_INET{,6} transports
2265 			 * that can handle a T_CONN_REQ without being bound.
2266 			 */
2267 			so_automatic_bind(so);
2268 		} else {
2269 			error = sotpi_bind(so, NULL, 0,
2270 			    _SOBIND_UNSPEC|_SOBIND_LOCK_HELD, cr);
2271 			if (error)
2272 				goto done;
2273 		}
2274 		ASSERT(so->so_state & SS_ISBOUND);
2275 		flags |= _SOCONNECT_DID_BIND;
2276 	}
2277 
2278 	/*
2279 	 * Handle a connect to a name parameter of type AF_UNSPEC like a
2280 	 * connect to a null address. This is the portable method to
2281 	 * unconnect a socket.
2282 	 */
2283 	if ((namelen >= sizeof (sa_family_t)) &&
2284 	    (name->sa_family == AF_UNSPEC)) {
2285 		name = NULL;
2286 		namelen = 0;
2287 	}
2288 
2289 	/*
2290 	 * Check that we are not already connected.
2291 	 * A connection-oriented socket cannot be reconnected.
2292 	 * A connected connection-less socket can be
2293 	 * - connected to a different address by a subsequent connect
2294 	 * - "unconnected" by a connect to the NULL address
2295 	 */
2296 	if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) {
2297 		ASSERT(!(flags & _SOCONNECT_DID_BIND));
2298 		if (so->so_mode & SM_CONNREQUIRED) {
2299 			/* Connection-oriented socket */
2300 			error = so->so_state & SS_ISCONNECTED ?
2301 			    EISCONN : EALREADY;
2302 			goto done;
2303 		}
2304 		/* Connection-less socket */
2305 		if (name == NULL) {
2306 			/*
2307 			 * Remove the connected state and clear SO_DGRAM_ERRIND
2308 			 * since it was set when the socket was connected.
2309 			 * If this is UDP also send down a T_DISCON_REQ.
2310 			 */
2311 			int val;
2312 
2313 			if ((so->so_family == AF_INET ||
2314 			    so->so_family == AF_INET6) &&
2315 			    (so->so_type == SOCK_DGRAM ||
2316 			    so->so_type == SOCK_RAW) &&
2317 			    /*CONSTCOND*/
2318 			    !soconnect_tpi_udp) {
2319 				/* XXX What about implicitly unbinding here? */
2320 				error = sodisconnect(so, -1,
2321 				    _SODISCONNECT_LOCK_HELD);
2322 			} else {
2323 				so->so_state &=
2324 				    ~(SS_ISCONNECTED | SS_ISCONNECTING);
2325 				sti->sti_faddr_valid = 0;
2326 				sti->sti_faddr_len = 0;
2327 			}
2328 
2329 			/* Remove SOLOCKED since setsockopt will grab it */
2330 			so_unlock_single(so, SOLOCKED);
2331 			mutex_exit(&so->so_lock);
2332 
2333 			val = 0;
2334 			(void) sotpi_setsockopt(so, SOL_SOCKET,
2335 			    SO_DGRAM_ERRIND, &val, (t_uscalar_t)sizeof (val),
2336 			    cr);
2337 
2338 			mutex_enter(&so->so_lock);
2339 			so_lock_single(so);	/* Set SOLOCKED */
2340 			goto done;
2341 		}
2342 	}
2343 	ASSERT(so->so_state & SS_ISBOUND);
2344 
2345 	if (name == NULL || namelen == 0) {
2346 		error = EINVAL;
2347 		goto done;
2348 	}
2349 	/*
2350 	 * Mark the socket if sti_faddr_sa represents the transport level
2351 	 * address.
2352 	 */
2353 	if (flags & _SOCONNECT_NOXLATE) {
2354 		struct sockaddr_ux	*soaddr_ux;
2355 
2356 		ASSERT(so->so_family == AF_UNIX);
2357 		if (namelen != sizeof (struct sockaddr_ux)) {
2358 			error = EINVAL;
2359 			goto done;
2360 		}
2361 		soaddr_ux = (struct sockaddr_ux *)name;
2362 		name = (struct sockaddr *)&soaddr_ux->sou_addr;
2363 		namelen = sizeof (soaddr_ux->sou_addr);
2364 		sti->sti_faddr_noxlate = 1;
2365 	}
2366 
2367 	/*
2368 	 * Length and family checks.
2369 	 */
2370 	error = so_addr_verify(so, name, namelen);
2371 	if (error)
2372 		goto bad;
2373 
2374 	/*
2375 	 * Save foreign address. Needed for AF_UNIX as well as
2376 	 * transport providers that do not support TI_GETPEERNAME.
2377 	 * Also used for cached foreign address for TCP and UDP.
2378 	 */
2379 	if (namelen > (t_uscalar_t)sti->sti_faddr_maxlen) {
2380 		error = EINVAL;
2381 		goto done;
2382 	}
2383 	sti->sti_faddr_len = (socklen_t)namelen;
2384 	ASSERT(sti->sti_faddr_len <= sti->sti_faddr_maxlen);
2385 	bcopy(name, sti->sti_faddr_sa, namelen);
2386 	sti->sti_faddr_valid = 1;
2387 
2388 	if (so->so_family == AF_UNIX) {
2389 		if (sti->sti_faddr_noxlate) {
2390 			/*
2391 			 * sti_faddr is a transport-level address, so
2392 			 * don't pass it as an option.  Do save it in
2393 			 * sti_ux_faddr, used for connected DG send.
2394 			 */
2395 			src = NULL;
2396 			srclen = 0;
2397 			addr = sti->sti_faddr_sa;
2398 			addrlen = (t_uscalar_t)sti->sti_faddr_len;
2399 			bcopy(addr, &sti->sti_ux_faddr,
2400 			    sizeof (sti->sti_ux_faddr));
2401 		} else {
2402 			/*
2403 			 * Pass the sockaddr_un source address as an option
2404 			 * and translate the remote address.
2405 			 * Holding so_lock thus sti_laddr_sa can not change.
2406 			 */
2407 			src = sti->sti_laddr_sa;
2408 			srclen = (t_uscalar_t)sti->sti_laddr_len;
2409 			dprintso(so, 1,
2410 			    ("sotpi_connect UNIX: srclen %d, src %p\n",
2411 			    srclen, src));
2412 			/*
2413 			 * Translate the destination address into our
2414 			 * internal form, and save it in sti_ux_faddr.
2415 			 * After this call, addr==&sti->sti_ux_taddr,
2416 			 * and we copy that to sti->sti_ux_faddr so
2417 			 * we save the connected peer address.
2418 			 */
2419 			error = so_ux_addr_xlate(so,
2420 			    sti->sti_faddr_sa, (socklen_t)sti->sti_faddr_len,
2421 			    (flags & _SOCONNECT_XPG4_2),
2422 			    &addr, &addrlen);
2423 			if (error)
2424 				goto bad;
2425 			bcopy(&sti->sti_ux_taddr, &sti->sti_ux_faddr,
2426 			    sizeof (sti->sti_ux_faddr));
2427 		}
2428 	} else {
2429 		addr = sti->sti_faddr_sa;
2430 		addrlen = (t_uscalar_t)sti->sti_faddr_len;
2431 		src = NULL;
2432 		srclen = 0;
2433 	}
2434 	/*
2435 	 * When connecting a datagram socket we issue the SO_DGRAM_ERRIND
2436 	 * option which asks the transport provider to send T_UDERR_IND
2437 	 * messages. These T_UDERR_IND messages are used to return connected
2438 	 * style errors (e.g. ECONNRESET) for connected datagram sockets.
2439 	 *
2440 	 * In addition, for UDP (and SOCK_RAW AF_INET{,6} sockets)
2441 	 * we send down a T_CONN_REQ. This is needed to let the
2442 	 * transport assign a local address that is consistent with
2443 	 * the remote address. Applications depend on a getsockname()
2444 	 * after a connect() to retrieve the "source" IP address for
2445 	 * the connected socket.  Invalidate the cached local address
2446 	 * to force getsockname() to enquire of the transport.
2447 	 */
2448 	if (!(so->so_mode & SM_CONNREQUIRED)) {
2449 		/*
2450 		 * Datagram socket.
2451 		 */
2452 		int32_t val;
2453 
2454 		so_unlock_single(so, SOLOCKED);
2455 		mutex_exit(&so->so_lock);
2456 
2457 		val = 1;
2458 		(void) sotpi_setsockopt(so, SOL_SOCKET, SO_DGRAM_ERRIND,
2459 		    &val, (t_uscalar_t)sizeof (val), cr);
2460 
2461 		mutex_enter(&so->so_lock);
2462 		so_lock_single(so);	/* Set SOLOCKED */
2463 		if ((so->so_family != AF_INET && so->so_family != AF_INET6) ||
2464 		    (so->so_type != SOCK_DGRAM && so->so_type != SOCK_RAW) ||
2465 		    soconnect_tpi_udp) {
2466 			soisconnected(so);
2467 			goto done;
2468 		}
2469 		/*
2470 		 * Send down T_CONN_REQ etc.
2471 		 * Clear fflag to avoid returning EWOULDBLOCK.
2472 		 */
2473 		fflag = 0;
2474 		ASSERT(so->so_family != AF_UNIX);
2475 		sti->sti_laddr_valid = 0;
2476 	} else if (sti->sti_laddr_len != 0) {
2477 		/*
2478 		 * If the local address or port was "any" then it may be
2479 		 * changed by the transport as a result of the
2480 		 * connect.  Invalidate the cached version if we have one.
2481 		 */
2482 		switch (so->so_family) {
2483 		case AF_INET:
2484 			ASSERT(sti->sti_laddr_len == (socklen_t)sizeof (sin_t));
2485 			if (((sin_t *)sti->sti_laddr_sa)->sin_addr.s_addr ==
2486 			    INADDR_ANY ||
2487 			    ((sin_t *)sti->sti_laddr_sa)->sin_port == 0)
2488 				sti->sti_laddr_valid = 0;
2489 			break;
2490 
2491 		case AF_INET6:
2492 			ASSERT(sti->sti_laddr_len ==
2493 			    (socklen_t)sizeof (sin6_t));
2494 			if (IN6_IS_ADDR_UNSPECIFIED(
2495 			    &((sin6_t *)sti->sti_laddr_sa) ->sin6_addr) ||
2496 			    IN6_IS_ADDR_V4MAPPED_ANY(
2497 			    &((sin6_t *)sti->sti_laddr_sa)->sin6_addr) ||
2498 			    ((sin6_t *)sti->sti_laddr_sa)->sin6_port == 0)
2499 				sti->sti_laddr_valid = 0;
2500 			break;
2501 
2502 		default:
2503 			break;
2504 		}
2505 	}
2506 
2507 	/*
2508 	 * Check for failure of an earlier call
2509 	 */
2510 	if (so->so_error != 0)
2511 		goto so_bad;
2512 
2513 	/*
2514 	 * Send down T_CONN_REQ. Message was allocated above.
2515 	 */
2516 	conn_req.PRIM_type = T_CONN_REQ;
2517 	conn_req.DEST_length = addrlen;
2518 	conn_req.DEST_offset = (t_scalar_t)sizeof (conn_req);
2519 	if (srclen == 0) {
2520 		conn_req.OPT_length = 0;
2521 		conn_req.OPT_offset = 0;
2522 		soappendmsg(mp, &conn_req, sizeof (conn_req));
2523 		soappendmsg(mp, addr, addrlen);
2524 	} else {
2525 		/*
2526 		 * There is a AF_UNIX sockaddr_un to include as a source
2527 		 * address option.
2528 		 */
2529 		struct T_opthdr toh;
2530 
2531 		toh.level = SOL_SOCKET;
2532 		toh.name = SO_SRCADDR;
2533 		toh.len = (t_uscalar_t)(srclen + sizeof (struct T_opthdr));
2534 		toh.status = 0;
2535 		conn_req.OPT_length =
2536 		    (t_scalar_t)(sizeof (toh) + _TPI_ALIGN_TOPT(srclen));
2537 		conn_req.OPT_offset = (t_scalar_t)(sizeof (conn_req) +
2538 		    _TPI_ALIGN_TOPT(addrlen));
2539 
2540 		soappendmsg(mp, &conn_req, sizeof (conn_req));
2541 		soappendmsg(mp, addr, addrlen);
2542 		mp->b_wptr += _TPI_ALIGN_TOPT(addrlen) - addrlen;
2543 		soappendmsg(mp, &toh, sizeof (toh));
2544 		soappendmsg(mp, src, srclen);
2545 		mp->b_wptr += _TPI_ALIGN_TOPT(srclen) - srclen;
2546 		ASSERT(mp->b_wptr <= mp->b_datap->db_lim);
2547 	}
2548 	/*
2549 	 * Set SS_ISCONNECTING before sending down the T_CONN_REQ
2550 	 * in order to have the right state when the T_CONN_CON shows up.
2551 	 */
2552 	soisconnecting(so);
2553 	mutex_exit(&so->so_lock);
2554 
2555 	if (AU_AUDITING())
2556 		audit_sock(T_CONN_REQ, strvp2wq(SOTOV(so)), mp, 0);
2557 
2558 	error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0,
2559 	    MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0);
2560 	mp = NULL;
2561 	mutex_enter(&so->so_lock);
2562 	if (error != 0)
2563 		goto bad;
2564 
2565 	if ((error = sowaitokack(so, T_CONN_REQ)) != 0)
2566 		goto bad;
2567 
2568 	/* Allow other threads to access the socket */
2569 	so_unlock_single(so, SOLOCKED);
2570 	need_unlock = B_FALSE;
2571 
2572 	/*
2573 	 * Wait until we get a T_CONN_CON or an error
2574 	 */
2575 	if ((error = sowaitconnected(so, fflag, 0)) != 0) {
2576 		so_lock_single(so);	/* Set SOLOCKED */
2577 		need_unlock = B_TRUE;
2578 	}
2579 
2580 done:
2581 	freemsg(mp);
2582 	switch (error) {
2583 	case EINPROGRESS:
2584 	case EALREADY:
2585 	case EISCONN:
2586 	case EINTR:
2587 		/* Non-fatal errors */
2588 		sti->sti_laddr_valid = 0;
2589 		/* FALLTHRU */
2590 	case 0:
2591 		break;
2592 	default:
2593 		ASSERT(need_unlock);
2594 		/*
2595 		 * Fatal errors: clear SS_ISCONNECTING in case it was set,
2596 		 * and invalidate local-address cache
2597 		 */
2598 		so->so_state &= ~SS_ISCONNECTING;
2599 		sti->sti_laddr_valid = 0;
2600 		/* A discon_ind might have already unbound us */
2601 		if ((flags & _SOCONNECT_DID_BIND) &&
2602 		    (so->so_state & SS_ISBOUND)) {
2603 			int err;
2604 
2605 			err = sotpi_unbind(so, 0);
2606 			/* LINTED - statement has no conseq */
2607 			if (err) {
2608 				eprintsoline(so, err);
2609 			}
2610 		}
2611 		break;
2612 	}
2613 	if (need_unlock)
2614 		so_unlock_single(so, SOLOCKED);
2615 	mutex_exit(&so->so_lock);
2616 	return (error);
2617 
2618 so_bad:	error = sogeterr(so, B_TRUE);
2619 bad:	eprintsoline(so, error);
2620 	goto done;
2621 }
2622 
2623 /* ARGSUSED */
2624 int
sotpi_shutdown(struct sonode * so,int how,struct cred * cr)2625 sotpi_shutdown(struct sonode *so, int how, struct cred *cr)
2626 {
2627 	struct T_ordrel_req	ordrel_req;
2628 	mblk_t			*mp;
2629 	uint_t			old_state, state_change;
2630 	int			error = 0;
2631 	sotpi_info_t		*sti = SOTOTPI(so);
2632 
2633 	dprintso(so, 1, ("sotpi_shutdown(%p, %d) %s\n",
2634 	    (void *)so, how, pr_state(so->so_state, so->so_mode)));
2635 
2636 	mutex_enter(&so->so_lock);
2637 	so_lock_single(so);	/* Set SOLOCKED */
2638 
2639 	/*
2640 	 * SunOS 4.X has no check for datagram sockets.
2641 	 * 5.X checks that it is connected (ENOTCONN)
2642 	 * X/Open requires that we check the connected state.
2643 	 */
2644 	if (!(so->so_state & SS_ISCONNECTED)) {
2645 		if (!xnet_skip_checks) {
2646 			error = ENOTCONN;
2647 			if (xnet_check_print) {
2648 				printf("sockfs: X/Open shutdown check "
2649 				    "caused ENOTCONN\n");
2650 			}
2651 		}
2652 		goto done;
2653 	}
2654 	/*
2655 	 * Record the current state and then perform any state changes.
2656 	 * Then use the difference between the old and new states to
2657 	 * determine which messages need to be sent.
2658 	 * This prevents e.g. duplicate T_ORDREL_REQ when there are
2659 	 * duplicate calls to shutdown().
2660 	 */
2661 	old_state = so->so_state;
2662 
2663 	switch (how) {
2664 	case 0:
2665 		socantrcvmore(so);
2666 		break;
2667 	case 1:
2668 		socantsendmore(so);
2669 		break;
2670 	case 2:
2671 		socantsendmore(so);
2672 		socantrcvmore(so);
2673 		break;
2674 	default:
2675 		error = EINVAL;
2676 		goto done;
2677 	}
2678 
2679 	/*
2680 	 * Assumes that the SS_CANT* flags are never cleared in the above code.
2681 	 */
2682 	state_change = (so->so_state & (SS_CANTRCVMORE|SS_CANTSENDMORE)) -
2683 	    (old_state & (SS_CANTRCVMORE|SS_CANTSENDMORE));
2684 	ASSERT((state_change & ~(SS_CANTRCVMORE|SS_CANTSENDMORE)) == 0);
2685 
2686 	switch (state_change) {
2687 	case 0:
2688 		dprintso(so, 1,
2689 		    ("sotpi_shutdown: nothing to send in state 0x%x\n",
2690 		    so->so_state));
2691 		goto done;
2692 
2693 	case SS_CANTRCVMORE:
2694 		mutex_exit(&so->so_lock);
2695 		strseteof(SOTOV(so), 1);
2696 		/*
2697 		 * strseteof takes care of read side wakeups,
2698 		 * pollwakeups, and signals.
2699 		 */
2700 		/*
2701 		 * Get the read lock before flushing data to avoid problems
2702 		 * with the T_EXDATA_IND MSG_PEEK code in sotpi_recvmsg.
2703 		 */
2704 		mutex_enter(&so->so_lock);
2705 		(void) so_lock_read(so, 0);	/* Set SOREADLOCKED */
2706 		mutex_exit(&so->so_lock);
2707 
2708 		/* Flush read side queue */
2709 		strflushrq(SOTOV(so), FLUSHALL);
2710 
2711 		mutex_enter(&so->so_lock);
2712 		so_unlock_read(so);		/* Clear SOREADLOCKED */
2713 		break;
2714 
2715 	case SS_CANTSENDMORE:
2716 		mutex_exit(&so->so_lock);
2717 		strsetwerror(SOTOV(so), 0, 0, sogetwrerr);
2718 		mutex_enter(&so->so_lock);
2719 		break;
2720 
2721 	case SS_CANTSENDMORE|SS_CANTRCVMORE:
2722 		mutex_exit(&so->so_lock);
2723 		strsetwerror(SOTOV(so), 0, 0, sogetwrerr);
2724 		strseteof(SOTOV(so), 1);
2725 		/*
2726 		 * strseteof takes care of read side wakeups,
2727 		 * pollwakeups, and signals.
2728 		 */
2729 		/*
2730 		 * Get the read lock before flushing data to avoid problems
2731 		 * with the T_EXDATA_IND MSG_PEEK code in sotpi_recvmsg.
2732 		 */
2733 		mutex_enter(&so->so_lock);
2734 		(void) so_lock_read(so, 0);	/* Set SOREADLOCKED */
2735 		mutex_exit(&so->so_lock);
2736 
2737 		/* Flush read side queue */
2738 		strflushrq(SOTOV(so), FLUSHALL);
2739 
2740 		mutex_enter(&so->so_lock);
2741 		so_unlock_read(so);		/* Clear SOREADLOCKED */
2742 		break;
2743 	}
2744 
2745 	ASSERT(MUTEX_HELD(&so->so_lock));
2746 
2747 	/*
2748 	 * If either SS_CANTSENDMORE or SS_CANTRCVMORE or both of them
2749 	 * was set due to this call and the new state has both of them set:
2750 	 *	Send the AF_UNIX close indication
2751 	 *	For T_COTS send a discon_ind
2752 	 *
2753 	 * If cantsend was set due to this call:
2754 	 *	For T_COTSORD send an ordrel_ind
2755 	 *
2756 	 * Note that for T_CLTS there is no message sent here.
2757 	 */
2758 	if ((so->so_state & (SS_CANTRCVMORE|SS_CANTSENDMORE)) ==
2759 	    (SS_CANTRCVMORE|SS_CANTSENDMORE)) {
2760 		/*
2761 		 * For SunOS 4.X compatibility we tell the other end
2762 		 * that we are unable to receive at this point.
2763 		 */
2764 		if (so->so_family == AF_UNIX && sti->sti_serv_type != T_CLTS)
2765 			so_unix_close(so);
2766 
2767 		if (sti->sti_serv_type == T_COTS)
2768 			error = sodisconnect(so, -1, _SODISCONNECT_LOCK_HELD);
2769 	}
2770 	if ((state_change & SS_CANTSENDMORE) &&
2771 	    (sti->sti_serv_type == T_COTS_ORD)) {
2772 		/* Send an orderly release */
2773 		ordrel_req.PRIM_type = T_ORDREL_REQ;
2774 
2775 		mutex_exit(&so->so_lock);
2776 		mp = soallocproto1(&ordrel_req, sizeof (ordrel_req),
2777 		    0, _ALLOC_SLEEP, cr);
2778 		/*
2779 		 * Send down the T_ORDREL_REQ even if there is flow control.
2780 		 * This prevents shutdown from blocking.
2781 		 * Note that there is no T_OK_ACK for ordrel_req.
2782 		 */
2783 		error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0,
2784 		    MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR|MSG_IGNFLOW, 0);
2785 		mutex_enter(&so->so_lock);
2786 		if (error) {
2787 			eprintsoline(so, error);
2788 			goto done;
2789 		}
2790 	}
2791 
2792 done:
2793 	so_unlock_single(so, SOLOCKED);
2794 	mutex_exit(&so->so_lock);
2795 	return (error);
2796 }
2797 
2798 /*
2799  * For any connected SOCK_STREAM/SOCK_SEQPACKET AF_UNIX socket we send
2800  * a zero-length T_OPTDATA_REQ with the SO_UNIX_CLOSE option to inform the peer
2801  * that we have closed.
2802  * Also, for connected AF_UNIX SOCK_DGRAM sockets we send a zero-length
2803  * T_UNITDATA_REQ containing the same option.
2804  *
2805  * For SOCK_DGRAM half-connections (somebody connected to this end
2806  * but this end is not connect) we don't know where to send any
2807  * SO_UNIX_CLOSE.
2808  *
2809  * We have to ignore stream head errors just in case there has been
2810  * a shutdown(output).
2811  * Ignore any flow control to try to get the message more quickly to the peer.
2812  * While locally ignoring flow control solves the problem when there
2813  * is only the loopback transport on the stream it would not provide
2814  * the correct AF_UNIX socket semantics when one or more modules have
2815  * been pushed.
2816  */
2817 void
so_unix_close(struct sonode * so)2818 so_unix_close(struct sonode *so)
2819 {
2820 	struct T_opthdr	toh;
2821 	mblk_t		*mp;
2822 	sotpi_info_t	*sti = SOTOTPI(so);
2823 
2824 	ASSERT(MUTEX_HELD(&so->so_lock));
2825 
2826 	ASSERT(so->so_family == AF_UNIX);
2827 
2828 	if ((so->so_state & (SS_ISCONNECTED|SS_ISBOUND)) !=
2829 	    (SS_ISCONNECTED|SS_ISBOUND))
2830 		return;
2831 
2832 	dprintso(so, 1, ("so_unix_close(%p) %s\n",
2833 	    (void *)so, pr_state(so->so_state, so->so_mode)));
2834 
2835 	toh.level = SOL_SOCKET;
2836 	toh.name = SO_UNIX_CLOSE;
2837 
2838 	/* zero length + header */
2839 	toh.len = (t_uscalar_t)sizeof (struct T_opthdr);
2840 	toh.status = 0;
2841 
2842 	if (so->so_type == SOCK_STREAM || so->so_type == SOCK_SEQPACKET) {
2843 		struct T_optdata_req tdr;
2844 
2845 		tdr.PRIM_type = T_OPTDATA_REQ;
2846 		tdr.DATA_flag = 0;
2847 
2848 		tdr.OPT_length = (t_scalar_t)sizeof (toh);
2849 		tdr.OPT_offset = (t_scalar_t)sizeof (tdr);
2850 
2851 		/* NOTE: holding so_lock while sleeping */
2852 		mp = soallocproto2(&tdr, sizeof (tdr),
2853 		    &toh, sizeof (toh), 0, _ALLOC_SLEEP, CRED());
2854 	} else {
2855 		struct T_unitdata_req	tudr;
2856 		void			*addr;
2857 		socklen_t		addrlen;
2858 		void			*src;
2859 		socklen_t		srclen;
2860 		struct T_opthdr		toh2;
2861 		t_scalar_t		size;
2862 
2863 		/*
2864 		 * We know this is an AF_UNIX connected DGRAM socket.
2865 		 * We therefore already have the destination address
2866 		 * in the internal form needed for this send.  This is
2867 		 * similar to the sosend_dgram call later in this file
2868 		 * when there's no user-specified destination address.
2869 		 */
2870 		if (sti->sti_faddr_noxlate) {
2871 			/*
2872 			 * Already have a transport internal address. Do not
2873 			 * pass any (transport internal) source address.
2874 			 */
2875 			addr = sti->sti_faddr_sa;
2876 			addrlen = (t_uscalar_t)sti->sti_faddr_len;
2877 			src = NULL;
2878 			srclen = 0;
2879 		} else {
2880 			/*
2881 			 * Pass the sockaddr_un source address as an option
2882 			 * and translate the remote address.
2883 			 * Holding so_lock thus sti_laddr_sa can not change.
2884 			 */
2885 			src = sti->sti_laddr_sa;
2886 			srclen = (socklen_t)sti->sti_laddr_len;
2887 			dprintso(so, 1,
2888 			    ("so_ux_close: srclen %d, src %p\n",
2889 			    srclen, src));
2890 			/*
2891 			 * Use the destination address saved in connect.
2892 			 */
2893 			addr = &sti->sti_ux_faddr;
2894 			addrlen = sizeof (sti->sti_ux_faddr);
2895 		}
2896 		tudr.PRIM_type = T_UNITDATA_REQ;
2897 		tudr.DEST_length = addrlen;
2898 		tudr.DEST_offset = (t_scalar_t)sizeof (tudr);
2899 		if (srclen == 0) {
2900 			tudr.OPT_length = (t_scalar_t)sizeof (toh);
2901 			tudr.OPT_offset = (t_scalar_t)(sizeof (tudr) +
2902 			    _TPI_ALIGN_TOPT(addrlen));
2903 
2904 			size = tudr.OPT_offset + tudr.OPT_length;
2905 			/* NOTE: holding so_lock while sleeping */
2906 			mp = soallocproto2(&tudr, sizeof (tudr),
2907 			    addr, addrlen, size, _ALLOC_SLEEP, CRED());
2908 			mp->b_wptr += (_TPI_ALIGN_TOPT(addrlen) - addrlen);
2909 			soappendmsg(mp, &toh, sizeof (toh));
2910 		} else {
2911 			/*
2912 			 * There is a AF_UNIX sockaddr_un to include as a
2913 			 * source address option.
2914 			 */
2915 			tudr.OPT_length = (t_scalar_t)(2 * sizeof (toh) +
2916 			    _TPI_ALIGN_TOPT(srclen));
2917 			tudr.OPT_offset = (t_scalar_t)(sizeof (tudr) +
2918 			    _TPI_ALIGN_TOPT(addrlen));
2919 
2920 			toh2.level = SOL_SOCKET;
2921 			toh2.name = SO_SRCADDR;
2922 			toh2.len = (t_uscalar_t)(srclen +
2923 			    sizeof (struct T_opthdr));
2924 			toh2.status = 0;
2925 
2926 			size = tudr.OPT_offset + tudr.OPT_length;
2927 
2928 			/* NOTE: holding so_lock while sleeping */
2929 			mp = soallocproto2(&tudr, sizeof (tudr),
2930 			    addr, addrlen, size, _ALLOC_SLEEP, CRED());
2931 			mp->b_wptr += _TPI_ALIGN_TOPT(addrlen) - addrlen;
2932 			soappendmsg(mp, &toh, sizeof (toh));
2933 			soappendmsg(mp, &toh2, sizeof (toh2));
2934 			soappendmsg(mp, src, srclen);
2935 			mp->b_wptr += _TPI_ALIGN_TOPT(srclen) - srclen;
2936 		}
2937 		ASSERT(mp->b_wptr <= mp->b_datap->db_lim);
2938 	}
2939 	mutex_exit(&so->so_lock);
2940 	(void) kstrputmsg(SOTOV(so), mp, NULL, 0, 0,
2941 	    MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR|MSG_IGNFLOW, 0);
2942 	mutex_enter(&so->so_lock);
2943 }
2944 
2945 /*
2946  * Called by sotpi_recvmsg when reading a non-zero amount of data.
2947  * In addition, the caller typically verifies that there is some
2948  * potential state to clear by checking
2949  *	if (so->so_state & (SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK))
2950  * before calling this routine.
2951  * Note that such a check can be made without holding so_lock since
2952  * sotpi_recvmsg is single-threaded (using SOREADLOCKED) and only sotpi_recvmsg
2953  * decrements sti_oobsigcnt.
2954  *
2955  * When data is read *after* the point that all pending
2956  * oob data has been consumed the oob indication is cleared.
2957  *
2958  * This logic keeps select/poll returning POLLRDBAND and
2959  * SIOCATMARK returning true until we have read past
2960  * the mark.
2961  */
2962 static void
sorecv_update_oobstate(struct sonode * so)2963 sorecv_update_oobstate(struct sonode *so)
2964 {
2965 	sotpi_info_t *sti = SOTOTPI(so);
2966 
2967 	mutex_enter(&so->so_lock);
2968 	ASSERT(so_verify_oobstate(so));
2969 	dprintso(so, 1,
2970 	    ("sorecv_update_oobstate: counts %d/%d state %s\n",
2971 	    sti->sti_oobsigcnt,
2972 	    sti->sti_oobcnt, pr_state(so->so_state, so->so_mode)));
2973 	if (sti->sti_oobsigcnt == 0) {
2974 		/* No more pending oob indications */
2975 		so->so_state &= ~(SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK);
2976 		freemsg(so->so_oobmsg);
2977 		so->so_oobmsg = NULL;
2978 	}
2979 	ASSERT(so_verify_oobstate(so));
2980 	mutex_exit(&so->so_lock);
2981 }
2982 
2983 /*
2984  * Handle recv* calls for an so which has NL7C saved recv mblk_t(s).
2985  */
2986 static int
nl7c_sorecv(struct sonode * so,mblk_t ** rmp,uio_t * uiop,rval_t * rp)2987 nl7c_sorecv(struct sonode *so, mblk_t **rmp, uio_t *uiop, rval_t *rp)
2988 {
2989 	sotpi_info_t *sti = SOTOTPI(so);
2990 	int	error = 0;
2991 	mblk_t *tmp = NULL;
2992 	mblk_t *pmp = NULL;
2993 	mblk_t *nmp = sti->sti_nl7c_rcv_mp;
2994 
2995 	ASSERT(nmp != NULL);
2996 
2997 	while (nmp != NULL && uiop->uio_resid > 0) {
2998 		ssize_t n;
2999 
3000 		if (DB_TYPE(nmp) == M_DATA) {
3001 			/*
3002 			 * We have some data, uiomove up to resid bytes.
3003 			 */
3004 			n = MIN(MBLKL(nmp), uiop->uio_resid);
3005 			if (n > 0)
3006 				error = uiomove(nmp->b_rptr, n, UIO_READ, uiop);
3007 			nmp->b_rptr += n;
3008 			if (nmp->b_rptr == nmp->b_wptr) {
3009 				pmp = nmp;
3010 				nmp = nmp->b_cont;
3011 			}
3012 			if (error)
3013 				break;
3014 		} else {
3015 			/*
3016 			 * We only handle data, save for caller to handle.
3017 			 */
3018 			if (pmp != NULL) {
3019 				pmp->b_cont = nmp->b_cont;
3020 			}
3021 			nmp->b_cont = NULL;
3022 			if (*rmp == NULL) {
3023 				*rmp = nmp;
3024 			} else {
3025 				tmp->b_cont = nmp;
3026 			}
3027 			nmp = nmp->b_cont;
3028 			tmp = nmp;
3029 		}
3030 	}
3031 	if (pmp != NULL) {
3032 		/* Free any mblk_t(s) which we have consumed */
3033 		pmp->b_cont = NULL;
3034 		freemsg(sti->sti_nl7c_rcv_mp);
3035 	}
3036 	if ((sti->sti_nl7c_rcv_mp = nmp) == NULL) {
3037 		/* Last mblk_t so return the saved kstrgetmsg() rval/error */
3038 		if (error == 0) {
3039 			rval_t	*p = (rval_t *)&sti->sti_nl7c_rcv_rval;
3040 
3041 			error = p->r_v.r_v2;
3042 			p->r_v.r_v2 = 0;
3043 		}
3044 		rp->r_vals = sti->sti_nl7c_rcv_rval;
3045 		sti->sti_nl7c_rcv_rval = 0;
3046 	} else {
3047 		/* More mblk_t(s) to process so no rval to return */
3048 		rp->r_vals = 0;
3049 	}
3050 	return (error);
3051 }
3052 /*
3053  * Receive the next message on the queue.
3054  * If msg_controllen is non-zero when called the caller is interested in
3055  * any received control info (options).
3056  * If msg_namelen is non-zero when called the caller is interested in
3057  * any received source address.
3058  * The routine returns with msg_control and msg_name pointing to
3059  * kmem_alloc'ed memory which the caller has to free.
3060  */
3061 /* ARGSUSED */
3062 int
sotpi_recvmsg(struct sonode * so,struct nmsghdr * msg,struct uio * uiop,struct cred * cr)3063 sotpi_recvmsg(struct sonode *so, struct nmsghdr *msg, struct uio *uiop,
3064     struct cred *cr)
3065 {
3066 	union T_primitives	*tpr;
3067 	mblk_t			*mp;
3068 	uchar_t			pri;
3069 	int			pflag, opflag;
3070 	void			*control;
3071 	t_uscalar_t		controllen;
3072 	t_uscalar_t		namelen;
3073 	int			so_state = so->so_state; /* Snapshot */
3074 	ssize_t			saved_resid;
3075 	rval_t			rval;
3076 	int			flags;
3077 	clock_t			timout;
3078 	int			error = 0;
3079 	sotpi_info_t		*sti = SOTOTPI(so);
3080 
3081 	flags = msg->msg_flags;
3082 	msg->msg_flags = 0;
3083 
3084 	dprintso(so, 1, ("sotpi_recvmsg(%p, %p, 0x%x) state %s err %d\n",
3085 	    (void *)so, (void *)msg, flags,
3086 	    pr_state(so->so_state, so->so_mode), so->so_error));
3087 
3088 	if (so->so_version == SOV_STREAM) {
3089 		so_update_attrs(so, SOACC);
3090 		/* The imaginary "sockmod" has been popped - act as a stream */
3091 		return (strread(SOTOV(so), uiop, cr));
3092 	}
3093 
3094 	/*
3095 	 * If we are not connected because we have never been connected
3096 	 * we return ENOTCONN. If we have been connected (but are no longer
3097 	 * connected) then SS_CANTRCVMORE is set and we let kstrgetmsg return
3098 	 * the EOF.
3099 	 *
3100 	 * An alternative would be to post an ENOTCONN error in stream head
3101 	 * (read+write) and clear it when we're connected. However, that error
3102 	 * would cause incorrect poll/select behavior!
3103 	 */
3104 	if ((so_state & (SS_ISCONNECTED|SS_CANTRCVMORE)) == 0 &&
3105 	    (so->so_mode & SM_CONNREQUIRED)) {
3106 		return (ENOTCONN);
3107 	}
3108 
3109 	/*
3110 	 * Note: SunOS 4.X checks uio_resid == 0 before going to sleep (but
3111 	 * after checking that the read queue is empty) and returns zero.
3112 	 * This implementation will sleep (in kstrgetmsg) even if uio_resid
3113 	 * is zero.
3114 	 */
3115 
3116 	if (flags & MSG_OOB) {
3117 		/* Check that the transport supports OOB */
3118 		if (!(so->so_mode & SM_EXDATA))
3119 			return (EOPNOTSUPP);
3120 		so_update_attrs(so, SOACC);
3121 		return (sorecvoob(so, msg, uiop, flags,
3122 		    (so->so_options & SO_OOBINLINE)));
3123 	}
3124 
3125 	so_update_attrs(so, SOACC);
3126 
3127 	/*
3128 	 * Set msg_controllen and msg_namelen to zero here to make it
3129 	 * simpler in the cases that no control or name is returned.
3130 	 */
3131 	controllen = msg->msg_controllen;
3132 	namelen = msg->msg_namelen;
3133 	msg->msg_controllen = 0;
3134 	msg->msg_namelen = 0;
3135 
3136 	dprintso(so, 1, ("sotpi_recvmsg: namelen %d controllen %d\n",
3137 	    namelen, controllen));
3138 
3139 	mutex_enter(&so->so_lock);
3140 	/*
3141 	 * If an NL7C enabled socket and not waiting for write data.
3142 	 */
3143 	if ((sti->sti_nl7c_flags & (NL7C_ENABLED | NL7C_WAITWRITE)) ==
3144 	    NL7C_ENABLED) {
3145 		if (sti->sti_nl7c_uri) {
3146 			/* Close uri processing for a previous request */
3147 			nl7c_close(so);
3148 		}
3149 		if ((so_state & SS_CANTRCVMORE) &&
3150 		    sti->sti_nl7c_rcv_mp == NULL) {
3151 			/* Nothing to process, EOF */
3152 			mutex_exit(&so->so_lock);
3153 			return (0);
3154 		} else if (sti->sti_nl7c_flags & NL7C_SOPERSIST) {
3155 			/* Persistent NL7C socket, try to process request */
3156 			boolean_t ret;
3157 
3158 			ret = nl7c_process(so,
3159 			    (so->so_state & (SS_NONBLOCK|SS_NDELAY)));
3160 			rval.r_vals = sti->sti_nl7c_rcv_rval;
3161 			error = rval.r_v.r_v2;
3162 			if (error) {
3163 				/* Error of some sort, return it */
3164 				mutex_exit(&so->so_lock);
3165 				return (error);
3166 			}
3167 			if (sti->sti_nl7c_flags &&
3168 			    ! (sti->sti_nl7c_flags & NL7C_WAITWRITE)) {
3169 				/*
3170 				 * Still an NL7C socket and no data
3171 				 * to pass up to the caller.
3172 				 */
3173 				mutex_exit(&so->so_lock);
3174 				if (ret) {
3175 					/* EOF */
3176 					return (0);
3177 				} else {
3178 					/* Need more data */
3179 					return (EAGAIN);
3180 				}
3181 			}
3182 		} else {
3183 			/*
3184 			 * Not persistent so no further NL7C processing.
3185 			 */
3186 			sti->sti_nl7c_flags = 0;
3187 		}
3188 	}
3189 	/*
3190 	 * Only one reader is allowed at any given time. This is needed
3191 	 * for T_EXDATA handling and, in the future, MSG_WAITALL.
3192 	 *
3193 	 * This is slightly different that BSD behavior in that it fails with
3194 	 * EWOULDBLOCK when using nonblocking io. In BSD the read queue access
3195 	 * is single-threaded using sblock(), which is dropped while waiting
3196 	 * for data to appear. The difference shows up e.g. if one
3197 	 * file descriptor does not have O_NONBLOCK but a dup'ed file descriptor
3198 	 * does use nonblocking io and different threads are reading each
3199 	 * file descriptor. In BSD there would never be an EWOULDBLOCK error
3200 	 * in this case as long as the read queue doesn't get empty.
3201 	 * In this implementation the thread using nonblocking io can
3202 	 * get an EWOULDBLOCK error due to the blocking thread executing
3203 	 * e.g. in the uiomove in kstrgetmsg.
3204 	 * This difference is not believed to be significant.
3205 	 */
3206 	/* Set SOREADLOCKED */
3207 	error = so_lock_read_intr(so,
3208 	    uiop->uio_fmode | ((flags & MSG_DONTWAIT) ? FNONBLOCK : 0));
3209 	mutex_exit(&so->so_lock);
3210 	if (error)
3211 		return (error);
3212 
3213 	/*
3214 	 * Tell kstrgetmsg to not inspect the stream head errors until all
3215 	 * queued data has been consumed.
3216 	 * Use a timeout=-1 to wait forever unless MSG_DONTWAIT is set.
3217 	 * Also, If uio_fmode indicates nonblocking kstrgetmsg will not block.
3218 	 *
3219 	 * MSG_WAITALL only applies to M_DATA and T_DATA_IND messages and
3220 	 * to T_OPTDATA_IND that do not contain any user-visible control msg.
3221 	 * Note that MSG_WAITALL set with MSG_PEEK is a noop.
3222 	 */
3223 	pflag = MSG_ANY | MSG_DELAYERROR;
3224 	if (flags & MSG_PEEK) {
3225 		pflag |= MSG_IPEEK;
3226 		flags &= ~MSG_WAITALL;
3227 	}
3228 	if (so->so_mode & SM_ATOMIC)
3229 		pflag |= MSG_DISCARDTAIL;
3230 
3231 	if (flags & MSG_DONTWAIT)
3232 		timout = 0;
3233 	else if (so->so_rcvtimeo != 0)
3234 		timout = TICK_TO_MSEC(so->so_rcvtimeo);
3235 	else
3236 		timout = -1;
3237 	opflag = pflag;
3238 retry:
3239 	saved_resid = uiop->uio_resid;
3240 	pri = 0;
3241 	mp = NULL;
3242 	if (sti->sti_nl7c_rcv_mp != NULL) {
3243 		/* Already kstrgetmsg()ed saved mblk(s) from NL7C */
3244 		error = nl7c_sorecv(so, &mp, uiop, &rval);
3245 	} else {
3246 		error = kstrgetmsg(SOTOV(so), &mp, uiop, &pri, &pflag,
3247 		    timout, &rval);
3248 	}
3249 	if (error != 0) {
3250 		/* kstrgetmsg returns ETIME when timeout expires */
3251 		if (error == ETIME)
3252 			error = EWOULDBLOCK;
3253 		goto out;
3254 	}
3255 	/*
3256 	 * For datagrams the MOREDATA flag is used to set MSG_TRUNC.
3257 	 * For non-datagrams MOREDATA is used to set MSG_EOR.
3258 	 */
3259 	ASSERT(!(rval.r_val1 & MORECTL));
3260 	if ((rval.r_val1 & MOREDATA) && (so->so_mode & SM_ATOMIC))
3261 		msg->msg_flags |= MSG_TRUNC;
3262 
3263 	if (mp == NULL) {
3264 		dprintso(so, 1, ("sotpi_recvmsg: got M_DATA\n"));
3265 		/*
3266 		 * 4.3BSD and 4.4BSD clears the mark when peeking across it.
3267 		 * The draft Posix socket spec states that the mark should
3268 		 * not be cleared when peeking. We follow the latter.
3269 		 */
3270 		if ((so->so_state &
3271 		    (SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK)) &&
3272 		    (uiop->uio_resid != saved_resid) &&
3273 		    !(flags & MSG_PEEK)) {
3274 			sorecv_update_oobstate(so);
3275 		}
3276 
3277 		mutex_enter(&so->so_lock);
3278 		/* Set MSG_EOR based on MOREDATA */
3279 		if (!(rval.r_val1 & MOREDATA)) {
3280 			if (so->so_state & SS_SAVEDEOR) {
3281 				msg->msg_flags |= MSG_EOR;
3282 				so->so_state &= ~SS_SAVEDEOR;
3283 			}
3284 		}
3285 		/*
3286 		 * If some data was received (i.e. not EOF) and the
3287 		 * read/recv* has not been satisfied wait for some more.
3288 		 */
3289 		if ((flags & MSG_WAITALL) && !(msg->msg_flags & MSG_EOR) &&
3290 		    uiop->uio_resid != saved_resid && uiop->uio_resid > 0) {
3291 			mutex_exit(&so->so_lock);
3292 			pflag = opflag | MSG_NOMARK;
3293 			goto retry;
3294 		}
3295 		goto out_locked;
3296 	}
3297 
3298 	/* strsock_proto has already verified length and alignment */
3299 	tpr = (union T_primitives *)mp->b_rptr;
3300 	dprintso(so, 1, ("sotpi_recvmsg: type %d\n", tpr->type));
3301 
3302 	switch (tpr->type) {
3303 	case T_DATA_IND: {
3304 		if ((so->so_state &
3305 		    (SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK)) &&
3306 		    (uiop->uio_resid != saved_resid) &&
3307 		    !(flags & MSG_PEEK)) {
3308 			sorecv_update_oobstate(so);
3309 		}
3310 
3311 		/*
3312 		 * Set msg_flags to MSG_EOR based on
3313 		 * MORE_flag and MOREDATA.
3314 		 */
3315 		mutex_enter(&so->so_lock);
3316 		so->so_state &= ~SS_SAVEDEOR;
3317 		if (!(tpr->data_ind.MORE_flag & 1)) {
3318 			if (!(rval.r_val1 & MOREDATA))
3319 				msg->msg_flags |= MSG_EOR;
3320 			else
3321 				so->so_state |= SS_SAVEDEOR;
3322 		}
3323 		freemsg(mp);
3324 		/*
3325 		 * If some data was received (i.e. not EOF) and the
3326 		 * read/recv* has not been satisfied wait for some more.
3327 		 */
3328 		if ((flags & MSG_WAITALL) && !(msg->msg_flags & MSG_EOR) &&
3329 		    uiop->uio_resid != saved_resid && uiop->uio_resid > 0) {
3330 			mutex_exit(&so->so_lock);
3331 			pflag = opflag | MSG_NOMARK;
3332 			goto retry;
3333 		}
3334 		goto out_locked;
3335 	}
3336 	case T_UNITDATA_IND: {
3337 		void *addr;
3338 		t_uscalar_t addrlen;
3339 		void *abuf;
3340 		t_uscalar_t optlen;
3341 		void *opt;
3342 
3343 		if ((so->so_state &
3344 		    (SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK)) &&
3345 		    (uiop->uio_resid != saved_resid) &&
3346 		    !(flags & MSG_PEEK)) {
3347 			sorecv_update_oobstate(so);
3348 		}
3349 
3350 		if (namelen != 0) {
3351 			/* Caller wants source address */
3352 			addrlen = tpr->unitdata_ind.SRC_length;
3353 			addr = sogetoff(mp,
3354 			    tpr->unitdata_ind.SRC_offset,
3355 			    addrlen, 1);
3356 			if (addr == NULL) {
3357 				freemsg(mp);
3358 				error = EPROTO;
3359 				eprintsoline(so, error);
3360 				goto out;
3361 			}
3362 			if (so->so_family == AF_UNIX) {
3363 				/*
3364 				 * Can not use the transport level address.
3365 				 * If there is a SO_SRCADDR option carrying
3366 				 * the socket level address it will be
3367 				 * extracted below.
3368 				 */
3369 				addr = NULL;
3370 				addrlen = 0;
3371 			}
3372 		}
3373 		optlen = tpr->unitdata_ind.OPT_length;
3374 		if (optlen != 0) {
3375 			t_uscalar_t ncontrollen;
3376 
3377 			/*
3378 			 * Extract any source address option.
3379 			 * Determine how large cmsg buffer is needed.
3380 			 */
3381 			opt = sogetoff(mp,
3382 			    tpr->unitdata_ind.OPT_offset,
3383 			    optlen, __TPI_ALIGN_SIZE);
3384 
3385 			if (opt == NULL) {
3386 				freemsg(mp);
3387 				error = EPROTO;
3388 				eprintsoline(so, error);
3389 				goto out;
3390 			}
3391 			if (so->so_family == AF_UNIX)
3392 				so_getopt_srcaddr(opt, optlen, &addr, &addrlen);
3393 			ncontrollen = so_cmsglen(mp, opt, optlen,
3394 			    !(flags & MSG_XPG4_2));
3395 			if (controllen != 0)
3396 				controllen = ncontrollen;
3397 			else if (ncontrollen != 0)
3398 				msg->msg_flags |= MSG_CTRUNC;
3399 		} else {
3400 			controllen = 0;
3401 		}
3402 
3403 		if (namelen != 0) {
3404 			/*
3405 			 * Return address to caller.
3406 			 * Caller handles truncation if length
3407 			 * exceeds msg_namelen.
3408 			 * NOTE: AF_UNIX NUL termination is ensured by
3409 			 * the sender's copyin_name().
3410 			 */
3411 			abuf = kmem_alloc(addrlen, KM_SLEEP);
3412 
3413 			bcopy(addr, abuf, addrlen);
3414 			msg->msg_name = abuf;
3415 			msg->msg_namelen = addrlen;
3416 		}
3417 
3418 		if (controllen != 0) {
3419 			/*
3420 			 * Return control msg to caller.
3421 			 * Caller handles truncation if length
3422 			 * exceeds msg_controllen.
3423 			 */
3424 			control = kmem_zalloc(controllen, KM_SLEEP);
3425 
3426 			error = so_opt2cmsg(mp, opt, optlen,
3427 			    !(flags & MSG_XPG4_2),
3428 			    control, controllen);
3429 			if (error) {
3430 				freemsg(mp);
3431 				if (msg->msg_namelen != 0)
3432 					kmem_free(msg->msg_name,
3433 					    msg->msg_namelen);
3434 				kmem_free(control, controllen);
3435 				eprintsoline(so, error);
3436 				goto out;
3437 			}
3438 			msg->msg_control = control;
3439 			msg->msg_controllen = controllen;
3440 		}
3441 
3442 		freemsg(mp);
3443 		goto out;
3444 	}
3445 	case T_OPTDATA_IND: {
3446 		struct T_optdata_req *tdr;
3447 		void *opt;
3448 		t_uscalar_t optlen;
3449 
3450 		if ((so->so_state &
3451 		    (SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK)) &&
3452 		    (uiop->uio_resid != saved_resid) &&
3453 		    !(flags & MSG_PEEK)) {
3454 			sorecv_update_oobstate(so);
3455 		}
3456 
3457 		tdr = (struct T_optdata_req *)mp->b_rptr;
3458 		optlen = tdr->OPT_length;
3459 		if (optlen != 0) {
3460 			t_uscalar_t ncontrollen;
3461 			/*
3462 			 * Determine how large cmsg buffer is needed.
3463 			 */
3464 			opt = sogetoff(mp,
3465 			    tpr->optdata_ind.OPT_offset,
3466 			    optlen, __TPI_ALIGN_SIZE);
3467 
3468 			if (opt == NULL) {
3469 				freemsg(mp);
3470 				error = EPROTO;
3471 				eprintsoline(so, error);
3472 				goto out;
3473 			}
3474 
3475 			ncontrollen = so_cmsglen(mp, opt, optlen,
3476 			    !(flags & MSG_XPG4_2));
3477 			if (controllen != 0)
3478 				controllen = ncontrollen;
3479 			else if (ncontrollen != 0)
3480 				msg->msg_flags |= MSG_CTRUNC;
3481 		} else {
3482 			controllen = 0;
3483 		}
3484 
3485 		if (controllen != 0) {
3486 			/*
3487 			 * Return control msg to caller.
3488 			 * Caller handles truncation if length
3489 			 * exceeds msg_controllen.
3490 			 */
3491 			control = kmem_zalloc(controllen, KM_SLEEP);
3492 
3493 			error = so_opt2cmsg(mp, opt, optlen,
3494 			    !(flags & MSG_XPG4_2),
3495 			    control, controllen);
3496 			if (error) {
3497 				freemsg(mp);
3498 				kmem_free(control, controllen);
3499 				eprintsoline(so, error);
3500 				goto out;
3501 			}
3502 			msg->msg_control = control;
3503 			msg->msg_controllen = controllen;
3504 		}
3505 
3506 		/*
3507 		 * Set msg_flags to MSG_EOR based on
3508 		 * DATA_flag and MOREDATA.
3509 		 */
3510 		mutex_enter(&so->so_lock);
3511 		so->so_state &= ~SS_SAVEDEOR;
3512 		if (!(tpr->data_ind.MORE_flag & 1)) {
3513 			if (!(rval.r_val1 & MOREDATA))
3514 				msg->msg_flags |= MSG_EOR;
3515 			else
3516 				so->so_state |= SS_SAVEDEOR;
3517 		}
3518 		freemsg(mp);
3519 		/*
3520 		 * If some data was received (i.e. not EOF) and the
3521 		 * read/recv* has not been satisfied wait for some more.
3522 		 * Not possible to wait if control info was received.
3523 		 */
3524 		if ((flags & MSG_WAITALL) && !(msg->msg_flags & MSG_EOR) &&
3525 		    controllen == 0 &&
3526 		    uiop->uio_resid != saved_resid && uiop->uio_resid > 0) {
3527 			mutex_exit(&so->so_lock);
3528 			pflag = opflag | MSG_NOMARK;
3529 			goto retry;
3530 		}
3531 		goto out_locked;
3532 	}
3533 	case T_EXDATA_IND: {
3534 		dprintso(so, 1,
3535 		    ("sotpi_recvmsg: EXDATA_IND counts %d/%d consumed %ld "
3536 		    "state %s\n",
3537 		    sti->sti_oobsigcnt, sti->sti_oobcnt,
3538 		    saved_resid - uiop->uio_resid,
3539 		    pr_state(so->so_state, so->so_mode)));
3540 		/*
3541 		 * kstrgetmsg handles MSGMARK so there is nothing to
3542 		 * inspect in the T_EXDATA_IND.
3543 		 * strsock_proto makes the stream head queue the T_EXDATA_IND
3544 		 * as a separate message with no M_DATA component. Furthermore,
3545 		 * the stream head does not consolidate M_DATA messages onto
3546 		 * an MSGMARK'ed message ensuring that the T_EXDATA_IND
3547 		 * remains a message by itself. This is needed since MSGMARK
3548 		 * marks both the whole message as well as the last byte
3549 		 * of the message.
3550 		 */
3551 		freemsg(mp);
3552 		ASSERT(uiop->uio_resid == saved_resid);	/* No data */
3553 		if (flags & MSG_PEEK) {
3554 			/*
3555 			 * Even though we are peeking we consume the
3556 			 * T_EXDATA_IND thereby moving the mark information
3557 			 * to SS_RCVATMARK. Then the oob code below will
3558 			 * retry the peeking kstrgetmsg.
3559 			 * Note that the stream head read queue is
3560 			 * never flushed without holding SOREADLOCKED
3561 			 * thus the T_EXDATA_IND can not disappear
3562 			 * underneath us.
3563 			 */
3564 			dprintso(so, 1,
3565 			    ("sotpi_recvmsg: consume EXDATA_IND "
3566 			    "counts %d/%d state %s\n",
3567 			    sti->sti_oobsigcnt,
3568 			    sti->sti_oobcnt,
3569 			    pr_state(so->so_state, so->so_mode)));
3570 
3571 			pflag = MSG_ANY | MSG_DELAYERROR;
3572 			if (so->so_mode & SM_ATOMIC)
3573 				pflag |= MSG_DISCARDTAIL;
3574 
3575 			pri = 0;
3576 			mp = NULL;
3577 
3578 			error = kstrgetmsg(SOTOV(so), &mp, uiop,
3579 			    &pri, &pflag, (clock_t)-1, &rval);
3580 			ASSERT(uiop->uio_resid == saved_resid);
3581 
3582 			if (error) {
3583 #ifdef SOCK_DEBUG
3584 				if (error != EWOULDBLOCK && error != EINTR) {
3585 					eprintsoline(so, error);
3586 				}
3587 #endif /* SOCK_DEBUG */
3588 				goto out;
3589 			}
3590 			ASSERT(mp);
3591 			tpr = (union T_primitives *)mp->b_rptr;
3592 			ASSERT(tpr->type == T_EXDATA_IND);
3593 			freemsg(mp);
3594 		} /* end "if (flags & MSG_PEEK)" */
3595 
3596 		/*
3597 		 * Decrement the number of queued and pending oob.
3598 		 *
3599 		 * SS_RCVATMARK is cleared when we read past a mark.
3600 		 * SS_HAVEOOBDATA is cleared when we've read past the
3601 		 * last mark.
3602 		 * SS_OOBPEND is cleared if we've read past the last
3603 		 * mark and no (new) SIGURG has been posted.
3604 		 */
3605 		mutex_enter(&so->so_lock);
3606 		ASSERT(so_verify_oobstate(so));
3607 		ASSERT(sti->sti_oobsigcnt >= sti->