xref: /titanic_52/usr/src/uts/common/fs/sockfs/sockcommon_subr.c (revision 2320a8c1cca132d99d97f0f0025b5b4b4bfaf5b6)
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 #include <sys/types.h>
28 #include <sys/param.h>
29 #include <sys/signal.h>
30 #include <sys/cmn_err.h>
31 
32 #include <sys/stropts.h>
33 #include <sys/socket.h>
34 #include <sys/socketvar.h>
35 #include <sys/sockio.h>
36 #include <sys/strsubr.h>
37 #include <sys/strsun.h>
38 #include <sys/atomic.h>
39 #include <sys/tihdr.h>
40 
41 #include <fs/sockfs/sockcommon.h>
42 #include <fs/sockfs/socktpi.h>
43 #include <fs/sockfs/sodirect.h>
44 #include <sys/ddi.h>
45 #include <inet/ip.h>
46 #include <sys/time.h>
47 #include <sys/cmn_err.h>
48 
49 #ifdef SOCK_TEST
50 extern int do_useracc;
51 extern clock_t sock_test_timelimit;
52 #endif /* SOCK_TEST */
53 
54 #define	MBLK_PULL_LEN 64
55 uint32_t so_mblk_pull_len = MBLK_PULL_LEN;
56 
57 #ifdef DEBUG
58 boolean_t so_debug_length = B_FALSE;
59 static boolean_t so_check_length(sonode_t *so);
60 #endif
61 
62 int
63 so_acceptq_enqueue_locked(struct sonode *so, struct sonode *nso)
64 {
65 	ASSERT(MUTEX_HELD(&so->so_acceptq_lock));
66 	ASSERT(nso->so_acceptq_next == NULL);
67 
68 	*so->so_acceptq_tail = nso;
69 	so->so_acceptq_tail = &nso->so_acceptq_next;
70 	so->so_acceptq_len++;
71 
72 	/* Wakeup a single consumer */
73 	cv_signal(&so->so_acceptq_cv);
74 
75 	return (so->so_acceptq_len);
76 }
77 
78 /*
79  * int so_acceptq_enqueue(struct sonode *so, struct sonode *nso)
80  *
81  * Enqueue an incoming connection on a listening socket.
82  *
83  * Arguments:
84  *   so	  - listening socket
85  *   nso  - new connection
86  *
87  * Returns:
88  *   Number of queued connections, including the new connection
89  */
90 int
91 so_acceptq_enqueue(struct sonode *so, struct sonode *nso)
92 {
93 	int conns;
94 
95 	mutex_enter(&so->so_acceptq_lock);
96 	conns = so_acceptq_enqueue_locked(so, nso);
97 	mutex_exit(&so->so_acceptq_lock);
98 
99 	return (conns);
100 }
101 
102 static int
103 so_acceptq_dequeue_locked(struct sonode *so, boolean_t dontblock,
104     struct sonode **nsop)
105 {
106 	struct sonode *nso = NULL;
107 
108 	*nsop = NULL;
109 	ASSERT(MUTEX_HELD(&so->so_acceptq_lock));
110 	while ((nso = so->so_acceptq_head) == NULL) {
111 		/*
112 		 * No need to check so_error here, because it is not
113 		 * possible for a listening socket to be reset or otherwise
114 		 * disconnected.
115 		 *
116 		 * So now we just need check if it's ok to wait.
117 		 */
118 		if (dontblock)
119 			return (EWOULDBLOCK);
120 		if (so->so_state & (SS_CLOSING | SS_FALLBACK_PENDING))
121 			return (EINTR);
122 
123 		if (cv_wait_sig_swap(&so->so_acceptq_cv,
124 		    &so->so_acceptq_lock) == 0)
125 			return (EINTR);
126 	}
127 
128 	ASSERT(nso != NULL);
129 	so->so_acceptq_head = nso->so_acceptq_next;
130 	nso->so_acceptq_next = NULL;
131 
132 	if (so->so_acceptq_head == NULL) {
133 		ASSERT(so->so_acceptq_tail == &nso->so_acceptq_next);
134 		so->so_acceptq_tail = &so->so_acceptq_head;
135 	}
136 	ASSERT(so->so_acceptq_len > 0);
137 	--so->so_acceptq_len;
138 
139 	*nsop = nso;
140 
141 	return (0);
142 }
143 
144 /*
145  * int so_acceptq_dequeue(struct sonode *, boolean_t, struct sonode **)
146  *
147  * Pulls a connection off of the accept queue.
148  *
149  * Arguments:
150  *   so	       - listening socket
151  *   dontblock - indicate whether it's ok to sleep if there are no
152  *		 connections on the queue
153  *   nsop      - Value-return argument
154  *
155  * Return values:
156  *   0 when a connection is successfully dequeued, in which case nsop
157  *   is set to point to the new connection. Upon failure a non-zero
158  *   value is returned, and the value of nsop is set to NULL.
159  *
160  * Note:
161  *   so_acceptq_dequeue() may return prematurly if the socket is falling
162  *   back to TPI.
163  */
164 int
165 so_acceptq_dequeue(struct sonode *so, boolean_t dontblock,
166     struct sonode **nsop)
167 {
168 	int error;
169 
170 	mutex_enter(&so->so_acceptq_lock);
171 	error = so_acceptq_dequeue_locked(so, dontblock, nsop);
172 	mutex_exit(&so->so_acceptq_lock);
173 
174 	return (error);
175 }
176 
177 /*
178  * void so_acceptq_flush(struct sonode *so, boolean_t doclose)
179  *
180  * Removes all pending connections from a listening socket, and
181  * frees the associated resources.
182  *
183  * Arguments
184  *   so	     - listening socket
185  *   doclose - make a close downcall for each socket on the accept queue
186  *             (Note, only SCTP and SDP sockets rely on this)
187  *
188  * Return values:
189  *   None.
190  *
191  * Note:
192  *   The caller has to ensure that no calls to so_acceptq_enqueue() or
193  *   so_acceptq_dequeue() occur while the accept queue is being flushed.
194  *   So either the socket needs to be in a state where no operations
195  *   would come in, or so_lock needs to be obtained.
196  */
197 void
198 so_acceptq_flush(struct sonode *so, boolean_t doclose)
199 {
200 	struct sonode *nso;
201 
202 	while ((nso = so->so_acceptq_head) != NULL) {
203 		so->so_acceptq_head = nso->so_acceptq_next;
204 		nso->so_acceptq_next = NULL;
205 
206 		if (doclose) {
207 			socket_close(nso, 0, CRED());
208 		} else {
209 			/*
210 			 * Since the socket is on the accept queue, there can
211 			 * only be one reference. We drop the reference and
212 			 * just blow off the socket.
213 			 */
214 			ASSERT(nso->so_count == 1);
215 			nso->so_count--;
216 		}
217 		socket_destroy(nso);
218 	}
219 
220 	so->so_acceptq_head = NULL;
221 	so->so_acceptq_tail = &so->so_acceptq_head;
222 	so->so_acceptq_len = 0;
223 }
224 
225 int
226 so_wait_connected_locked(struct sonode *so, boolean_t nonblock,
227     sock_connid_t id)
228 {
229 	ASSERT(MUTEX_HELD(&so->so_lock));
230 
231 	/*
232 	 * The protocol has notified us that a connection attempt is being
233 	 * made, so before we wait for a notification to arrive we must
234 	 * clear out any errors associated with earlier connection attempts.
235 	 */
236 	if (so->so_error != 0 && SOCK_CONNID_LT(so->so_proto_connid, id))
237 		so->so_error = 0;
238 
239 	while (SOCK_CONNID_LT(so->so_proto_connid, id)) {
240 		if (nonblock)
241 			return (EINPROGRESS);
242 
243 		if (so->so_state & (SS_CLOSING | SS_FALLBACK_PENDING))
244 			return (EINTR);
245 
246 		if (cv_wait_sig_swap(&so->so_state_cv, &so->so_lock) == 0)
247 			return (EINTR);
248 	}
249 
250 	if (so->so_error != 0)
251 		return (sogeterr(so, B_TRUE));
252 	/*
253 	 * Under normal circumstances, so_error should contain an error
254 	 * in case the connect failed. However, it is possible for another
255 	 * thread to come in a consume the error, so generate a sensible
256 	 * error in that case.
257 	 */
258 	if ((so->so_state & SS_ISCONNECTED) == 0)
259 		return (ECONNREFUSED);
260 
261 	return (0);
262 }
263 
264 /*
265  * int so_wait_connected(struct sonode *so, boolean_t nonblock,
266  *    sock_connid_t id)
267  *
268  * Wait until the socket is connected or an error has occured.
269  *
270  * Arguments:
271  *   so	      - socket
272  *   nonblock - indicate whether it's ok to sleep if the connection has
273  *		not yet been established
274  *   gen      - generation number that was returned by the protocol
275  *		when the operation was started
276  *
277  * Returns:
278  *   0 if the connection attempt was successful, or an error indicating why
279  *   the connection attempt failed.
280  */
281 int
282 so_wait_connected(struct sonode *so, boolean_t nonblock, sock_connid_t id)
283 {
284 	int error;
285 
286 	mutex_enter(&so->so_lock);
287 	error = so_wait_connected_locked(so, nonblock, id);
288 	mutex_exit(&so->so_lock);
289 
290 	return (error);
291 }
292 
293 int
294 so_snd_wait_qnotfull_locked(struct sonode *so, boolean_t dontblock)
295 {
296 	int error;
297 
298 	ASSERT(MUTEX_HELD(&so->so_lock));
299 	while (so->so_snd_qfull) {
300 		if (so->so_state & SS_CANTSENDMORE)
301 			return (EPIPE);
302 		if (dontblock)
303 			return (EWOULDBLOCK);
304 
305 		if (so->so_state & (SS_CLOSING | SS_FALLBACK_PENDING))
306 			return (EINTR);
307 
308 		if (so->so_sndtimeo == 0) {
309 			/*
310 			 * Zero means disable timeout.
311 			 */
312 			error = cv_wait_sig(&so->so_snd_cv, &so->so_lock);
313 		} else {
314 			clock_t now;
315 
316 			time_to_wait(&now, so->so_sndtimeo);
317 			error = cv_timedwait_sig(&so->so_snd_cv, &so->so_lock,
318 			    now);
319 		}
320 		if (error == 0)
321 			return (EINTR);
322 		else if (error == -1)
323 			return (EAGAIN);
324 	}
325 	return (0);
326 }
327 
328 /*
329  * int so_wait_sendbuf(struct sonode *so, boolean_t dontblock)
330  *
331  * Wait for the transport to notify us about send buffers becoming
332  * available.
333  */
334 int
335 so_snd_wait_qnotfull(struct sonode *so, boolean_t dontblock)
336 {
337 	int error = 0;
338 
339 	mutex_enter(&so->so_lock);
340 	if (so->so_snd_qfull) {
341 		so->so_snd_wakeup = B_TRUE;
342 		error = so_snd_wait_qnotfull_locked(so, dontblock);
343 		so->so_snd_wakeup = B_FALSE;
344 	}
345 	mutex_exit(&so->so_lock);
346 
347 	return (error);
348 }
349 
350 void
351 so_snd_qfull(struct sonode *so)
352 {
353 	mutex_enter(&so->so_lock);
354 	so->so_snd_qfull = B_TRUE;
355 	mutex_exit(&so->so_lock);
356 }
357 
358 void
359 so_snd_qnotfull(struct sonode *so)
360 {
361 	mutex_enter(&so->so_lock);
362 	so->so_snd_qfull = B_FALSE;
363 	/* wake up everyone waiting for buffers */
364 	cv_broadcast(&so->so_snd_cv);
365 	mutex_exit(&so->so_lock);
366 }
367 
368 /*
369  * Change the process/process group to which SIGIO is sent.
370  */
371 int
372 socket_chgpgrp(struct sonode *so, pid_t pid)
373 {
374 	int error;
375 
376 	ASSERT(MUTEX_HELD(&so->so_lock));
377 	if (pid != 0) {
378 		/*
379 		 * Permissions check by sending signal 0.
380 		 * Note that when kill fails it does a
381 		 * set_errno causing the system call to fail.
382 		 */
383 		error = kill(pid, 0);
384 		if (error != 0) {
385 			return (error);
386 		}
387 	}
388 	so->so_pgrp = pid;
389 	return (0);
390 }
391 
392 
393 /*
394  * Generate a SIGIO, for 'writable' events include siginfo structure,
395  * for read events just send the signal.
396  */
397 /*ARGSUSED*/
398 static void
399 socket_sigproc(proc_t *proc, int event)
400 {
401 	k_siginfo_t info;
402 
403 	ASSERT(event & (SOCKETSIG_WRITE | SOCKETSIG_READ | SOCKETSIG_URG));
404 
405 	if (event & SOCKETSIG_WRITE) {
406 		info.si_signo = SIGPOLL;
407 		info.si_code = POLL_OUT;
408 		info.si_errno = 0;
409 		info.si_fd = 0;
410 		info.si_band = 0;
411 		sigaddq(proc, NULL, &info, KM_NOSLEEP);
412 	}
413 	if (event & SOCKETSIG_READ) {
414 		sigtoproc(proc, NULL, SIGPOLL);
415 	}
416 	if (event & SOCKETSIG_URG) {
417 		sigtoproc(proc, NULL, SIGURG);
418 	}
419 }
420 
421 void
422 socket_sendsig(struct sonode *so, int event)
423 {
424 	proc_t *proc;
425 
426 	ASSERT(MUTEX_HELD(&so->so_lock));
427 
428 	if (so->so_pgrp == 0 || (!(so->so_state & SS_ASYNC) &&
429 	    event != SOCKETSIG_URG)) {
430 		return;
431 	}
432 
433 	dprint(3, ("sending sig %d to %d\n", event, so->so_pgrp));
434 
435 	if (so->so_pgrp > 0) {
436 		/*
437 		 * XXX This unfortunately still generates
438 		 * a signal when a fd is closed but
439 		 * the proc is active.
440 		 */
441 		mutex_enter(&pidlock);
442 		proc = prfind(so->so_pgrp);
443 		if (proc == NULL) {
444 			mutex_exit(&pidlock);
445 			return;
446 		}
447 		mutex_enter(&proc->p_lock);
448 		mutex_exit(&pidlock);
449 		socket_sigproc(proc, event);
450 		mutex_exit(&proc->p_lock);
451 	} else {
452 		/*
453 		 * Send to process group. Hold pidlock across
454 		 * calls to socket_sigproc().
455 		 */
456 		pid_t pgrp = -so->so_pgrp;
457 
458 		mutex_enter(&pidlock);
459 		proc = pgfind(pgrp);
460 		while (proc != NULL) {
461 			mutex_enter(&proc->p_lock);
462 			socket_sigproc(proc, event);
463 			mutex_exit(&proc->p_lock);
464 			proc = proc->p_pglink;
465 		}
466 		mutex_exit(&pidlock);
467 	}
468 }
469 
470 #define	MIN(a, b) ((a) < (b) ? (a) : (b))
471 /* Copy userdata into a new mblk_t */
472 mblk_t *
473 socopyinuio(uio_t *uiop, ssize_t iosize, size_t wroff, ssize_t maxblk,
474     size_t tail_len, int *errorp, cred_t *cr)
475 {
476 	mblk_t	*head = NULL, **tail = &head;
477 
478 	ASSERT(iosize == INFPSZ || iosize > 0);
479 
480 	if (iosize == INFPSZ || iosize > uiop->uio_resid)
481 		iosize = uiop->uio_resid;
482 
483 	if (maxblk == INFPSZ)
484 		maxblk = iosize;
485 
486 	/* Nothing to do in these cases, so we're done */
487 	if (iosize < 0 || maxblk < 0 || (maxblk == 0 && iosize > 0))
488 		goto done;
489 
490 	/*
491 	 * We will enter the loop below if iosize is 0; it will allocate an
492 	 * empty message block and call uiomove(9F) which will just return.
493 	 * We could avoid that with an extra check but would only slow
494 	 * down the much more likely case where iosize is larger than 0.
495 	 */
496 	do {
497 		ssize_t blocksize;
498 		mblk_t	*mp;
499 
500 		blocksize = MIN(iosize, maxblk);
501 		ASSERT(blocksize >= 0);
502 		if (is_system_labeled())
503 			mp = allocb_cred(wroff + blocksize + tail_len,
504 			    cr, curproc->p_pid);
505 		else
506 			mp = allocb(wroff + blocksize + tail_len, BPRI_MED);
507 		if (mp == NULL) {
508 			*errorp = ENOMEM;
509 			return (head);
510 		}
511 		mp->b_rptr += wroff;
512 		mp->b_wptr = mp->b_rptr + blocksize;
513 
514 		*tail = mp;
515 		tail = &mp->b_cont;
516 
517 		/* uiomove(9F) either returns 0 or EFAULT */
518 		if ((*errorp = uiomove(mp->b_rptr, (size_t)blocksize,
519 		    UIO_WRITE, uiop)) != 0) {
520 			ASSERT(*errorp != ENOMEM);
521 			freemsg(head);
522 			return (NULL);
523 		}
524 
525 		iosize -= blocksize;
526 	} while (iosize > 0);
527 
528 done:
529 	*errorp = 0;
530 	return (head);
531 }
532 
533 mblk_t *
534 socopyoutuio(mblk_t *mp, struct uio *uiop, ssize_t max_read, int *errorp)
535 {
536 	int error;
537 	ptrdiff_t n;
538 	mblk_t *nmp;
539 
540 	ASSERT(mp->b_wptr >= mp->b_rptr);
541 
542 	/*
543 	 * max_read is the offset of the oobmark and read can not go pass
544 	 * the oobmark.
545 	 */
546 	if (max_read == INFPSZ || max_read > uiop->uio_resid)
547 		max_read = uiop->uio_resid;
548 
549 	do {
550 		if ((n = MIN(max_read, MBLKL(mp))) != 0) {
551 			ASSERT(n > 0);
552 
553 			error = uiomove(mp->b_rptr, n, UIO_READ, uiop);
554 			if (error != 0) {
555 				freemsg(mp);
556 				*errorp = error;
557 				return (NULL);
558 			}
559 		}
560 
561 		mp->b_rptr += n;
562 		max_read -= n;
563 		while (mp != NULL && (mp->b_rptr >= mp->b_wptr)) {
564 			/*
565 			 * get rid of zero length mblks
566 			 */
567 			nmp = mp;
568 			mp = mp->b_cont;
569 			freeb(nmp);
570 		}
571 	} while (mp != NULL && max_read > 0);
572 
573 	*errorp = 0;
574 	return (mp);
575 }
576 
577 static void
578 so_prepend_msg(struct sonode *so, mblk_t *mp, mblk_t *last_tail)
579 {
580 	ASSERT(last_tail != NULL);
581 	mp->b_next = so->so_rcv_q_head;
582 	mp->b_prev = last_tail;
583 	ASSERT(!(DB_FLAGS(mp) & DBLK_UIOA));
584 
585 	if (so->so_rcv_q_head == NULL) {
586 		ASSERT(so->so_rcv_q_last_head == NULL);
587 		so->so_rcv_q_last_head = mp;
588 #ifdef DEBUG
589 	} else {
590 		ASSERT(!(DB_FLAGS(so->so_rcv_q_head) & DBLK_UIOA));
591 #endif
592 	}
593 	so->so_rcv_q_head = mp;
594 
595 #ifdef DEBUG
596 	if (so_debug_length) {
597 		mutex_enter(&so->so_lock);
598 		ASSERT(so_check_length(so));
599 		mutex_exit(&so->so_lock);
600 	}
601 #endif
602 }
603 
604 /*
605  * Move a mblk chain (mp_head, mp_last_head) to the sonode's rcv queue so it
606  * can be processed by so_dequeue_msg().
607  */
608 void
609 so_process_new_message(struct sonode *so, mblk_t *mp_head, mblk_t *mp_last_head)
610 {
611 	ASSERT(mp_head->b_prev != NULL);
612 	if (so->so_rcv_q_head  == NULL) {
613 		so->so_rcv_q_head = mp_head;
614 		so->so_rcv_q_last_head = mp_last_head;
615 		ASSERT(so->so_rcv_q_last_head->b_prev != NULL);
616 	} else {
617 		boolean_t flag_equal = ((DB_FLAGS(mp_head) & DBLK_UIOA) ==
618 		    (DB_FLAGS(so->so_rcv_q_last_head) & DBLK_UIOA));
619 
620 		if (mp_head->b_next == NULL &&
621 		    DB_TYPE(mp_head) == M_DATA &&
622 		    DB_TYPE(so->so_rcv_q_last_head) == M_DATA && flag_equal) {
623 			so->so_rcv_q_last_head->b_prev->b_cont = mp_head;
624 			so->so_rcv_q_last_head->b_prev = mp_head->b_prev;
625 			mp_head->b_prev = NULL;
626 		} else if (flag_equal && (DB_FLAGS(mp_head) & DBLK_UIOA)) {
627 			/*
628 			 * Append to last_head if more than one mblks, and both
629 			 * mp_head and last_head are I/OAT mblks.
630 			 */
631 			ASSERT(mp_head->b_next != NULL);
632 			so->so_rcv_q_last_head->b_prev->b_cont = mp_head;
633 			so->so_rcv_q_last_head->b_prev = mp_head->b_prev;
634 			mp_head->b_prev = NULL;
635 
636 			so->so_rcv_q_last_head->b_next = mp_head->b_next;
637 			mp_head->b_next = NULL;
638 			so->so_rcv_q_last_head = mp_last_head;
639 		} else {
640 #ifdef DEBUG
641 			{
642 				mblk_t *tmp_mblk;
643 				tmp_mblk = mp_head;
644 				while (tmp_mblk != NULL) {
645 					ASSERT(tmp_mblk->b_prev != NULL);
646 					tmp_mblk = tmp_mblk->b_next;
647 				}
648 			}
649 #endif
650 			so->so_rcv_q_last_head->b_next = mp_head;
651 			so->so_rcv_q_last_head = mp_last_head;
652 		}
653 	}
654 }
655 
656 /*
657  * Check flow control on a given sonode.  Must have so_lock held, and
658  * this function will release the hold.
659  */
660 
661 static void
662 so_check_flow_control(struct sonode *so)
663 {
664 	ASSERT(MUTEX_HELD(&so->so_lock));
665 
666 	if (so->so_flowctrld && so->so_rcv_queued < so->so_rcvlowat) {
667 		so->so_flowctrld = B_FALSE;
668 		mutex_exit(&so->so_lock);
669 		/*
670 		 * Open up flow control. SCTP does not have any downcalls, and
671 		 * it will clr flow ctrl in sosctp_recvmsg().
672 		 */
673 		if (so->so_downcalls != NULL &&
674 		    so->so_downcalls->sd_clr_flowctrl != NULL) {
675 			(*so->so_downcalls->sd_clr_flowctrl)
676 			    (so->so_proto_handle);
677 		}
678 	} else {
679 		mutex_exit(&so->so_lock);
680 	}
681 }
682 
683 int
684 so_dequeue_msg(struct sonode *so, mblk_t **mctlp, struct uio *uiop,
685     rval_t *rvalp, int flags)
686 {
687 	mblk_t	*mp, *nmp;
688 	mblk_t	*savemp, *savemptail;
689 	mblk_t	*new_msg_head;
690 	mblk_t	*new_msg_last_head;
691 	mblk_t	*last_tail;
692 	boolean_t partial_read;
693 	boolean_t reset_atmark = B_FALSE;
694 	int more = 0;
695 	int error;
696 	ssize_t oobmark;
697 	sodirect_t *sodp = so->so_direct;
698 
699 	partial_read = B_FALSE;
700 	*mctlp = NULL;
701 again:
702 	mutex_enter(&so->so_lock);
703 again1:
704 #ifdef DEBUG
705 	if (so_debug_length) {
706 		ASSERT(so_check_length(so));
707 	}
708 #endif
709 	if (so->so_state & SS_RCVATMARK) {
710 		/* Check whether the caller is OK to read past the mark */
711 		if (flags & MSG_NOMARK) {
712 			mutex_exit(&so->so_lock);
713 			return (EWOULDBLOCK);
714 		}
715 		reset_atmark = B_TRUE;
716 	}
717 	/*
718 	 * First move messages from the dump area to processing area
719 	 */
720 	if (sodp != NULL) {
721 		if (sodp->sod_enabled) {
722 			if (sodp->sod_uioa.uioa_state & UIOA_ALLOC) {
723 				/* nothing to uioamove */
724 				sodp = NULL;
725 			} else if (sodp->sod_uioa.uioa_state & UIOA_INIT) {
726 				sodp->sod_uioa.uioa_state &= UIOA_CLR;
727 				sodp->sod_uioa.uioa_state |= UIOA_ENABLED;
728 				/*
729 				 * try to uioamove() the data that
730 				 * has already queued.
731 				 */
732 				sod_uioa_so_init(so, sodp, uiop);
733 			}
734 		} else {
735 			sodp = NULL;
736 		}
737 	}
738 	new_msg_head = so->so_rcv_head;
739 	new_msg_last_head = so->so_rcv_last_head;
740 	so->so_rcv_head = NULL;
741 	so->so_rcv_last_head = NULL;
742 	oobmark = so->so_oobmark;
743 	/*
744 	 * We can release the lock as there can only be one reader
745 	 */
746 	mutex_exit(&so->so_lock);
747 
748 	if (new_msg_head != NULL) {
749 		so_process_new_message(so, new_msg_head, new_msg_last_head);
750 	}
751 	savemp = savemptail = NULL;
752 	rvalp->r_val1 = 0;
753 	error = 0;
754 	mp = so->so_rcv_q_head;
755 
756 	if (mp != NULL &&
757 	    (so->so_rcv_timer_tid == 0 ||
758 	    so->so_rcv_queued >= so->so_rcv_thresh)) {
759 		partial_read = B_FALSE;
760 
761 		if (flags & MSG_PEEK) {
762 			if ((nmp = dupmsg(mp)) == NULL &&
763 			    (nmp = copymsg(mp)) == NULL) {
764 				size_t size = msgsize(mp);
765 
766 				error = strwaitbuf(size, BPRI_HI);
767 				if (error) {
768 					return (error);
769 				}
770 				goto again;
771 			}
772 			mp = nmp;
773 		} else {
774 			ASSERT(mp->b_prev != NULL);
775 			last_tail = mp->b_prev;
776 			mp->b_prev = NULL;
777 			so->so_rcv_q_head = mp->b_next;
778 			if (so->so_rcv_q_head == NULL) {
779 				so->so_rcv_q_last_head = NULL;
780 			}
781 			mp->b_next = NULL;
782 		}
783 
784 		ASSERT(mctlp != NULL);
785 		/*
786 		 * First process PROTO or PCPROTO blocks, if any.
787 		 */
788 		if (DB_TYPE(mp) != M_DATA) {
789 			*mctlp = mp;
790 			savemp = mp;
791 			savemptail = mp;
792 			ASSERT(DB_TYPE(mp) == M_PROTO ||
793 			    DB_TYPE(mp) == M_PCPROTO);
794 			while (mp->b_cont != NULL &&
795 			    DB_TYPE(mp->b_cont) != M_DATA) {
796 				ASSERT(DB_TYPE(mp->b_cont) == M_PROTO ||
797 				    DB_TYPE(mp->b_cont) == M_PCPROTO);
798 				mp = mp->b_cont;
799 				savemptail = mp;
800 			}
801 			mp = savemptail->b_cont;
802 			savemptail->b_cont = NULL;
803 		}
804 
805 		ASSERT(DB_TYPE(mp) == M_DATA);
806 		/*
807 		 * Now process DATA blocks, if any. Note that for sodirect
808 		 * enabled socket, uio_resid can be 0.
809 		 */
810 		if (uiop->uio_resid >= 0) {
811 			ssize_t copied = 0;
812 
813 			if (sodp != NULL && (DB_FLAGS(mp) & DBLK_UIOA)) {
814 				mutex_enter(&so->so_lock);
815 				ASSERT(uiop == (uio_t *)&sodp->sod_uioa);
816 				copied = sod_uioa_mblk(so, mp);
817 				if (copied > 0)
818 					partial_read = B_TRUE;
819 				mutex_exit(&so->so_lock);
820 				/* mark this mblk as processed */
821 				mp = NULL;
822 			} else {
823 				ssize_t oldresid = uiop->uio_resid;
824 
825 				if (MBLKL(mp) < so_mblk_pull_len) {
826 					if (pullupmsg(mp, -1) == 1) {
827 						last_tail = mp;
828 					}
829 				}
830 				/*
831 				 * Can not read beyond the oobmark
832 				 */
833 				mp = socopyoutuio(mp, uiop,
834 				    oobmark == 0 ? INFPSZ : oobmark, &error);
835 				if (error != 0) {
836 					freemsg(*mctlp);
837 					*mctlp = NULL;
838 					more = 0;
839 					goto done;
840 				}
841 				ASSERT(oldresid >= uiop->uio_resid);
842 				copied = oldresid - uiop->uio_resid;
843 				if (oldresid > uiop->uio_resid)
844 					partial_read = B_TRUE;
845 			}
846 			ASSERT(copied >= 0);
847 			if (copied > 0 && !(flags & MSG_PEEK)) {
848 				mutex_enter(&so->so_lock);
849 				so->so_rcv_queued -= copied;
850 				ASSERT(so->so_oobmark >= 0);
851 				if (so->so_oobmark > 0) {
852 					so->so_oobmark -= copied;
853 					ASSERT(so->so_oobmark >= 0);
854 					if (so->so_oobmark == 0) {
855 						ASSERT(so->so_state &
856 						    SS_OOBPEND);
857 						so->so_oobmark = 0;
858 						so->so_state |= SS_RCVATMARK;
859 					}
860 				}
861 				/*
862 				 * so_check_flow_control() will drop
863 				 * so->so_lock.
864 				 */
865 				so_check_flow_control(so);
866 			}
867 		}
868 		if (mp != NULL) { /* more data blocks in msg */
869 			more |= MOREDATA;
870 			if ((flags & (MSG_PEEK|MSG_TRUNC))) {
871 				if (flags & MSG_PEEK) {
872 					freemsg(mp);
873 				} else {
874 					unsigned int msize = msgdsize(mp);
875 
876 					freemsg(mp);
877 					mutex_enter(&so->so_lock);
878 					so->so_rcv_queued -= msize;
879 					/*
880 					 * so_check_flow_control() will drop
881 					 * so->so_lock.
882 					 */
883 					so_check_flow_control(so);
884 				}
885 			} else if (partial_read && !somsghasdata(mp)) {
886 				/*
887 				 * Avoid queuing a zero-length tail part of
888 				 * a message. partial_read == 1 indicates that
889 				 * we read some of the message.
890 				 */
891 				freemsg(mp);
892 				more &= ~MOREDATA;
893 			} else {
894 				if (savemp != NULL &&
895 				    (flags & MSG_DUPCTRL)) {
896 					mblk_t *nmp;
897 					/*
898 					 * There should only be non data mblks
899 					 */
900 					ASSERT(DB_TYPE(savemp) != M_DATA &&
901 					    DB_TYPE(savemptail) != M_DATA);
902 try_again:
903 					if ((nmp = dupmsg(savemp)) == NULL &&
904 					    (nmp = copymsg(savemp)) == NULL) {
905 
906 						size_t size = msgsize(savemp);
907 
908 						error = strwaitbuf(size,
909 						    BPRI_HI);
910 						if (error != 0) {
911 							/*
912 							 * In case we
913 							 * cannot copy
914 							 * control data
915 							 * free the remaining
916 							 * data.
917 							 */
918 							freemsg(mp);
919 							goto done;
920 						}
921 						goto try_again;
922 					}
923 
924 					ASSERT(nmp != NULL);
925 					ASSERT(DB_TYPE(nmp) != M_DATA);
926 					savemptail->b_cont = mp;
927 					*mctlp = nmp;
928 					mp = savemp;
929 				}
930 				/*
931 				 * putback mp
932 				 */
933 				so_prepend_msg(so, mp, last_tail);
934 			}
935 		}
936 
937 		/* fast check so_rcv_head if there is more data */
938 		if (partial_read && !(so->so_state & SS_RCVATMARK) &&
939 		    *mctlp == NULL && uiop->uio_resid > 0 &&
940 		    !(flags & MSG_PEEK) && so->so_rcv_head != NULL) {
941 			goto again;
942 		}
943 	} else if (!partial_read) {
944 		mutex_enter(&so->so_lock);
945 		if (so->so_error != 0) {
946 			error = sogeterr(so, !(flags & MSG_PEEK));
947 			mutex_exit(&so->so_lock);
948 			return (error);
949 		}
950 		/*
951 		 * No pending data. Return right away for nonblocking
952 		 * socket, otherwise sleep waiting for data.
953 		 */
954 		if (!(so->so_state & SS_CANTRCVMORE) && uiop->uio_resid > 0) {
955 			if ((uiop->uio_fmode & (FNDELAY|FNONBLOCK)) ||
956 			    (flags & MSG_DONTWAIT)) {
957 				error = EWOULDBLOCK;
958 			} else {
959 				if (so->so_state & (SS_CLOSING |
960 				    SS_FALLBACK_PENDING)) {
961 					mutex_exit(&so->so_lock);
962 					error = EINTR;
963 					goto done;
964 				}
965 
966 				if (so->so_rcv_head != NULL) {
967 					goto again1;
968 				}
969 				so->so_rcv_wakeup = B_TRUE;
970 				so->so_rcv_wanted = uiop->uio_resid;
971 				if (so->so_rcvtimeo == 0) {
972 					/*
973 					 * Zero means disable timeout.
974 					 */
975 					error = cv_wait_sig(&so->so_rcv_cv,
976 					    &so->so_lock);
977 				} else {
978 					clock_t now;
979 					time_to_wait(&now, so->so_rcvtimeo);
980 					error = cv_timedwait_sig(&so->so_rcv_cv,
981 					    &so->so_lock, now);
982 				}
983 				so->so_rcv_wakeup = B_FALSE;
984 				so->so_rcv_wanted = 0;
985 
986 				if (error == 0) {
987 					error = EINTR;
988 				} else if (error == -1) {
989 					error = EAGAIN;
990 				} else {
991 					goto again1;
992 				}
993 			}
994 		}
995 		mutex_exit(&so->so_lock);
996 	}
997 	if (reset_atmark && partial_read && !(flags & MSG_PEEK)) {
998 		/*
999 		 * We are passed the mark, update state
1000 		 * 4.3BSD and 4.4BSD clears the mark when peeking across it.
1001 		 * The draft Posix socket spec states that the mark should
1002 		 * not be cleared when peeking. We follow the latter.
1003 		 */
1004 		mutex_enter(&so->so_lock);
1005 		ASSERT(so_verify_oobstate(so));
1006 		so->so_state &= ~(SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK);
1007 		freemsg(so->so_oobmsg);
1008 		so->so_oobmsg = NULL;
1009 		ASSERT(so_verify_oobstate(so));
1010 		mutex_exit(&so->so_lock);
1011 	}
1012 	ASSERT(so->so_rcv_wakeup == B_FALSE);
1013 done:
1014 	if (sodp != NULL) {
1015 		mutex_enter(&so->so_lock);
1016 		if (sodp->sod_enabled &&
1017 		    (sodp->sod_uioa.uioa_state & UIOA_ENABLED)) {
1018 			SOD_UIOAFINI(sodp);
1019 			if (sodp->sod_uioa.uioa_mbytes > 0) {
1020 				ASSERT(so->so_rcv_q_head != NULL ||
1021 				    so->so_rcv_head != NULL);
1022 				so->so_rcv_queued -= sod_uioa_mblk(so, NULL);
1023 				if (error == EWOULDBLOCK)
1024 					error = 0;
1025 			}
1026 		}
1027 		mutex_exit(&so->so_lock);
1028 	}
1029 #ifdef DEBUG
1030 	if (so_debug_length) {
1031 		mutex_enter(&so->so_lock);
1032 		ASSERT(so_check_length(so));
1033 		mutex_exit(&so->so_lock);
1034 	}
1035 #endif
1036 	rvalp->r_val1 = more;
1037 	ASSERT(MUTEX_NOT_HELD(&so->so_lock));
1038 	return (error);
1039 }
1040 
1041 /*
1042  * Enqueue data from the protocol on the socket's rcv queue.
1043  *
1044  * We try to hook new M_DATA mblks onto an existing chain, however,
1045  * that cannot be done if the existing chain has already been
1046  * processed by I/OAT. Non-M_DATA mblks are just linked together via
1047  * b_next. In all cases the b_prev of the enqueued mblk is set to
1048  * point to the last mblk in its b_cont chain.
1049  */
1050 void
1051 so_enqueue_msg(struct sonode *so, mblk_t *mp, size_t msg_size)
1052 {
1053 	ASSERT(MUTEX_HELD(&so->so_lock));
1054 
1055 #ifdef DEBUG
1056 	if (so_debug_length) {
1057 		ASSERT(so_check_length(so));
1058 	}
1059 #endif
1060 	so->so_rcv_queued += msg_size;
1061 
1062 	if (so->so_rcv_head == NULL) {
1063 		ASSERT(so->so_rcv_last_head == NULL);
1064 		so->so_rcv_head = mp;
1065 		so->so_rcv_last_head = mp;
1066 	} else if ((DB_TYPE(mp) == M_DATA &&
1067 	    DB_TYPE(so->so_rcv_last_head) == M_DATA) &&
1068 	    ((DB_FLAGS(mp) & DBLK_UIOA) ==
1069 	    (DB_FLAGS(so->so_rcv_last_head) & DBLK_UIOA))) {
1070 		/* Added to the end */
1071 		ASSERT(so->so_rcv_last_head != NULL);
1072 		ASSERT(so->so_rcv_last_head->b_prev != NULL);
1073 		so->so_rcv_last_head->b_prev->b_cont = mp;
1074 	} else {
1075 		/* Start a new end */
1076 		so->so_rcv_last_head->b_next = mp;
1077 		so->so_rcv_last_head = mp;
1078 	}
1079 	while (mp->b_cont != NULL)
1080 		mp = mp->b_cont;
1081 
1082 	so->so_rcv_last_head->b_prev = mp;
1083 #ifdef DEBUG
1084 	if (so_debug_length) {
1085 		ASSERT(so_check_length(so));
1086 	}
1087 #endif
1088 }
1089 
1090 /*
1091  * Return B_TRUE if there is data in the message, B_FALSE otherwise.
1092  */
1093 boolean_t
1094 somsghasdata(mblk_t *mp)
1095 {
1096 	for (; mp; mp = mp->b_cont)
1097 		if (mp->b_datap->db_type == M_DATA) {
1098 			ASSERT(mp->b_wptr >= mp->b_rptr);
1099 			if (mp->b_wptr > mp->b_rptr)
1100 				return (B_TRUE);
1101 		}
1102 	return (B_FALSE);
1103 }
1104 
1105 /*
1106  * Flush the read side of sockfs.
1107  *
1108  * The caller must be sure that a reader is not already active when the
1109  * buffer is being flushed.
1110  */
1111 void
1112 so_rcv_flush(struct sonode *so)
1113 {
1114 	mblk_t  *mp;
1115 
1116 	ASSERT(MUTEX_HELD(&so->so_lock));
1117 
1118 	if (so->so_oobmsg != NULL) {
1119 		freemsg(so->so_oobmsg);
1120 		so->so_oobmsg = NULL;
1121 		so->so_oobmark = 0;
1122 		so->so_state &=
1123 		    ~(SS_OOBPEND|SS_HAVEOOBDATA|SS_HADOOBDATA|SS_RCVATMARK);
1124 	}
1125 
1126 	/*
1127 	 * Free messages sitting in the send and recv queue
1128 	 */
1129 	while (so->so_rcv_q_head != NULL) {
1130 		mp = so->so_rcv_q_head;
1131 		so->so_rcv_q_head = mp->b_next;
1132 		mp->b_next = mp->b_prev = NULL;
1133 		freemsg(mp);
1134 	}
1135 	while (so->so_rcv_head != NULL) {
1136 		mp = so->so_rcv_head;
1137 		so->so_rcv_head = mp->b_next;
1138 		mp->b_next = mp->b_prev = NULL;
1139 		freemsg(mp);
1140 	}
1141 	so->so_rcv_queued = 0;
1142 	so->so_rcv_q_head = NULL;
1143 	so->so_rcv_q_last_head = NULL;
1144 	so->so_rcv_head = NULL;
1145 	so->so_rcv_last_head = NULL;
1146 }
1147 
1148 /*
1149  * Handle recv* calls that set MSG_OOB or MSG_OOB together with MSG_PEEK.
1150  */
1151 int
1152 sorecvoob(struct sonode *so, struct nmsghdr *msg, struct uio *uiop, int flags,
1153     boolean_t oob_inline)
1154 {
1155 	mblk_t		*mp, *nmp;
1156 	int		error;
1157 
1158 	dprintso(so, 1, ("sorecvoob(%p, %p, 0x%x)\n", (void *)so, (void *)msg,
1159 	    flags));
1160 
1161 	if (msg != NULL) {
1162 		/*
1163 		 * There is never any oob data with addresses or control since
1164 		 * the T_EXDATA_IND does not carry any options.
1165 		 */
1166 		msg->msg_controllen = 0;
1167 		msg->msg_namelen = 0;
1168 		msg->msg_flags = 0;
1169 	}
1170 
1171 	mutex_enter(&so->so_lock);
1172 	ASSERT(so_verify_oobstate(so));
1173 	if (oob_inline ||
1174 	    (so->so_state & (SS_OOBPEND|SS_HADOOBDATA)) != SS_OOBPEND) {
1175 		dprintso(so, 1, ("sorecvoob: inline or data consumed\n"));
1176 		mutex_exit(&so->so_lock);
1177 		return (EINVAL);
1178 	}
1179 	if (!(so->so_state & SS_HAVEOOBDATA)) {
1180 		dprintso(so, 1, ("sorecvoob: no data yet\n"));
1181 		mutex_exit(&so->so_lock);
1182 		return (EWOULDBLOCK);
1183 	}
1184 	ASSERT(so->so_oobmsg != NULL);
1185 	mp = so->so_oobmsg;
1186 	if (flags & MSG_PEEK) {
1187 		/*
1188 		 * Since recv* can not return ENOBUFS we can not use dupmsg.
1189 		 * Instead we revert to the consolidation private
1190 		 * allocb_wait plus bcopy.
1191 		 */
1192 		mblk_t *mp1;
1193 
1194 		mp1 = allocb_wait(msgdsize(mp), BPRI_MED, STR_NOSIG, NULL);
1195 		ASSERT(mp1);
1196 
1197 		while (mp != NULL) {
1198 			ssize_t size;
1199 
1200 			size = MBLKL(mp);
1201 			bcopy(mp->b_rptr, mp1->b_wptr, size);
1202 			mp1->b_wptr += size;
1203 			ASSERT(mp1->b_wptr <= mp1->b_datap->db_lim);
1204 			mp = mp->b_cont;
1205 		}
1206 		mp = mp1;
1207 	} else {
1208 		/*
1209 		 * Update the state indicating that the data has been consumed.
1210 		 * Keep SS_OOBPEND set until data is consumed past the mark.
1211 		 */
1212 		so->so_oobmsg = NULL;
1213 		so->so_state ^= SS_HAVEOOBDATA|SS_HADOOBDATA;
1214 	}
1215 	ASSERT(so_verify_oobstate(so));
1216 	mutex_exit(&so->so_lock);
1217 
1218 	error = 0;
1219 	nmp = mp;
1220 	while (nmp != NULL && uiop->uio_resid > 0) {
1221 		ssize_t n = MBLKL(nmp);
1222 
1223 		n = MIN(n, uiop->uio_resid);
1224 		if (n > 0)
1225 			error = uiomove(nmp->b_rptr, n,
1226 			    UIO_READ, uiop);
1227 		if (error)
1228 			break;
1229 		nmp = nmp->b_cont;
1230 	}
1231 	ASSERT(mp->b_next == NULL && mp->b_prev == NULL);
1232 	freemsg(mp);
1233 	return (error);
1234 }
1235 
1236 /*
1237  * Allocate and initializ sonode
1238  */
1239 /* ARGSUSED */
1240 struct sonode *
1241 socket_sonode_create(struct sockparams *sp, int family, int type,
1242     int protocol, int version, int sflags, int *errorp, struct cred *cr)
1243 {
1244 	sonode_t *so;
1245 	int	kmflags;
1246 
1247 	/*
1248 	 * Choose the right set of sonodeops based on the upcall and
1249 	 * down call version that the protocol has provided
1250 	 */
1251 	if (SOCK_UC_VERSION != sp->sp_smod_info->smod_uc_version ||
1252 	    SOCK_DC_VERSION != sp->sp_smod_info->smod_dc_version) {
1253 		/*
1254 		 * mismatch
1255 		 */
1256 #ifdef DEBUG
1257 		cmn_err(CE_CONT, "protocol and socket module version mismatch");
1258 #endif
1259 		*errorp = EINVAL;
1260 		return (NULL);
1261 	}
1262 
1263 	kmflags = (sflags & SOCKET_NOSLEEP) ? KM_NOSLEEP : KM_SLEEP;
1264 
1265 	so = kmem_cache_alloc(socket_cache, kmflags);
1266 	if (so == NULL) {
1267 		*errorp = ENOMEM;
1268 		return (NULL);
1269 	}
1270 
1271 	sonode_init(so, sp, family, type, protocol, &so_sonodeops);
1272 
1273 	if (version == SOV_DEFAULT)
1274 		version = so_default_version;
1275 
1276 	so->so_version = (short)version;
1277 
1278 	/*
1279 	 * set the default values to be INFPSZ
1280 	 * if a protocol desires it can change the value later
1281 	 */
1282 	so->so_proto_props.sopp_rxhiwat = SOCKET_RECVHIWATER;
1283 	so->so_proto_props.sopp_rxlowat = SOCKET_RECVLOWATER;
1284 	so->so_proto_props.sopp_maxpsz = INFPSZ;
1285 	so->so_proto_props.sopp_maxblk = INFPSZ;
1286 
1287 	return (so);
1288 }
1289 
1290 int
1291 socket_init_common(struct sonode *so, struct sonode *pso, int flags, cred_t *cr)
1292 {
1293 	int error = 0;
1294 
1295 	if (pso != NULL) {
1296 		/*
1297 		 * We have a passive open, so inherit basic state from
1298 		 * the parent (listener).
1299 		 *
1300 		 * No need to grab the new sonode's lock, since there is no
1301 		 * one that can have a reference to it.
1302 		 */
1303 		mutex_enter(&pso->so_lock);
1304 
1305 		so->so_state |= SS_ISCONNECTED | (pso->so_state & SS_ASYNC);
1306 		so->so_pgrp = pso->so_pgrp;
1307 		so->so_rcvtimeo = pso->so_rcvtimeo;
1308 		so->so_sndtimeo = pso->so_sndtimeo;
1309 		so->so_xpg_rcvbuf = pso->so_xpg_rcvbuf;
1310 		/*
1311 		 * Make note of the socket level options. TCP and IP level
1312 		 * options are already inherited. We could do all this after
1313 		 * accept is successful but doing it here simplifies code and
1314 		 * no harm done for error case.
1315 		 */
1316 		so->so_options = pso->so_options & (SO_DEBUG|SO_REUSEADDR|
1317 		    SO_KEEPALIVE|SO_DONTROUTE|SO_BROADCAST|SO_USELOOPBACK|
1318 		    SO_OOBINLINE|SO_DGRAM_ERRIND|SO_LINGER);
1319 		so->so_proto_props = pso->so_proto_props;
1320 		so->so_mode = pso->so_mode;
1321 		so->so_pollev = pso->so_pollev & SO_POLLEV_ALWAYS;
1322 
1323 		mutex_exit(&pso->so_lock);
1324 	} else {
1325 		struct sockparams *sp = so->so_sockparams;
1326 		sock_upcalls_t *upcalls_to_use;
1327 
1328 		/*
1329 		 * Based on the version number select the right upcalls to
1330 		 * pass down. Currently we only have one version so choose
1331 		 * default
1332 		 */
1333 		upcalls_to_use = &so_upcalls;
1334 
1335 		/* active open, so create a lower handle */
1336 		so->so_proto_handle =
1337 		    sp->sp_smod_info->smod_proto_create_func(so->so_family,
1338 		    so->so_type, so->so_protocol, &so->so_downcalls,
1339 		    &so->so_mode, &error, flags, cr);
1340 
1341 		if (so->so_proto_handle == NULL) {
1342 			ASSERT(error != 0);
1343 			/*
1344 			 * To be safe; if a lower handle cannot be created, and
1345 			 * the proto does not give a reason why, assume there
1346 			 * was a lack of memory.
1347 			 */
1348 			return ((error == 0) ? ENOMEM : error);
1349 		}
1350 		ASSERT(so->so_downcalls != NULL);
1351 		ASSERT(so->so_downcalls->sd_send != NULL ||
1352 		    so->so_downcalls->sd_send_uio != NULL);
1353 		if (so->so_downcalls->sd_recv_uio != NULL) {
1354 			ASSERT(so->so_downcalls->sd_poll != NULL);
1355 			so->so_pollev |= SO_POLLEV_ALWAYS;
1356 		}
1357 
1358 		(*so->so_downcalls->sd_activate)(so->so_proto_handle,
1359 		    (sock_upper_handle_t)so, upcalls_to_use, 0, cr);
1360 
1361 		/* Wildcard */
1362 
1363 		/*
1364 		 * FIXME No need for this, the protocol can deal with it in
1365 		 * sd_create(). Should update ICMP.
1366 		 */
1367 		if (so->so_protocol != so->so_sockparams->sp_protocol) {
1368 			int protocol = so->so_protocol;
1369 			int error;
1370 			/*
1371 			 * Issue SO_PROTOTYPE setsockopt.
1372 			 */
1373 			error = socket_setsockopt(so, SOL_SOCKET, SO_PROTOTYPE,
1374 			    &protocol, (t_uscalar_t)sizeof (protocol), cr);
1375 			if (error) {
1376 				(void) (*so->so_downcalls->sd_close)
1377 				    (so->so_proto_handle, 0, cr);
1378 
1379 				mutex_enter(&so->so_lock);
1380 				so_rcv_flush(so);
1381 				mutex_exit(&so->so_lock);
1382 				/*
1383 				 * Setsockopt often fails with ENOPROTOOPT but
1384 				 * socket() should fail with
1385 				 * EPROTONOSUPPORT/EPROTOTYPE.
1386 				 */
1387 				return (EPROTONOSUPPORT);
1388 			}
1389 		}
1390 	}
1391 
1392 	if (uioasync.enabled)
1393 		sod_sock_init(so);
1394 
1395 	return (0);
1396 }
1397 
1398 /*
1399  * int socket_ioctl_common(struct sonode *so, int cmd, intptr_t arg, int mode,
1400  *         struct cred *cr, int32_t *rvalp)
1401  *
1402  * Handle ioctls that manipulate basic socket state; non-blocking,
1403  * async, etc.
1404  *
1405  * Returns:
1406  *   < 0  - ioctl was not handle
1407  *  >= 0  - ioctl was handled, if > 0, then it is an errno
1408  *
1409  * Notes:
1410  *   Assumes the standard receive buffer is used to obtain info for
1411  *   NREAD.
1412  */
1413 /* ARGSUSED */
1414 int
1415 socket_ioctl_common(struct sonode *so, int cmd, intptr_t arg, int mode,
1416     struct cred *cr, int32_t *rvalp)
1417 {
1418 	switch (cmd) {
1419 	case SIOCSQPTR:
1420 		/*
1421 		 * SIOCSQPTR is valid only when helper stream is created
1422 		 * by the protocol.
1423 		 */
1424 
1425 		return (EOPNOTSUPP);
1426 	case FIONBIO: {
1427 		int32_t value;
1428 
1429 		if (so_copyin((void *)arg, &value, sizeof (int32_t),
1430 		    (mode & (int)FKIOCTL)))
1431 			return (EFAULT);
1432 
1433 		mutex_enter(&so->so_lock);
1434 		if (value) {
1435 			so->so_state |= SS_NDELAY;
1436 		} else {
1437 			so->so_state &= ~SS_NDELAY;
1438 		}
1439 		mutex_exit(&so->so_lock);
1440 		return (0);
1441 	}
1442 	case FIOASYNC: {
1443 		int32_t value;
1444 
1445 		if (so_copyin((void *)arg, &value, sizeof (int32_t),
1446 		    (mode & (int)FKIOCTL)))
1447 			return (EFAULT);
1448 
1449 		mutex_enter(&so->so_lock);
1450 
1451 		if (value) {
1452 			/* Turn on SIGIO */
1453 			so->so_state |= SS_ASYNC;
1454 		} else {
1455 			/* Turn off SIGIO */
1456 			so->so_state &= ~SS_ASYNC;
1457 		}
1458 		mutex_exit(&so->so_lock);
1459 
1460 		return (0);
1461 	}
1462 
1463 	case SIOCSPGRP:
1464 	case FIOSETOWN: {
1465 		int error;
1466 		pid_t pid;
1467 
1468 		if (so_copyin((void *)arg, &pid, sizeof (pid_t),
1469 		    (mode & (int)FKIOCTL)))
1470 			return (EFAULT);
1471 
1472 		mutex_enter(&so->so_lock);
1473 		error = (pid != so->so_pgrp) ? socket_chgpgrp(so, pid) : 0;
1474 		mutex_exit(&so->so_lock);
1475 		return (error);
1476 	}
1477 	case SIOCGPGRP:
1478 	case FIOGETOWN:
1479 		if (so_copyout(&so->so_pgrp, (void *)arg,
1480 		    sizeof (pid_t), (mode & (int)FKIOCTL)))
1481 			return (EFAULT);
1482 
1483 		return (0);
1484 	case SIOCATMARK: {
1485 		int retval;
1486 
1487 		/*
1488 		 * Only protocols that support urgent data can handle ATMARK.
1489 		 */
1490 		if ((so->so_mode & SM_EXDATA) == 0)
1491 			return (EINVAL);
1492 
1493 		/*
1494 		 * If the protocol is maintaining its own buffer, then the
1495 		 * request must be passed down.
1496 		 */
1497 		if (so->so_downcalls->sd_recv_uio != NULL)
1498 			return (-1);
1499 
1500 		retval = (so->so_state & SS_RCVATMARK) != 0;
1501 
1502 		if (so_copyout(&retval, (void *)arg, sizeof (int),
1503 		    (mode & (int)FKIOCTL))) {
1504 			return (EFAULT);
1505 		}
1506 		return (0);
1507 	}
1508 
1509 	case FIONREAD: {
1510 		int retval;
1511 
1512 		/*
1513 		 * If the protocol is maintaining its own buffer, then the
1514 		 * request must be passed down.
1515 		 */
1516 		if (so->so_downcalls->sd_recv_uio != NULL)
1517 			return (-1);
1518 
1519 		retval = MIN(so->so_rcv_queued, INT_MAX);
1520 
1521 		if (so_copyout(&retval, (void *)arg,
1522 		    sizeof (retval), (mode & (int)FKIOCTL))) {
1523 			return (EFAULT);
1524 		}
1525 		return (0);
1526 	}
1527 
1528 	case _I_GETPEERCRED: {
1529 		int error = 0;
1530 
1531 		if ((mode & FKIOCTL) == 0)
1532 			return (EINVAL);
1533 
1534 		mutex_enter(&so->so_lock);
1535 		if ((so->so_mode & SM_CONNREQUIRED) == 0) {
1536 			error = ENOTSUP;
1537 		} else if ((so->so_state & SS_ISCONNECTED) == 0) {
1538 			error = ENOTCONN;
1539 		} else if (so->so_peercred != NULL) {
1540 			k_peercred_t *kp = (k_peercred_t *)arg;
1541 			kp->pc_cr = so->so_peercred;
1542 			kp->pc_cpid = so->so_cpid;
1543 			crhold(so->so_peercred);
1544 		} else {
1545 			error = EINVAL;
1546 		}
1547 		mutex_exit(&so->so_lock);
1548 		return (error);
1549 	}
1550 	default:
1551 		return (-1);
1552 	}
1553 }
1554 
1555 /*
1556  * Handle the I_NREAD STREAM ioctl.
1557  */
1558 static int
1559 so_strioc_nread(struct sonode *so, intptr_t arg, int mode, int32_t *rvalp)
1560 {
1561 	size_t size = 0;
1562 	int retval;
1563 	int count = 0;
1564 	mblk_t *mp;
1565 
1566 	if (so->so_downcalls == NULL ||
1567 	    so->so_downcalls->sd_recv_uio != NULL)
1568 		return (EINVAL);
1569 
1570 	mutex_enter(&so->so_lock);
1571 	/* Wait for reader to get out of the way. */
1572 	while (so->so_flag & SOREADLOCKED) {
1573 		/*
1574 		 * If reader is waiting for data, then there should be nothing
1575 		 * on the rcv queue.
1576 		 */
1577 		if (so->so_rcv_wakeup)
1578 			goto out;
1579 
1580 		so->so_flag |= SOWANT;
1581 		/* Do a timed sleep, in case the reader goes to sleep. */
1582 		(void) cv_timedwait(&so->so_state_cv, &so->so_lock,
1583 		    lbolt + drv_usectohz(10));
1584 	}
1585 
1586 	/*
1587 	 * Since we are holding so_lock no new reader will come in, and the
1588 	 * protocol will not be able to enqueue data. So it's safe to walk
1589 	 * both rcv queues.
1590 	 */
1591 	mp = so->so_rcv_q_head;
1592 	if (mp != NULL) {
1593 		size = msgdsize(so->so_rcv_q_head);
1594 		for (; mp != NULL; mp = mp->b_next)
1595 			count++;
1596 	} else {
1597 		/*
1598 		 * In case the processing list was empty, get the size of the
1599 		 * next msg in line.
1600 		 */
1601 		size = msgdsize(so->so_rcv_head);
1602 	}
1603 
1604 	for (mp = so->so_rcv_head; mp != NULL; mp = mp->b_next)
1605 		count++;
1606 out:
1607 	mutex_exit(&so->so_lock);
1608 
1609 	/*
1610 	 * Drop down from size_t to the "int" required by the
1611 	 * interface.  Cap at INT_MAX.
1612 	 */
1613 	retval = MIN(size, INT_MAX);
1614 	if (so_copyout(&retval, (void *)arg, sizeof (retval),
1615 	    (mode & (int)FKIOCTL))) {
1616 		return (EFAULT);
1617 	} else {
1618 		*rvalp = count;
1619 		return (0);
1620 	}
1621 }
1622 
1623 /*
1624  * Process STREAM ioctls.
1625  *
1626  * Returns:
1627  *   < 0  - ioctl was not handle
1628  *  >= 0  - ioctl was handled, if > 0, then it is an errno
1629  */
1630 int
1631 socket_strioc_common(struct sonode *so, int cmd, intptr_t arg, int mode,
1632     struct cred *cr, int32_t *rvalp)
1633 {
1634 	int retval;
1635 
1636 	/* Only STREAM iotcls are handled here */
1637 	if ((cmd & 0xffffff00U) != STR)
1638 		return (-1);
1639 
1640 	switch (cmd) {
1641 	case I_CANPUT:
1642 		/*
1643 		 * We return an error for I_CANPUT so that isastream(3C) will
1644 		 * not report the socket as being a STREAM.
1645 		 */
1646 		return (EOPNOTSUPP);
1647 	case I_NREAD:
1648 		/* Avoid doing a fallback for I_NREAD. */
1649 		return (so_strioc_nread(so, arg, mode, rvalp));
1650 	case I_LOOK:
1651 		/* Avoid doing a fallback for I_LOOK. */
1652 		if (so_copyout("sockmod", (void *)arg, strlen("sockmod") + 1,
1653 		    (mode & (int)FKIOCTL))) {
1654 			return (EFAULT);
1655 		}
1656 		return (0);
1657 	default:
1658 		break;
1659 	}
1660 
1661 	/*
1662 	 * Try to fall back to TPI, and if successful, reissue the ioctl.
1663 	 */
1664 	if ((retval = so_tpi_fallback(so, cr)) == 0) {
1665 		/* Reissue the ioctl */
1666 		ASSERT(so->so_rcv_q_head == NULL);
1667 		return (SOP_IOCTL(so, cmd, arg, mode, cr, rvalp));
1668 	} else {
1669 		return (retval);
1670 	}
1671 }
1672 
1673 /*
1674  * This is called for all socket types to verify that the buffer size is large
1675  * enough for the option, and if we can, handle the request as well. Most
1676  * options will be forwarded to the protocol.
1677  */
1678 int
1679 socket_getopt_common(struct sonode *so, int level, int option_name,
1680     void *optval, socklen_t *optlenp, int flags)
1681 {
1682 	if (level != SOL_SOCKET)
1683 		return (-1);
1684 
1685 	switch (option_name) {
1686 	case SO_ERROR:
1687 	case SO_DOMAIN:
1688 	case SO_TYPE:
1689 	case SO_ACCEPTCONN: {
1690 		int32_t value;
1691 		socklen_t optlen = *optlenp;
1692 
1693 		if (optlen < (t_uscalar_t)sizeof (int32_t)) {
1694 			return (EINVAL);
1695 		}
1696 
1697 		switch (option_name) {
1698 		case SO_ERROR:
1699 			mutex_enter(&so->so_lock);
1700 			value = sogeterr(so, B_TRUE);
1701 			mutex_exit(&so->so_lock);
1702 			break;
1703 		case SO_DOMAIN:
1704 			value = so->so_family;
1705 			break;
1706 		case SO_TYPE:
1707 			value = so->so_type;
1708 			break;
1709 		case SO_ACCEPTCONN:
1710 			if (so->so_state & SS_ACCEPTCONN)
1711 				value = SO_ACCEPTCONN;
1712 			else
1713 				value = 0;
1714 			break;
1715 		}
1716 
1717 		bcopy(&value, optval, sizeof (value));
1718 		*optlenp = sizeof (value);
1719 
1720 		return (0);
1721 	}
1722 	case SO_SNDTIMEO:
1723 	case SO_RCVTIMEO: {
1724 		clock_t value;
1725 		socklen_t optlen = *optlenp;
1726 
1727 		if (get_udatamodel() == DATAMODEL_NONE ||
1728 		    get_udatamodel() == DATAMODEL_NATIVE) {
1729 			if (optlen < sizeof (struct timeval))
1730 				return (EINVAL);
1731 		} else {
1732 			if (optlen < sizeof (struct timeval32))
1733 				return (EINVAL);
1734 		}
1735 		if (option_name == SO_RCVTIMEO)
1736 			value = drv_hztousec(so->so_rcvtimeo);
1737 		else
1738 			value = drv_hztousec(so->so_sndtimeo);
1739 
1740 		if (get_udatamodel() == DATAMODEL_NONE ||
1741 		    get_udatamodel() == DATAMODEL_NATIVE) {
1742 			((struct timeval *)(optval))->tv_sec =
1743 			    value / (1000 * 1000);
1744 			((struct timeval *)(optval))->tv_usec =
1745 			    value % (1000 * 1000);
1746 			*optlenp = sizeof (struct timeval);
1747 		} else {
1748 			((struct timeval32 *)(optval))->tv_sec =
1749 			    value / (1000 * 1000);
1750 			((struct timeval32 *)(optval))->tv_usec =
1751 			    value % (1000 * 1000);
1752 			*optlenp = sizeof (struct timeval32);
1753 		}
1754 		return (0);
1755 	}
1756 	case SO_DEBUG:
1757 	case SO_REUSEADDR:
1758 	case SO_KEEPALIVE:
1759 	case SO_DONTROUTE:
1760 	case SO_BROADCAST:
1761 	case SO_USELOOPBACK:
1762 	case SO_OOBINLINE:
1763 	case SO_SNDBUF:
1764 #ifdef notyet
1765 	case SO_SNDLOWAT:
1766 	case SO_RCVLOWAT:
1767 #endif /* notyet */
1768 	case SO_DGRAM_ERRIND: {
1769 		socklen_t optlen = *optlenp;
1770 
1771 		if (optlen < (t_uscalar_t)sizeof (int32_t))
1772 			return (EINVAL);
1773 		break;
1774 	}
1775 	case SO_RCVBUF: {
1776 		socklen_t optlen = *optlenp;
1777 
1778 		if (optlen < (t_uscalar_t)sizeof (int32_t))
1779 			return (EINVAL);
1780 
1781 		if ((flags & _SOGETSOCKOPT_XPG4_2) && so->so_xpg_rcvbuf != 0) {
1782 			/*
1783 			 * XXX If SO_RCVBUF has been set and this is an
1784 			 * XPG 4.2 application then do not ask the transport
1785 			 * since the transport might adjust the value and not
1786 			 * return exactly what was set by the application.
1787 			 * For non-XPG 4.2 application we return the value
1788 			 * that the transport is actually using.
1789 			 */
1790 			*(int32_t *)optval = so->so_xpg_rcvbuf;
1791 			*optlenp = sizeof (so->so_xpg_rcvbuf);
1792 			return (0);
1793 		}
1794 		/*
1795 		 * If the option has not been set then get a default
1796 		 * value from the transport.
1797 		 */
1798 		break;
1799 	}
1800 	case SO_LINGER: {
1801 		socklen_t optlen = *optlenp;
1802 
1803 		if (optlen < (t_uscalar_t)sizeof (struct linger))
1804 			return (EINVAL);
1805 		break;
1806 	}
1807 	case SO_SND_BUFINFO: {
1808 		socklen_t optlen = *optlenp;
1809 
1810 		if (optlen < (t_uscalar_t)sizeof (struct so_snd_bufinfo))
1811 			return (EINVAL);
1812 		((struct so_snd_bufinfo *)(optval))->sbi_wroff =
1813 		    (so->so_proto_props).sopp_wroff;
1814 		((struct so_snd_bufinfo *)(optval))->sbi_maxblk =
1815 		    (so->so_proto_props).sopp_maxblk;
1816 		((struct so_snd_bufinfo *)(optval))->sbi_maxpsz =
1817 		    (so->so_proto_props).sopp_maxpsz;
1818 		((struct so_snd_bufinfo *)(optval))->sbi_tail =
1819 		    (so->so_proto_props).sopp_tail;
1820 		*optlenp = sizeof (struct so_snd_bufinfo);
1821 		return (0);
1822 	}
1823 	default:
1824 		break;
1825 	}
1826 
1827 	/* Unknown Option */
1828 	return (-1);
1829 }
1830 
1831 void
1832 socket_sonode_destroy(struct sonode *so)
1833 {
1834 	sonode_fini(so);
1835 	kmem_cache_free(socket_cache, so);
1836 }
1837 
1838 int
1839 so_zcopy_wait(struct sonode *so)
1840 {
1841 	int error = 0;
1842 
1843 	mutex_enter(&so->so_lock);
1844 	while (!(so->so_copyflag & STZCNOTIFY)) {
1845 		if (so->so_state & SS_CLOSING) {
1846 			mutex_exit(&so->so_lock);
1847 			return (EINTR);
1848 		}
1849 		if (cv_wait_sig(&so->so_copy_cv, &so->so_lock) == 0) {
1850 			error = EINTR;
1851 			break;
1852 		}
1853 	}
1854 	so->so_copyflag &= ~STZCNOTIFY;
1855 	mutex_exit(&so->so_lock);
1856 	return (error);
1857 }
1858 
1859 void
1860 so_timer_callback(void *arg)
1861 {
1862 	struct sonode *so = (struct sonode *)arg;
1863 
1864 	mutex_enter(&so->so_lock);
1865 
1866 	so->so_rcv_timer_tid = 0;
1867 	if (so->so_rcv_queued > 0) {
1868 		so_notify_data(so, so->so_rcv_queued);
1869 	} else {
1870 		mutex_exit(&so->so_lock);
1871 	}
1872 }
1873 
1874 #ifdef DEBUG
1875 /*
1876  * Verify that the length stored in so_rcv_queued and the length of data blocks
1877  * queued is same.
1878  */
1879 static boolean_t
1880 so_check_length(sonode_t *so)
1881 {
1882 	mblk_t *mp = so->so_rcv_q_head;
1883 	int len = 0;
1884 
1885 	ASSERT(MUTEX_HELD(&so->so_lock));
1886 
1887 	if (mp != NULL) {
1888 		len = msgdsize(mp);
1889 		while ((mp = mp->b_next) != NULL)
1890 			len += msgdsize(mp);
1891 	}
1892 	mp = so->so_rcv_head;
1893 	if (mp != NULL) {
1894 		len += msgdsize(mp);
1895 		while ((mp = mp->b_next) != NULL)
1896 			len += msgdsize(mp);
1897 	}
1898 	return ((len == so->so_rcv_queued) ? B_TRUE : B_FALSE);
1899 }
1900 #endif
1901 
1902 int
1903 so_get_mod_version(struct sockparams *sp)
1904 {
1905 	ASSERT(sp != NULL && sp->sp_smod_info != NULL);
1906 	return (sp->sp_smod_info->smod_version);
1907 }
1908 
1909 /*
1910  * so_start_fallback()
1911  *
1912  * Block new socket operations from coming in, and wait for active operations
1913  * to complete. Threads that are sleeping will be woken up so they can get
1914  * out of the way.
1915  *
1916  * The caller must be a reader on so_fallback_rwlock.
1917  */
1918 static boolean_t
1919 so_start_fallback(struct sonode *so)
1920 {
1921 	ASSERT(RW_READ_HELD(&so->so_fallback_rwlock));
1922 
1923 	mutex_enter(&so->so_lock);
1924 	if (so->so_state & SS_FALLBACK_PENDING) {
1925 		mutex_exit(&so->so_lock);
1926 		return (B_FALSE);
1927 	}
1928 	so->so_state |= SS_FALLBACK_PENDING;
1929 	/*
1930 	 * Poke all threads that might be sleeping. Any operation that comes
1931 	 * in after the cv_broadcast will observe the fallback pending flag
1932 	 * which cause the call to return where it would normally sleep.
1933 	 */
1934 	cv_broadcast(&so->so_state_cv);		/* threads in connect() */
1935 	cv_broadcast(&so->so_rcv_cv);		/* threads in recvmsg() */
1936 	cv_broadcast(&so->so_snd_cv);		/* threads in sendmsg() */
1937 	mutex_enter(&so->so_acceptq_lock);
1938 	cv_broadcast(&so->so_acceptq_cv);	/* threads in accept() */
1939 	mutex_exit(&so->so_acceptq_lock);
1940 	mutex_exit(&so->so_lock);
1941 
1942 	/*
1943 	 * The main reason for the rw_tryupgrade call is to provide
1944 	 * observability during the fallback process. We want to
1945 	 * be able to see if there are pending operations.
1946 	 */
1947 	if (rw_tryupgrade(&so->so_fallback_rwlock) == 0) {
1948 		/*
1949 		 * It is safe to drop and reaquire the fallback lock, because
1950 		 * we are guaranteed that another fallback cannot take place.
1951 		 */
1952 		rw_exit(&so->so_fallback_rwlock);
1953 		DTRACE_PROBE1(pending__ops__wait, (struct sonode *), so);
1954 		rw_enter(&so->so_fallback_rwlock, RW_WRITER);
1955 		DTRACE_PROBE1(pending__ops__complete, (struct sonode *), so);
1956 	}
1957 
1958 	return (B_TRUE);
1959 }
1960 
1961 /*
1962  * so_end_fallback()
1963  *
1964  * Allow socket opertions back in.
1965  *
1966  * The caller must be a writer on so_fallback_rwlock.
1967  */
1968 static void
1969 so_end_fallback(struct sonode *so)
1970 {
1971 	ASSERT(RW_ISWRITER(&so->so_fallback_rwlock));
1972 
1973 	mutex_enter(&so->so_lock);
1974 	so->so_state &= ~(SS_FALLBACK_PENDING|SS_FALLBACK_DRAIN);
1975 	mutex_exit(&so->so_lock);
1976 
1977 	rw_downgrade(&so->so_fallback_rwlock);
1978 }
1979 
1980 /*
1981  * so_quiesced_cb()
1982  *
1983  * Callback passed to the protocol during fallback. It is called once
1984  * the endpoint is quiescent.
1985  *
1986  * No requests from the user, no notifications from the protocol, so it
1987  * is safe to synchronize the state. Data can also be moved without
1988  * risk for reordering.
1989  *
1990  * We do not need to hold so_lock, since there can be only one thread
1991  * operating on the sonode.
1992  */
1993 static void
1994 so_quiesced_cb(sock_upper_handle_t sock_handle, queue_t *q,
1995     struct T_capability_ack *tcap, struct sockaddr *laddr, socklen_t laddrlen,
1996     struct sockaddr *faddr, socklen_t faddrlen, short opts)
1997 {
1998 	struct sonode *so = (struct sonode *)sock_handle;
1999 	boolean_t atmark;
2000 
2001 	sotpi_update_state(so, tcap, laddr, laddrlen, faddr, faddrlen, opts);
2002 
2003 	/*
2004 	 * Some protocols do not quiece the data path during fallback. Once
2005 	 * we set the SS_FALLBACK_DRAIN flag any attempt to queue data will
2006 	 * fail and the protocol is responsible for saving the data for later
2007 	 * delivery (i.e., once the fallback has completed).
2008 	 */
2009 	mutex_enter(&so->so_lock);
2010 	so->so_state |= SS_FALLBACK_DRAIN;
2011 	SOCKET_TIMER_CANCEL(so);
2012 	mutex_exit(&so->so_lock);
2013 
2014 	if (so->so_rcv_head != NULL) {
2015 		if (so->so_rcv_q_last_head == NULL)
2016 			so->so_rcv_q_head = so->so_rcv_head;
2017 		else
2018 			so->so_rcv_q_last_head->b_next = so->so_rcv_head;
2019 		so->so_rcv_q_last_head = so->so_rcv_last_head;
2020 	}
2021 
2022 	atmark = (so->so_state & SS_RCVATMARK) != 0;
2023 	/*
2024 	 * Clear any OOB state having to do with pending data. The TPI
2025 	 * code path will set the appropriate oob state when we move the
2026 	 * oob data to the STREAM head. We leave SS_HADOOBDATA since the oob
2027 	 * data has already been consumed.
2028 	 */
2029 	so->so_state &= ~(SS_RCVATMARK|SS_OOBPEND|SS_HAVEOOBDATA);
2030 
2031 	ASSERT(so->so_oobmsg != NULL || so->so_oobmark <= so->so_rcv_queued);
2032 
2033 	/*
2034 	 * Move data to the STREAM head.
2035 	 */
2036 	while (so->so_rcv_q_head != NULL) {
2037 		mblk_t *mp = so->so_rcv_q_head;
2038 		size_t mlen = msgdsize(mp);
2039 
2040 		so->so_rcv_q_head = mp->b_next;
2041 		mp->b_next = NULL;
2042 		mp->b_prev = NULL;
2043 
2044 		/*
2045 		 * Send T_EXDATA_IND if we are at the oob mark.
2046 		 */
2047 		if (atmark) {
2048 			struct T_exdata_ind *tei;
2049 			mblk_t *mp1 = SOTOTPI(so)->sti_exdata_mp;
2050 
2051 			SOTOTPI(so)->sti_exdata_mp = NULL;
2052 			ASSERT(mp1 != NULL);
2053 			mp1->b_datap->db_type = M_PROTO;
2054 			tei = (struct T_exdata_ind *)mp1->b_rptr;
2055 			tei->PRIM_type = T_EXDATA_IND;
2056 			tei->MORE_flag = 0;
2057 			mp1->b_wptr = (uchar_t *)&tei[1];
2058 
2059 			if (IS_SO_OOB_INLINE(so)) {
2060 				mp1->b_cont = mp;
2061 			} else {
2062 				ASSERT(so->so_oobmsg != NULL);
2063 				mp1->b_cont = so->so_oobmsg;
2064 				so->so_oobmsg = NULL;
2065 
2066 				/* process current mp next time around */
2067 				mp->b_next = so->so_rcv_q_head;
2068 				so->so_rcv_q_head = mp;
2069 				mlen = 0;
2070 			}
2071 			mp = mp1;
2072 
2073 			/* we have consumed the oob mark */
2074 			atmark = B_FALSE;
2075 		} else if (so->so_oobmark > 0) {
2076 			/*
2077 			 * Check if the OOB mark is within the current
2078 			 * mblk chain. In that case we have to split it up.
2079 			 */
2080 			if (so->so_oobmark < mlen) {
2081 				mblk_t *urg_mp = mp;
2082 
2083 				atmark = B_TRUE;
2084 				mp = NULL;
2085 				mlen = so->so_oobmark;
2086 
2087 				/*
2088 				 * It is assumed that the OOB mark does
2089 				 * not land within a mblk.
2090 				 */
2091 				do {
2092 					so->so_oobmark -= MBLKL(urg_mp);
2093 					mp = urg_mp;
2094 					urg_mp = urg_mp->b_cont;
2095 				} while (so->so_oobmark > 0);
2096 				mp->b_cont = NULL;
2097 				if (urg_mp != NULL) {
2098 					urg_mp->b_next = so->so_rcv_q_head;
2099 					so->so_rcv_q_head = urg_mp;
2100 				}
2101 			} else {
2102 				so->so_oobmark -= mlen;
2103 				if (so->so_oobmark == 0)
2104 					atmark = B_TRUE;
2105 			}
2106 		}
2107 
2108 		/*
2109 		 * Queue data on the STREAM head.
2110 		 */
2111 		so->so_rcv_queued -= mlen;
2112 		putnext(q, mp);
2113 	}
2114 	so->so_rcv_head = NULL;
2115 	so->so_rcv_last_head = NULL;
2116 	so->so_rcv_q_head = NULL;
2117 	so->so_rcv_q_last_head = NULL;
2118 
2119 	/*
2120 	 * Check if the oob byte is at the end of the data stream, or if the
2121 	 * oob byte has not yet arrived. In the latter case we have to send a
2122 	 * SIGURG and a mark indicator to the STREAM head. The mark indicator
2123 	 * is needed to guarantee correct behavior for SIOCATMARK. See block
2124 	 * comment in socktpi.h for more details.
2125 	 */
2126 	if (atmark || so->so_oobmark > 0) {
2127 		mblk_t *mp;
2128 
2129 		if (atmark && so->so_oobmsg != NULL) {
2130 			struct T_exdata_ind *tei;
2131 
2132 			mp = SOTOTPI(so)->sti_exdata_mp;
2133 			SOTOTPI(so)->sti_exdata_mp = NULL;
2134 			ASSERT(mp != NULL);
2135 			mp->b_datap->db_type = M_PROTO;
2136 			tei = (struct T_exdata_ind *)mp->b_rptr;
2137 			tei->PRIM_type = T_EXDATA_IND;
2138 			tei->MORE_flag = 0;
2139 			mp->b_wptr = (uchar_t *)&tei[1];
2140 
2141 			mp->b_cont = so->so_oobmsg;
2142 			so->so_oobmsg = NULL;
2143 
2144 			putnext(q, mp);
2145 		} else {
2146 			/* Send up the signal */
2147 			mp = SOTOTPI(so)->sti_exdata_mp;
2148 			SOTOTPI(so)->sti_exdata_mp = NULL;
2149 			ASSERT(mp != NULL);
2150 			DB_TYPE(mp) = M_PCSIG;
2151 			*mp->b_wptr++ = (uchar_t)SIGURG;
2152 			putnext(q, mp);
2153 
2154 			/* Send up the mark indicator */
2155 			mp = SOTOTPI(so)->sti_urgmark_mp;
2156 			SOTOTPI(so)->sti_urgmark_mp = NULL;
2157 			mp->b_flag = atmark ? MSGMARKNEXT : MSGNOTMARKNEXT;
2158 			putnext(q, mp);
2159 
2160 			so->so_oobmark = 0;
2161 		}
2162 	}
2163 
2164 	if (SOTOTPI(so)->sti_exdata_mp != NULL) {
2165 		freeb(SOTOTPI(so)->sti_exdata_mp);
2166 		SOTOTPI(so)->sti_exdata_mp = NULL;
2167 	}
2168 
2169 	if (SOTOTPI(so)->sti_urgmark_mp != NULL) {
2170 		freeb(SOTOTPI(so)->sti_urgmark_mp);
2171 		SOTOTPI(so)->sti_urgmark_mp = NULL;
2172 	}
2173 
2174 	ASSERT(so->so_oobmark == 0);
2175 	ASSERT(so->so_rcv_queued == 0);
2176 }
2177 
2178 #ifdef DEBUG
2179 /*
2180  * Do an integrity check of the sonode. This should be done if a
2181  * fallback fails after sonode has initially been converted to use
2182  * TPI and subsequently have to be reverted.
2183  *
2184  * Failure to pass the integrity check will panic the system.
2185  */
2186 void
2187 so_integrity_check(struct sonode *cur, struct sonode *orig)
2188 {
2189 	VERIFY(cur->so_vnode == orig->so_vnode);
2190 	VERIFY(cur->so_ops == orig->so_ops);
2191 	/*
2192 	 * For so_state we can only VERIFY the state flags in CHECK_STATE.
2193 	 * The other state flags might be affected by a notification from the
2194 	 * protocol.
2195 	 */
2196 #define	CHECK_STATE	(SS_CANTRCVMORE|SS_CANTSENDMORE|SS_NDELAY|SS_NONBLOCK| \
2197 	SS_ASYNC|SS_ACCEPTCONN|SS_SAVEDEOR|SS_RCVATMARK|SS_OOBPEND| \
2198 	SS_HAVEOOBDATA|SS_HADOOBDATA|SS_SENTLASTREADSIG|SS_SENTLASTWRITESIG)
2199 	VERIFY((cur->so_state & (orig->so_state & CHECK_STATE)) ==
2200 	    (orig->so_state & CHECK_STATE));
2201 	VERIFY(cur->so_mode == orig->so_mode);
2202 	VERIFY(cur->so_flag == orig->so_flag);
2203 	VERIFY(cur->so_count == orig->so_count);
2204 	/* Cannot VERIFY so_proto_connid; proto can update it */
2205 	VERIFY(cur->so_sockparams == orig->so_sockparams);
2206 	/* an error might have been recorded, but it can not be lost */
2207 	VERIFY(cur->so_error != 0 || orig->so_error == 0);
2208 	VERIFY(cur->so_family == orig->so_family);
2209 	VERIFY(cur->so_type == orig->so_type);
2210 	VERIFY(cur->so_protocol == orig->so_protocol);
2211 	VERIFY(cur->so_version == orig->so_version);
2212 	/* New conns might have arrived, but none should have been lost */
2213 	VERIFY(cur->so_acceptq_len >= orig->so_acceptq_len);
2214 	VERIFY(cur->so_acceptq_head == orig->so_acceptq_head);
2215 	VERIFY(cur->so_backlog == orig->so_backlog);
2216 	/* New OOB migth have arrived, but mark should not have been lost */
2217 	VERIFY(cur->so_oobmark >= orig->so_oobmark);
2218 	/* Cannot VERIFY so_oobmsg; the proto might have sent up a new one */
2219 	VERIFY(cur->so_pgrp == orig->so_pgrp);
2220 	VERIFY(cur->so_peercred == orig->so_peercred);
2221 	VERIFY(cur->so_cpid == orig->so_cpid);
2222 	VERIFY(cur->so_zoneid == orig->so_zoneid);
2223 	/* New data migth have arrived, but none should have been lost */
2224 	VERIFY(cur->so_rcv_queued >= orig->so_rcv_queued);
2225 	VERIFY(cur->so_rcv_q_head == orig->so_rcv_q_head);
2226 	VERIFY(cur->so_rcv_head == orig->so_rcv_head);
2227 	VERIFY(cur->so_proto_handle == orig->so_proto_handle);
2228 	VERIFY(cur->so_downcalls == orig->so_downcalls);
2229 	/* Cannot VERIFY so_proto_props; they can be updated by proto */
2230 }
2231 #endif
2232 
2233 /*
2234  * so_tpi_fallback()
2235  *
2236  * This is the fallback initation routine; things start here.
2237  *
2238  * Basic strategy:
2239  *   o Block new socket operations from coming in
2240  *   o Allocate/initate info needed by TPI
2241  *   o Quiesce the connection, at which point we sync
2242  *     state and move data
2243  *   o Change operations (sonodeops) associated with the socket
2244  *   o Unblock threads waiting for the fallback to finish
2245  */
2246 int
2247 so_tpi_fallback(struct sonode *so, struct cred *cr)
2248 {
2249 	int error;
2250 	queue_t *q;
2251 	struct sockparams *sp;
2252 	struct sockparams *newsp = NULL;
2253 	so_proto_fallback_func_t fbfunc;
2254 	boolean_t direct;
2255 	struct sonode *nso;
2256 #ifdef DEBUG
2257 	struct sonode origso;
2258 #endif
2259 	error = 0;
2260 	sp = so->so_sockparams;
2261 	fbfunc = sp->sp_smod_info->smod_proto_fallback_func;
2262 
2263 	/*
2264 	 * Fallback can only happen if there is a device associated
2265 	 * with the sonode, and the socket module has a fallback function.
2266 	 */
2267 	if (!SOCKPARAMS_HAS_DEVICE(sp) || fbfunc == NULL)
2268 		return (EINVAL);
2269 
2270 	/*
2271 	 * Initiate fallback; upon success we know that no new requests
2272 	 * will come in from the user.
2273 	 */
2274 	if (!so_start_fallback(so))
2275 		return (EAGAIN);
2276 #ifdef DEBUG
2277 	/*
2278 	 * Make a copy of the sonode in case we need to make an integrity
2279 	 * check later on.
2280 	 */
2281 	bcopy(so, &origso, sizeof (*so));
2282 #endif
2283 
2284 	sp->sp_stats.sps_nfallback.value.ui64++;
2285 
2286 	newsp = sockparams_hold_ephemeral_bydev(so->so_family, so->so_type,
2287 	    so->so_protocol, so->so_sockparams->sp_sdev_info.sd_devpath,
2288 	    KM_SLEEP, &error);
2289 	if (error != 0)
2290 		goto out;
2291 
2292 	if (so->so_direct != NULL) {
2293 		sodirect_t *sodp = so->so_direct;
2294 		mutex_enter(&so->so_lock);
2295 
2296 		so->so_direct->sod_enabled = B_FALSE;
2297 		so->so_state &= ~SS_SODIRECT;
2298 		ASSERT(sodp->sod_uioafh == NULL);
2299 		mutex_exit(&so->so_lock);
2300 	}
2301 
2302 	/* Turn sonode into a TPI socket */
2303 	error = sotpi_convert_sonode(so, newsp, &direct, &q, cr);
2304 	if (error != 0)
2305 		goto out;
2306 
2307 
2308 	/*
2309 	 * Now tell the protocol to start using TPI. so_quiesced_cb be
2310 	 * called once it's safe to synchronize state.
2311 	 */
2312 	DTRACE_PROBE1(proto__fallback__begin, struct sonode *, so);
2313 	error = (*fbfunc)(so->so_proto_handle, q, direct, so_quiesced_cb);
2314 	DTRACE_PROBE1(proto__fallback__end, struct sonode *, so);
2315 
2316 	if (error != 0) {
2317 		/* protocol was unable to do a fallback, revert the sonode */
2318 		sotpi_revert_sonode(so, cr);
2319 		goto out;
2320 	}
2321 
2322 	/*
2323 	 * Walk the accept queue and notify the proto that they should
2324 	 * fall back to TPI. The protocol will send up the T_CONN_IND.
2325 	 */
2326 	nso = so->so_acceptq_head;
2327 	while (nso != NULL) {
2328 		int rval;
2329 
2330 		DTRACE_PROBE1(proto__fallback__begin, struct sonode *, nso);
2331 		rval = (*fbfunc)(nso->so_proto_handle, NULL, direct, NULL);
2332 		DTRACE_PROBE1(proto__fallback__end, struct sonode *, nso);
2333 		if (rval != 0) {
2334 			zcmn_err(getzoneid(), CE_WARN,
2335 			    "Failed to convert socket in accept queue to TPI. "
2336 			    "Pid = %d\n", curproc->p_pid);
2337 		}
2338 		nso = nso->so_acceptq_next;
2339 	}
2340 
2341 	/*
2342 	 * Now flush the acceptq, this will destroy all sockets. They will
2343 	 * be recreated in sotpi_accept().
2344 	 */
2345 	so_acceptq_flush(so, B_FALSE);
2346 
2347 	mutex_enter(&so->so_lock);
2348 	so->so_state |= SS_FALLBACK_COMP;
2349 	mutex_exit(&so->so_lock);
2350 
2351 	/*
2352 	 * Swap the sonode ops. Socket opertations that come in once this
2353 	 * is done will proceed without blocking.
2354 	 */
2355 	so->so_ops = &sotpi_sonodeops;
2356 
2357 	/*
2358 	 * Wake up any threads stuck in poll. This is needed since the poll
2359 	 * head changes when the fallback happens (moves from the sonode to
2360 	 * the STREAMS head).
2361 	 */
2362 	pollwakeup(&so->so_poll_list, POLLERR);
2363 out:
2364 	so_end_fallback(so);
2365 
2366 	if (error != 0) {
2367 #ifdef DEBUG
2368 		so_integrity_check(so, &origso);
2369 #endif
2370 		zcmn_err(getzoneid(), CE_WARN,
2371 		    "Failed to convert socket to TPI (err=%d). Pid = %d\n",
2372 		    error, curproc->p_pid);
2373 		if (newsp != NULL)
2374 			SOCKPARAMS_DEC_REF(newsp);
2375 	}
2376 
2377 	return (error);
2378 }
2379