blob: 24a3726687f63207a3cf55a74d132257ed469f31 [file] [log] [blame]
Yu Xiangning0f1702c2008-12-11 20:04:13 -08001/*
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/*
Rao Shoaibbfcb55b2009-01-05 10:51:43 -080023 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
Yu Xiangning0f1702c2008-12-11 20:04:13 -080024 * 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>
Yu Xiangning0f1702c2008-12-11 20:04:13 -080036#include <sys/strsubr.h>
37#include <sys/strsun.h>
38#include <sys/atomic.h>
Anders Persson41174432009-02-12 17:35:05 -080039#include <sys/tihdr.h>
Yu Xiangning0f1702c2008-12-11 20:04:13 -080040
41#include <fs/sockfs/sockcommon.h>
42#include <fs/sockfs/socktpi.h>
Anders Perssonbbc000e2009-04-28 12:10:59 -070043#include <fs/sockfs/sodirect.h>
Yu Xiangning0f1702c2008-12-11 20:04:13 -080044#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
50extern int do_useracc;
51extern clock_t sock_test_timelimit;
52#endif /* SOCK_TEST */
53
54#define MBLK_PULL_LEN 64
55uint32_t so_mblk_pull_len = MBLK_PULL_LEN;
56
57#ifdef DEBUG
58boolean_t so_debug_length = B_FALSE;
59static boolean_t so_check_length(sonode_t *so);
60#endif
61
62int
63so_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 */
90int
91so_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
102static int
103so_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 */
164int
165so_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 */
195void
196so_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
223int
224so_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 */
279int
280so_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
291int
292so_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)
shenjian34dfe682009-01-21 10:04:42 +0800321 return (EAGAIN);
Yu Xiangning0f1702c2008-12-11 20:04:13 -0800322 }
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 */
332int
333so_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
348void
349so_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
356void
357so_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 */
369int
370socket_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*/
396static void
397socket_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
419void
420socket_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 */
470mblk_t *
471socopyinuio(uio_t *uiop, ssize_t iosize, size_t wroff, ssize_t maxblk,
Erik Nordmarkde8c4a12009-02-12 08:42:06 -0800472 size_t tail_len, int *errorp, cred_t *cr)
Yu Xiangning0f1702c2008-12-11 20:04:13 -0800473{
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);
Erik Nordmarkde8c4a12009-02-12 08:42:06 -0800500 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) {
Yu Xiangning0f1702c2008-12-11 20:04:13 -0800506 *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
526done:
527 *errorp = 0;
528 return (head);
529}
530
531mblk_t *
532socopyoutuio(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
575static void
576so_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
Anders Perssone4b767e2009-03-26 17:08:33 -0700602/*
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 */
606void
607so_process_new_message(struct sonode *so, mblk_t *mp_head, mblk_t *mp_last_head)
Yu Xiangning0f1702c2008-12-11 20:04:13 -0800608{
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
Rao Shoaib5795faa2009-07-28 13:53:49 -0700654/*
655 * Check flow control on a given sonode. Must have so_lock held, and
656 * this function will release the hold.
657 */
658
659static void
660so_check_flow_control(struct sonode *so)
661{
662 ASSERT(MUTEX_HELD(&so->so_lock));
663
664 if (so->so_flowctrld && so->so_rcv_queued < so->so_rcvlowat) {
665 so->so_flowctrld = B_FALSE;
666 mutex_exit(&so->so_lock);
667 /*
668 * Open up flow control. SCTP does not have any downcalls, and
669 * it will clr flow ctrl in sosctp_recvmsg().
670 */
671 if (so->so_downcalls != NULL &&
672 so->so_downcalls->sd_clr_flowctrl != NULL) {
673 (*so->so_downcalls->sd_clr_flowctrl)
674 (so->so_proto_handle);
675 }
676 } else {
677 mutex_exit(&so->so_lock);
678 }
679}
680
Yu Xiangning0f1702c2008-12-11 20:04:13 -0800681int
682so_dequeue_msg(struct sonode *so, mblk_t **mctlp, struct uio *uiop,
683 rval_t *rvalp, int flags)
684{
685 mblk_t *mp, *nmp;
686 mblk_t *savemp, *savemptail;
687 mblk_t *new_msg_head;
688 mblk_t *new_msg_last_head;
689 mblk_t *last_tail;
690 boolean_t partial_read;
691 boolean_t reset_atmark = B_FALSE;
692 int more = 0;
693 int error;
694 ssize_t oobmark;
695 sodirect_t *sodp = so->so_direct;
696
697 partial_read = B_FALSE;
698 *mctlp = NULL;
699again:
700 mutex_enter(&so->so_lock);
701again1:
702#ifdef DEBUG
703 if (so_debug_length) {
704 ASSERT(so_check_length(so));
705 }
706#endif
Anders Persson8591a192009-05-29 09:33:18 -0700707 if (so->so_state & SS_RCVATMARK) {
708 /* Check whether the caller is OK to read past the mark */
709 if (flags & MSG_NOMARK) {
710 mutex_exit(&so->so_lock);
711 return (EWOULDBLOCK);
712 }
713 reset_atmark = B_TRUE;
714 }
Yu Xiangning0f1702c2008-12-11 20:04:13 -0800715 /*
716 * First move messages from the dump area to processing area
717 */
718 if (sodp != NULL) {
Anders Perssonbbc000e2009-04-28 12:10:59 -0700719 if (sodp->sod_enabled) {
Yu Xiangning0f1702c2008-12-11 20:04:13 -0800720 if (sodp->sod_uioa.uioa_state & UIOA_ALLOC) {
721 /* nothing to uioamove */
722 sodp = NULL;
723 } else if (sodp->sod_uioa.uioa_state & UIOA_INIT) {
724 sodp->sod_uioa.uioa_state &= UIOA_CLR;
725 sodp->sod_uioa.uioa_state |= UIOA_ENABLED;
726 /*
727 * try to uioamove() the data that
728 * has already queued.
729 */
730 sod_uioa_so_init(so, sodp, uiop);
731 }
732 } else {
733 sodp = NULL;
734 }
735 }
736 new_msg_head = so->so_rcv_head;
737 new_msg_last_head = so->so_rcv_last_head;
738 so->so_rcv_head = NULL;
739 so->so_rcv_last_head = NULL;
740 oobmark = so->so_oobmark;
741 /*
742 * We can release the lock as there can only be one reader
743 */
744 mutex_exit(&so->so_lock);
745
Yu Xiangning0f1702c2008-12-11 20:04:13 -0800746 if (new_msg_head != NULL) {
Anders Perssone4b767e2009-03-26 17:08:33 -0700747 so_process_new_message(so, new_msg_head, new_msg_last_head);
Yu Xiangning0f1702c2008-12-11 20:04:13 -0800748 }
749 savemp = savemptail = NULL;
750 rvalp->r_val1 = 0;
751 error = 0;
752 mp = so->so_rcv_q_head;
753
754 if (mp != NULL &&
755 (so->so_rcv_timer_tid == 0 ||
756 so->so_rcv_queued >= so->so_rcv_thresh)) {
757 partial_read = B_FALSE;
758
759 if (flags & MSG_PEEK) {
760 if ((nmp = dupmsg(mp)) == NULL &&
761 (nmp = copymsg(mp)) == NULL) {
762 size_t size = msgsize(mp);
763
764 error = strwaitbuf(size, BPRI_HI);
765 if (error) {
766 return (error);
767 }
768 goto again;
769 }
770 mp = nmp;
771 } else {
772 ASSERT(mp->b_prev != NULL);
773 last_tail = mp->b_prev;
774 mp->b_prev = NULL;
775 so->so_rcv_q_head = mp->b_next;
776 if (so->so_rcv_q_head == NULL) {
777 so->so_rcv_q_last_head = NULL;
778 }
779 mp->b_next = NULL;
780 }
781
782 ASSERT(mctlp != NULL);
783 /*
784 * First process PROTO or PCPROTO blocks, if any.
785 */
786 if (DB_TYPE(mp) != M_DATA) {
787 *mctlp = mp;
788 savemp = mp;
789 savemptail = mp;
790 ASSERT(DB_TYPE(mp) == M_PROTO ||
791 DB_TYPE(mp) == M_PCPROTO);
792 while (mp->b_cont != NULL &&
793 DB_TYPE(mp->b_cont) != M_DATA) {
794 ASSERT(DB_TYPE(mp->b_cont) == M_PROTO ||
795 DB_TYPE(mp->b_cont) == M_PCPROTO);
796 mp = mp->b_cont;
797 savemptail = mp;
798 }
799 mp = savemptail->b_cont;
800 savemptail->b_cont = NULL;
801 }
802
803 ASSERT(DB_TYPE(mp) == M_DATA);
804 /*
805 * Now process DATA blocks, if any. Note that for sodirect
806 * enabled socket, uio_resid can be 0.
807 */
808 if (uiop->uio_resid >= 0) {
809 ssize_t copied = 0;
810
811 if (sodp != NULL && (DB_FLAGS(mp) & DBLK_UIOA)) {
Anders Perssonbbc000e2009-04-28 12:10:59 -0700812 mutex_enter(&so->so_lock);
Yu Xiangning0f1702c2008-12-11 20:04:13 -0800813 ASSERT(uiop == (uio_t *)&sodp->sod_uioa);
814 copied = sod_uioa_mblk(so, mp);
815 if (copied > 0)
816 partial_read = B_TRUE;
Anders Perssonbbc000e2009-04-28 12:10:59 -0700817 mutex_exit(&so->so_lock);
Yu Xiangning0f1702c2008-12-11 20:04:13 -0800818 /* mark this mblk as processed */
819 mp = NULL;
820 } else {
821 ssize_t oldresid = uiop->uio_resid;
822
823 if (MBLKL(mp) < so_mblk_pull_len) {
824 if (pullupmsg(mp, -1) == 1) {
825 last_tail = mp;
826 }
827 }
828 /*
829 * Can not read beyond the oobmark
830 */
831 mp = socopyoutuio(mp, uiop,
832 oobmark == 0 ? INFPSZ : oobmark, &error);
833 if (error != 0) {
834 freemsg(*mctlp);
835 *mctlp = NULL;
836 more = 0;
837 goto done;
838 }
839 ASSERT(oldresid >= uiop->uio_resid);
840 copied = oldresid - uiop->uio_resid;
841 if (oldresid > uiop->uio_resid)
842 partial_read = B_TRUE;
843 }
844 ASSERT(copied >= 0);
845 if (copied > 0 && !(flags & MSG_PEEK)) {
846 mutex_enter(&so->so_lock);
847 so->so_rcv_queued -= copied;
848 ASSERT(so->so_oobmark >= 0);
849 if (so->so_oobmark > 0) {
850 so->so_oobmark -= copied;
851 ASSERT(so->so_oobmark >= 0);
852 if (so->so_oobmark == 0) {
853 ASSERT(so->so_state &
854 SS_OOBPEND);
855 so->so_oobmark = 0;
856 so->so_state |= SS_RCVATMARK;
857 }
858 }
Rao Shoaib5795faa2009-07-28 13:53:49 -0700859 /*
860 * so_check_flow_control() will drop
861 * so->so_lock.
862 */
863 so_check_flow_control(so);
Yu Xiangning0f1702c2008-12-11 20:04:13 -0800864 }
865 }
866 if (mp != NULL) { /* more data blocks in msg */
867 more |= MOREDATA;
868 if ((flags & (MSG_PEEK|MSG_TRUNC))) {
Rao Shoaib5795faa2009-07-28 13:53:49 -0700869 if (flags & MSG_PEEK) {
870 freemsg(mp);
871 } else {
872 unsigned int msize = msgdsize(mp);
873
874 freemsg(mp);
Yu Xiangning0f1702c2008-12-11 20:04:13 -0800875 mutex_enter(&so->so_lock);
Rao Shoaib5795faa2009-07-28 13:53:49 -0700876 so->so_rcv_queued -= msize;
877 /*
878 * so_check_flow_control() will drop
879 * so->so_lock.
880 */
881 so_check_flow_control(so);
Yu Xiangning0f1702c2008-12-11 20:04:13 -0800882 }
Yu Xiangning0f1702c2008-12-11 20:04:13 -0800883 } else if (partial_read && !somsghasdata(mp)) {
884 /*
885 * Avoid queuing a zero-length tail part of
886 * a message. partial_read == 1 indicates that
887 * we read some of the message.
888 */
889 freemsg(mp);
890 more &= ~MOREDATA;
891 } else {
892 if (savemp != NULL &&
893 (flags & MSG_DUPCTRL)) {
894 mblk_t *nmp;
895 /*
896 * There should only be non data mblks
897 */
898 ASSERT(DB_TYPE(savemp) != M_DATA &&
899 DB_TYPE(savemptail) != M_DATA);
900try_again:
901 if ((nmp = dupmsg(savemp)) == NULL &&
902 (nmp = copymsg(savemp)) == NULL) {
903
904 size_t size = msgsize(savemp);
905
906 error = strwaitbuf(size,
907 BPRI_HI);
908 if (error != 0) {
909 /*
910 * In case we
911 * cannot copy
912 * control data
913 * free the remaining
914 * data.
915 */
916 freemsg(mp);
917 goto done;
918 }
919 goto try_again;
920 }
921
922 ASSERT(nmp != NULL);
923 ASSERT(DB_TYPE(nmp) != M_DATA);
924 savemptail->b_cont = mp;
925 *mctlp = nmp;
926 mp = savemp;
927 }
928 /*
929 * putback mp
930 */
931 so_prepend_msg(so, mp, last_tail);
932 }
933 }
934
935 /* fast check so_rcv_head if there is more data */
936 if (partial_read && !(so->so_state & SS_RCVATMARK) &&
937 *mctlp == NULL && uiop->uio_resid > 0 &&
938 !(flags & MSG_PEEK) && so->so_rcv_head != NULL) {
939 goto again;
940 }
941 } else if (!partial_read) {
942 mutex_enter(&so->so_lock);
943 if (so->so_error != 0) {
944 error = sogeterr(so, !(flags & MSG_PEEK));
945 mutex_exit(&so->so_lock);
946 return (error);
947 }
948 /*
949 * No pending data. Return right away for nonblocking
950 * socket, otherwise sleep waiting for data.
951 */
Mike Cheng2caa6592008-12-29 14:01:03 +0800952 if (!(so->so_state & SS_CANTRCVMORE) && uiop->uio_resid > 0) {
Yu Xiangning0f1702c2008-12-11 20:04:13 -0800953 if ((uiop->uio_fmode & (FNDELAY|FNONBLOCK)) ||
954 (flags & MSG_DONTWAIT)) {
955 error = EWOULDBLOCK;
956 } else {
957 if (so->so_state & (SS_CLOSING |
958 SS_FALLBACK_PENDING)) {
959 mutex_exit(&so->so_lock);
960 error = EINTR;
961 goto done;
962 }
963
964 if (so->so_rcv_head != NULL) {
965 goto again1;
966 }
967 so->so_rcv_wakeup = B_TRUE;
968 so->so_rcv_wanted = uiop->uio_resid;
969 if (so->so_rcvtimeo == 0) {
970 /*
971 * Zero means disable timeout.
972 */
973 error = cv_wait_sig(&so->so_rcv_cv,
974 &so->so_lock);
975 } else {
976 clock_t now;
977 time_to_wait(&now, so->so_rcvtimeo);
978 error = cv_timedwait_sig(&so->so_rcv_cv,
979 &so->so_lock, now);
980 }
981 so->so_rcv_wakeup = B_FALSE;
982 so->so_rcv_wanted = 0;
983
984 if (error == 0) {
985 error = EINTR;
986 } else if (error == -1) {
shenjian34dfe682009-01-21 10:04:42 +0800987 error = EAGAIN;
Yu Xiangning0f1702c2008-12-11 20:04:13 -0800988 } else {
989 goto again1;
990 }
991 }
992 }
993 mutex_exit(&so->so_lock);
994 }
995 if (reset_atmark && partial_read && !(flags & MSG_PEEK)) {
996 /*
997 * We are passed the mark, update state
998 * 4.3BSD and 4.4BSD clears the mark when peeking across it.
999 * The draft Posix socket spec states that the mark should
1000 * not be cleared when peeking. We follow the latter.
1001 */
1002 mutex_enter(&so->so_lock);
1003 ASSERT(so_verify_oobstate(so));
1004 so->so_state &= ~(SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK);
1005 freemsg(so->so_oobmsg);
1006 so->so_oobmsg = NULL;
1007 ASSERT(so_verify_oobstate(so));
1008 mutex_exit(&so->so_lock);
1009 }
1010 ASSERT(so->so_rcv_wakeup == B_FALSE);
1011done:
1012 if (sodp != NULL) {
Anders Perssonbbc000e2009-04-28 12:10:59 -07001013 mutex_enter(&so->so_lock);
1014 if (sodp->sod_enabled &&
Yu Xiangning0f1702c2008-12-11 20:04:13 -08001015 (sodp->sod_uioa.uioa_state & UIOA_ENABLED)) {
1016 SOD_UIOAFINI(sodp);
1017 if (sodp->sod_uioa.uioa_mbytes > 0) {
1018 ASSERT(so->so_rcv_q_head != NULL ||
1019 so->so_rcv_head != NULL);
1020 so->so_rcv_queued -= sod_uioa_mblk(so, NULL);
1021 if (error == EWOULDBLOCK)
1022 error = 0;
1023 }
1024 }
Anders Perssonbbc000e2009-04-28 12:10:59 -07001025 mutex_exit(&so->so_lock);
Yu Xiangning0f1702c2008-12-11 20:04:13 -08001026 }
1027#ifdef DEBUG
1028 if (so_debug_length) {
1029 mutex_enter(&so->so_lock);
1030 ASSERT(so_check_length(so));
1031 mutex_exit(&so->so_lock);
1032 }
1033#endif
1034 rvalp->r_val1 = more;
Rao Shoaib5795faa2009-07-28 13:53:49 -07001035 ASSERT(MUTEX_NOT_HELD(&so->so_lock));
Yu Xiangning0f1702c2008-12-11 20:04:13 -08001036 return (error);
1037}
1038
Anders Perssone4b767e2009-03-26 17:08:33 -07001039/*
1040 * Enqueue data from the protocol on the socket's rcv queue.
1041 *
1042 * We try to hook new M_DATA mblks onto an existing chain, however,
1043 * that cannot be done if the existing chain has already been
1044 * processed by I/OAT. Non-M_DATA mblks are just linked together via
1045 * b_next. In all cases the b_prev of the enqueued mblk is set to
1046 * point to the last mblk in its b_cont chain.
1047 */
Yu Xiangning0f1702c2008-12-11 20:04:13 -08001048void
1049so_enqueue_msg(struct sonode *so, mblk_t *mp, size_t msg_size)
1050{
1051 ASSERT(MUTEX_HELD(&so->so_lock));
1052
1053#ifdef DEBUG
1054 if (so_debug_length) {
1055 ASSERT(so_check_length(so));
1056 }
1057#endif
1058 so->so_rcv_queued += msg_size;
1059
1060 if (so->so_rcv_head == NULL) {
1061 ASSERT(so->so_rcv_last_head == NULL);
1062 so->so_rcv_head = mp;
1063 so->so_rcv_last_head = mp;
1064 } else if ((DB_TYPE(mp) == M_DATA &&
1065 DB_TYPE(so->so_rcv_last_head) == M_DATA) &&
1066 ((DB_FLAGS(mp) & DBLK_UIOA) ==
1067 (DB_FLAGS(so->so_rcv_last_head) & DBLK_UIOA))) {
1068 /* Added to the end */
1069 ASSERT(so->so_rcv_last_head != NULL);
1070 ASSERT(so->so_rcv_last_head->b_prev != NULL);
1071 so->so_rcv_last_head->b_prev->b_cont = mp;
1072 } else {
1073 /* Start a new end */
1074 so->so_rcv_last_head->b_next = mp;
1075 so->so_rcv_last_head = mp;
1076 }
1077 while (mp->b_cont != NULL)
1078 mp = mp->b_cont;
1079
1080 so->so_rcv_last_head->b_prev = mp;
1081#ifdef DEBUG
1082 if (so_debug_length) {
1083 ASSERT(so_check_length(so));
1084 }
1085#endif
1086}
1087
1088/*
1089 * Return B_TRUE if there is data in the message, B_FALSE otherwise.
1090 */
1091boolean_t
1092somsghasdata(mblk_t *mp)
1093{
1094 for (; mp; mp = mp->b_cont)
1095 if (mp->b_datap->db_type == M_DATA) {
1096 ASSERT(mp->b_wptr >= mp->b_rptr);
1097 if (mp->b_wptr > mp->b_rptr)
1098 return (B_TRUE);
1099 }
1100 return (B_FALSE);
1101}
1102
1103/*
1104 * Flush the read side of sockfs.
1105 *
1106 * The caller must be sure that a reader is not already active when the
1107 * buffer is being flushed.
1108 */
1109void
1110so_rcv_flush(struct sonode *so)
1111{
1112 mblk_t *mp;
1113
1114 ASSERT(MUTEX_HELD(&so->so_lock));
1115
1116 if (so->so_oobmsg != NULL) {
1117 freemsg(so->so_oobmsg);
1118 so->so_oobmsg = NULL;
1119 so->so_oobmark = 0;
1120 so->so_state &=
1121 ~(SS_OOBPEND|SS_HAVEOOBDATA|SS_HADOOBDATA|SS_RCVATMARK);
1122 }
1123
1124 /*
1125 * Free messages sitting in the send and recv queue
1126 */
1127 while (so->so_rcv_q_head != NULL) {
1128 mp = so->so_rcv_q_head;
1129 so->so_rcv_q_head = mp->b_next;
1130 mp->b_next = mp->b_prev = NULL;
1131 freemsg(mp);
1132 }
1133 while (so->so_rcv_head != NULL) {
1134 mp = so->so_rcv_head;
1135 so->so_rcv_head = mp->b_next;
1136 mp->b_next = mp->b_prev = NULL;
1137 freemsg(mp);
1138 }
1139 so->so_rcv_queued = 0;
1140 so->so_rcv_q_head = NULL;
1141 so->so_rcv_q_last_head = NULL;
1142 so->so_rcv_head = NULL;
1143 so->so_rcv_last_head = NULL;
1144}
1145
1146/*
1147 * Handle recv* calls that set MSG_OOB or MSG_OOB together with MSG_PEEK.
1148 */
1149int
1150sorecvoob(struct sonode *so, struct nmsghdr *msg, struct uio *uiop, int flags,
1151 boolean_t oob_inline)
1152{
1153 mblk_t *mp, *nmp;
1154 int error;
1155
1156 dprintso(so, 1, ("sorecvoob(%p, %p, 0x%x)\n", (void *)so, (void *)msg,
1157 flags));
1158
1159 if (msg != NULL) {
1160 /*
1161 * There is never any oob data with addresses or control since
1162 * the T_EXDATA_IND does not carry any options.
1163 */
1164 msg->msg_controllen = 0;
1165 msg->msg_namelen = 0;
1166 msg->msg_flags = 0;
1167 }
1168
1169 mutex_enter(&so->so_lock);
1170 ASSERT(so_verify_oobstate(so));
1171 if (oob_inline ||
1172 (so->so_state & (SS_OOBPEND|SS_HADOOBDATA)) != SS_OOBPEND) {
1173 dprintso(so, 1, ("sorecvoob: inline or data consumed\n"));
1174 mutex_exit(&so->so_lock);
1175 return (EINVAL);
1176 }
1177 if (!(so->so_state & SS_HAVEOOBDATA)) {
1178 dprintso(so, 1, ("sorecvoob: no data yet\n"));
1179 mutex_exit(&so->so_lock);
1180 return (EWOULDBLOCK);
1181 }
1182 ASSERT(so->so_oobmsg != NULL);
1183 mp = so->so_oobmsg;
1184 if (flags & MSG_PEEK) {
1185 /*
1186 * Since recv* can not return ENOBUFS we can not use dupmsg.
1187 * Instead we revert to the consolidation private
1188 * allocb_wait plus bcopy.
1189 */
1190 mblk_t *mp1;
1191
1192 mp1 = allocb_wait(msgdsize(mp), BPRI_MED, STR_NOSIG, NULL);
1193 ASSERT(mp1);
1194
1195 while (mp != NULL) {
1196 ssize_t size;
1197
1198 size = MBLKL(mp);
1199 bcopy(mp->b_rptr, mp1->b_wptr, size);
1200 mp1->b_wptr += size;
1201 ASSERT(mp1->b_wptr <= mp1->b_datap->db_lim);
1202 mp = mp->b_cont;
1203 }
1204 mp = mp1;
1205 } else {
1206 /*
1207 * Update the state indicating that the data has been consumed.
1208 * Keep SS_OOBPEND set until data is consumed past the mark.
1209 */
1210 so->so_oobmsg = NULL;
1211 so->so_state ^= SS_HAVEOOBDATA|SS_HADOOBDATA;
1212 }
1213 ASSERT(so_verify_oobstate(so));
1214 mutex_exit(&so->so_lock);
1215
1216 error = 0;
1217 nmp = mp;
1218 while (nmp != NULL && uiop->uio_resid > 0) {
1219 ssize_t n = MBLKL(nmp);
1220
1221 n = MIN(n, uiop->uio_resid);
1222 if (n > 0)
1223 error = uiomove(nmp->b_rptr, n,
1224 UIO_READ, uiop);
1225 if (error)
1226 break;
1227 nmp = nmp->b_cont;
1228 }
1229 ASSERT(mp->b_next == NULL && mp->b_prev == NULL);
1230 freemsg(mp);
1231 return (error);
1232}
1233
1234/*
1235 * Allocate and initializ sonode
1236 */
1237/* ARGSUSED */
1238struct sonode *
1239socket_sonode_create(struct sockparams *sp, int family, int type,
1240 int protocol, int version, int sflags, int *errorp, struct cred *cr)
1241{
1242 sonode_t *so;
1243 int kmflags;
1244
1245 /*
1246 * Choose the right set of sonodeops based on the upcall and
1247 * down call version that the protocol has provided
1248 */
1249 if (SOCK_UC_VERSION != sp->sp_smod_info->smod_uc_version ||
1250 SOCK_DC_VERSION != sp->sp_smod_info->smod_dc_version) {
1251 /*
1252 * mismatch
1253 */
1254#ifdef DEBUG
1255 cmn_err(CE_CONT, "protocol and socket module version mismatch");
1256#endif
1257 *errorp = EINVAL;
1258 return (NULL);
1259 }
1260
1261 kmflags = (sflags & SOCKET_NOSLEEP) ? KM_NOSLEEP : KM_SLEEP;
1262
1263 so = kmem_cache_alloc(socket_cache, kmflags);
1264 if (so == NULL) {
1265 *errorp = ENOMEM;
1266 return (NULL);
1267 }
1268
1269 sonode_init(so, sp, family, type, protocol, &so_sonodeops);
1270
1271 if (version == SOV_DEFAULT)
1272 version = so_default_version;
1273
1274 so->so_version = (short)version;
1275
1276 /*
1277 * set the default values to be INFPSZ
1278 * if a protocol desires it can change the value later
1279 */
1280 so->so_proto_props.sopp_rxhiwat = SOCKET_RECVHIWATER;
1281 so->so_proto_props.sopp_rxlowat = SOCKET_RECVLOWATER;
1282 so->so_proto_props.sopp_maxpsz = INFPSZ;
1283 so->so_proto_props.sopp_maxblk = INFPSZ;
1284
1285 return (so);
1286}
1287
1288int
1289socket_init_common(struct sonode *so, struct sonode *pso, int flags, cred_t *cr)
1290{
1291 int error = 0;
1292
1293 if (pso != NULL) {
1294 /*
1295 * We have a passive open, so inherit basic state from
1296 * the parent (listener).
1297 *
1298 * No need to grab the new sonode's lock, since there is no
1299 * one that can have a reference to it.
1300 */
1301 mutex_enter(&pso->so_lock);
1302
1303 so->so_state |= SS_ISCONNECTED | (pso->so_state & SS_ASYNC);
1304 so->so_pgrp = pso->so_pgrp;
1305 so->so_rcvtimeo = pso->so_rcvtimeo;
1306 so->so_sndtimeo = pso->so_sndtimeo;
Yu Xiangninga5adac42008-12-29 13:56:29 +08001307 so->so_xpg_rcvbuf = pso->so_xpg_rcvbuf;
Yu Xiangning0f1702c2008-12-11 20:04:13 -08001308 /*
1309 * Make note of the socket level options. TCP and IP level
1310 * options are already inherited. We could do all this after
1311 * accept is successful but doing it here simplifies code and
1312 * no harm done for error case.
1313 */
1314 so->so_options = pso->so_options & (SO_DEBUG|SO_REUSEADDR|
Yu Xiangninga5adac42008-12-29 13:56:29 +08001315 SO_KEEPALIVE|SO_DONTROUTE|SO_BROADCAST|SO_USELOOPBACK|
Yu Xiangning0f1702c2008-12-11 20:04:13 -08001316 SO_OOBINLINE|SO_DGRAM_ERRIND|SO_LINGER);
1317 so->so_proto_props = pso->so_proto_props;
1318 so->so_mode = pso->so_mode;
andersf0267582008-12-20 22:46:32 -08001319 so->so_pollev = pso->so_pollev & SO_POLLEV_ALWAYS;
Yu Xiangning0f1702c2008-12-11 20:04:13 -08001320
1321 mutex_exit(&pso->so_lock);
Yu Xiangning0f1702c2008-12-11 20:04:13 -08001322 } else {
1323 struct sockparams *sp = so->so_sockparams;
1324 sock_upcalls_t *upcalls_to_use;
1325
1326 /*
1327 * Based on the version number select the right upcalls to
1328 * pass down. Currently we only have one version so choose
1329 * default
1330 */
1331 upcalls_to_use = &so_upcalls;
1332
1333 /* active open, so create a lower handle */
1334 so->so_proto_handle =
1335 sp->sp_smod_info->smod_proto_create_func(so->so_family,
1336 so->so_type, so->so_protocol, &so->so_downcalls,
1337 &so->so_mode, &error, flags, cr);
1338
1339 if (so->so_proto_handle == NULL) {
1340 ASSERT(error != 0);
1341 /*
1342 * To be safe; if a lower handle cannot be created, and
1343 * the proto does not give a reason why, assume there
1344 * was a lack of memory.
1345 */
1346 return ((error == 0) ? ENOMEM : error);
1347 }
1348 ASSERT(so->so_downcalls != NULL);
1349 ASSERT(so->so_downcalls->sd_send != NULL ||
1350 so->so_downcalls->sd_send_uio != NULL);
1351 if (so->so_downcalls->sd_recv_uio != NULL) {
1352 ASSERT(so->so_downcalls->sd_poll != NULL);
1353 so->so_pollev |= SO_POLLEV_ALWAYS;
1354 }
1355
1356 (*so->so_downcalls->sd_activate)(so->so_proto_handle,
1357 (sock_upper_handle_t)so, upcalls_to_use, 0, cr);
1358
1359 /* Wildcard */
1360
1361 /*
1362 * FIXME No need for this, the protocol can deal with it in
1363 * sd_create(). Should update ICMP.
1364 */
1365 if (so->so_protocol != so->so_sockparams->sp_protocol) {
1366 int protocol = so->so_protocol;
1367 int error;
1368 /*
1369 * Issue SO_PROTOTYPE setsockopt.
1370 */
1371 error = socket_setsockopt(so, SOL_SOCKET, SO_PROTOTYPE,
1372 &protocol, (t_uscalar_t)sizeof (protocol), cr);
1373 if (error) {
1374 (void) (*so->so_downcalls->sd_close)
1375 (so->so_proto_handle, 0, cr);
1376
1377 mutex_enter(&so->so_lock);
1378 so_rcv_flush(so);
1379 mutex_exit(&so->so_lock);
1380 /*
1381 * Setsockopt often fails with ENOPROTOOPT but
1382 * socket() should fail with
1383 * EPROTONOSUPPORT/EPROTOTYPE.
1384 */
1385 return (EPROTONOSUPPORT);
1386 }
1387 }
Yu Xiangning0f1702c2008-12-11 20:04:13 -08001388 }
Anders Perssonbbc000e2009-04-28 12:10:59 -07001389
1390 if (uioasync.enabled)
1391 sod_sock_init(so);
1392
1393 return (0);
Yu Xiangning0f1702c2008-12-11 20:04:13 -08001394}
1395
1396/*
1397 * int socket_ioctl_common(struct sonode *so, int cmd, intptr_t arg, int mode,
1398 * struct cred *cr, int32_t *rvalp)
1399 *
1400 * Handle ioctls that manipulate basic socket state; non-blocking,
1401 * async, etc.
1402 *
1403 * Returns:
1404 * < 0 - ioctl was not handle
1405 * >= 0 - ioctl was handled, if > 0, then it is an errno
1406 *
1407 * Notes:
1408 * Assumes the standard receive buffer is used to obtain info for
1409 * NREAD.
1410 */
1411/* ARGSUSED */
1412int
1413socket_ioctl_common(struct sonode *so, int cmd, intptr_t arg, int mode,
1414 struct cred *cr, int32_t *rvalp)
1415{
1416 switch (cmd) {
Rao Shoaibbfcb55b2009-01-05 10:51:43 -08001417 case SIOCSQPTR:
1418 /*
1419 * SIOCSQPTR is valid only when helper stream is created
1420 * by the protocol.
1421 */
1422
1423 return (EOPNOTSUPP);
Yu Xiangning0f1702c2008-12-11 20:04:13 -08001424 case FIONBIO: {
1425 int32_t value;
1426
1427 if (so_copyin((void *)arg, &value, sizeof (int32_t),
1428 (mode & (int)FKIOCTL)))
1429 return (EFAULT);
1430
1431 mutex_enter(&so->so_lock);
1432 if (value) {
1433 so->so_state |= SS_NDELAY;
1434 } else {
1435 so->so_state &= ~SS_NDELAY;
1436 }
1437 mutex_exit(&so->so_lock);
1438 return (0);
1439 }
1440 case FIOASYNC: {
1441 int32_t value;
1442
1443 if (so_copyin((void *)arg, &value, sizeof (int32_t),
1444 (mode & (int)FKIOCTL)))
1445 return (EFAULT);
1446
1447 mutex_enter(&so->so_lock);
1448
1449 if (value) {
1450 /* Turn on SIGIO */
1451 so->so_state |= SS_ASYNC;
1452 } else {
1453 /* Turn off SIGIO */
1454 so->so_state &= ~SS_ASYNC;
1455 }
1456 mutex_exit(&so->so_lock);
1457
1458 return (0);
1459 }
1460
1461 case SIOCSPGRP:
1462 case FIOSETOWN: {
1463 int error;
1464 pid_t pid;
1465
1466 if (so_copyin((void *)arg, &pid, sizeof (pid_t),
1467 (mode & (int)FKIOCTL)))
1468 return (EFAULT);
1469
1470 mutex_enter(&so->so_lock);
1471 error = (pid != so->so_pgrp) ? socket_chgpgrp(so, pid) : 0;
1472 mutex_exit(&so->so_lock);
1473 return (error);
1474 }
1475 case SIOCGPGRP:
1476 case FIOGETOWN:
1477 if (so_copyout(&so->so_pgrp, (void *)arg,
1478 sizeof (pid_t), (mode & (int)FKIOCTL)))
1479 return (EFAULT);
1480
1481 return (0);
1482 case SIOCATMARK: {
1483 int retval;
1484
1485 /*
1486 * Only protocols that support urgent data can handle ATMARK.
1487 */
1488 if ((so->so_mode & SM_EXDATA) == 0)
1489 return (EINVAL);
1490
1491 /*
1492 * If the protocol is maintaining its own buffer, then the
1493 * request must be passed down.
1494 */
1495 if (so->so_downcalls->sd_recv_uio != NULL)
1496 return (-1);
1497
1498 retval = (so->so_state & SS_RCVATMARK) != 0;
1499
1500 if (so_copyout(&retval, (void *)arg, sizeof (int),
1501 (mode & (int)FKIOCTL))) {
1502 return (EFAULT);
1503 }
1504 return (0);
1505 }
1506
1507 case FIONREAD: {
1508 int retval;
1509
1510 /*
1511 * If the protocol is maintaining its own buffer, then the
1512 * request must be passed down.
1513 */
1514 if (so->so_downcalls->sd_recv_uio != NULL)
1515 return (-1);
1516
1517 retval = MIN(so->so_rcv_queued, INT_MAX);
1518
1519 if (so_copyout(&retval, (void *)arg,
1520 sizeof (retval), (mode & (int)FKIOCTL))) {
1521 return (EFAULT);
1522 }
1523 return (0);
1524 }
1525
1526 case _I_GETPEERCRED: {
1527 int error = 0;
1528
1529 if ((mode & FKIOCTL) == 0)
1530 return (EINVAL);
1531
1532 mutex_enter(&so->so_lock);
1533 if ((so->so_mode & SM_CONNREQUIRED) == 0) {
1534 error = ENOTSUP;
1535 } else if ((so->so_state & SS_ISCONNECTED) == 0) {
1536 error = ENOTCONN;
1537 } else if (so->so_peercred != NULL) {
1538 k_peercred_t *kp = (k_peercred_t *)arg;
1539 kp->pc_cr = so->so_peercred;
1540 kp->pc_cpid = so->so_cpid;
1541 crhold(so->so_peercred);
1542 } else {
1543 error = EINVAL;
1544 }
1545 mutex_exit(&so->so_lock);
1546 return (error);
1547 }
1548 default:
1549 return (-1);
1550 }
1551}
1552
1553/*
Anders Persson41174432009-02-12 17:35:05 -08001554 * Handle the I_NREAD STREAM ioctl.
1555 */
1556static int
1557so_strioc_nread(struct sonode *so, intptr_t arg, int mode, int32_t *rvalp)
1558{
1559 size_t size = 0;
1560 int retval;
1561 int count = 0;
1562 mblk_t *mp;
1563
1564 if (so->so_downcalls == NULL ||
1565 so->so_downcalls->sd_recv_uio != NULL)
1566 return (EINVAL);
1567
1568 mutex_enter(&so->so_lock);
1569 /* Wait for reader to get out of the way. */
1570 while (so->so_flag & SOREADLOCKED) {
1571 /*
1572 * If reader is waiting for data, then there should be nothing
1573 * on the rcv queue.
1574 */
1575 if (so->so_rcv_wakeup)
1576 goto out;
1577
1578 so->so_flag |= SOWANT;
1579 /* Do a timed sleep, in case the reader goes to sleep. */
1580 (void) cv_timedwait(&so->so_state_cv, &so->so_lock,
1581 lbolt + drv_usectohz(10));
1582 }
1583
1584 /*
1585 * Since we are holding so_lock no new reader will come in, and the
1586 * protocol will not be able to enqueue data. So it's safe to walk
1587 * both rcv queues.
1588 */
1589 mp = so->so_rcv_q_head;
1590 if (mp != NULL) {
1591 size = msgdsize(so->so_rcv_q_head);
1592 for (; mp != NULL; mp = mp->b_next)
1593 count++;
1594 } else {
1595 /*
1596 * In case the processing list was empty, get the size of the
1597 * next msg in line.
1598 */
1599 size = msgdsize(so->so_rcv_head);
1600 }
1601
1602 for (mp = so->so_rcv_head; mp != NULL; mp = mp->b_next)
1603 count++;
1604out:
1605 mutex_exit(&so->so_lock);
1606
1607 /*
1608 * Drop down from size_t to the "int" required by the
1609 * interface. Cap at INT_MAX.
1610 */
1611 retval = MIN(size, INT_MAX);
1612 if (so_copyout(&retval, (void *)arg, sizeof (retval),
1613 (mode & (int)FKIOCTL))) {
1614 return (EFAULT);
1615 } else {
1616 *rvalp = count;
1617 return (0);
1618 }
1619}
1620
1621/*
1622 * Process STREAM ioctls.
Yu Xiangning0f1702c2008-12-11 20:04:13 -08001623 *
1624 * Returns:
1625 * < 0 - ioctl was not handle
1626 * >= 0 - ioctl was handled, if > 0, then it is an errno
1627 */
1628int
1629socket_strioc_common(struct sonode *so, int cmd, intptr_t arg, int mode,
1630 struct cred *cr, int32_t *rvalp)
1631{
Anders Persson41174432009-02-12 17:35:05 -08001632 int retval;
1633
1634 /* Only STREAM iotcls are handled here */
1635 if ((cmd & 0xffffff00U) != STR)
1636 return (-1);
1637
Yu Xiangning0f1702c2008-12-11 20:04:13 -08001638 switch (cmd) {
Anders Persson41174432009-02-12 17:35:05 -08001639 case I_CANPUT:
1640 /*
1641 * We return an error for I_CANPUT so that isastream(3C) will
1642 * not report the socket as being a STREAM.
1643 */
Yu Xiangning0f1702c2008-12-11 20:04:13 -08001644 return (EOPNOTSUPP);
Anders Persson41174432009-02-12 17:35:05 -08001645 case I_NREAD:
1646 /* Avoid doing a fallback for I_NREAD. */
1647 return (so_strioc_nread(so, arg, mode, rvalp));
Yu Xiangning0f1702c2008-12-11 20:04:13 -08001648 case I_LOOK:
Anders Persson41174432009-02-12 17:35:05 -08001649 /* Avoid doing a fallback for I_LOOK. */
Yu Xiangning0f1702c2008-12-11 20:04:13 -08001650 if (so_copyout("sockmod", (void *)arg, strlen("sockmod") + 1,
1651 (mode & (int)FKIOCTL))) {
1652 return (EFAULT);
1653 }
1654 return (0);
1655 default:
Anders Persson41174432009-02-12 17:35:05 -08001656 break;
1657 }
1658
1659 /*
1660 * Try to fall back to TPI, and if successful, reissue the ioctl.
1661 */
1662 if ((retval = so_tpi_fallback(so, cr)) == 0) {
1663 /* Reissue the ioctl */
1664 ASSERT(so->so_rcv_q_head == NULL);
1665 return (SOP_IOCTL(so, cmd, arg, mode, cr, rvalp));
1666 } else {
1667 return (retval);
Yu Xiangning0f1702c2008-12-11 20:04:13 -08001668 }
1669}
1670
Anders Persson2c632ad2009-10-21 19:52:57 -07001671/*
1672 * This is called for all socket types to verify that the buffer size is large
1673 * enough for the option, and if we can, handle the request as well. Most
1674 * options will be forwarded to the protocol.
1675 */
Yu Xiangning0f1702c2008-12-11 20:04:13 -08001676int
1677socket_getopt_common(struct sonode *so, int level, int option_name,
Yu Xiangninga5adac42008-12-29 13:56:29 +08001678 void *optval, socklen_t *optlenp, int flags)
Yu Xiangning0f1702c2008-12-11 20:04:13 -08001679{
1680 if (level != SOL_SOCKET)
1681 return (-1);
1682
1683 switch (option_name) {
1684 case SO_ERROR:
1685 case SO_DOMAIN:
1686 case SO_TYPE:
1687 case SO_ACCEPTCONN: {
1688 int32_t value;
1689 socklen_t optlen = *optlenp;
1690
1691 if (optlen < (t_uscalar_t)sizeof (int32_t)) {
1692 return (EINVAL);
1693 }
1694
1695 switch (option_name) {
1696 case SO_ERROR:
1697 mutex_enter(&so->so_lock);
1698 value = sogeterr(so, B_TRUE);
1699 mutex_exit(&so->so_lock);
1700 break;
1701 case SO_DOMAIN:
1702 value = so->so_family;
1703 break;
1704 case SO_TYPE:
1705 value = so->so_type;
1706 break;
1707 case SO_ACCEPTCONN:
1708 if (so->so_state & SS_ACCEPTCONN)
1709 value = SO_ACCEPTCONN;
1710 else
1711 value = 0;
1712 break;
1713 }
1714
1715 bcopy(&value, optval, sizeof (value));
1716 *optlenp = sizeof (value);
1717
1718 return (0);
1719 }
1720 case SO_SNDTIMEO:
1721 case SO_RCVTIMEO: {
1722 clock_t value;
1723 socklen_t optlen = *optlenp;
shenjiane5083e82009-01-20 14:46:11 +08001724
1725 if (get_udatamodel() == DATAMODEL_NONE ||
1726 get_udatamodel() == DATAMODEL_NATIVE) {
shenjian22238f72009-01-07 13:45:08 +08001727 if (optlen < sizeof (struct timeval))
1728 return (EINVAL);
1729 } else {
1730 if (optlen < sizeof (struct timeval32))
1731 return (EINVAL);
Yu Xiangning0f1702c2008-12-11 20:04:13 -08001732 }
1733 if (option_name == SO_RCVTIMEO)
1734 value = drv_hztousec(so->so_rcvtimeo);
1735 else
1736 value = drv_hztousec(so->so_sndtimeo);
shenjian22238f72009-01-07 13:45:08 +08001737
shenjiane5083e82009-01-20 14:46:11 +08001738 if (get_udatamodel() == DATAMODEL_NONE ||
1739 get_udatamodel() == DATAMODEL_NATIVE) {
shenjian22238f72009-01-07 13:45:08 +08001740 ((struct timeval *)(optval))->tv_sec =
1741 value / (1000 * 1000);
1742 ((struct timeval *)(optval))->tv_usec =
1743 value % (1000 * 1000);
1744 *optlenp = sizeof (struct timeval);
1745 } else {
1746 ((struct timeval32 *)(optval))->tv_sec =
1747 value / (1000 * 1000);
1748 ((struct timeval32 *)(optval))->tv_usec =
1749 value % (1000 * 1000);
1750 *optlenp = sizeof (struct timeval32);
1751 }
Yu Xiangning0f1702c2008-12-11 20:04:13 -08001752 return (0);
1753 }
1754 case SO_DEBUG:
1755 case SO_REUSEADDR:
1756 case SO_KEEPALIVE:
1757 case SO_DONTROUTE:
1758 case SO_BROADCAST:
1759 case SO_USELOOPBACK:
1760 case SO_OOBINLINE:
1761 case SO_SNDBUF:
Yu Xiangning0f1702c2008-12-11 20:04:13 -08001762#ifdef notyet
1763 case SO_SNDLOWAT:
1764 case SO_RCVLOWAT:
1765#endif /* notyet */
1766 case SO_DGRAM_ERRIND: {
1767 socklen_t optlen = *optlenp;
1768
1769 if (optlen < (t_uscalar_t)sizeof (int32_t))
1770 return (EINVAL);
1771 break;
1772 }
Yu Xiangninga5adac42008-12-29 13:56:29 +08001773 case SO_RCVBUF: {
1774 socklen_t optlen = *optlenp;
1775
1776 if (optlen < (t_uscalar_t)sizeof (int32_t))
1777 return (EINVAL);
1778
1779 if ((flags & _SOGETSOCKOPT_XPG4_2) && so->so_xpg_rcvbuf != 0) {
1780 /*
1781 * XXX If SO_RCVBUF has been set and this is an
1782 * XPG 4.2 application then do not ask the transport
1783 * since the transport might adjust the value and not
1784 * return exactly what was set by the application.
1785 * For non-XPG 4.2 application we return the value
1786 * that the transport is actually using.
1787 */
1788 *(int32_t *)optval = so->so_xpg_rcvbuf;
1789 *optlenp = sizeof (so->so_xpg_rcvbuf);
1790 return (0);
1791 }
1792 /*
1793 * If the option has not been set then get a default
1794 * value from the transport.
1795 */
1796 break;
1797 }
Yu Xiangning0f1702c2008-12-11 20:04:13 -08001798 case SO_LINGER: {
1799 socklen_t optlen = *optlenp;
1800
1801 if (optlen < (t_uscalar_t)sizeof (struct linger))
1802 return (EINVAL);
1803 break;
1804 }
1805 case SO_SND_BUFINFO: {
1806 socklen_t optlen = *optlenp;
1807
1808 if (optlen < (t_uscalar_t)sizeof (struct so_snd_bufinfo))
1809 return (EINVAL);
1810 ((struct so_snd_bufinfo *)(optval))->sbi_wroff =
1811 (so->so_proto_props).sopp_wroff;
1812 ((struct so_snd_bufinfo *)(optval))->sbi_maxblk =
1813 (so->so_proto_props).sopp_maxblk;
1814 ((struct so_snd_bufinfo *)(optval))->sbi_maxpsz =
1815 (so->so_proto_props).sopp_maxpsz;
1816 ((struct so_snd_bufinfo *)(optval))->sbi_tail =
1817 (so->so_proto_props).sopp_tail;
1818 *optlenp = sizeof (struct so_snd_bufinfo);
1819 return (0);
1820 }
1821 default:
1822 break;
1823 }
1824
1825 /* Unknown Option */
1826 return (-1);
1827}
1828
1829void
1830socket_sonode_destroy(struct sonode *so)
1831{
1832 sonode_fini(so);
1833 kmem_cache_free(socket_cache, so);
1834}
1835
1836int
1837so_zcopy_wait(struct sonode *so)
1838{
1839 int error = 0;
1840
1841 mutex_enter(&so->so_lock);
1842 while (!(so->so_copyflag & STZCNOTIFY)) {
1843 if (so->so_state & SS_CLOSING) {
1844 mutex_exit(&so->so_lock);
1845 return (EINTR);
1846 }
1847 if (cv_wait_sig(&so->so_copy_cv, &so->so_lock) == 0) {
1848 error = EINTR;
1849 break;
1850 }
1851 }
1852 so->so_copyflag &= ~STZCNOTIFY;
1853 mutex_exit(&so->so_lock);
1854 return (error);
1855}
1856
1857void
1858so_timer_callback(void *arg)
1859{
1860 struct sonode *so = (struct sonode *)arg;
1861
1862 mutex_enter(&so->so_lock);
1863
1864 so->so_rcv_timer_tid = 0;
1865 if (so->so_rcv_queued > 0) {
1866 so_notify_data(so, so->so_rcv_queued);
1867 } else {
1868 mutex_exit(&so->so_lock);
1869 }
1870}
1871
1872#ifdef DEBUG
1873/*
1874 * Verify that the length stored in so_rcv_queued and the length of data blocks
1875 * queued is same.
1876 */
1877static boolean_t
1878so_check_length(sonode_t *so)
1879{
1880 mblk_t *mp = so->so_rcv_q_head;
1881 int len = 0;
1882
1883 ASSERT(MUTEX_HELD(&so->so_lock));
1884
1885 if (mp != NULL) {
1886 len = msgdsize(mp);
1887 while ((mp = mp->b_next) != NULL)
1888 len += msgdsize(mp);
1889 }
1890 mp = so->so_rcv_head;
1891 if (mp != NULL) {
1892 len += msgdsize(mp);
1893 while ((mp = mp->b_next) != NULL)
1894 len += msgdsize(mp);
1895 }
1896 return ((len == so->so_rcv_queued) ? B_TRUE : B_FALSE);
1897}
1898#endif
1899
1900int
1901so_get_mod_version(struct sockparams *sp)
1902{
1903 ASSERT(sp != NULL && sp->sp_smod_info != NULL);
1904 return (sp->sp_smod_info->smod_version);
1905}
1906
1907/*
1908 * so_start_fallback()
1909 *
1910 * Block new socket operations from coming in, and wait for active operations
1911 * to complete. Threads that are sleeping will be woken up so they can get
1912 * out of the way.
1913 *
1914 * The caller must be a reader on so_fallback_rwlock.
1915 */
1916static boolean_t
1917so_start_fallback(struct sonode *so)
1918{
1919 ASSERT(RW_READ_HELD(&so->so_fallback_rwlock));
1920
1921 mutex_enter(&so->so_lock);
1922 if (so->so_state & SS_FALLBACK_PENDING) {
1923 mutex_exit(&so->so_lock);
1924 return (B_FALSE);
1925 }
1926 so->so_state |= SS_FALLBACK_PENDING;
1927 /*
1928 * Poke all threads that might be sleeping. Any operation that comes
1929 * in after the cv_broadcast will observe the fallback pending flag
1930 * which cause the call to return where it would normally sleep.
1931 */
1932 cv_broadcast(&so->so_state_cv); /* threads in connect() */
1933 cv_broadcast(&so->so_rcv_cv); /* threads in recvmsg() */
1934 cv_broadcast(&so->so_snd_cv); /* threads in sendmsg() */
1935 mutex_enter(&so->so_acceptq_lock);
1936 cv_broadcast(&so->so_acceptq_cv); /* threads in accept() */
1937 mutex_exit(&so->so_acceptq_lock);
1938 mutex_exit(&so->so_lock);
1939
1940 /*
1941 * The main reason for the rw_tryupgrade call is to provide
1942 * observability during the fallback process. We want to
1943 * be able to see if there are pending operations.
1944 */
1945 if (rw_tryupgrade(&so->so_fallback_rwlock) == 0) {
1946 /*
1947 * It is safe to drop and reaquire the fallback lock, because
1948 * we are guaranteed that another fallback cannot take place.
1949 */
1950 rw_exit(&so->so_fallback_rwlock);
1951 DTRACE_PROBE1(pending__ops__wait, (struct sonode *), so);
1952 rw_enter(&so->so_fallback_rwlock, RW_WRITER);
1953 DTRACE_PROBE1(pending__ops__complete, (struct sonode *), so);
1954 }
1955
1956 return (B_TRUE);
1957}
1958
1959/*
1960 * so_end_fallback()
1961 *
1962 * Allow socket opertions back in.
1963 *
1964 * The caller must be a writer on so_fallback_rwlock.
1965 */
1966static void
1967so_end_fallback(struct sonode *so)
1968{
1969 ASSERT(RW_ISWRITER(&so->so_fallback_rwlock));
1970
1971 mutex_enter(&so->so_lock);
Anders Persson41174432009-02-12 17:35:05 -08001972 so->so_state &= ~(SS_FALLBACK_PENDING|SS_FALLBACK_DRAIN);
Yu Xiangning0f1702c2008-12-11 20:04:13 -08001973 mutex_exit(&so->so_lock);
1974
1975 rw_downgrade(&so->so_fallback_rwlock);
1976}
1977
1978/*
1979 * so_quiesced_cb()
1980 *
1981 * Callback passed to the protocol during fallback. It is called once
1982 * the endpoint is quiescent.
1983 *
1984 * No requests from the user, no notifications from the protocol, so it
1985 * is safe to synchronize the state. Data can also be moved without
1986 * risk for reordering.
1987 *
Yu Xiangning0f1702c2008-12-11 20:04:13 -08001988 * We do not need to hold so_lock, since there can be only one thread
1989 * operating on the sonode.
1990 */
1991static void
1992so_quiesced_cb(sock_upper_handle_t sock_handle, queue_t *q,
1993 struct T_capability_ack *tcap, struct sockaddr *laddr, socklen_t laddrlen,
1994 struct sockaddr *faddr, socklen_t faddrlen, short opts)
1995{
1996 struct sonode *so = (struct sonode *)sock_handle;
Anders Persson41174432009-02-12 17:35:05 -08001997 boolean_t atmark;
Yu Xiangning0f1702c2008-12-11 20:04:13 -08001998
1999 sotpi_update_state(so, tcap, laddr, laddrlen, faddr, faddrlen, opts);
2000
Anders Persson41174432009-02-12 17:35:05 -08002001 /*
2002 * Some protocols do not quiece the data path during fallback. Once
2003 * we set the SS_FALLBACK_DRAIN flag any attempt to queue data will
2004 * fail and the protocol is responsible for saving the data for later
2005 * delivery (i.e., once the fallback has completed).
2006 */
Yu Xiangning0f1702c2008-12-11 20:04:13 -08002007 mutex_enter(&so->so_lock);
Anders Persson41174432009-02-12 17:35:05 -08002008 so->so_state |= SS_FALLBACK_DRAIN;
Yu Xiangning0f1702c2008-12-11 20:04:13 -08002009 SOCKET_TIMER_CANCEL(so);
2010 mutex_exit(&so->so_lock);
Anders Persson41174432009-02-12 17:35:05 -08002011
Yu Xiangning0f1702c2008-12-11 20:04:13 -08002012 if (so->so_rcv_head != NULL) {
2013 if (so->so_rcv_q_last_head == NULL)
2014 so->so_rcv_q_head = so->so_rcv_head;
2015 else
2016 so->so_rcv_q_last_head->b_next = so->so_rcv_head;
2017 so->so_rcv_q_last_head = so->so_rcv_last_head;
2018 }
2019
Anders Persson41174432009-02-12 17:35:05 -08002020 atmark = (so->so_state & SS_RCVATMARK) != 0;
2021 /*
2022 * Clear any OOB state having to do with pending data. The TPI
2023 * code path will set the appropriate oob state when we move the
2024 * oob data to the STREAM head. We leave SS_HADOOBDATA since the oob
2025 * data has already been consumed.
2026 */
2027 so->so_state &= ~(SS_RCVATMARK|SS_OOBPEND|SS_HAVEOOBDATA);
2028
2029 ASSERT(so->so_oobmsg != NULL || so->so_oobmark <= so->so_rcv_queued);
2030
2031 /*
2032 * Move data to the STREAM head.
2033 */
Yu Xiangning0f1702c2008-12-11 20:04:13 -08002034 while (so->so_rcv_q_head != NULL) {
2035 mblk_t *mp = so->so_rcv_q_head;
2036 size_t mlen = msgdsize(mp);
2037
2038 so->so_rcv_q_head = mp->b_next;
2039 mp->b_next = NULL;
2040 mp->b_prev = NULL;
Anders Persson41174432009-02-12 17:35:05 -08002041
2042 /*
2043 * Send T_EXDATA_IND if we are at the oob mark.
2044 */
2045 if (atmark) {
2046 struct T_exdata_ind *tei;
2047 mblk_t *mp1 = SOTOTPI(so)->sti_exdata_mp;
2048
2049 SOTOTPI(so)->sti_exdata_mp = NULL;
2050 ASSERT(mp1 != NULL);
2051 mp1->b_datap->db_type = M_PROTO;
2052 tei = (struct T_exdata_ind *)mp1->b_rptr;
2053 tei->PRIM_type = T_EXDATA_IND;
2054 tei->MORE_flag = 0;
2055 mp1->b_wptr = (uchar_t *)&tei[1];
2056
2057 if (IS_SO_OOB_INLINE(so)) {
2058 mp1->b_cont = mp;
2059 } else {
2060 ASSERT(so->so_oobmsg != NULL);
2061 mp1->b_cont = so->so_oobmsg;
2062 so->so_oobmsg = NULL;
2063
2064 /* process current mp next time around */
2065 mp->b_next = so->so_rcv_q_head;
2066 so->so_rcv_q_head = mp;
2067 mlen = 0;
2068 }
2069 mp = mp1;
2070
2071 /* we have consumed the oob mark */
2072 atmark = B_FALSE;
2073 } else if (so->so_oobmark > 0) {
2074 /*
2075 * Check if the OOB mark is within the current
2076 * mblk chain. In that case we have to split it up.
2077 */
2078 if (so->so_oobmark < mlen) {
2079 mblk_t *urg_mp = mp;
2080
2081 atmark = B_TRUE;
2082 mp = NULL;
2083 mlen = so->so_oobmark;
2084
2085 /*
2086 * It is assumed that the OOB mark does
2087 * not land within a mblk.
2088 */
2089 do {
2090 so->so_oobmark -= MBLKL(urg_mp);
2091 mp = urg_mp;
2092 urg_mp = urg_mp->b_cont;
2093 } while (so->so_oobmark > 0);
2094 mp->b_cont = NULL;
2095 if (urg_mp != NULL) {
2096 urg_mp->b_next = so->so_rcv_q_head;
2097 so->so_rcv_q_head = urg_mp;
2098 }
2099 } else {
2100 so->so_oobmark -= mlen;
2101 if (so->so_oobmark == 0)
2102 atmark = B_TRUE;
2103 }
2104 }
2105
2106 /*
2107 * Queue data on the STREAM head.
2108 */
Yu Xiangning0f1702c2008-12-11 20:04:13 -08002109 so->so_rcv_queued -= mlen;
2110 putnext(q, mp);
2111 }
Yu Xiangning0f1702c2008-12-11 20:04:13 -08002112 so->so_rcv_head = NULL;
2113 so->so_rcv_last_head = NULL;
2114 so->so_rcv_q_head = NULL;
2115 so->so_rcv_q_last_head = NULL;
2116
Anders Persson41174432009-02-12 17:35:05 -08002117 /*
2118 * Check if the oob byte is at the end of the data stream, or if the
2119 * oob byte has not yet arrived. In the latter case we have to send a
2120 * SIGURG and a mark indicator to the STREAM head. The mark indicator
2121 * is needed to guarantee correct behavior for SIOCATMARK. See block
2122 * comment in socktpi.h for more details.
2123 */
2124 if (atmark || so->so_oobmark > 0) {
2125 mblk_t *mp;
Yu Xiangning0f1702c2008-12-11 20:04:13 -08002126
Anders Persson41174432009-02-12 17:35:05 -08002127 if (atmark && so->so_oobmsg != NULL) {
2128 struct T_exdata_ind *tei;
2129
2130 mp = SOTOTPI(so)->sti_exdata_mp;
2131 SOTOTPI(so)->sti_exdata_mp = NULL;
2132 ASSERT(mp != NULL);
2133 mp->b_datap->db_type = M_PROTO;
2134 tei = (struct T_exdata_ind *)mp->b_rptr;
2135 tei->PRIM_type = T_EXDATA_IND;
2136 tei->MORE_flag = 0;
2137 mp->b_wptr = (uchar_t *)&tei[1];
2138
2139 mp->b_cont = so->so_oobmsg;
2140 so->so_oobmsg = NULL;
2141
2142 putnext(q, mp);
2143 } else {
2144 /* Send up the signal */
2145 mp = SOTOTPI(so)->sti_exdata_mp;
2146 SOTOTPI(so)->sti_exdata_mp = NULL;
2147 ASSERT(mp != NULL);
2148 DB_TYPE(mp) = M_PCSIG;
2149 *mp->b_wptr++ = (uchar_t)SIGURG;
2150 putnext(q, mp);
2151
2152 /* Send up the mark indicator */
2153 mp = SOTOTPI(so)->sti_urgmark_mp;
2154 SOTOTPI(so)->sti_urgmark_mp = NULL;
2155 mp->b_flag = atmark ? MSGMARKNEXT : MSGNOTMARKNEXT;
2156 putnext(q, mp);
2157
2158 so->so_oobmark = 0;
2159 }
2160 }
2161
2162 if (SOTOTPI(so)->sti_exdata_mp != NULL) {
2163 freeb(SOTOTPI(so)->sti_exdata_mp);
2164 SOTOTPI(so)->sti_exdata_mp = NULL;
2165 }
2166
2167 if (SOTOTPI(so)->sti_urgmark_mp != NULL) {
2168 freeb(SOTOTPI(so)->sti_urgmark_mp);
2169 SOTOTPI(so)->sti_urgmark_mp = NULL;
2170 }
2171
2172 ASSERT(so->so_oobmark == 0);
Yu Xiangning0f1702c2008-12-11 20:04:13 -08002173 ASSERT(so->so_rcv_queued == 0);
2174}
2175
Anders Persson41174432009-02-12 17:35:05 -08002176#ifdef DEBUG
2177/*
2178 * Do an integrity check of the sonode. This should be done if a
2179 * fallback fails after sonode has initially been converted to use
2180 * TPI and subsequently have to be reverted.
2181 *
2182 * Failure to pass the integrity check will panic the system.
2183 */
2184void
2185so_integrity_check(struct sonode *cur, struct sonode *orig)
2186{
2187 VERIFY(cur->so_vnode == orig->so_vnode);
2188 VERIFY(cur->so_ops == orig->so_ops);
2189 /*
2190 * For so_state we can only VERIFY the state flags in CHECK_STATE.
2191 * The other state flags might be affected by a notification from the
2192 * protocol.
2193 */
2194#define CHECK_STATE (SS_CANTRCVMORE|SS_CANTSENDMORE|SS_NDELAY|SS_NONBLOCK| \
2195 SS_ASYNC|SS_ACCEPTCONN|SS_SAVEDEOR|SS_RCVATMARK|SS_OOBPEND| \
2196 SS_HAVEOOBDATA|SS_HADOOBDATA|SS_SENTLASTREADSIG|SS_SENTLASTWRITESIG)
2197 VERIFY((cur->so_state & (orig->so_state & CHECK_STATE)) ==
2198 (orig->so_state & CHECK_STATE));
2199 VERIFY(cur->so_mode == orig->so_mode);
2200 VERIFY(cur->so_flag == orig->so_flag);
2201 VERIFY(cur->so_count == orig->so_count);
2202 /* Cannot VERIFY so_proto_connid; proto can update it */
2203 VERIFY(cur->so_sockparams == orig->so_sockparams);
2204 /* an error might have been recorded, but it can not be lost */
2205 VERIFY(cur->so_error != 0 || orig->so_error == 0);
2206 VERIFY(cur->so_family == orig->so_family);
2207 VERIFY(cur->so_type == orig->so_type);
2208 VERIFY(cur->so_protocol == orig->so_protocol);
2209 VERIFY(cur->so_version == orig->so_version);
2210 /* New conns might have arrived, but none should have been lost */
2211 VERIFY(cur->so_acceptq_len >= orig->so_acceptq_len);
2212 VERIFY(cur->so_acceptq_head == orig->so_acceptq_head);
2213 VERIFY(cur->so_backlog == orig->so_backlog);
2214 /* New OOB migth have arrived, but mark should not have been lost */
2215 VERIFY(cur->so_oobmark >= orig->so_oobmark);
2216 /* Cannot VERIFY so_oobmsg; the proto might have sent up a new one */
2217 VERIFY(cur->so_pgrp == orig->so_pgrp);
2218 VERIFY(cur->so_peercred == orig->so_peercred);
2219 VERIFY(cur->so_cpid == orig->so_cpid);
2220 VERIFY(cur->so_zoneid == orig->so_zoneid);
2221 /* New data migth have arrived, but none should have been lost */
2222 VERIFY(cur->so_rcv_queued >= orig->so_rcv_queued);
2223 VERIFY(cur->so_rcv_q_head == orig->so_rcv_q_head);
2224 VERIFY(cur->so_rcv_head == orig->so_rcv_head);
2225 VERIFY(cur->so_proto_handle == orig->so_proto_handle);
2226 VERIFY(cur->so_downcalls == orig->so_downcalls);
2227 /* Cannot VERIFY so_proto_props; they can be updated by proto */
2228}
2229#endif
2230
Yu Xiangning0f1702c2008-12-11 20:04:13 -08002231/*
2232 * so_tpi_fallback()
2233 *
Anders Persson41174432009-02-12 17:35:05 -08002234 * This is the fallback initation routine; things start here.
Yu Xiangning0f1702c2008-12-11 20:04:13 -08002235 *
2236 * Basic strategy:
2237 * o Block new socket operations from coming in
2238 * o Allocate/initate info needed by TPI
2239 * o Quiesce the connection, at which point we sync
2240 * state and move data
2241 * o Change operations (sonodeops) associated with the socket
2242 * o Unblock threads waiting for the fallback to finish
2243 */
2244int
2245so_tpi_fallback(struct sonode *so, struct cred *cr)
2246{
2247 int error;
2248 queue_t *q;
2249 struct sockparams *sp;
Anders Persson41174432009-02-12 17:35:05 -08002250 struct sockparams *newsp = NULL;
Yu Xiangning0f1702c2008-12-11 20:04:13 -08002251 so_proto_fallback_func_t fbfunc;
2252 boolean_t direct;
Anders Persson41174432009-02-12 17:35:05 -08002253 struct sonode *nso;
2254#ifdef DEBUG
2255 struct sonode origso;
2256#endif
Yu Xiangning0f1702c2008-12-11 20:04:13 -08002257 error = 0;
2258 sp = so->so_sockparams;
2259 fbfunc = sp->sp_smod_info->smod_proto_fallback_func;
2260
2261 /*
2262 * Fallback can only happen if there is a device associated
2263 * with the sonode, and the socket module has a fallback function.
2264 */
2265 if (!SOCKPARAMS_HAS_DEVICE(sp) || fbfunc == NULL)
2266 return (EINVAL);
2267
2268 /*
2269 * Initiate fallback; upon success we know that no new requests
2270 * will come in from the user.
2271 */
2272 if (!so_start_fallback(so))
2273 return (EAGAIN);
Anders Persson41174432009-02-12 17:35:05 -08002274#ifdef DEBUG
2275 /*
2276 * Make a copy of the sonode in case we need to make an integrity
2277 * check later on.
2278 */
2279 bcopy(so, &origso, sizeof (*so));
2280#endif
Yu Xiangning0f1702c2008-12-11 20:04:13 -08002281
Anders Persson7d64f412009-02-11 15:38:45 -08002282 sp->sp_stats.sps_nfallback.value.ui64++;
2283
Yu Xiangning0f1702c2008-12-11 20:04:13 -08002284 newsp = sockparams_hold_ephemeral_bydev(so->so_family, so->so_type,
2285 so->so_protocol, so->so_sockparams->sp_sdev_info.sd_devpath,
2286 KM_SLEEP, &error);
2287 if (error != 0)
2288 goto out;
2289
2290 if (so->so_direct != NULL) {
2291 sodirect_t *sodp = so->so_direct;
Anders Perssonbbc000e2009-04-28 12:10:59 -07002292 mutex_enter(&so->so_lock);
Yu Xiangning0f1702c2008-12-11 20:04:13 -08002293
Anders Perssonbbc000e2009-04-28 12:10:59 -07002294 so->so_direct->sod_enabled = B_FALSE;
Yu Xiangning0f1702c2008-12-11 20:04:13 -08002295 so->so_state &= ~SS_SODIRECT;
2296 ASSERT(sodp->sod_uioafh == NULL);
Anders Perssonbbc000e2009-04-28 12:10:59 -07002297 mutex_exit(&so->so_lock);
Yu Xiangning0f1702c2008-12-11 20:04:13 -08002298 }
2299
2300 /* Turn sonode into a TPI socket */
Anders Persson41174432009-02-12 17:35:05 -08002301 error = sotpi_convert_sonode(so, newsp, &direct, &q, cr);
2302 if (error != 0)
Yu Xiangning0f1702c2008-12-11 20:04:13 -08002303 goto out;
Anders Persson41174432009-02-12 17:35:05 -08002304
Yu Xiangning0f1702c2008-12-11 20:04:13 -08002305
2306 /*
2307 * Now tell the protocol to start using TPI. so_quiesced_cb be
2308 * called once it's safe to synchronize state.
2309 */
2310 DTRACE_PROBE1(proto__fallback__begin, struct sonode *, so);
Anders Persson41174432009-02-12 17:35:05 -08002311 error = (*fbfunc)(so->so_proto_handle, q, direct, so_quiesced_cb);
Yu Xiangning0f1702c2008-12-11 20:04:13 -08002312 DTRACE_PROBE1(proto__fallback__end, struct sonode *, so);
2313
Anders Persson41174432009-02-12 17:35:05 -08002314 if (error != 0) {
2315 /* protocol was unable to do a fallback, revert the sonode */
2316 sotpi_revert_sonode(so, cr);
2317 goto out;
2318 }
2319
Yu Xiangning0f1702c2008-12-11 20:04:13 -08002320 /*
Anders Persson41174432009-02-12 17:35:05 -08002321 * Walk the accept queue and notify the proto that they should
2322 * fall back to TPI. The protocol will send up the T_CONN_IND.
2323 */
2324 nso = so->so_acceptq_head;
2325 while (nso != NULL) {
2326 int rval;
2327
2328 DTRACE_PROBE1(proto__fallback__begin, struct sonode *, nso);
2329 rval = (*fbfunc)(nso->so_proto_handle, NULL, direct, NULL);
2330 DTRACE_PROBE1(proto__fallback__end, struct sonode *, nso);
2331 if (rval != 0) {
2332 zcmn_err(getzoneid(), CE_WARN,
2333 "Failed to convert socket in accept queue to TPI. "
2334 "Pid = %d\n", curproc->p_pid);
2335 }
2336 nso = nso->so_acceptq_next;
2337 }
2338
2339 /*
2340 * Now flush the acceptq, this will destroy all sockets. They will
2341 * be recreated in sotpi_accept().
Yu Xiangning0f1702c2008-12-11 20:04:13 -08002342 */
2343 so_acceptq_flush(so);
2344
2345 mutex_enter(&so->so_lock);
2346 so->so_state |= SS_FALLBACK_COMP;
2347 mutex_exit(&so->so_lock);
2348
2349 /*
2350 * Swap the sonode ops. Socket opertations that come in once this
2351 * is done will proceed without blocking.
2352 */
2353 so->so_ops = &sotpi_sonodeops;
2354
2355 /*
Yu Xiangning0f1702c2008-12-11 20:04:13 -08002356 * Wake up any threads stuck in poll. This is needed since the poll
2357 * head changes when the fallback happens (moves from the sonode to
2358 * the STREAMS head).
2359 */
2360 pollwakeup(&so->so_poll_list, POLLERR);
2361out:
2362 so_end_fallback(so);
2363
Anders Persson41174432009-02-12 17:35:05 -08002364 if (error != 0) {
2365#ifdef DEBUG
2366 so_integrity_check(so, &origso);
2367#endif
2368 zcmn_err(getzoneid(), CE_WARN,
2369 "Failed to convert socket to TPI (err=%d). Pid = %d\n",
2370 error, curproc->p_pid);
2371 if (newsp != NULL)
2372 SOCKPARAMS_DEC_REF(newsp);
2373 }
2374
Yu Xiangning0f1702c2008-12-11 20:04:13 -08002375 return (error);
2376}