xref: /titanic_44/usr/src/uts/common/fs/sockfs/sockcommon_sops.c (revision 379c004d1f26b343f034bba8a350290691d00d38)
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 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"@(#)sockcommon_sops.c	1.1	07/06/14 SMI"
28 
29 #include <sys/types.h>
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/sysmacros.h>
33 #include <sys/debug.h>
34 #include <sys/cmn_err.h>
35 
36 #include <sys/stropts.h>
37 #include <sys/socket.h>
38 #include <sys/socketvar.h>
39 
40 #define	_SUN_TPI_VERSION	2
41 #include <sys/tihdr.h>
42 #include <sys/sockio.h>
43 #include <sys/sodirect.h>
44 #include <sys/kmem_impl.h>
45 
46 #include <sys/strsubr.h>
47 #include <sys/strsun.h>
48 #include <sys/ddi.h>
49 #include <netinet/in.h>
50 #include <inet/ip.h>
51 
52 #include <fs/sockfs/sockcommon.h>
53 
54 #include <sys/socket_proto.h>
55 
56 #include <fs/sockfs/socktpi_impl.h>
57 #include <sys/tihdr.h>
58 #include <fs/sockfs/nl7c.h>
59 #include <inet/kssl/ksslapi.h>
60 
61 
62 extern int xnet_skip_checks;
63 extern int xnet_check_print;
64 
65 static void so_queue_oob(sock_upper_handle_t, mblk_t *, size_t);
66 
67 
68 /*ARGSUSED*/
69 int
70 so_accept_notsupp(struct sonode *lso, int fflag,
71     struct cred *cr, struct sonode **nsop)
72 {
73 	return (EOPNOTSUPP);
74 }
75 
76 /*ARGSUSED*/
77 int
78 so_listen_notsupp(struct sonode *so, int backlog, struct cred *cr)
79 {
80 	return (EOPNOTSUPP);
81 }
82 
83 /*ARGSUSED*/
84 int
85 so_getsockname_notsupp(struct sonode *so, struct sockaddr *sa,
86     socklen_t *len, struct cred *cr)
87 {
88 	return (EOPNOTSUPP);
89 }
90 
91 /*ARGSUSED*/
92 int
93 so_getpeername_notsupp(struct sonode *so, struct sockaddr *addr,
94     socklen_t *addrlen, boolean_t accept, struct cred *cr)
95 {
96 	return (EOPNOTSUPP);
97 }
98 
99 /*ARGSUSED*/
100 int
101 so_shutdown_notsupp(struct sonode *so, int how, struct cred *cr)
102 {
103 	return (EOPNOTSUPP);
104 }
105 
106 /*ARGSUSED*/
107 int
108 so_sendmblk_notsupp(struct sonode *so, struct msghdr *msg, int fflag,
109     struct cred *cr, mblk_t **mpp)
110 {
111 	return (EOPNOTSUPP);
112 }
113 
114 /*
115  * Generic Socket Ops
116  */
117 
118 /* ARGSUSED */
119 int
120 so_init(struct sonode *so, struct sonode *pso, struct cred *cr, int flags)
121 {
122 	return (socket_init_common(so, pso, flags, cr));
123 }
124 
125 int
126 so_bind(struct sonode *so, struct sockaddr *name, socklen_t namelen,
127     int flags, struct cred *cr)
128 {
129 	int error;
130 
131 	SO_BLOCK_FALLBACK(so, SOP_BIND(so, name, namelen, flags, cr));
132 
133 	ASSERT(flags == _SOBIND_XPG4_2 || flags == _SOBIND_SOCKBSD);
134 
135 	/* X/Open requires this check */
136 	if ((so->so_state & SS_CANTSENDMORE) && !xnet_skip_checks) {
137 		if (xnet_check_print) {
138 			printf("sockfs: X/Open bind state check "
139 			    "caused EINVAL\n");
140 		}
141 		error = EINVAL;
142 		goto done;
143 	}
144 
145 	/*
146 	 * a bind to a NULL address is interpreted as unbind. So just
147 	 * do the downcall.
148 	 */
149 	if (name == NULL)
150 		goto dobind;
151 
152 	switch (so->so_family) {
153 	case AF_INET:
154 		if ((size_t)namelen != sizeof (sin_t)) {
155 			error = name->sa_family != so->so_family ?
156 			    EAFNOSUPPORT : EINVAL;
157 			eprintsoline(so, error);
158 			goto done;
159 		}
160 
161 		if ((flags & _SOBIND_XPG4_2) &&
162 		    (name->sa_family != so->so_family)) {
163 			/*
164 			 * This check has to be made for X/Open
165 			 * sockets however application failures have
166 			 * been observed when it is applied to
167 			 * all sockets.
168 			 */
169 			error = EAFNOSUPPORT;
170 			eprintsoline(so, error);
171 			goto done;
172 		}
173 		/*
174 		 * Force a zero sa_family to match so_family.
175 		 *
176 		 * Some programs like inetd(1M) don't set the
177 		 * family field. Other programs leave
178 		 * sin_family set to garbage - SunOS 4.X does
179 		 * not check the family field on a bind.
180 		 * We use the family field that
181 		 * was passed in to the socket() call.
182 		 */
183 		name->sa_family = so->so_family;
184 		break;
185 
186 	case AF_INET6: {
187 #ifdef DEBUG
188 		sin6_t *sin6 = (sin6_t *)name;
189 #endif
190 		if ((size_t)namelen != sizeof (sin6_t)) {
191 			error = name->sa_family != so->so_family ?
192 			    EAFNOSUPPORT : EINVAL;
193 			eprintsoline(so, error);
194 			goto done;
195 		}
196 
197 		if (name->sa_family != so->so_family) {
198 			/*
199 			 * With IPv6 we require the family to match
200 			 * unlike in IPv4.
201 			 */
202 			error = EAFNOSUPPORT;
203 			eprintsoline(so, error);
204 			goto done;
205 		}
206 #ifdef DEBUG
207 		/*
208 		 * Verify that apps don't forget to clear
209 		 * sin6_scope_id etc
210 		 */
211 		if (sin6->sin6_scope_id != 0 &&
212 		    !IN6_IS_ADDR_LINKSCOPE(&sin6->sin6_addr)) {
213 			zcmn_err(getzoneid(), CE_WARN,
214 			    "bind with uninitialized sin6_scope_id "
215 			    "(%d) on socket. Pid = %d\n",
216 			    (int)sin6->sin6_scope_id,
217 			    (int)curproc->p_pid);
218 		}
219 		if (sin6->__sin6_src_id != 0) {
220 			zcmn_err(getzoneid(), CE_WARN,
221 			    "bind with uninitialized __sin6_src_id "
222 			    "(%d) on socket. Pid = %d\n",
223 			    (int)sin6->__sin6_src_id,
224 			    (int)curproc->p_pid);
225 		}
226 #endif /* DEBUG */
227 
228 		break;
229 	}
230 	default:
231 		/* Just pass the request to the protocol */
232 		goto dobind;
233 	}
234 
235 	/*
236 	 * First we check if either NCA or KSSL has been enabled for
237 	 * the requested address, and if so, we fall back to TPI.
238 	 * If neither of those two services are enabled, then we just
239 	 * pass the request to the protocol.
240 	 *
241 	 * Note that KSSL can only be enabled on a socket if NCA is NOT
242 	 * enabled for that socket, hence the else-statement below.
243 	 */
244 	if (nl7c_enabled && ((so->so_family == AF_INET ||
245 	    so->so_family == AF_INET6) &&
246 	    nl7c_lookup_addr(name, namelen) != NULL)) {
247 		/*
248 		 * NL7C is not supported in non-global zones,
249 		 * we enforce this restriction here.
250 		 */
251 		if (so->so_zoneid == GLOBAL_ZONEID) {
252 			/* NCA should be used, so fall back to TPI */
253 			error = so_tpi_fallback(so, cr);
254 			SO_UNBLOCK_FALLBACK(so);
255 			if (error)
256 				return (error);
257 			else
258 				return (SOP_BIND(so, name, namelen, flags, cr));
259 		}
260 	} else if (so->so_type == SOCK_STREAM) {
261 		/* Check if KSSL has been configured for this address */
262 		kssl_ent_t ent;
263 		kssl_endpt_type_t type;
264 		struct T_bind_req bind_req;
265 		mblk_t *mp;
266 
267 		/*
268 		 * TODO: Check with KSSL team if we could add a function call
269 		 * that only queries whether KSSL is enabled for the given
270 		 * address.
271 		 */
272 		bind_req.PRIM_type = T_BIND_REQ;
273 		bind_req.ADDR_length = namelen;
274 		bind_req.ADDR_offset = (t_scalar_t)sizeof (bind_req);
275 		mp = soallocproto2(&bind_req, sizeof (bind_req),
276 		    name, namelen, 0, _ALLOC_SLEEP);
277 
278 		type = kssl_check_proxy(mp, so, &ent);
279 		freemsg(mp);
280 
281 		if (type != KSSL_NO_PROXY) {
282 			/*
283 			 * KSSL has been configured for this address, so
284 			 * we must fall back to TPI.
285 			 */
286 			kssl_release_ent(ent, so, type);
287 			error = so_tpi_fallback(so, cr);
288 			SO_UNBLOCK_FALLBACK(so);
289 			if (error)
290 				return (error);
291 			else
292 				return (SOP_BIND(so, name, namelen, flags, cr));
293 		}
294 	}
295 
296 dobind:
297 	error = (*so->so_downcalls->sd_bind)
298 	    (so->so_proto_handle, name, namelen, cr);
299 done:
300 	SO_UNBLOCK_FALLBACK(so);
301 
302 	return (error);
303 }
304 
305 int
306 so_listen(struct sonode *so, int backlog, struct cred *cr)
307 {
308 	int	error = 0;
309 
310 	ASSERT(MUTEX_NOT_HELD(&so->so_lock));
311 	SO_BLOCK_FALLBACK(so, SOP_LISTEN(so, backlog, cr));
312 
313 	error = (*so->so_downcalls->sd_listen)(so->so_proto_handle, backlog,
314 	    cr);
315 
316 	SO_UNBLOCK_FALLBACK(so);
317 
318 	return (error);
319 }
320 
321 
322 int
323 so_connect(struct sonode *so, const struct sockaddr *name,
324     socklen_t namelen, int fflag, int flags, struct cred *cr)
325 {
326 	int error = 0;
327 	sock_connid_t id;
328 
329 	ASSERT(MUTEX_NOT_HELD(&so->so_lock));
330 	SO_BLOCK_FALLBACK(so, SOP_CONNECT(so, name, namelen, fflag, flags, cr));
331 
332 	/*
333 	 * If there is a pending error, return error
334 	 * This can happen if a non blocking operation caused an error.
335 	 */
336 
337 	if (so->so_error != 0) {
338 		mutex_enter(&so->so_lock);
339 		error = sogeterr(so, B_TRUE);
340 		mutex_exit(&so->so_lock);
341 		if (error != 0)
342 			goto done;
343 	}
344 
345 	error = (*so->so_downcalls->sd_connect)(so->so_proto_handle,
346 	    name, namelen, &id, cr);
347 
348 	if (error == EINPROGRESS)
349 		error = so_wait_connected(so, fflag & (FNONBLOCK|FNDELAY), id);
350 
351 done:
352 	SO_UNBLOCK_FALLBACK(so);
353 	return (error);
354 }
355 
356 /*ARGSUSED*/
357 int
358 so_accept(struct sonode *so, int fflag, struct cred *cr, struct sonode **nsop)
359 {
360 	int error = 0;
361 	struct sonode *nso;
362 
363 	*nsop = NULL;
364 
365 	SO_BLOCK_FALLBACK(so, SOP_ACCEPT(so, fflag, cr, nsop));
366 	if ((so->so_state & SS_ACCEPTCONN) == 0) {
367 		SO_UNBLOCK_FALLBACK(so);
368 		return ((so->so_type == SOCK_DGRAM || so->so_type == SOCK_RAW) ?
369 		    EOPNOTSUPP : EINVAL);
370 	}
371 
372 	if ((error = so_acceptq_dequeue(so, (fflag & (FNONBLOCK|FNDELAY)),
373 	    &nso)) == 0) {
374 		ASSERT(nso != NULL);
375 
376 		/* finish the accept */
377 		error = (*so->so_downcalls->sd_accept)(so->so_proto_handle,
378 		    nso->so_proto_handle, (sock_upper_handle_t)nso, cr);
379 		if (error != 0) {
380 			(void) socket_close(nso, 0, cr);
381 			socket_destroy(nso);
382 		} else {
383 			*nsop = nso;
384 		}
385 	}
386 
387 	SO_UNBLOCK_FALLBACK(so);
388 	return (error);
389 }
390 
391 int
392 so_sendmsg(struct sonode *so, struct nmsghdr *msg, struct uio *uiop,
393     struct cred *cr)
394 {
395 	int error, flags;
396 	boolean_t dontblock;
397 	ssize_t orig_resid;
398 	mblk_t  *mp;
399 
400 	SO_BLOCK_FALLBACK(so, SOP_SENDMSG(so, msg, uiop, cr));
401 
402 	flags = msg->msg_flags;
403 	error = 0;
404 	dontblock = (flags & MSG_DONTWAIT) ||
405 	    (uiop->uio_fmode & (FNONBLOCK|FNDELAY));
406 
407 	if (!(flags & MSG_XPG4_2) && msg->msg_controllen != 0) {
408 		/*
409 		 * Old way of passing fd's is not supported
410 		 */
411 		SO_UNBLOCK_FALLBACK(so);
412 		return (EOPNOTSUPP);
413 	}
414 
415 	if ((so->so_mode & SM_ATOMIC) &&
416 	    uiop->uio_resid > so->so_proto_props.sopp_maxpsz &&
417 	    so->so_proto_props.sopp_maxpsz != -1) {
418 		SO_UNBLOCK_FALLBACK(so);
419 		return (EMSGSIZE);
420 	}
421 
422 	/*
423 	 * For atomic sends we will only do one iteration.
424 	 */
425 	do {
426 		if (so->so_state & SS_CANTSENDMORE) {
427 			error = EPIPE;
428 			break;
429 		}
430 
431 		if (so->so_error != 0) {
432 			mutex_enter(&so->so_lock);
433 			error = sogeterr(so, B_TRUE);
434 			mutex_exit(&so->so_lock);
435 			if (error != 0)
436 				break;
437 		}
438 
439 		/*
440 		 * Send down OOB messages even if the send path is being
441 		 * flow controlled (assuming the protocol supports OOB data).
442 		 */
443 		if (flags & MSG_OOB) {
444 			if ((so->so_mode & SM_EXDATA) == 0) {
445 				error = EOPNOTSUPP;
446 				break;
447 			}
448 		} else if (so->so_snd_qfull) {
449 			/*
450 			 * Need to wait until the protocol is ready to receive
451 			 * more data for transmission.
452 			 */
453 			if ((error = so_snd_wait_qnotfull(so, dontblock)) != 0)
454 				break;
455 		}
456 
457 		/*
458 		 * Time to send data to the protocol. We either copy the
459 		 * data into mblks or pass the uio directly to the protocol.
460 		 * We decide what to do based on the available down calls.
461 		 */
462 		if (so->so_downcalls->sd_send_uio != NULL) {
463 			error = (*so->so_downcalls->sd_send_uio)
464 			    (so->so_proto_handle, uiop, msg, cr);
465 			if (error != 0)
466 				break;
467 		} else {
468 			/* save the resid in case of failure */
469 			orig_resid = uiop->uio_resid;
470 
471 			if ((mp = socopyinuio(uiop,
472 			    so->so_proto_props.sopp_maxpsz,
473 			    so->so_proto_props.sopp_wroff,
474 			    so->so_proto_props.sopp_maxblk,
475 			    so->so_proto_props.sopp_tail, &error)) == NULL) {
476 				break;
477 			}
478 			ASSERT(uiop->uio_resid >= 0);
479 
480 			error = (*so->so_downcalls->sd_send)
481 			    (so->so_proto_handle, mp, msg, cr);
482 			if (error != 0) {
483 				/*
484 				 * The send failed. We do not have to free the
485 				 * mblks, because that is the protocol's
486 				 * responsibility. However, uio_resid must
487 				 * remain accurate, so adjust that here.
488 				 */
489 				uiop->uio_resid = orig_resid;
490 					break;
491 			}
492 		}
493 	} while (uiop->uio_resid > 0);
494 
495 	SO_UNBLOCK_FALLBACK(so);
496 
497 	return (error);
498 }
499 
500 int
501 so_sendmblk(struct sonode *so, struct nmsghdr *msg, int fflag,
502     struct cred *cr, mblk_t **mpp)
503 {
504 	int error;
505 	boolean_t dontblock;
506 	size_t size;
507 	mblk_t *mp = *mpp;
508 
509 	SO_BLOCK_FALLBACK(so, SOP_SENDMBLK(so, msg, fflag, cr, mpp));
510 
511 	error = 0;
512 	dontblock = (msg->msg_flags & MSG_DONTWAIT) ||
513 	    (fflag & (FNONBLOCK|FNDELAY));
514 	size = msgdsize(mp);
515 
516 	if ((so->so_mode & SM_SENDFILESUPP) == 0 ||
517 	    so->so_downcalls->sd_send == NULL) {
518 		SO_UNBLOCK_FALLBACK(so);
519 		return (EOPNOTSUPP);
520 	}
521 
522 	if ((so->so_mode & SM_ATOMIC) &&
523 	    size > so->so_proto_props.sopp_maxpsz &&
524 	    so->so_proto_props.sopp_maxpsz != -1) {
525 		SO_UNBLOCK_FALLBACK(so);
526 		return (EMSGSIZE);
527 	}
528 
529 	while (mp != NULL) {
530 		mblk_t *nmp, *last_mblk;
531 		size_t mlen;
532 
533 		if (so->so_state & SS_CANTSENDMORE) {
534 			error = EPIPE;
535 			break;
536 		}
537 		if (so->so_error != 0) {
538 			mutex_enter(&so->so_lock);
539 			error = sogeterr(so, B_TRUE);
540 			mutex_exit(&so->so_lock);
541 			if (error != 0)
542 				break;
543 		}
544 		if (so->so_snd_qfull) {
545 			/*
546 			 * Need to wait until the protocol is ready to receive
547 			 * more data for transmission.
548 			 */
549 			if ((error = so_snd_wait_qnotfull(so, dontblock)) != 0)
550 				break;
551 		}
552 
553 		/*
554 		 * We only allow so_maxpsz of data to be sent down to
555 		 * the protocol at time.
556 		 */
557 		mlen = MBLKL(mp);
558 		nmp = mp->b_cont;
559 		last_mblk = mp;
560 		while (nmp != NULL) {
561 			mlen += MBLKL(nmp);
562 			if (mlen > so->so_proto_props.sopp_maxpsz) {
563 				last_mblk->b_cont = NULL;
564 				break;
565 			}
566 			last_mblk = nmp;
567 			nmp = nmp->b_cont;
568 		}
569 
570 		error = (*so->so_downcalls->sd_send)
571 		    (so->so_proto_handle, mp, msg, cr);
572 		if (error != 0) {
573 			/*
574 			 * The send failed. The protocol will free the mblks
575 			 * that were sent down. Let the caller deal with the
576 			 * rest.
577 			 */
578 			*mpp = nmp;
579 			break;
580 		}
581 
582 		*mpp = mp = nmp;
583 	}
584 
585 	SO_UNBLOCK_FALLBACK(so);
586 
587 	return (error);
588 }
589 
590 int
591 so_shutdown(struct sonode *so, int how, struct cred *cr)
592 {
593 	int error;
594 
595 	SO_BLOCK_FALLBACK(so, SOP_SHUTDOWN(so, how, cr));
596 
597 	/*
598 	 * SunOS 4.X has no check for datagram sockets.
599 	 * 5.X checks that it is connected (ENOTCONN)
600 	 * X/Open requires that we check the connected state.
601 	 */
602 	if (!(so->so_state & SS_ISCONNECTED)) {
603 		if (!xnet_skip_checks) {
604 			error = ENOTCONN;
605 			if (xnet_check_print) {
606 				printf("sockfs: X/Open shutdown check "
607 				    "caused ENOTCONN\n");
608 			}
609 		}
610 		goto done;
611 	}
612 
613 	error = ((*so->so_downcalls->sd_shutdown)(so->so_proto_handle,
614 	    how, cr));
615 
616 	/*
617 	 * Protocol agreed to shutdown. We need to flush the
618 	 * receive buffer if the receive side is being shutdown.
619 	 */
620 	if (error == 0 && how != SHUT_WR) {
621 		mutex_enter(&so->so_lock);
622 		/* wait for active reader to finish */
623 		(void) so_lock_read(so, 0);
624 
625 		so_rcv_flush(so);
626 
627 		so_unlock_read(so);
628 		mutex_exit(&so->so_lock);
629 	}
630 
631 done:
632 	SO_UNBLOCK_FALLBACK(so);
633 	return (error);
634 }
635 
636 int
637 so_getsockname(struct sonode *so, struct sockaddr *addr,
638     socklen_t *addrlen, struct cred *cr)
639 {
640 	int error;
641 
642 	SO_BLOCK_FALLBACK(so, SOP_GETSOCKNAME(so, addr, addrlen, cr));
643 
644 	error = (*so->so_downcalls->sd_getsockname)
645 	    (so->so_proto_handle, addr, addrlen, cr);
646 
647 	SO_UNBLOCK_FALLBACK(so);
648 	return (error);
649 }
650 
651 int
652 so_getpeername(struct sonode *so, struct sockaddr *addr,
653     socklen_t *addrlen, boolean_t accept, struct cred *cr)
654 {
655 	int error;
656 
657 	SO_BLOCK_FALLBACK(so, SOP_GETPEERNAME(so, addr, addrlen, accept, cr));
658 
659 	if (accept) {
660 		error = (*so->so_downcalls->sd_getpeername)
661 		    (so->so_proto_handle, addr, addrlen, cr);
662 	} else if (!(so->so_state & SS_ISCONNECTED)) {
663 		error = ENOTCONN;
664 	} else if ((so->so_state & SS_CANTSENDMORE) && !xnet_skip_checks) {
665 		/* Added this check for X/Open */
666 		error = EINVAL;
667 		if (xnet_check_print) {
668 			printf("sockfs: X/Open getpeername check => EINVAL\n");
669 		}
670 	} else {
671 		error = (*so->so_downcalls->sd_getpeername)
672 		    (so->so_proto_handle, addr, addrlen, cr);
673 	}
674 
675 	SO_UNBLOCK_FALLBACK(so);
676 	return (error);
677 }
678 
679 int
680 so_getsockopt(struct sonode *so, int level, int option_name,
681     void *optval, socklen_t *optlenp, int flags, struct cred *cr)
682 {
683 	int error = 0;
684 
685 	ASSERT(MUTEX_NOT_HELD(&so->so_lock));
686 	SO_BLOCK_FALLBACK(so,
687 	    SOP_GETSOCKOPT(so, level, option_name, optval, optlenp, flags, cr));
688 
689 	error = socket_getopt_common(so, level, option_name, optval, optlenp,
690 	    flags);
691 	if (error < 0) {
692 		error = (*so->so_downcalls->sd_getsockopt)
693 		    (so->so_proto_handle, level, option_name, optval, optlenp,
694 		    cr);
695 		if (error ==  ENOPROTOOPT) {
696 			if (level == SOL_SOCKET) {
697 				/*
698 				 * If a protocol does not support a particular
699 				 * socket option, set can fail (not allowed)
700 				 * but get can not fail. This is the previous
701 				 * sockfs bahvior.
702 				 */
703 				switch (option_name) {
704 				case SO_LINGER:
705 					if (*optlenp < (t_uscalar_t)
706 					    sizeof (struct linger)) {
707 						error = EINVAL;
708 						break;
709 					}
710 					error = 0;
711 					bzero(optval, sizeof (struct linger));
712 					*optlenp = sizeof (struct linger);
713 					break;
714 				case SO_RCVTIMEO:
715 				case SO_SNDTIMEO:
716 					if (*optlenp < (t_uscalar_t)
717 					    sizeof (struct timeval)) {
718 						error = EINVAL;
719 						break;
720 					}
721 					error = 0;
722 					bzero(optval, sizeof (struct timeval));
723 					*optlenp = sizeof (struct timeval);
724 					break;
725 				case SO_SND_BUFINFO:
726 					if (*optlenp < (t_uscalar_t)
727 					    sizeof (struct so_snd_bufinfo)) {
728 						error = EINVAL;
729 						break;
730 					}
731 					error = 0;
732 					bzero(optval,
733 					    sizeof (struct so_snd_bufinfo));
734 					*optlenp =
735 					    sizeof (struct so_snd_bufinfo);
736 					break;
737 				case SO_DEBUG:
738 				case SO_REUSEADDR:
739 				case SO_KEEPALIVE:
740 				case SO_DONTROUTE:
741 				case SO_BROADCAST:
742 				case SO_USELOOPBACK:
743 				case SO_OOBINLINE:
744 				case SO_DGRAM_ERRIND:
745 				case SO_SNDBUF:
746 				case SO_RCVBUF:
747 					error = 0;
748 					*((int32_t *)optval) = 0;
749 					*optlenp = sizeof (int32_t);
750 					break;
751 				default:
752 					break;
753 				}
754 			}
755 		}
756 	}
757 
758 	SO_UNBLOCK_FALLBACK(so);
759 	return (error);
760 }
761 
762 int
763 so_setsockopt(struct sonode *so, int level, int option_name,
764     const void *optval, socklen_t optlen, struct cred *cr)
765 {
766 	int error = 0;
767 
768 	SO_BLOCK_FALLBACK(so,
769 	    SOP_SETSOCKOPT(so, level, option_name, optval, optlen, cr));
770 
771 	/* X/Open requires this check */
772 	if (so->so_state & SS_CANTSENDMORE && !xnet_skip_checks) {
773 		SO_UNBLOCK_FALLBACK(so);
774 		if (xnet_check_print)
775 			printf("sockfs: X/Open setsockopt check => EINVAL\n");
776 		return (EINVAL);
777 	}
778 
779 	if (level == SOL_SOCKET) {
780 		switch (option_name) {
781 		case SO_RCVTIMEO:
782 		case SO_SNDTIMEO: {
783 			struct timeval tl;
784 			clock_t t_usec;
785 
786 			if (get_udatamodel() == DATAMODEL_NATIVE) {
787 				if (optlen != sizeof (struct timeval)) {
788 					error = EINVAL;
789 					goto done;
790 				}
791 				bcopy((struct timeval *)optval, &tl,
792 				    sizeof (struct timeval));
793 			} else {
794 				if (optlen != sizeof (struct timeval32)) {
795 					error = EINVAL;
796 					goto done;
797 				}
798 				TIMEVAL32_TO_TIMEVAL(&tl,
799 				    (struct timeval32 *)optval);
800 			}
801 			t_usec = tl.tv_sec * 1000 * 1000 + tl.tv_usec;
802 			mutex_enter(&so->so_lock);
803 			if (option_name == SO_RCVTIMEO)
804 				so->so_rcvtimeo = drv_usectohz(t_usec);
805 			else
806 				so->so_sndtimeo = drv_usectohz(t_usec);
807 			mutex_exit(&so->so_lock);
808 			SO_UNBLOCK_FALLBACK(so);
809 			return (0);
810 		}
811 		case SO_RCVBUF:
812 			/*
813 			 * XXX XPG 4.2 applications retrieve SO_RCVBUF from
814 			 * sockfs since the transport might adjust the value
815 			 * and not return exactly what was set by the
816 			 * application.
817 			 */
818 			so->so_xpg_rcvbuf = *(int32_t *)optval;
819 			break;
820 		}
821 	}
822 	error = (*so->so_downcalls->sd_setsockopt)
823 	    (so->so_proto_handle, level, option_name, optval, optlen, cr);
824 done:
825 	SO_UNBLOCK_FALLBACK(so);
826 	return (error);
827 }
828 
829 int
830 so_ioctl(struct sonode *so, int cmd, intptr_t arg, int mode,
831     struct cred *cr, int32_t *rvalp)
832 {
833 	int error = 0;
834 
835 	SO_BLOCK_FALLBACK(so, SOP_IOCTL(so, cmd, arg, mode, cr, rvalp));
836 
837 	/*
838 	 * If there is a pending error, return error
839 	 * This can happen if a non blocking operation caused an error.
840 	 */
841 	if (so->so_error != 0) {
842 		mutex_enter(&so->so_lock);
843 		error = sogeterr(so, B_TRUE);
844 		mutex_exit(&so->so_lock);
845 		if (error != 0)
846 			goto done;
847 	}
848 
849 	/*
850 	 * calling strioc can result in the socket falling back to TPI,
851 	 * if that is supported.
852 	 */
853 	if ((error = socket_ioctl_common(so, cmd, arg, mode, cr, rvalp)) < 0 &&
854 	    (error = socket_strioc_common(so, cmd, arg, mode, cr, rvalp)) < 0) {
855 		error = (*so->so_downcalls->sd_ioctl)(so->so_proto_handle,
856 		    cmd, arg, mode, rvalp, cr);
857 	}
858 
859 done:
860 	SO_UNBLOCK_FALLBACK(so);
861 
862 	return (error);
863 }
864 
865 int
866 so_poll(struct sonode *so, short events, int anyyet, short *reventsp,
867     struct pollhead **phpp)
868 {
869 	int state = so->so_state;
870 	*reventsp = 0;
871 
872 	if (so->so_error != 0 &&
873 	    ((POLLIN|POLLRDNORM|POLLOUT) & events)  != 0) {
874 		*reventsp = (POLLIN|POLLRDNORM|POLLOUT) & events;
875 		return (0);
876 	}
877 
878 	/*
879 	 * As long as there is buffer to send data, and the socket is
880 	 * in a state where it can send data (i.e., connected for
881 	 * connection oriented protocols), then turn on POLLOUT events
882 	 */
883 	if (!so->so_snd_qfull && ((so->so_mode & SM_CONNREQUIRED) == 0 ||
884 	    state & SS_ISCONNECTED)) {
885 		*reventsp |= POLLOUT & events;
886 	}
887 
888 	/*
889 	 * Turn on POLLIN whenever there is data on the receive queue,
890 	 * or the socket is in a state where no more data will be received.
891 	 * Also, if the socket is accepting connections, flip the bit if
892 	 * there is something on the queue.
893 	 *
894 	 * We do an initial check for events without holding locks. However,
895 	 * if there are no event available, then we redo the check for POLLIN
896 	 * events under the lock.
897 	 */
898 
899 	/* Pending connections */
900 	if (so->so_acceptq_len > 0)
901 		*reventsp |= (POLLIN|POLLRDNORM) & events;
902 
903 	/* Data */
904 	/* so_downcalls is null for sctp */
905 	if (so->so_downcalls != NULL && so->so_downcalls->sd_poll != NULL) {
906 		*reventsp |= (*so->so_downcalls->sd_poll)
907 		    (so->so_proto_handle, events & SO_PROTO_POLLEV, anyyet,
908 		    CRED()) & events;
909 		ASSERT((*reventsp & ~events) == 0);
910 		/* do not recheck events */
911 		events &= ~SO_PROTO_POLLEV;
912 	} else {
913 		if (SO_HAVE_DATA(so))
914 			*reventsp |= (POLLIN|POLLRDNORM) & events;
915 
916 		/* Urgent data */
917 		if ((state & SS_OOBPEND) != 0)
918 			*reventsp |= (POLLRDBAND) & events;
919 	}
920 
921 	if (!*reventsp && !anyyet) {
922 		/* Check for read events again, but this time under lock */
923 		if (events & (POLLIN|POLLRDNORM)) {
924 			mutex_enter(&so->so_lock);
925 			if (SO_HAVE_DATA(so) || so->so_acceptq_len > 0) {
926 				mutex_exit(&so->so_lock);
927 				*reventsp |= (POLLIN|POLLRDNORM) & events;
928 				return (0);
929 			} else {
930 				so->so_pollev |= SO_POLLEV_IN;
931 				mutex_exit(&so->so_lock);
932 			}
933 		}
934 		*phpp = &so->so_poll_list;
935 	}
936 	return (0);
937 }
938 
939 /*
940  * Generic Upcalls
941  */
942 void
943 so_connected(sock_upper_handle_t sock_handle, sock_connid_t id,
944     cred_t *peer_cred, pid_t peer_cpid)
945 {
946 	struct sonode *so = (struct sonode *)sock_handle;
947 
948 	mutex_enter(&so->so_lock);
949 	ASSERT(so->so_proto_handle != NULL);
950 
951 	if (peer_cred != NULL) {
952 		if (so->so_peercred != NULL)
953 			crfree(so->so_peercred);
954 		crhold(peer_cred);
955 		so->so_peercred = peer_cred;
956 		so->so_cpid = peer_cpid;
957 	}
958 
959 	so->so_proto_connid = id;
960 	soisconnected(so);
961 	/*
962 	 * Wake ones who're waiting for conn to become established.
963 	 */
964 	so_notify_connected(so);
965 }
966 
967 int
968 so_disconnected(sock_upper_handle_t sock_handle, sock_connid_t id, int error)
969 {
970 	struct sonode *so = (struct sonode *)sock_handle;
971 
972 	mutex_enter(&so->so_lock);
973 
974 	so->so_proto_connid = id;
975 	soisdisconnected(so, error);
976 	so_notify_disconnected(so, error);
977 
978 	return (0);
979 }
980 
981 void
982 so_opctl(sock_upper_handle_t sock_handle, sock_opctl_action_t action,
983     uintptr_t arg)
984 {
985 	struct sonode *so = (struct sonode *)sock_handle;
986 
987 	switch (action) {
988 	case SOCK_OPCTL_SHUT_SEND:
989 		mutex_enter(&so->so_lock);
990 		socantsendmore(so);
991 		so_notify_disconnecting(so);
992 		break;
993 	case SOCK_OPCTL_SHUT_RECV: {
994 		mutex_enter(&so->so_lock);
995 		socantrcvmore(so);
996 		so_notify_eof(so);
997 		break;
998 	}
999 	case SOCK_OPCTL_ENAB_ACCEPT:
1000 		mutex_enter(&so->so_lock);
1001 		so->so_state |= SS_ACCEPTCONN;
1002 		so->so_backlog = (unsigned int)arg;
1003 		mutex_exit(&so->so_lock);
1004 		break;
1005 	default:
1006 		ASSERT(0);
1007 		break;
1008 	}
1009 }
1010 
1011 void
1012 so_txq_full(sock_upper_handle_t sock_handle, boolean_t qfull)
1013 {
1014 	struct sonode *so = (struct sonode *)sock_handle;
1015 
1016 	if (qfull) {
1017 		so_snd_qfull(so);
1018 	} else {
1019 		so_snd_qnotfull(so);
1020 		mutex_enter(&so->so_lock);
1021 		so_notify_writable(so);
1022 	}
1023 }
1024 
1025 sock_upper_handle_t
1026 so_newconn(sock_upper_handle_t parenthandle,
1027     sock_lower_handle_t proto_handle, sock_downcalls_t *sock_downcalls,
1028     struct cred *peer_cred, pid_t peer_cpid, sock_upcalls_t **sock_upcallsp)
1029 {
1030 	struct sonode	*so = (struct sonode *)parenthandle;
1031 	struct sonode	*nso;
1032 	int error;
1033 
1034 	ASSERT(proto_handle != NULL);
1035 
1036 	if ((so->so_state & SS_ACCEPTCONN) == 0 ||
1037 	    so->so_acceptq_len >= so->so_backlog)
1038 		return (NULL);
1039 
1040 	nso = socket_newconn(so, proto_handle, sock_downcalls, SOCKET_NOSLEEP,
1041 	    &error);
1042 	if (nso == NULL)
1043 		return (NULL);
1044 
1045 	if (peer_cred != NULL) {
1046 		crhold(peer_cred);
1047 		nso->so_peercred = peer_cred;
1048 		nso->so_cpid = peer_cpid;
1049 	}
1050 
1051 	(void) so_acceptq_enqueue(so, nso);
1052 	mutex_enter(&so->so_lock);
1053 	so_notify_newconn(so);
1054 
1055 	*sock_upcallsp = &so_upcalls;
1056 
1057 	return ((sock_upper_handle_t)nso);
1058 }
1059 
1060 void
1061 so_set_prop(sock_upper_handle_t sock_handle, struct sock_proto_props *soppp)
1062 {
1063 	struct sonode *so;
1064 
1065 	so = (struct sonode *)sock_handle;
1066 
1067 	mutex_enter(&so->so_lock);
1068 
1069 	if (soppp->sopp_flags & SOCKOPT_MAXBLK)
1070 		so->so_proto_props.sopp_maxblk = soppp->sopp_maxblk;
1071 	if (soppp->sopp_flags & SOCKOPT_WROFF)
1072 		so->so_proto_props.sopp_wroff = soppp->sopp_wroff;
1073 	if (soppp->sopp_flags & SOCKOPT_TAIL)
1074 		so->so_proto_props.sopp_tail = soppp->sopp_tail;
1075 	if (soppp->sopp_flags & SOCKOPT_RCVHIWAT)
1076 		so->so_proto_props.sopp_rxhiwat = soppp->sopp_rxhiwat;
1077 	if (soppp->sopp_flags & SOCKOPT_RCVLOWAT)
1078 		so->so_proto_props.sopp_rxlowat = soppp->sopp_rxlowat;
1079 	if (soppp->sopp_flags & SOCKOPT_MAXPSZ)
1080 		so->so_proto_props.sopp_maxpsz = soppp->sopp_maxpsz;
1081 	if (soppp->sopp_flags & SOCKOPT_MINPSZ)
1082 		so->so_proto_props.sopp_minpsz = soppp->sopp_minpsz;
1083 	if (soppp->sopp_flags & SOCKOPT_ZCOPY) {
1084 		if (soppp->sopp_zcopyflag & ZCVMSAFE) {
1085 			so->so_proto_props.sopp_zcopyflag |= STZCVMSAFE;
1086 			so->so_proto_props.sopp_zcopyflag &= ~STZCVMUNSAFE;
1087 		} else if (soppp->sopp_zcopyflag & ZCVMUNSAFE) {
1088 			so->so_proto_props.sopp_zcopyflag |= STZCVMUNSAFE;
1089 			so->so_proto_props.sopp_zcopyflag &= ~STZCVMSAFE;
1090 		}
1091 
1092 		if (soppp->sopp_zcopyflag & COPYCACHED) {
1093 			so->so_proto_props.sopp_zcopyflag |= STRCOPYCACHED;
1094 		}
1095 	}
1096 	if (soppp->sopp_flags & SOCKOPT_OOBINLINE)
1097 		so->so_proto_props.sopp_oobinline = soppp->sopp_oobinline;
1098 	if (soppp->sopp_flags & SOCKOPT_RCVTIMER)
1099 		so->so_proto_props.sopp_rcvtimer = soppp->sopp_rcvtimer;
1100 	if (soppp->sopp_flags & SOCKOPT_RCVTHRESH)
1101 		so->so_proto_props.sopp_rcvthresh = soppp->sopp_rcvthresh;
1102 	if (soppp->sopp_flags & SOCKOPT_MAXADDRLEN)
1103 		so->so_proto_props.sopp_maxaddrlen = soppp->sopp_maxaddrlen;
1104 
1105 	mutex_exit(&so->so_lock);
1106 
1107 #ifdef DEBUG
1108 	soppp->sopp_flags &= ~(SOCKOPT_MAXBLK | SOCKOPT_WROFF | SOCKOPT_TAIL |
1109 	    SOCKOPT_RCVHIWAT | SOCKOPT_RCVLOWAT | SOCKOPT_MAXPSZ |
1110 	    SOCKOPT_ZCOPY | SOCKOPT_OOBINLINE | SOCKOPT_RCVTIMER |
1111 	    SOCKOPT_RCVTHRESH | SOCKOPT_MAXADDRLEN | SOCKOPT_MINPSZ);
1112 	ASSERT(soppp->sopp_flags == 0);
1113 #endif
1114 }
1115 
1116 /* ARGSUSED */
1117 ssize_t
1118 so_queue_msg(sock_upper_handle_t sock_handle, mblk_t *mp,
1119     size_t msg_size, int flags, int *errorp,  boolean_t *force_pushp)
1120 {
1121 	struct sonode *so = (struct sonode *)sock_handle;
1122 	boolean_t force_push = B_TRUE;
1123 	int space_left;
1124 	sodirect_t *sodp = so->so_direct;
1125 
1126 	ASSERT(errorp != NULL);
1127 	*errorp = 0;
1128 	if (mp == NULL) {
1129 		if (msg_size > 0) {
1130 			ASSERT(so->so_downcalls->sd_recv_uio != NULL);
1131 			mutex_enter(&so->so_lock);
1132 			/* the notify functions will drop the lock */
1133 			if (flags & MSG_OOB)
1134 				so_notify_oobdata(so, IS_SO_OOB_INLINE(so));
1135 			else
1136 				so_notify_data(so, msg_size);
1137 			return (0);
1138 		}
1139 		/*
1140 		 * recv space check
1141 		 */
1142 		mutex_enter(&so->so_lock);
1143 		space_left = so->so_rcvbuf - so->so_rcv_queued;
1144 		if (space_left <= 0) {
1145 			so->so_flowctrld = B_TRUE;
1146 			*errorp = ENOSPC;
1147 			space_left = -1;
1148 		}
1149 		goto done_unlock;
1150 	}
1151 
1152 	ASSERT(mp->b_next == NULL);
1153 	ASSERT(DB_TYPE(mp) == M_DATA || DB_TYPE(mp) == M_PROTO);
1154 	ASSERT(msg_size == msgdsize(mp));
1155 
1156 	if (flags & MSG_OOB) {
1157 		so_queue_oob(sock_handle, mp, msg_size);
1158 		return (0);
1159 	}
1160 
1161 	if (force_pushp != NULL)
1162 		force_push = *force_pushp;
1163 
1164 	if (DB_TYPE(mp) == M_PROTO && !__TPI_PRIM_ISALIGNED(mp->b_rptr)) {
1165 		/* The read pointer is not aligned correctly for TPI */
1166 		zcmn_err(getzoneid(), CE_WARN,
1167 		    "sockfs: Unaligned TPI message received. rptr = %p\n",
1168 		    (void *)mp->b_rptr);
1169 		freemsg(mp);
1170 		mutex_enter(sodp->sod_lockp);
1171 		SOD_UIOAFINI(sodp);
1172 		mutex_exit(sodp->sod_lockp);
1173 
1174 		return (so->so_rcvbuf - so->so_rcv_queued);
1175 	}
1176 
1177 	mutex_enter(&so->so_lock);
1178 	if (so->so_state & (SS_FALLBACK_PENDING | SS_FALLBACK_COMP)) {
1179 		SOD_DISABLE(sodp);
1180 		mutex_exit(&so->so_lock);
1181 		*errorp = EOPNOTSUPP;
1182 		return (-1);
1183 	}
1184 	if (so->so_state & SS_CANTRCVMORE) {
1185 		freemsg(mp);
1186 		SOD_DISABLE(sodp);
1187 		mutex_exit(&so->so_lock);
1188 		return (0);
1189 	}
1190 
1191 	/* process the mblk via I/OAT if capable */
1192 	if (sodp != NULL && (sodp->sod_state & SOD_ENABLED)) {
1193 		if (DB_TYPE(mp) == M_DATA) {
1194 			(void) sod_uioa_mblk_init(sodp, mp, msg_size);
1195 		} else {
1196 			SOD_UIOAFINI(sodp);
1197 		}
1198 	}
1199 
1200 	if (mp->b_next == NULL) {
1201 		so_enqueue_msg(so, mp, msg_size);
1202 	} else {
1203 		do {
1204 			mblk_t *nmp;
1205 
1206 			if ((nmp = mp->b_next) != NULL) {
1207 				mp->b_next = NULL;
1208 			}
1209 			so_enqueue_msg(so, mp, msgdsize(mp));
1210 			mp = nmp;
1211 		} while (mp != NULL);
1212 	}
1213 
1214 	space_left = so->so_rcvbuf - so->so_rcv_queued;
1215 	if (space_left <= 0) {
1216 		so->so_flowctrld = B_TRUE;
1217 		*errorp = ENOSPC;
1218 		space_left = -1;
1219 	}
1220 
1221 	if (force_push || so->so_rcv_queued >= so->so_rcv_thresh ||
1222 	    so->so_rcv_queued >= so->so_rcv_wanted ||
1223 	    (sodp != NULL && so->so_rcv_queued >= sodp->sod_want)) {
1224 		SOCKET_TIMER_CANCEL(so);
1225 		/*
1226 		 * so_notify_data will release the lock
1227 		 */
1228 		so_notify_data(so, so->so_rcv_queued);
1229 
1230 		if (force_pushp != NULL)
1231 			*force_pushp = B_TRUE;
1232 		goto done;
1233 	} else if (so->so_rcv_timer_tid == 0) {
1234 		/* Make sure the recv push timer is running */
1235 		SOCKET_TIMER_START(so);
1236 	}
1237 
1238 done_unlock:
1239 	mutex_exit(&so->so_lock);
1240 done:
1241 	return (space_left);
1242 }
1243 
1244 /*
1245  * Set the offset of where the oob data is relative to the bytes in
1246  * queued. Also generate SIGURG
1247  */
1248 void
1249 so_signal_oob(sock_upper_handle_t sock_handle, ssize_t offset)
1250 {
1251 	struct sonode *so;
1252 
1253 	ASSERT(offset >= 0);
1254 	so = (struct sonode *)sock_handle;
1255 	mutex_enter(&so->so_lock);
1256 	SOD_UIOAFINI(so->so_direct);
1257 
1258 	/*
1259 	 * New urgent data on the way so forget about any old
1260 	 * urgent data.
1261 	 */
1262 	so->so_state &= ~(SS_HAVEOOBDATA|SS_HADOOBDATA);
1263 
1264 	/*
1265 	 * Record that urgent data is pending.
1266 	 */
1267 	so->so_state |= SS_OOBPEND;
1268 
1269 	if (so->so_oobmsg != NULL) {
1270 		dprintso(so, 1, ("sock: discarding old oob\n"));
1271 		freemsg(so->so_oobmsg);
1272 		so->so_oobmsg = NULL;
1273 	}
1274 
1275 	/*
1276 	 * set the offset where the urgent byte is
1277 	 */
1278 	so->so_oobmark = so->so_rcv_queued + offset;
1279 	if (so->so_oobmark == 0)
1280 		so->so_state |= SS_RCVATMARK;
1281 	else
1282 		so->so_state &= ~SS_RCVATMARK;
1283 
1284 	so_notify_oobsig(so);
1285 }
1286 
1287 /*
1288  * Queue the OOB byte
1289  */
1290 static void
1291 so_queue_oob(sock_upper_handle_t sock_handle, mblk_t *mp, size_t len)
1292 {
1293 	struct sonode *so;
1294 
1295 	so = (struct sonode *)sock_handle;
1296 	mutex_enter(&so->so_lock);
1297 	SOD_UIOAFINI(so->so_direct);
1298 
1299 	ASSERT(mp != NULL);
1300 	if (!IS_SO_OOB_INLINE(so)) {
1301 		so->so_oobmsg = mp;
1302 		so->so_state |= SS_HAVEOOBDATA;
1303 	} else {
1304 		so_enqueue_msg(so, mp, len);
1305 	}
1306 
1307 	so_notify_oobdata(so, IS_SO_OOB_INLINE(so));
1308 }
1309 
1310 int
1311 so_close(struct sonode *so, int flag, struct cred *cr)
1312 {
1313 	int error;
1314 
1315 	error = (*so->so_downcalls->sd_close)(so->so_proto_handle, flag, cr);
1316 
1317 	/*
1318 	 * At this point there will be no more upcalls from the protocol
1319 	 */
1320 	mutex_enter(&so->so_lock);
1321 
1322 	ASSERT(so_verify_oobstate(so));
1323 
1324 	so_rcv_flush(so);
1325 	mutex_exit(&so->so_lock);
1326 
1327 	return (error);
1328 }
1329 
1330 void
1331 so_zcopy_notify(sock_upper_handle_t sock_handle)
1332 {
1333 	struct sonode *so = (struct sonode *)sock_handle;
1334 
1335 	mutex_enter(&so->so_lock);
1336 	so->so_copyflag |= STZCNOTIFY;
1337 	cv_broadcast(&so->so_copy_cv);
1338 	mutex_exit(&so->so_lock);
1339 }
1340 
1341 void
1342 so_set_error(sock_upper_handle_t sock_handle, int error)
1343 {
1344 	struct sonode *so = (struct sonode *)sock_handle;
1345 
1346 	mutex_enter(&so->so_lock);
1347 
1348 	soseterror(so, error);
1349 
1350 	so_notify_error(so);
1351 }
1352 
1353 /*
1354  * so_recvmsg - read data from the socket
1355  *
1356  * There are two ways of obtaining data; either we ask the protocol to
1357  * copy directly into the supplied buffer, or we copy data from the
1358  * sonode's receive queue. The decision which one to use depends on
1359  * whether the protocol has a sd_recv_uio down call.
1360  */
1361 int
1362 so_recvmsg(struct sonode *so, struct nmsghdr *msg, struct uio *uiop,
1363     struct cred *cr)
1364 {
1365 	rval_t 		rval;
1366 	int 		flags = 0;
1367 	t_uscalar_t	controllen, namelen;
1368 	int 		error = 0;
1369 	int ret;
1370 	mblk_t		*mctlp = NULL;
1371 	union T_primitives *tpr;
1372 	void		*control;
1373 	ssize_t		saved_resid;
1374 	struct uio	*suiop;
1375 
1376 	SO_BLOCK_FALLBACK(so, SOP_RECVMSG(so, msg, uiop, cr));
1377 
1378 	if ((so->so_state & (SS_ISCONNECTED|SS_CANTRCVMORE)) == 0 &&
1379 	    (so->so_mode & SM_CONNREQUIRED)) {
1380 		SO_UNBLOCK_FALLBACK(so);
1381 		return (ENOTCONN);
1382 	}
1383 
1384 	if (msg->msg_flags & MSG_PEEK)
1385 		msg->msg_flags &= ~MSG_WAITALL;
1386 
1387 	if (so->so_mode & SM_ATOMIC)
1388 		msg->msg_flags |= MSG_TRUNC;
1389 
1390 	if (msg->msg_flags & MSG_OOB) {
1391 		if ((so->so_mode & SM_EXDATA) == 0) {
1392 			error = EOPNOTSUPP;
1393 		} else if (so->so_downcalls->sd_recv_uio != NULL) {
1394 			error = (*so->so_downcalls->sd_recv_uio)
1395 			    (so->so_proto_handle, uiop, msg, cr);
1396 		} else {
1397 			error = sorecvoob(so, msg, uiop, msg->msg_flags,
1398 			    IS_SO_OOB_INLINE(so));
1399 		}
1400 		SO_UNBLOCK_FALLBACK(so);
1401 		return (error);
1402 	}
1403 
1404 	/*
1405 	 * If the protocol has the recv down call, then pass the request
1406 	 * down.
1407 	 */
1408 	if (so->so_downcalls->sd_recv_uio != NULL) {
1409 		error = (*so->so_downcalls->sd_recv_uio)
1410 		    (so->so_proto_handle, uiop, msg, cr);
1411 		SO_UNBLOCK_FALLBACK(so);
1412 		return (error);
1413 	}
1414 
1415 	/*
1416 	 * Reading data from the socket buffer
1417 	 */
1418 	flags = msg->msg_flags;
1419 	msg->msg_flags = 0;
1420 
1421 	/*
1422 	 * Set msg_controllen and msg_namelen to zero here to make it
1423 	 * simpler in the cases that no control or name is returned.
1424 	 */
1425 	controllen = msg->msg_controllen;
1426 	namelen = msg->msg_namelen;
1427 	msg->msg_controllen = 0;
1428 	msg->msg_namelen = 0;
1429 
1430 	mutex_enter(&so->so_lock);
1431 	/* Set SOREADLOCKED */
1432 	error = so_lock_read_intr(so,
1433 	    uiop->uio_fmode | ((flags & MSG_DONTWAIT) ? FNONBLOCK : 0));
1434 	mutex_exit(&so->so_lock);
1435 	if (error) {
1436 		SO_UNBLOCK_FALLBACK(so);
1437 		return (error);
1438 	}
1439 
1440 	suiop = sod_rcv_init(so, flags, &uiop);
1441 retry:
1442 	saved_resid = uiop->uio_resid;
1443 	error = so_dequeue_msg(so, &mctlp, uiop, &rval, flags);
1444 	if (error != 0) {
1445 		goto out;
1446 	}
1447 	/*
1448 	 * For datagrams the MOREDATA flag is used to set MSG_TRUNC.
1449 	 * For non-datagrams MOREDATA is used to set MSG_EOR.
1450 	 */
1451 	ASSERT(!(rval.r_val1 & MORECTL));
1452 	if ((rval.r_val1 & MOREDATA) && (so->so_mode & SM_ATOMIC))
1453 		msg->msg_flags |= MSG_TRUNC;
1454 	if (mctlp == NULL) {
1455 		dprintso(so, 1, ("so_recvmsg: got M_DATA\n"));
1456 
1457 		mutex_enter(&so->so_lock);
1458 		/* Set MSG_EOR based on MOREDATA */
1459 		if (!(rval.r_val1 & MOREDATA)) {
1460 			if (so->so_state & SS_SAVEDEOR) {
1461 				msg->msg_flags |= MSG_EOR;
1462 				so->so_state &= ~SS_SAVEDEOR;
1463 			}
1464 		}
1465 		/*
1466 		 * If some data was received (i.e. not EOF) and the
1467 		 * read/recv* has not been satisfied wait for some more.
1468 		 */
1469 		if ((flags & MSG_WAITALL) && !(msg->msg_flags & MSG_EOR) &&
1470 		    uiop->uio_resid != saved_resid && uiop->uio_resid > 0) {
1471 			mutex_exit(&so->so_lock);
1472 			goto retry;
1473 		}
1474 
1475 		goto out_locked;
1476 	}
1477 	/* strsock_proto has already verified length and alignment */
1478 	tpr = (union T_primitives *)mctlp->b_rptr;
1479 	dprintso(so, 1, ("so_recvmsg: type %d\n", tpr->type));
1480 	switch (tpr->type) {
1481 	case T_DATA_IND: {
1482 		/*
1483 		 * Set msg_flags to MSG_EOR based on
1484 		 * MORE_flag and MOREDATA.
1485 		 */
1486 		mutex_enter(&so->so_lock);
1487 		so->so_state &= ~SS_SAVEDEOR;
1488 		if (!(tpr->data_ind.MORE_flag & 1)) {
1489 			if (!(rval.r_val1 & MOREDATA))
1490 				msg->msg_flags |= MSG_EOR;
1491 			else
1492 				so->so_state |= SS_SAVEDEOR;
1493 		}
1494 		freemsg(mctlp);
1495 		/*
1496 		 * If some data was received (i.e. not EOF) and the
1497 		 * read/recv* has not been satisfied wait for some more.
1498 		 */
1499 		if ((flags & MSG_WAITALL) && !(msg->msg_flags & MSG_EOR) &&
1500 		    uiop->uio_resid != saved_resid && uiop->uio_resid > 0) {
1501 			mutex_exit(&so->so_lock);
1502 			goto retry;
1503 		}
1504 		goto out_locked;
1505 	}
1506 	case T_UNITDATA_IND: {
1507 		void *addr;
1508 		t_uscalar_t addrlen;
1509 		void *abuf;
1510 		t_uscalar_t optlen;
1511 		void *opt;
1512 
1513 		if (namelen != 0) {
1514 			/* Caller wants source address */
1515 			addrlen = tpr->unitdata_ind.SRC_length;
1516 			addr = sogetoff(mctlp, tpr->unitdata_ind.SRC_offset,
1517 			    addrlen, 1);
1518 			if (addr == NULL) {
1519 				freemsg(mctlp);
1520 				error = EPROTO;
1521 				eprintsoline(so, error);
1522 				goto out;
1523 			}
1524 			ASSERT(so->so_family != AF_UNIX);
1525 		}
1526 		optlen = tpr->unitdata_ind.OPT_length;
1527 		if (optlen != 0) {
1528 			t_uscalar_t ncontrollen;
1529 
1530 			/*
1531 			 * Extract any source address option.
1532 			 * Determine how large cmsg buffer is needed.
1533 			 */
1534 			opt = sogetoff(mctlp, tpr->unitdata_ind.OPT_offset,
1535 			    optlen, __TPI_ALIGN_SIZE);
1536 
1537 			if (opt == NULL) {
1538 				freemsg(mctlp);
1539 				error = EPROTO;
1540 				eprintsoline(so, error);
1541 				goto out;
1542 			}
1543 			if (so->so_family == AF_UNIX)
1544 				so_getopt_srcaddr(opt, optlen, &addr, &addrlen);
1545 			ncontrollen = so_cmsglen(mctlp, opt, optlen,
1546 			    !(flags & MSG_XPG4_2));
1547 			if (controllen != 0)
1548 				controllen = ncontrollen;
1549 			else if (ncontrollen != 0)
1550 				msg->msg_flags |= MSG_CTRUNC;
1551 		} else {
1552 			controllen = 0;
1553 		}
1554 
1555 		if (namelen != 0) {
1556 			/*
1557 			 * Return address to caller.
1558 			 * Caller handles truncation if length
1559 			 * exceeds msg_namelen.
1560 			 * NOTE: AF_UNIX NUL termination is ensured by
1561 			 * the sender's copyin_name().
1562 			 */
1563 			abuf = kmem_alloc(addrlen, KM_SLEEP);
1564 
1565 			bcopy(addr, abuf, addrlen);
1566 			msg->msg_name = abuf;
1567 			msg->msg_namelen = addrlen;
1568 		}
1569 
1570 		if (controllen != 0) {
1571 			/*
1572 			 * Return control msg to caller.
1573 			 * Caller handles truncation if length
1574 			 * exceeds msg_controllen.
1575 			 */
1576 			control = kmem_zalloc(controllen, KM_SLEEP);
1577 
1578 			error = so_opt2cmsg(mctlp, opt, optlen,
1579 			    !(flags & MSG_XPG4_2), control, controllen);
1580 			if (error) {
1581 				freemsg(mctlp);
1582 				if (msg->msg_namelen != 0)
1583 					kmem_free(msg->msg_name,
1584 					    msg->msg_namelen);
1585 				kmem_free(control, controllen);
1586 				eprintsoline(so, error);
1587 				goto out;
1588 			}
1589 			msg->msg_control = control;
1590 			msg->msg_controllen = controllen;
1591 		}
1592 
1593 		freemsg(mctlp);
1594 		goto out;
1595 	}
1596 	case T_OPTDATA_IND: {
1597 		struct T_optdata_req *tdr;
1598 		void *opt;
1599 		t_uscalar_t optlen;
1600 
1601 		tdr = (struct T_optdata_req *)mctlp->b_rptr;
1602 		optlen = tdr->OPT_length;
1603 		if (optlen != 0) {
1604 			t_uscalar_t ncontrollen;
1605 			/*
1606 			 * Determine how large cmsg buffer is needed.
1607 			 */
1608 			opt = sogetoff(mctlp,
1609 			    tpr->optdata_ind.OPT_offset, optlen,
1610 			    __TPI_ALIGN_SIZE);
1611 
1612 			if (opt == NULL) {
1613 				freemsg(mctlp);
1614 				error = EPROTO;
1615 				eprintsoline(so, error);
1616 				goto out;
1617 			}
1618 
1619 			ncontrollen = so_cmsglen(mctlp, opt, optlen,
1620 			    !(flags & MSG_XPG4_2));
1621 			if (controllen != 0)
1622 				controllen = ncontrollen;
1623 			else if (ncontrollen != 0)
1624 				msg->msg_flags |= MSG_CTRUNC;
1625 		} else {
1626 			controllen = 0;
1627 		}
1628 
1629 		if (controllen != 0) {
1630 			/*
1631 			 * Return control msg to caller.
1632 			 * Caller handles truncation if length
1633 			 * exceeds msg_controllen.
1634 			 */
1635 			control = kmem_zalloc(controllen, KM_SLEEP);
1636 
1637 			error = so_opt2cmsg(mctlp, opt, optlen,
1638 			    !(flags & MSG_XPG4_2), control, controllen);
1639 			if (error) {
1640 				freemsg(mctlp);
1641 				kmem_free(control, controllen);
1642 				eprintsoline(so, error);
1643 				goto out;
1644 			}
1645 			msg->msg_control = control;
1646 			msg->msg_controllen = controllen;
1647 		}
1648 
1649 		/*
1650 		 * Set msg_flags to MSG_EOR based on
1651 		 * DATA_flag and MOREDATA.
1652 		 */
1653 		mutex_enter(&so->so_lock);
1654 		so->so_state &= ~SS_SAVEDEOR;
1655 		if (!(tpr->data_ind.MORE_flag & 1)) {
1656 			if (!(rval.r_val1 & MOREDATA))
1657 				msg->msg_flags |= MSG_EOR;
1658 			else
1659 				so->so_state |= SS_SAVEDEOR;
1660 		}
1661 		freemsg(mctlp);
1662 		/*
1663 		 * If some data was received (i.e. not EOF) and the
1664 		 * read/recv* has not been satisfied wait for some more.
1665 		 * Not possible to wait if control info was received.
1666 		 */
1667 		if ((flags & MSG_WAITALL) && !(msg->msg_flags & MSG_EOR) &&
1668 		    controllen == 0 &&
1669 		    uiop->uio_resid != saved_resid && uiop->uio_resid > 0) {
1670 			mutex_exit(&so->so_lock);
1671 			goto retry;
1672 		}
1673 		goto out_locked;
1674 	}
1675 	default:
1676 		cmn_err(CE_CONT, "so_recvmsg bad type %x \n",
1677 		    tpr->type);
1678 		freemsg(mctlp);
1679 		error = EPROTO;
1680 		ASSERT(0);
1681 	}
1682 out:
1683 	mutex_enter(&so->so_lock);
1684 out_locked:
1685 	/* The sod_lockp pointers to the sonode so_lock */
1686 	ret = sod_rcv_done(so, suiop, uiop);
1687 	if (ret != 0 && error == 0)
1688 		error = ret;
1689 
1690 	so_unlock_read(so);	/* Clear SOREADLOCKED */
1691 	mutex_exit(&so->so_lock);
1692 
1693 	SO_UNBLOCK_FALLBACK(so);
1694 
1695 	return (error);
1696 }
1697 
1698 sonodeops_t so_sonodeops = {
1699 	so_init,		/* sop_init	*/
1700 	so_accept,		/* sop_accept   */
1701 	so_bind,		/* sop_bind	*/
1702 	so_listen,		/* sop_listen   */
1703 	so_connect,		/* sop_connect  */
1704 	so_recvmsg,		/* sop_recvmsg  */
1705 	so_sendmsg,		/* sop_sendmsg  */
1706 	so_sendmblk,		/* sop_sendmblk */
1707 	so_getpeername,		/* sop_getpeername */
1708 	so_getsockname,		/* sop_getsockname */
1709 	so_shutdown,		/* sop_shutdown */
1710 	so_getsockopt,		/* sop_getsockopt */
1711 	so_setsockopt,		/* sop_setsockopt */
1712 	so_ioctl,		/* sop_ioctl    */
1713 	so_poll,		/* sop_poll	*/
1714 	so_close,		/* sop_close */
1715 };
1716 
1717 sock_upcalls_t so_upcalls = {
1718 	so_newconn,
1719 	so_connected,
1720 	so_disconnected,
1721 	so_opctl,
1722 	so_queue_msg,
1723 	so_set_prop,
1724 	so_txq_full,
1725 	so_signal_oob,
1726 	so_zcopy_notify,
1727 	so_set_error
1728 };
1729