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