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 /* Data */
957 960 /* so_downcalls is null for sctp */
958 961 if (so->so_downcalls != NULL && so->so_downcalls->sd_poll != NULL) {
959 962 *reventsp |= (*so->so_downcalls->sd_poll)
960 963 (so->so_proto_handle, events & SO_PROTO_POLLEV, anyyet,
961 964 CRED()) & events;
962 965 ASSERT((*reventsp & ~events) == 0);
963 966 /* do not recheck events */
964 967 events &= ~SO_PROTO_POLLEV;
965 968 } else {
966 969 if (SO_HAVE_DATA(so))
967 970 *reventsp |= (POLLIN|POLLRDNORM) & events;
968 971
969 972 /* Urgent data */
970 973 if ((state & SS_OOBPEND) != 0) {
971 974 *reventsp |= (POLLRDBAND | POLLPRI) & events;
972 975 }
973 976
974 977 /*
975 978 * If the socket has become disconnected, we set POLLHUP.
976 979 * Note that if we are in this state, we will have set POLLIN
977 980 * (SO_HAVE_DATA() is true on a disconnected socket), but not
978 981 * POLLOUT (SS_ISCONNECTED is false). This is in keeping with
979 982 * the semantics of POLLHUP, which is defined to be mutually
980 983 * exclusive with respect to POLLOUT but not POLLIN. We are
981 984 * therefore setting POLLHUP primarily for the benefit of
982 985 * those not polling on POLLIN, as they have no other way of
983 986 * knowing that the socket has been disconnected.
984 987 */
985 988 mask = SS_SENTLASTREADSIG | SS_SENTLASTWRITESIG;
986 989
987 990 if ((state & (mask | SS_ISCONNECTED)) == mask)
988 991 *reventsp |= POLLHUP;
989 992 }
990 993
991 994 if (!*reventsp && !anyyet) {
992 995 /* Check for read events again, but this time under lock */
993 996 if (events & (POLLIN|POLLRDNORM)) {
994 997 mutex_enter(&so->so_lock);
995 998 if (SO_HAVE_DATA(so) ||
996 999 !list_is_empty(&so->so_acceptq_list)) {
997 1000 mutex_exit(&so->so_lock);
998 1001 *reventsp |= (POLLIN|POLLRDNORM) & events;
999 1002 return (0);
1000 1003 } else {
1001 1004 so->so_pollev |= SO_POLLEV_IN;
1002 1005 mutex_exit(&so->so_lock);
1003 1006 }
1004 1007 }
1005 1008 *phpp = &so->so_poll_list;
1006 1009 }
1007 1010 return (0);
1008 1011 }
1009 1012
1010 1013 /*
1011 1014 * Generic Upcalls
1012 1015 */
1013 1016 void
1014 1017 so_connected(sock_upper_handle_t sock_handle, sock_connid_t id,
1015 1018 cred_t *peer_cred, pid_t peer_cpid)
1016 1019 {
1017 1020 struct sonode *so = (struct sonode *)sock_handle;
1018 1021
1019 1022 mutex_enter(&so->so_lock);
1020 1023 ASSERT(so->so_proto_handle != NULL);
1021 1024
1022 1025 if (peer_cred != NULL) {
1023 1026 if (so->so_peercred != NULL)
1024 1027 crfree(so->so_peercred);
1025 1028 crhold(peer_cred);
1026 1029 so->so_peercred = peer_cred;
1027 1030 so->so_cpid = peer_cpid;
1028 1031 }
1029 1032
1030 1033 so->so_proto_connid = id;
1031 1034 soisconnected(so);
1032 1035 /*
1033 1036 * Wake ones who're waiting for conn to become established.
1034 1037 */
1035 1038 so_notify_connected(so);
1036 1039 }
1037 1040
1038 1041 int
1039 1042 so_disconnected(sock_upper_handle_t sock_handle, sock_connid_t id, int error)
1040 1043 {
1041 1044 struct sonode *so = (struct sonode *)sock_handle;
1042 1045 boolean_t connect_failed;
1043 1046
1044 1047 mutex_enter(&so->so_lock);
1045 1048
1046 1049 /*
1047 1050 * If we aren't currently connected, then this isn't a disconnect but
1048 1051 * rather a failure to connect.
1049 1052 */
1050 1053 connect_failed = !(so->so_state & SS_ISCONNECTED);
1051 1054
1052 1055 so->so_proto_connid = id;
1053 1056 soisdisconnected(so, error);
1054 1057 so_notify_disconnected(so, connect_failed, error);
1055 1058
1056 1059 return (0);
1057 1060 }
1058 1061
1059 1062 void
1060 1063 so_opctl(sock_upper_handle_t sock_handle, sock_opctl_action_t action,
1061 1064 uintptr_t arg)
1062 1065 {
1063 1066 struct sonode *so = (struct sonode *)sock_handle;
1064 1067
1065 1068 switch (action) {
1066 1069 case SOCK_OPCTL_SHUT_SEND:
1067 1070 mutex_enter(&so->so_lock);
1068 1071 socantsendmore(so);
1069 1072 so_notify_disconnecting(so);
1070 1073 break;
1071 1074 case SOCK_OPCTL_SHUT_RECV: {
1072 1075 mutex_enter(&so->so_lock);
1073 1076 socantrcvmore(so);
1074 1077 so_notify_eof(so);
1075 1078 break;
1076 1079 }
1077 1080 case SOCK_OPCTL_ENAB_ACCEPT:
1078 1081 mutex_enter(&so->so_lock);
1079 1082 so->so_state |= SS_ACCEPTCONN;
1080 1083 so->so_backlog = (unsigned int)arg;
1081 1084 /*
1082 1085 * The protocol can stop generating newconn upcalls when
1083 1086 * the backlog is full, so to make sure the listener does
1084 1087 * not end up with a queue full of deferred connections
1085 1088 * we reduce the backlog by one. Thus the listener will
1086 1089 * start closing deferred connections before the backlog
1087 1090 * is full.
1088 1091 */
1089 1092 if (so->so_filter_active > 0)
1090 1093 so->so_backlog = MAX(1, so->so_backlog - 1);
1091 1094 mutex_exit(&so->so_lock);
1092 1095 break;
1093 1096 default:
1094 1097 ASSERT(0);
1095 1098 break;
1096 1099 }
1097 1100 }
1098 1101
1099 1102 void
1100 1103 so_txq_full(sock_upper_handle_t sock_handle, boolean_t qfull)
1101 1104 {
1102 1105 struct sonode *so = (struct sonode *)sock_handle;
1103 1106
1104 1107 if (qfull) {
1105 1108 so_snd_qfull(so);
1106 1109 } else {
1107 1110 so_snd_qnotfull(so);
1108 1111 mutex_enter(&so->so_lock);
1109 1112 /* so_notify_writable drops so_lock */
1110 1113 so_notify_writable(so);
1111 1114 }
1112 1115 }
1113 1116
1114 1117 sock_upper_handle_t
1115 1118 so_newconn(sock_upper_handle_t parenthandle,
1116 1119 sock_lower_handle_t proto_handle, sock_downcalls_t *sock_downcalls,
1117 1120 struct cred *peer_cred, pid_t peer_cpid, sock_upcalls_t **sock_upcallsp)
1118 1121 {
1119 1122 struct sonode *so = (struct sonode *)parenthandle;
1120 1123 struct sonode *nso;
1121 1124 int error;
1122 1125
1123 1126 ASSERT(proto_handle != NULL);
1124 1127
1125 1128 if ((so->so_state & SS_ACCEPTCONN) == 0 ||
1126 1129 (so->so_acceptq_len >= so->so_backlog &&
1127 1130 (so->so_filter_active == 0 || !sof_sonode_drop_deferred(so)))) {
1128 1131 return (NULL);
1129 1132 }
1130 1133
1131 1134 nso = socket_newconn(so, proto_handle, sock_downcalls, SOCKET_NOSLEEP,
1132 1135 &error);
1133 1136 if (nso == NULL)
1134 1137 return (NULL);
1135 1138
1136 1139 if (peer_cred != NULL) {
1137 1140 crhold(peer_cred);
1138 1141 nso->so_peercred = peer_cred;
1139 1142 nso->so_cpid = peer_cpid;
1140 1143 }
1141 1144 nso->so_listener = so;
1142 1145
1143 1146 /*
1144 1147 * The new socket (nso), proto_handle and sock_upcallsp are all
1145 1148 * valid at this point. But as soon as nso is placed in the accept
1146 1149 * queue that can no longer be assumed (since an accept() thread may
1147 1150 * pull it off the queue and close the socket).
1148 1151 */
1149 1152 *sock_upcallsp = &so_upcalls;
1150 1153
1151 1154 mutex_enter(&so->so_acceptq_lock);
1152 1155 if (so->so_state & (SS_CLOSING|SS_FALLBACK_PENDING|SS_FALLBACK_COMP)) {
1153 1156 mutex_exit(&so->so_acceptq_lock);
1154 1157 ASSERT(nso->so_count == 1);
1155 1158 nso->so_count--;
1156 1159 nso->so_listener = NULL;
1157 1160 /* drop proto ref */
1158 1161 VN_RELE(SOTOV(nso));
1159 1162 socket_destroy(nso);
1160 1163 return (NULL);
1161 1164 } else {
1162 1165 so->so_acceptq_len++;
1163 1166 if (nso->so_state & SS_FIL_DEFER) {
1164 1167 list_insert_tail(&so->so_acceptq_defer, nso);
1165 1168 mutex_exit(&so->so_acceptq_lock);
1166 1169 } else {
1167 1170 list_insert_tail(&so->so_acceptq_list, nso);
1168 1171 cv_signal(&so->so_acceptq_cv);
1169 1172 mutex_exit(&so->so_acceptq_lock);
1170 1173 mutex_enter(&so->so_lock);
1171 1174 so_notify_newconn(so);
1172 1175 }
1173 1176
1174 1177 return ((sock_upper_handle_t)nso);
1175 1178 }
1176 1179 }
1177 1180
1178 1181 void
1179 1182 so_set_prop(sock_upper_handle_t sock_handle, struct sock_proto_props *soppp)
1180 1183 {
1181 1184 struct sonode *so;
1182 1185
1183 1186 so = (struct sonode *)sock_handle;
1184 1187
1185 1188 mutex_enter(&so->so_lock);
1186 1189
1187 1190 if (soppp->sopp_flags & SOCKOPT_MAXBLK)
1188 1191 so->so_proto_props.sopp_maxblk = soppp->sopp_maxblk;
1189 1192 if (soppp->sopp_flags & SOCKOPT_WROFF)
1190 1193 so->so_proto_props.sopp_wroff = soppp->sopp_wroff;
1191 1194 if (soppp->sopp_flags & SOCKOPT_TAIL)
1192 1195 so->so_proto_props.sopp_tail = soppp->sopp_tail;
1193 1196 if (soppp->sopp_flags & SOCKOPT_RCVHIWAT)
1194 1197 so->so_proto_props.sopp_rxhiwat = soppp->sopp_rxhiwat;
1195 1198 if (soppp->sopp_flags & SOCKOPT_RCVLOWAT)
1196 1199 so->so_proto_props.sopp_rxlowat = soppp->sopp_rxlowat;
1197 1200 if (soppp->sopp_flags & SOCKOPT_MAXPSZ)
1198 1201 so->so_proto_props.sopp_maxpsz = soppp->sopp_maxpsz;
1199 1202 if (soppp->sopp_flags & SOCKOPT_MINPSZ)
1200 1203 so->so_proto_props.sopp_minpsz = soppp->sopp_minpsz;
1201 1204 if (soppp->sopp_flags & SOCKOPT_ZCOPY) {
1202 1205 if (soppp->sopp_zcopyflag & ZCVMSAFE) {
1203 1206 so->so_proto_props.sopp_zcopyflag |= STZCVMSAFE;
1204 1207 so->so_proto_props.sopp_zcopyflag &= ~STZCVMUNSAFE;
1205 1208 } else if (soppp->sopp_zcopyflag & ZCVMUNSAFE) {
1206 1209 so->so_proto_props.sopp_zcopyflag |= STZCVMUNSAFE;
1207 1210 so->so_proto_props.sopp_zcopyflag &= ~STZCVMSAFE;
1208 1211 }
1209 1212
1210 1213 if (soppp->sopp_zcopyflag & COPYCACHED) {
1211 1214 so->so_proto_props.sopp_zcopyflag |= STRCOPYCACHED;
1212 1215 }
1213 1216 }
1214 1217 if (soppp->sopp_flags & SOCKOPT_OOBINLINE)
1215 1218 so->so_proto_props.sopp_oobinline = soppp->sopp_oobinline;
1216 1219 if (soppp->sopp_flags & SOCKOPT_RCVTIMER)
1217 1220 so->so_proto_props.sopp_rcvtimer = soppp->sopp_rcvtimer;
1218 1221 if (soppp->sopp_flags & SOCKOPT_RCVTHRESH)
1219 1222 so->so_proto_props.sopp_rcvthresh = soppp->sopp_rcvthresh;
1220 1223 if (soppp->sopp_flags & SOCKOPT_MAXADDRLEN)
1221 1224 so->so_proto_props.sopp_maxaddrlen = soppp->sopp_maxaddrlen;
1222 1225 if (soppp->sopp_flags & SOCKOPT_LOOPBACK)
1223 1226 so->so_proto_props.sopp_loopback = soppp->sopp_loopback;
1224 1227
1225 1228 mutex_exit(&so->so_lock);
1226 1229
1227 1230 if (so->so_filter_active > 0) {
1228 1231 sof_instance_t *inst;
1229 1232 ssize_t maxblk;
1230 1233 ushort_t wroff, tail;
1231 1234 maxblk = so->so_proto_props.sopp_maxblk;
1232 1235 wroff = so->so_proto_props.sopp_wroff;
1233 1236 tail = so->so_proto_props.sopp_tail;
1234 1237 for (inst = so->so_filter_bottom; inst != NULL;
1235 1238 inst = inst->sofi_prev) {
1236 1239 if (SOF_INTERESTED(inst, mblk_prop)) {
1237 1240 (*inst->sofi_ops->sofop_mblk_prop)(
1238 1241 (sof_handle_t)inst, inst->sofi_cookie,
1239 1242 &maxblk, &wroff, &tail);
1240 1243 }
1241 1244 }
1242 1245 mutex_enter(&so->so_lock);
1243 1246 so->so_proto_props.sopp_maxblk = maxblk;
1244 1247 so->so_proto_props.sopp_wroff = wroff;
1245 1248 so->so_proto_props.sopp_tail = tail;
1246 1249 mutex_exit(&so->so_lock);
1247 1250 }
1248 1251 #ifdef DEBUG
1249 1252 soppp->sopp_flags &= ~(SOCKOPT_MAXBLK | SOCKOPT_WROFF | SOCKOPT_TAIL |
1250 1253 SOCKOPT_RCVHIWAT | SOCKOPT_RCVLOWAT | SOCKOPT_MAXPSZ |
1251 1254 SOCKOPT_ZCOPY | SOCKOPT_OOBINLINE | SOCKOPT_RCVTIMER |
1252 1255 SOCKOPT_RCVTHRESH | SOCKOPT_MAXADDRLEN | SOCKOPT_MINPSZ |
1253 1256 SOCKOPT_LOOPBACK);
1254 1257 ASSERT(soppp->sopp_flags == 0);
1255 1258 #endif
1256 1259 }
1257 1260
1258 1261 /* ARGSUSED */
1259 1262 ssize_t
1260 1263 so_queue_msg_impl(struct sonode *so, mblk_t *mp,
1261 1264 size_t msg_size, int flags, int *errorp, boolean_t *force_pushp,
1262 1265 sof_instance_t *filter)
1263 1266 {
1264 1267 boolean_t force_push = B_TRUE;
1265 1268 int space_left;
1266 1269 sodirect_t *sodp = so->so_direct;
1267 1270
1268 1271 ASSERT(errorp != NULL);
1269 1272 *errorp = 0;
1270 1273 if (mp == NULL) {
1271 1274 if (so->so_downcalls->sd_recv_uio != NULL) {
1272 1275 mutex_enter(&so->so_lock);
1273 1276 /* the notify functions will drop the lock */
1274 1277 if (flags & MSG_OOB)
1275 1278 so_notify_oobdata(so, IS_SO_OOB_INLINE(so));
1276 1279 else
1277 1280 so_notify_data(so, msg_size);
1278 1281 return (0);
1279 1282 }
1280 1283 ASSERT(msg_size == 0);
1281 1284 mutex_enter(&so->so_lock);
1282 1285 goto space_check;
1283 1286 }
1284 1287
1285 1288 ASSERT(mp->b_next == NULL);
1286 1289 ASSERT(DB_TYPE(mp) == M_DATA || DB_TYPE(mp) == M_PROTO);
1287 1290 ASSERT(msg_size == msgdsize(mp));
1288 1291
1289 1292 if (DB_TYPE(mp) == M_PROTO && !__TPI_PRIM_ISALIGNED(mp->b_rptr)) {
1290 1293 /* The read pointer is not aligned correctly for TPI */
1291 1294 zcmn_err(getzoneid(), CE_WARN,
1292 1295 "sockfs: Unaligned TPI message received. rptr = %p\n",
1293 1296 (void *)mp->b_rptr);
1294 1297 freemsg(mp);
1295 1298 mutex_enter(&so->so_lock);
1296 1299 if (sodp != NULL)
1297 1300 SOD_UIOAFINI(sodp);
1298 1301 goto space_check;
1299 1302 }
1300 1303
1301 1304 if (so->so_filter_active > 0) {
1302 1305 for (; filter != NULL; filter = filter->sofi_prev) {
1303 1306 if (!SOF_INTERESTED(filter, data_in))
1304 1307 continue;
1305 1308 mp = (*filter->sofi_ops->sofop_data_in)(
1306 1309 (sof_handle_t)filter, filter->sofi_cookie, mp,
1307 1310 flags, &msg_size);
1308 1311 ASSERT(msgdsize(mp) == msg_size);
1309 1312 DTRACE_PROBE2(filter__data, (sof_instance_t), filter,
1310 1313 (mblk_t *), mp);
1311 1314 /* Data was consumed/dropped, just do space check */
1312 1315 if (msg_size == 0) {
1313 1316 mutex_enter(&so->so_lock);
1314 1317 goto space_check;
1315 1318 }
1316 1319 }
1317 1320 }
1318 1321
1319 1322 if (flags & MSG_OOB) {
1320 1323 so_queue_oob(so, mp, msg_size);
1321 1324 mutex_enter(&so->so_lock);
1322 1325 goto space_check;
1323 1326 }
1324 1327
1325 1328 if (force_pushp != NULL)
1326 1329 force_push = *force_pushp;
1327 1330
1328 1331 mutex_enter(&so->so_lock);
1329 1332 if (so->so_state & (SS_FALLBACK_DRAIN | SS_FALLBACK_COMP)) {
1330 1333 if (sodp != NULL)
1331 1334 SOD_DISABLE(sodp);
1332 1335 mutex_exit(&so->so_lock);
1333 1336 *errorp = EOPNOTSUPP;
1334 1337 return (-1);
1335 1338 }
1336 1339 if (so->so_state & (SS_CANTRCVMORE | SS_CLOSING)) {
1337 1340 freemsg(mp);
1338 1341 if (sodp != NULL)
1339 1342 SOD_DISABLE(sodp);
1340 1343 mutex_exit(&so->so_lock);
1341 1344 return (0);
1342 1345 }
1343 1346
1344 1347 /* process the mblk via I/OAT if capable */
1345 1348 if (sodp != NULL && sodp->sod_enabled) {
1346 1349 if (DB_TYPE(mp) == M_DATA) {
1347 1350 sod_uioa_mblk_init(sodp, mp, msg_size);
1348 1351 } else {
1349 1352 SOD_UIOAFINI(sodp);
1350 1353 }
1351 1354 }
1352 1355
1353 1356 if (mp->b_next == NULL) {
1354 1357 so_enqueue_msg(so, mp, msg_size);
1355 1358 } else {
1356 1359 do {
1357 1360 mblk_t *nmp;
1358 1361
1359 1362 if ((nmp = mp->b_next) != NULL) {
1360 1363 mp->b_next = NULL;
1361 1364 }
1362 1365 so_enqueue_msg(so, mp, msgdsize(mp));
1363 1366 mp = nmp;
1364 1367 } while (mp != NULL);
1365 1368 }
1366 1369
1367 1370 space_left = so->so_rcvbuf - so->so_rcv_queued;
1368 1371 if (space_left <= 0) {
1369 1372 so->so_flowctrld = B_TRUE;
1370 1373 *errorp = ENOSPC;
1371 1374 space_left = -1;
1372 1375 }
1373 1376
1374 1377 if (force_push || so->so_rcv_queued >= so->so_rcv_thresh ||
1375 1378 so->so_rcv_queued >= so->so_rcv_wanted) {
1376 1379 SOCKET_TIMER_CANCEL(so);
1377 1380 /*
1378 1381 * so_notify_data will release the lock
1379 1382 */
1380 1383 so_notify_data(so, so->so_rcv_queued);
1381 1384
1382 1385 if (force_pushp != NULL)
1383 1386 *force_pushp = B_TRUE;
1384 1387 goto done;
1385 1388 } else if (so->so_rcv_timer_tid == 0) {
1386 1389 /* Make sure the recv push timer is running */
1387 1390 SOCKET_TIMER_START(so);
1388 1391 }
1389 1392
1390 1393 done_unlock:
1391 1394 mutex_exit(&so->so_lock);
1392 1395 done:
1393 1396 return (space_left);
1394 1397
1395 1398 space_check:
1396 1399 space_left = so->so_rcvbuf - so->so_rcv_queued;
1397 1400 if (space_left <= 0) {
1398 1401 so->so_flowctrld = B_TRUE;
1399 1402 *errorp = ENOSPC;
1400 1403 space_left = -1;
1401 1404 }
1402 1405 goto done_unlock;
1403 1406 }
1404 1407
1405 1408 #pragma inline(so_queue_msg_impl)
1406 1409
1407 1410 ssize_t
1408 1411 so_queue_msg(sock_upper_handle_t sock_handle, mblk_t *mp,
1409 1412 size_t msg_size, int flags, int *errorp, boolean_t *force_pushp)
1410 1413 {
1411 1414 struct sonode *so = (struct sonode *)sock_handle;
1412 1415
1413 1416 return (so_queue_msg_impl(so, mp, msg_size, flags, errorp, force_pushp,
1414 1417 so->so_filter_bottom));
1415 1418 }
1416 1419
1417 1420 /*
1418 1421 * Set the offset of where the oob data is relative to the bytes in
1419 1422 * queued. Also generate SIGURG
1420 1423 */
1421 1424 void
1422 1425 so_signal_oob(sock_upper_handle_t sock_handle, ssize_t offset)
1423 1426 {
1424 1427 struct sonode *so;
1425 1428
1426 1429 ASSERT(offset >= 0);
1427 1430 so = (struct sonode *)sock_handle;
1428 1431 mutex_enter(&so->so_lock);
1429 1432 if (so->so_direct != NULL)
1430 1433 SOD_UIOAFINI(so->so_direct);
1431 1434
1432 1435 /*
1433 1436 * New urgent data on the way so forget about any old
1434 1437 * urgent data.
1435 1438 */
1436 1439 so->so_state &= ~(SS_HAVEOOBDATA|SS_HADOOBDATA);
1437 1440
1438 1441 /*
1439 1442 * Record that urgent data is pending.
1440 1443 */
1441 1444 so->so_state |= SS_OOBPEND;
1442 1445
1443 1446 if (so->so_oobmsg != NULL) {
1444 1447 dprintso(so, 1, ("sock: discarding old oob\n"));
1445 1448 freemsg(so->so_oobmsg);
1446 1449 so->so_oobmsg = NULL;
1447 1450 }
1448 1451
1449 1452 /*
1450 1453 * set the offset where the urgent byte is
1451 1454 */
1452 1455 so->so_oobmark = so->so_rcv_queued + offset;
1453 1456 if (so->so_oobmark == 0)
1454 1457 so->so_state |= SS_RCVATMARK;
1455 1458 else
1456 1459 so->so_state &= ~SS_RCVATMARK;
1457 1460
1458 1461 so_notify_oobsig(so);
1459 1462 }
1460 1463
1461 1464 /*
1462 1465 * Queue the OOB byte
1463 1466 */
1464 1467 static void
1465 1468 so_queue_oob(struct sonode *so, mblk_t *mp, size_t len)
1466 1469 {
1467 1470 mutex_enter(&so->so_lock);
1468 1471 if (so->so_direct != NULL)
1469 1472 SOD_UIOAFINI(so->so_direct);
1470 1473
1471 1474 ASSERT(mp != NULL);
1472 1475 if (!IS_SO_OOB_INLINE(so)) {
1473 1476 so->so_oobmsg = mp;
1474 1477 so->so_state |= SS_HAVEOOBDATA;
1475 1478 } else {
1476 1479 so_enqueue_msg(so, mp, len);
1477 1480 }
1478 1481
1479 1482 so_notify_oobdata(so, IS_SO_OOB_INLINE(so));
1480 1483 }
1481 1484
1482 1485 int
1483 1486 so_close(struct sonode *so, int flag, struct cred *cr)
1484 1487 {
1485 1488 int error;
1486 1489
1487 1490 /*
1488 1491 * No new data will be enqueued once the CLOSING flag is set.
1489 1492 */
1490 1493 mutex_enter(&so->so_lock);
1491 1494 so->so_state |= SS_CLOSING;
1492 1495 ASSERT(so_verify_oobstate(so));
1493 1496 so_rcv_flush(so);
1494 1497 mutex_exit(&so->so_lock);
1495 1498
1496 1499 if (so->so_filter_active > 0)
1497 1500 sof_sonode_closing(so);
1498 1501
1499 1502 if (so->so_state & SS_ACCEPTCONN) {
1500 1503 /*
1501 1504 * We grab and release the accept lock to ensure that any
1502 1505 * thread about to insert a socket in so_newconn completes
1503 1506 * before we flush the queue. Any thread calling so_newconn
1504 1507 * after we drop the lock will observe the SS_CLOSING flag,
1505 1508 * which will stop it from inserting the socket in the queue.
1506 1509 */
1507 1510 mutex_enter(&so->so_acceptq_lock);
1508 1511 mutex_exit(&so->so_acceptq_lock);
1509 1512
1510 1513 so_acceptq_flush(so, B_TRUE);
1511 1514 }
1512 1515
1513 1516 error = (*so->so_downcalls->sd_close)(so->so_proto_handle, flag, cr);
1514 1517 switch (error) {
1515 1518 default:
1516 1519 /* Protocol made a synchronous close; remove proto ref */
1517 1520 VN_RELE(SOTOV(so));
1518 1521 break;
1519 1522 case EINPROGRESS:
1520 1523 /*
1521 1524 * Protocol is in the process of closing, it will make a
1522 1525 * 'closed' upcall to remove the reference.
1523 1526 */
1524 1527 error = 0;
1525 1528 break;
1526 1529 }
1527 1530
1528 1531 return (error);
1529 1532 }
1530 1533
1531 1534 /*
1532 1535 * Upcall made by the protocol when it's doing an asynchronous close. It
↓ open down ↓ |
1163 lines elided |
↑ open up ↑ |
1533 1536 * will drop the protocol's reference on the socket.
1534 1537 */
1535 1538 void
1536 1539 so_closed(sock_upper_handle_t sock_handle)
1537 1540 {
1538 1541 struct sonode *so = (struct sonode *)sock_handle;
1539 1542
1540 1543 VN_RELE(SOTOV(so));
1541 1544 }
1542 1545
1546 +mblk_t *
1547 +so_get_sock_pid_mblk(sock_upper_handle_t sock_handle)
1548 +{
1549 + int sz, n = 0;
1550 + mblk_t *mblk;
1551 + pid_node_t *pn;
1552 + pid_t *pids;
1553 + conn_pid_info_t *cpi;
1554 + struct sonode *so = (struct sonode *)sock_handle;
1555 +
1556 + mutex_enter(&so->so_pid_list_lock);
1557 +
1558 + n = list_numnodes(&so->so_pid_list);
1559 + sz = sizeof (conn_pid_info_t);
1560 + sz += (n > 1) ? ((n - 1) * sizeof (pid_t)) : 0;
1561 + if ((mblk = allocb(sz, BPRI_HI)) == NULL) {
1562 + mutex_exit(&so->so_pid_list_lock);
1563 + return (NULL);
1564 + }
1565 + mblk->b_wptr += sz;
1566 + cpi = (conn_pid_info_t *)mblk->b_datap->db_base;
1567 +
1568 + cpi->cpi_magic = CONN_PID_INFO_MGC;
1569 + cpi->cpi_contents = CONN_PID_INFO_SOC;
1570 + cpi->cpi_pids_cnt = n;
1571 + cpi->cpi_tot_size = sz;
1572 + cpi->cpi_pids[0] = 0;
1573 +
1574 + if (cpi->cpi_pids_cnt > 0) {
1575 + pids = cpi->cpi_pids;
1576 + for (pn = list_head(&so->so_pid_list); pn != NULL;
1577 + pids++, pn = list_next(&so->so_pid_list, pn))
1578 + *pids = pn->pn_pid;
1579 + }
1580 + mutex_exit(&so->so_pid_list_lock);
1581 + return (mblk);
1582 +}
1583 +
1543 1584 void
1544 1585 so_zcopy_notify(sock_upper_handle_t sock_handle)
1545 1586 {
1546 1587 struct sonode *so = (struct sonode *)sock_handle;
1547 1588
1548 1589 mutex_enter(&so->so_lock);
1549 1590 so->so_copyflag |= STZCNOTIFY;
1550 1591 cv_broadcast(&so->so_copy_cv);
1551 1592 mutex_exit(&so->so_lock);
1552 1593 }
1553 1594
1554 1595 void
1555 1596 so_set_error(sock_upper_handle_t sock_handle, int error)
1556 1597 {
1557 1598 struct sonode *so = (struct sonode *)sock_handle;
1558 1599
1559 1600 mutex_enter(&so->so_lock);
1560 1601
1561 1602 soseterror(so, error);
1562 1603
1563 1604 so_notify_error(so);
1564 1605 }
1565 1606
1566 1607 /*
1567 1608 * so_recvmsg - read data from the socket
1568 1609 *
1569 1610 * There are two ways of obtaining data; either we ask the protocol to
1570 1611 * copy directly into the supplied buffer, or we copy data from the
1571 1612 * sonode's receive queue. The decision which one to use depends on
1572 1613 * whether the protocol has a sd_recv_uio down call.
1573 1614 */
1574 1615 int
1575 1616 so_recvmsg(struct sonode *so, struct nmsghdr *msg, struct uio *uiop,
1576 1617 struct cred *cr)
1577 1618 {
1578 1619 rval_t rval;
1579 1620 int flags = 0;
1580 1621 t_uscalar_t controllen, namelen;
1581 1622 int error = 0;
1582 1623 int ret;
1583 1624 mblk_t *mctlp = NULL;
1584 1625 union T_primitives *tpr;
1585 1626 void *control;
1586 1627 ssize_t saved_resid;
1587 1628 struct uio *suiop;
1588 1629
1589 1630 SO_BLOCK_FALLBACK(so, SOP_RECVMSG(so, msg, uiop, cr));
1590 1631
1591 1632 if ((so->so_state & (SS_ISCONNECTED|SS_CANTRCVMORE)) == 0 &&
1592 1633 (so->so_mode & SM_CONNREQUIRED)) {
1593 1634 SO_UNBLOCK_FALLBACK(so);
1594 1635 return (ENOTCONN);
1595 1636 }
1596 1637
1597 1638 if (msg->msg_flags & MSG_PEEK)
1598 1639 msg->msg_flags &= ~MSG_WAITALL;
1599 1640
1600 1641 if (so->so_mode & SM_ATOMIC)
1601 1642 msg->msg_flags |= MSG_TRUNC;
1602 1643
1603 1644 if (msg->msg_flags & MSG_OOB) {
1604 1645 if ((so->so_mode & SM_EXDATA) == 0) {
1605 1646 error = EOPNOTSUPP;
1606 1647 } else if (so->so_downcalls->sd_recv_uio != NULL) {
1607 1648 error = (*so->so_downcalls->sd_recv_uio)
1608 1649 (so->so_proto_handle, uiop, msg, cr);
1609 1650 } else {
1610 1651 error = sorecvoob(so, msg, uiop, msg->msg_flags,
1611 1652 IS_SO_OOB_INLINE(so));
1612 1653 }
1613 1654 SO_UNBLOCK_FALLBACK(so);
1614 1655 return (error);
1615 1656 }
1616 1657
1617 1658 /*
1618 1659 * If the protocol has the recv down call, then pass the request
1619 1660 * down.
1620 1661 */
1621 1662 if (so->so_downcalls->sd_recv_uio != NULL) {
1622 1663 error = (*so->so_downcalls->sd_recv_uio)
1623 1664 (so->so_proto_handle, uiop, msg, cr);
1624 1665 SO_UNBLOCK_FALLBACK(so);
1625 1666 return (error);
1626 1667 }
1627 1668
1628 1669 /*
1629 1670 * Reading data from the socket buffer
1630 1671 */
1631 1672 flags = msg->msg_flags;
1632 1673 msg->msg_flags = 0;
1633 1674
1634 1675 /*
1635 1676 * Set msg_controllen and msg_namelen to zero here to make it
1636 1677 * simpler in the cases that no control or name is returned.
1637 1678 */
1638 1679 controllen = msg->msg_controllen;
1639 1680 namelen = msg->msg_namelen;
1640 1681 msg->msg_controllen = 0;
1641 1682 msg->msg_namelen = 0;
1642 1683
1643 1684 mutex_enter(&so->so_lock);
1644 1685 /* Set SOREADLOCKED */
1645 1686 error = so_lock_read_intr(so,
1646 1687 uiop->uio_fmode | ((flags & MSG_DONTWAIT) ? FNONBLOCK : 0));
1647 1688 mutex_exit(&so->so_lock);
1648 1689 if (error) {
1649 1690 SO_UNBLOCK_FALLBACK(so);
1650 1691 return (error);
1651 1692 }
1652 1693
1653 1694 suiop = sod_rcv_init(so, flags, &uiop);
1654 1695 retry:
1655 1696 saved_resid = uiop->uio_resid;
1656 1697 error = so_dequeue_msg(so, &mctlp, uiop, &rval, flags);
1657 1698 if (error != 0) {
1658 1699 goto out;
1659 1700 }
1660 1701 /*
1661 1702 * For datagrams the MOREDATA flag is used to set MSG_TRUNC.
1662 1703 * For non-datagrams MOREDATA is used to set MSG_EOR.
1663 1704 */
1664 1705 ASSERT(!(rval.r_val1 & MORECTL));
1665 1706 if ((rval.r_val1 & MOREDATA) && (so->so_mode & SM_ATOMIC))
1666 1707 msg->msg_flags |= MSG_TRUNC;
1667 1708 if (mctlp == NULL) {
1668 1709 dprintso(so, 1, ("so_recvmsg: got M_DATA\n"));
1669 1710
1670 1711 mutex_enter(&so->so_lock);
1671 1712 /* Set MSG_EOR based on MOREDATA */
1672 1713 if (!(rval.r_val1 & MOREDATA)) {
1673 1714 if (so->so_state & SS_SAVEDEOR) {
1674 1715 msg->msg_flags |= MSG_EOR;
1675 1716 so->so_state &= ~SS_SAVEDEOR;
1676 1717 }
1677 1718 }
1678 1719 /*
1679 1720 * If some data was received (i.e. not EOF) and the
1680 1721 * read/recv* has not been satisfied wait for some more.
1681 1722 */
1682 1723 if ((flags & MSG_WAITALL) && !(msg->msg_flags & MSG_EOR) &&
1683 1724 uiop->uio_resid != saved_resid && uiop->uio_resid > 0) {
1684 1725 mutex_exit(&so->so_lock);
1685 1726 flags |= MSG_NOMARK;
1686 1727 goto retry;
1687 1728 }
1688 1729
1689 1730 goto out_locked;
1690 1731 }
1691 1732 /* so_queue_msg has already verified length and alignment */
1692 1733 tpr = (union T_primitives *)mctlp->b_rptr;
1693 1734 dprintso(so, 1, ("so_recvmsg: type %d\n", tpr->type));
1694 1735 switch (tpr->type) {
1695 1736 case T_DATA_IND: {
1696 1737 /*
1697 1738 * Set msg_flags to MSG_EOR based on
1698 1739 * MORE_flag and MOREDATA.
1699 1740 */
1700 1741 mutex_enter(&so->so_lock);
1701 1742 so->so_state &= ~SS_SAVEDEOR;
1702 1743 if (!(tpr->data_ind.MORE_flag & 1)) {
1703 1744 if (!(rval.r_val1 & MOREDATA))
1704 1745 msg->msg_flags |= MSG_EOR;
1705 1746 else
1706 1747 so->so_state |= SS_SAVEDEOR;
1707 1748 }
1708 1749 freemsg(mctlp);
1709 1750 /*
1710 1751 * If some data was received (i.e. not EOF) and the
1711 1752 * read/recv* has not been satisfied wait for some more.
1712 1753 */
1713 1754 if ((flags & MSG_WAITALL) && !(msg->msg_flags & MSG_EOR) &&
1714 1755 uiop->uio_resid != saved_resid && uiop->uio_resid > 0) {
1715 1756 mutex_exit(&so->so_lock);
1716 1757 flags |= MSG_NOMARK;
1717 1758 goto retry;
1718 1759 }
1719 1760 goto out_locked;
1720 1761 }
1721 1762 case T_UNITDATA_IND: {
1722 1763 void *addr;
1723 1764 t_uscalar_t addrlen;
1724 1765 void *abuf;
1725 1766 t_uscalar_t optlen;
1726 1767 void *opt;
1727 1768
1728 1769 if (namelen != 0) {
1729 1770 /* Caller wants source address */
1730 1771 addrlen = tpr->unitdata_ind.SRC_length;
1731 1772 addr = sogetoff(mctlp, tpr->unitdata_ind.SRC_offset,
1732 1773 addrlen, 1);
1733 1774 if (addr == NULL) {
1734 1775 freemsg(mctlp);
1735 1776 error = EPROTO;
1736 1777 eprintsoline(so, error);
1737 1778 goto out;
1738 1779 }
1739 1780 ASSERT(so->so_family != AF_UNIX);
1740 1781 }
1741 1782 optlen = tpr->unitdata_ind.OPT_length;
1742 1783 if (optlen != 0) {
1743 1784 t_uscalar_t ncontrollen;
1744 1785
1745 1786 /*
1746 1787 * Extract any source address option.
1747 1788 * Determine how large cmsg buffer is needed.
1748 1789 */
1749 1790 opt = sogetoff(mctlp, tpr->unitdata_ind.OPT_offset,
1750 1791 optlen, __TPI_ALIGN_SIZE);
1751 1792
1752 1793 if (opt == NULL) {
1753 1794 freemsg(mctlp);
1754 1795 error = EPROTO;
1755 1796 eprintsoline(so, error);
1756 1797 goto out;
1757 1798 }
1758 1799 if (so->so_family == AF_UNIX)
1759 1800 so_getopt_srcaddr(opt, optlen, &addr, &addrlen);
1760 1801 ncontrollen = so_cmsglen(mctlp, opt, optlen,
1761 1802 !(flags & MSG_XPG4_2));
1762 1803 if (controllen != 0)
1763 1804 controllen = ncontrollen;
1764 1805 else if (ncontrollen != 0)
1765 1806 msg->msg_flags |= MSG_CTRUNC;
1766 1807 } else {
1767 1808 controllen = 0;
1768 1809 }
1769 1810
1770 1811 if (namelen != 0) {
1771 1812 /*
1772 1813 * Return address to caller.
1773 1814 * Caller handles truncation if length
1774 1815 * exceeds msg_namelen.
1775 1816 * NOTE: AF_UNIX NUL termination is ensured by
1776 1817 * the sender's copyin_name().
1777 1818 */
1778 1819 abuf = kmem_alloc(addrlen, KM_SLEEP);
1779 1820
1780 1821 bcopy(addr, abuf, addrlen);
1781 1822 msg->msg_name = abuf;
1782 1823 msg->msg_namelen = addrlen;
1783 1824 }
1784 1825
1785 1826 if (controllen != 0) {
1786 1827 /*
1787 1828 * Return control msg to caller.
1788 1829 * Caller handles truncation if length
1789 1830 * exceeds msg_controllen.
1790 1831 */
1791 1832 control = kmem_zalloc(controllen, KM_SLEEP);
1792 1833
1793 1834 error = so_opt2cmsg(mctlp, opt, optlen,
1794 1835 !(flags & MSG_XPG4_2), control, controllen);
1795 1836 if (error) {
1796 1837 freemsg(mctlp);
1797 1838 if (msg->msg_namelen != 0)
1798 1839 kmem_free(msg->msg_name,
1799 1840 msg->msg_namelen);
1800 1841 kmem_free(control, controllen);
1801 1842 eprintsoline(so, error);
1802 1843 goto out;
1803 1844 }
1804 1845 msg->msg_control = control;
1805 1846 msg->msg_controllen = controllen;
1806 1847 }
1807 1848
1808 1849 freemsg(mctlp);
1809 1850 goto out;
1810 1851 }
1811 1852 case T_OPTDATA_IND: {
1812 1853 struct T_optdata_req *tdr;
1813 1854 void *opt;
1814 1855 t_uscalar_t optlen;
1815 1856
1816 1857 tdr = (struct T_optdata_req *)mctlp->b_rptr;
1817 1858 optlen = tdr->OPT_length;
1818 1859 if (optlen != 0) {
1819 1860 t_uscalar_t ncontrollen;
1820 1861 /*
1821 1862 * Determine how large cmsg buffer is needed.
1822 1863 */
1823 1864 opt = sogetoff(mctlp,
1824 1865 tpr->optdata_ind.OPT_offset, optlen,
1825 1866 __TPI_ALIGN_SIZE);
1826 1867
1827 1868 if (opt == NULL) {
1828 1869 freemsg(mctlp);
1829 1870 error = EPROTO;
1830 1871 eprintsoline(so, error);
1831 1872 goto out;
1832 1873 }
1833 1874
1834 1875 ncontrollen = so_cmsglen(mctlp, opt, optlen,
1835 1876 !(flags & MSG_XPG4_2));
1836 1877 if (controllen != 0)
1837 1878 controllen = ncontrollen;
1838 1879 else if (ncontrollen != 0)
1839 1880 msg->msg_flags |= MSG_CTRUNC;
1840 1881 } else {
1841 1882 controllen = 0;
1842 1883 }
1843 1884
1844 1885 if (controllen != 0) {
1845 1886 /*
1846 1887 * Return control msg to caller.
1847 1888 * Caller handles truncation if length
1848 1889 * exceeds msg_controllen.
1849 1890 */
1850 1891 control = kmem_zalloc(controllen, KM_SLEEP);
1851 1892
1852 1893 error = so_opt2cmsg(mctlp, opt, optlen,
1853 1894 !(flags & MSG_XPG4_2), control, controllen);
1854 1895 if (error) {
1855 1896 freemsg(mctlp);
1856 1897 kmem_free(control, controllen);
1857 1898 eprintsoline(so, error);
1858 1899 goto out;
1859 1900 }
1860 1901 msg->msg_control = control;
1861 1902 msg->msg_controllen = controllen;
1862 1903 }
1863 1904
1864 1905 /*
1865 1906 * Set msg_flags to MSG_EOR based on
1866 1907 * DATA_flag and MOREDATA.
1867 1908 */
1868 1909 mutex_enter(&so->so_lock);
1869 1910 so->so_state &= ~SS_SAVEDEOR;
1870 1911 if (!(tpr->data_ind.MORE_flag & 1)) {
1871 1912 if (!(rval.r_val1 & MOREDATA))
1872 1913 msg->msg_flags |= MSG_EOR;
1873 1914 else
1874 1915 so->so_state |= SS_SAVEDEOR;
1875 1916 }
1876 1917 freemsg(mctlp);
1877 1918 /*
1878 1919 * If some data was received (i.e. not EOF) and the
1879 1920 * read/recv* has not been satisfied wait for some more.
1880 1921 * Not possible to wait if control info was received.
1881 1922 */
1882 1923 if ((flags & MSG_WAITALL) && !(msg->msg_flags & MSG_EOR) &&
1883 1924 controllen == 0 &&
1884 1925 uiop->uio_resid != saved_resid && uiop->uio_resid > 0) {
1885 1926 mutex_exit(&so->so_lock);
1886 1927 flags |= MSG_NOMARK;
1887 1928 goto retry;
1888 1929 }
1889 1930 goto out_locked;
1890 1931 }
1891 1932 default:
1892 1933 cmn_err(CE_CONT, "so_recvmsg bad type %x \n",
1893 1934 tpr->type);
1894 1935 freemsg(mctlp);
1895 1936 error = EPROTO;
1896 1937 ASSERT(0);
1897 1938 }
1898 1939 out:
1899 1940 mutex_enter(&so->so_lock);
1900 1941 out_locked:
1901 1942 ret = sod_rcv_done(so, suiop, uiop);
1902 1943 if (ret != 0 && error == 0)
1903 1944 error = ret;
1904 1945
1905 1946 so_unlock_read(so); /* Clear SOREADLOCKED */
1906 1947 mutex_exit(&so->so_lock);
1907 1948
1908 1949 SO_UNBLOCK_FALLBACK(so);
1909 1950
1910 1951 return (error);
1911 1952 }
1912 1953
1913 1954 sonodeops_t so_sonodeops = {
1914 1955 so_init, /* sop_init */
1915 1956 so_accept, /* sop_accept */
1916 1957 so_bind, /* sop_bind */
1917 1958 so_listen, /* sop_listen */
1918 1959 so_connect, /* sop_connect */
1919 1960 so_recvmsg, /* sop_recvmsg */
1920 1961 so_sendmsg, /* sop_sendmsg */
1921 1962 so_sendmblk, /* sop_sendmblk */
1922 1963 so_getpeername, /* sop_getpeername */
1923 1964 so_getsockname, /* sop_getsockname */
1924 1965 so_shutdown, /* sop_shutdown */
1925 1966 so_getsockopt, /* sop_getsockopt */
1926 1967 so_setsockopt, /* sop_setsockopt */
1927 1968 so_ioctl, /* sop_ioctl */
1928 1969 so_poll, /* sop_poll */
1929 1970 so_close, /* sop_close */
1930 1971 };
1931 1972
1932 1973 sock_upcalls_t so_upcalls = {
↓ open down ↓ |
380 lines elided |
↑ open up ↑ |
1933 1974 so_newconn,
1934 1975 so_connected,
1935 1976 so_disconnected,
1936 1977 so_opctl,
1937 1978 so_queue_msg,
1938 1979 so_set_prop,
1939 1980 so_txq_full,
1940 1981 so_signal_oob,
1941 1982 so_zcopy_notify,
1942 1983 so_set_error,
1943 - so_closed
1984 + so_closed,
1985 + so_get_sock_pid_mblk
1944 1986 };
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX