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