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