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 + sonode_insert_pid(nso, curproc);
360 362 }
361 363 }
362 364
363 365 SO_UNBLOCK_FALLBACK(so);
364 366 return (error);
365 367 }
366 368
367 369 int
368 370 so_sendmsg(struct sonode *so, struct nmsghdr *msg, struct uio *uiop,
369 371 struct cred *cr)
370 372 {
371 373 int error, flags;
372 374 boolean_t dontblock;
373 375 ssize_t orig_resid;
374 376 mblk_t *mp;
375 377
376 378 SO_BLOCK_FALLBACK(so, SOP_SENDMSG(so, msg, uiop, cr));
377 379
378 380 flags = msg->msg_flags;
379 381 error = 0;
380 382 dontblock = (flags & MSG_DONTWAIT) ||
381 383 (uiop->uio_fmode & (FNONBLOCK|FNDELAY));
382 384
383 385 if (!(flags & MSG_XPG4_2) && msg->msg_controllen != 0) {
384 386 /*
385 387 * Old way of passing fd's is not supported
386 388 */
387 389 SO_UNBLOCK_FALLBACK(so);
388 390 return (EOPNOTSUPP);
389 391 }
390 392
391 393 if ((so->so_mode & SM_ATOMIC) &&
392 394 uiop->uio_resid > so->so_proto_props.sopp_maxpsz &&
393 395 so->so_proto_props.sopp_maxpsz != -1) {
394 396 SO_UNBLOCK_FALLBACK(so);
395 397 return (EMSGSIZE);
396 398 }
397 399
398 400 /*
399 401 * For atomic sends we will only do one iteration.
400 402 */
401 403 do {
402 404 if (so->so_state & SS_CANTSENDMORE) {
403 405 error = EPIPE;
404 406 break;
405 407 }
406 408
407 409 if (so->so_error != 0) {
408 410 mutex_enter(&so->so_lock);
409 411 error = sogeterr(so, B_TRUE);
410 412 mutex_exit(&so->so_lock);
411 413 if (error != 0)
412 414 break;
413 415 }
414 416
415 417 /*
416 418 * Send down OOB messages even if the send path is being
417 419 * flow controlled (assuming the protocol supports OOB data).
418 420 */
419 421 if (flags & MSG_OOB) {
420 422 if ((so->so_mode & SM_EXDATA) == 0) {
421 423 error = EOPNOTSUPP;
422 424 break;
423 425 }
424 426 } else if (SO_SND_FLOWCTRLD(so)) {
425 427 /*
426 428 * Need to wait until the protocol is ready to receive
427 429 * more data for transmission.
428 430 */
429 431 if ((error = so_snd_wait_qnotfull(so, dontblock)) != 0)
430 432 break;
431 433 }
432 434
433 435 /*
434 436 * Time to send data to the protocol. We either copy the
435 437 * data into mblks or pass the uio directly to the protocol.
436 438 * We decide what to do based on the available down calls.
437 439 */
438 440 if (so->so_downcalls->sd_send_uio != NULL) {
439 441 error = (*so->so_downcalls->sd_send_uio)
440 442 (so->so_proto_handle, uiop, msg, cr);
441 443 if (error != 0)
442 444 break;
443 445 } else {
444 446 /* save the resid in case of failure */
445 447 orig_resid = uiop->uio_resid;
446 448
447 449 if ((mp = socopyinuio(uiop,
448 450 so->so_proto_props.sopp_maxpsz,
449 451 so->so_proto_props.sopp_wroff,
450 452 so->so_proto_props.sopp_maxblk,
451 453 so->so_proto_props.sopp_tail, &error)) == NULL) {
452 454 break;
453 455 }
454 456 ASSERT(uiop->uio_resid >= 0);
455 457
456 458 if (so->so_filter_active > 0 &&
457 459 ((mp = SOF_FILTER_DATA_OUT(so, mp, msg, cr,
458 460 &error)) == NULL)) {
459 461 if (error != 0)
460 462 break;
461 463 continue;
462 464 }
463 465 error = (*so->so_downcalls->sd_send)
464 466 (so->so_proto_handle, mp, msg, cr);
465 467 if (error != 0) {
466 468 /*
467 469 * The send failed. We do not have to free the
468 470 * mblks, because that is the protocol's
469 471 * responsibility. However, uio_resid must
470 472 * remain accurate, so adjust that here.
471 473 */
472 474 uiop->uio_resid = orig_resid;
473 475 break;
474 476 }
475 477 }
476 478 } while (uiop->uio_resid > 0);
477 479
478 480 SO_UNBLOCK_FALLBACK(so);
479 481
480 482 return (error);
481 483 }
482 484
483 485 int
484 486 so_sendmblk_impl(struct sonode *so, struct nmsghdr *msg, int fflag,
485 487 struct cred *cr, mblk_t **mpp, sof_instance_t *fil,
486 488 boolean_t fil_inject)
487 489 {
488 490 int error;
489 491 boolean_t dontblock;
490 492 size_t size;
491 493 mblk_t *mp = *mpp;
492 494
493 495 if (so->so_downcalls->sd_send == NULL)
494 496 return (EOPNOTSUPP);
495 497
496 498 error = 0;
497 499 dontblock = (msg->msg_flags & MSG_DONTWAIT) ||
498 500 (fflag & (FNONBLOCK|FNDELAY));
499 501 size = msgdsize(mp);
500 502
501 503 if ((so->so_mode & SM_ATOMIC) &&
502 504 size > so->so_proto_props.sopp_maxpsz &&
503 505 so->so_proto_props.sopp_maxpsz != -1) {
504 506 SO_UNBLOCK_FALLBACK(so);
505 507 return (EMSGSIZE);
506 508 }
507 509
508 510 while (mp != NULL) {
509 511 mblk_t *nmp, *last_mblk;
510 512 size_t mlen;
511 513
512 514 if (so->so_state & SS_CANTSENDMORE) {
513 515 error = EPIPE;
514 516 break;
515 517 }
516 518 if (so->so_error != 0) {
517 519 mutex_enter(&so->so_lock);
518 520 error = sogeterr(so, B_TRUE);
519 521 mutex_exit(&so->so_lock);
520 522 if (error != 0)
521 523 break;
522 524 }
523 525 /* Socket filters are not flow controlled */
524 526 if (SO_SND_FLOWCTRLD(so) && !fil_inject) {
525 527 /*
526 528 * Need to wait until the protocol is ready to receive
527 529 * more data for transmission.
528 530 */
529 531 if ((error = so_snd_wait_qnotfull(so, dontblock)) != 0)
530 532 break;
531 533 }
532 534
533 535 /*
534 536 * We only allow so_maxpsz of data to be sent down to
535 537 * the protocol at time.
536 538 */
537 539 mlen = MBLKL(mp);
538 540 nmp = mp->b_cont;
539 541 last_mblk = mp;
540 542 while (nmp != NULL) {
541 543 mlen += MBLKL(nmp);
542 544 if (mlen > so->so_proto_props.sopp_maxpsz) {
543 545 last_mblk->b_cont = NULL;
544 546 break;
545 547 }
546 548 last_mblk = nmp;
547 549 nmp = nmp->b_cont;
548 550 }
549 551
550 552 if (so->so_filter_active > 0 &&
551 553 (mp = SOF_FILTER_DATA_OUT_FROM(so, fil, mp, msg,
552 554 cr, &error)) == NULL) {
553 555 *mpp = mp = nmp;
554 556 if (error != 0)
555 557 break;
556 558 continue;
557 559 }
558 560 error = (*so->so_downcalls->sd_send)
559 561 (so->so_proto_handle, mp, msg, cr);
560 562 if (error != 0) {
561 563 /*
562 564 * The send failed. The protocol will free the mblks
563 565 * that were sent down. Let the caller deal with the
564 566 * rest.
565 567 */
566 568 *mpp = nmp;
567 569 break;
568 570 }
569 571
570 572 *mpp = mp = nmp;
571 573 }
572 574 /* Let the filter know whether the protocol is flow controlled */
573 575 if (fil_inject && error == 0 && SO_SND_FLOWCTRLD(so))
574 576 error = ENOSPC;
575 577
576 578 return (error);
577 579 }
578 580
579 581 #pragma inline(so_sendmblk_impl)
580 582
581 583 int
582 584 so_sendmblk(struct sonode *so, struct nmsghdr *msg, int fflag,
583 585 struct cred *cr, mblk_t **mpp)
584 586 {
585 587 int error;
586 588
587 589 SO_BLOCK_FALLBACK(so, SOP_SENDMBLK(so, msg, fflag, cr, mpp));
588 590
589 591 if ((so->so_mode & SM_SENDFILESUPP) == 0) {
590 592 SO_UNBLOCK_FALLBACK(so);
591 593 return (EOPNOTSUPP);
592 594 }
593 595
594 596 error = so_sendmblk_impl(so, msg, fflag, cr, mpp, so->so_filter_top,
595 597 B_FALSE);
596 598
597 599 SO_UNBLOCK_FALLBACK(so);
598 600
599 601 return (error);
600 602 }
601 603
602 604 int
603 605 so_shutdown(struct sonode *so, int how, struct cred *cr)
604 606 {
605 607 int error;
606 608
607 609 SO_BLOCK_FALLBACK(so, SOP_SHUTDOWN(so, how, cr));
608 610
609 611 /*
610 612 * SunOS 4.X has no check for datagram sockets.
611 613 * 5.X checks that it is connected (ENOTCONN)
612 614 * X/Open requires that we check the connected state.
613 615 */
614 616 if (!(so->so_state & SS_ISCONNECTED)) {
615 617 if (!xnet_skip_checks) {
616 618 error = ENOTCONN;
617 619 if (xnet_check_print) {
618 620 printf("sockfs: X/Open shutdown check "
619 621 "caused ENOTCONN\n");
620 622 }
621 623 }
622 624 goto done;
623 625 }
624 626
625 627 if (so->so_filter_active == 0 ||
626 628 (error = sof_filter_shutdown(so, &how, cr)) < 0)
627 629 error = ((*so->so_downcalls->sd_shutdown)(so->so_proto_handle,
628 630 how, cr));
629 631
630 632 /*
631 633 * Protocol agreed to shutdown. We need to flush the
632 634 * receive buffer if the receive side is being shutdown.
633 635 */
634 636 if (error == 0 && how != SHUT_WR) {
635 637 mutex_enter(&so->so_lock);
636 638 /* wait for active reader to finish */
637 639 (void) so_lock_read(so, 0);
638 640
639 641 so_rcv_flush(so);
640 642
641 643 so_unlock_read(so);
642 644 mutex_exit(&so->so_lock);
643 645 }
644 646
645 647 done:
646 648 SO_UNBLOCK_FALLBACK(so);
647 649 return (error);
648 650 }
649 651
650 652 int
651 653 so_getsockname(struct sonode *so, struct sockaddr *addr,
652 654 socklen_t *addrlen, struct cred *cr)
653 655 {
654 656 int error;
655 657
656 658 SO_BLOCK_FALLBACK(so, SOP_GETSOCKNAME(so, addr, addrlen, cr));
657 659
658 660 if (so->so_filter_active == 0 ||
659 661 (error = sof_filter_getsockname(so, addr, addrlen, cr)) < 0)
660 662 error = (*so->so_downcalls->sd_getsockname)
661 663 (so->so_proto_handle, addr, addrlen, cr);
662 664
663 665 SO_UNBLOCK_FALLBACK(so);
664 666 return (error);
665 667 }
666 668
667 669 int
668 670 so_getpeername(struct sonode *so, struct sockaddr *addr,
669 671 socklen_t *addrlen, boolean_t accept, struct cred *cr)
670 672 {
671 673 int error;
672 674
673 675 SO_BLOCK_FALLBACK(so, SOP_GETPEERNAME(so, addr, addrlen, accept, cr));
674 676
675 677 if (accept) {
676 678 error = (*so->so_downcalls->sd_getpeername)
677 679 (so->so_proto_handle, addr, addrlen, cr);
678 680 } else if (!(so->so_state & SS_ISCONNECTED)) {
679 681 error = ENOTCONN;
680 682 } else if ((so->so_state & SS_CANTSENDMORE) && !xnet_skip_checks) {
681 683 /* Added this check for X/Open */
682 684 error = EINVAL;
683 685 if (xnet_check_print) {
684 686 printf("sockfs: X/Open getpeername check => EINVAL\n");
685 687 }
686 688 } else if (so->so_filter_active == 0 ||
687 689 (error = sof_filter_getpeername(so, addr, addrlen, cr)) < 0) {
688 690 error = (*so->so_downcalls->sd_getpeername)
689 691 (so->so_proto_handle, addr, addrlen, cr);
690 692 }
691 693
692 694 SO_UNBLOCK_FALLBACK(so);
693 695 return (error);
694 696 }
695 697
696 698 int
697 699 so_getsockopt(struct sonode *so, int level, int option_name,
698 700 void *optval, socklen_t *optlenp, int flags, struct cred *cr)
699 701 {
700 702 int error = 0;
701 703
702 704 if (level == SOL_FILTER)
703 705 return (sof_getsockopt(so, option_name, optval, optlenp, cr));
704 706
705 707 SO_BLOCK_FALLBACK(so,
706 708 SOP_GETSOCKOPT(so, level, option_name, optval, optlenp, flags, cr));
707 709
708 710 if ((so->so_filter_active == 0 ||
709 711 (error = sof_filter_getsockopt(so, level, option_name, optval,
710 712 optlenp, cr)) < 0) &&
711 713 (error = socket_getopt_common(so, level, option_name, optval,
712 714 optlenp, flags)) < 0) {
713 715 error = (*so->so_downcalls->sd_getsockopt)
714 716 (so->so_proto_handle, level, option_name, optval, optlenp,
715 717 cr);
716 718 if (error == ENOPROTOOPT) {
717 719 if (level == SOL_SOCKET) {
718 720 /*
719 721 * If a protocol does not support a particular
720 722 * socket option, set can fail (not allowed)
721 723 * but get can not fail. This is the previous
722 724 * sockfs bahvior.
723 725 */
724 726 switch (option_name) {
725 727 case SO_LINGER:
726 728 if (*optlenp < (t_uscalar_t)
727 729 sizeof (struct linger)) {
728 730 error = EINVAL;
729 731 break;
730 732 }
731 733 error = 0;
732 734 bzero(optval, sizeof (struct linger));
733 735 *optlenp = sizeof (struct linger);
734 736 break;
735 737 case SO_RCVTIMEO:
736 738 case SO_SNDTIMEO:
737 739 if (*optlenp < (t_uscalar_t)
738 740 sizeof (struct timeval)) {
739 741 error = EINVAL;
740 742 break;
741 743 }
742 744 error = 0;
743 745 bzero(optval, sizeof (struct timeval));
744 746 *optlenp = sizeof (struct timeval);
745 747 break;
746 748 case SO_SND_BUFINFO:
747 749 if (*optlenp < (t_uscalar_t)
748 750 sizeof (struct so_snd_bufinfo)) {
749 751 error = EINVAL;
750 752 break;
751 753 }
752 754 error = 0;
753 755 bzero(optval,
754 756 sizeof (struct so_snd_bufinfo));
755 757 *optlenp =
756 758 sizeof (struct so_snd_bufinfo);
757 759 break;
758 760 case SO_DEBUG:
759 761 case SO_REUSEADDR:
760 762 case SO_KEEPALIVE:
761 763 case SO_DONTROUTE:
762 764 case SO_BROADCAST:
763 765 case SO_USELOOPBACK:
764 766 case SO_OOBINLINE:
765 767 case SO_DGRAM_ERRIND:
766 768 case SO_SNDBUF:
767 769 case SO_RCVBUF:
768 770 error = 0;
769 771 *((int32_t *)optval) = 0;
770 772 *optlenp = sizeof (int32_t);
771 773 break;
772 774 default:
773 775 break;
774 776 }
775 777 }
776 778 }
777 779 }
778 780
779 781 SO_UNBLOCK_FALLBACK(so);
780 782 return (error);
781 783 }
782 784
783 785 int
784 786 so_setsockopt(struct sonode *so, int level, int option_name,
785 787 const void *optval, socklen_t optlen, struct cred *cr)
786 788 {
787 789 int error = 0;
788 790 struct timeval tl;
789 791 const void *opt = optval;
790 792
791 793 if (level == SOL_FILTER)
792 794 return (sof_setsockopt(so, option_name, optval, optlen, cr));
793 795
794 796 SO_BLOCK_FALLBACK(so,
795 797 SOP_SETSOCKOPT(so, level, option_name, optval, optlen, cr));
796 798
797 799 /* X/Open requires this check */
798 800 if (so->so_state & SS_CANTSENDMORE && !xnet_skip_checks) {
799 801 SO_UNBLOCK_FALLBACK(so);
800 802 if (xnet_check_print)
801 803 printf("sockfs: X/Open setsockopt check => EINVAL\n");
802 804 return (EINVAL);
803 805 }
804 806
805 807 if (so->so_filter_active > 0 &&
806 808 (error = sof_filter_setsockopt(so, level, option_name,
807 809 (void *)optval, &optlen, cr)) >= 0)
808 810 goto done;
809 811
810 812 if (level == SOL_SOCKET) {
811 813 switch (option_name) {
812 814 case SO_RCVTIMEO:
813 815 case SO_SNDTIMEO: {
814 816 /*
815 817 * We pass down these two options to protocol in order
816 818 * to support some third part protocols which need to
817 819 * know them. For those protocols which don't care
818 820 * these two options, simply return 0.
819 821 */
820 822 clock_t t_usec;
821 823
822 824 if (get_udatamodel() == DATAMODEL_NONE ||
823 825 get_udatamodel() == DATAMODEL_NATIVE) {
824 826 if (optlen != sizeof (struct timeval)) {
825 827 error = EINVAL;
826 828 goto done;
827 829 }
828 830 bcopy((struct timeval *)optval, &tl,
829 831 sizeof (struct timeval));
830 832 } else {
831 833 if (optlen != sizeof (struct timeval32)) {
832 834 error = EINVAL;
833 835 goto done;
834 836 }
835 837 TIMEVAL32_TO_TIMEVAL(&tl,
836 838 (struct timeval32 *)optval);
837 839 }
838 840 opt = &tl;
839 841 optlen = sizeof (tl);
840 842 t_usec = tl.tv_sec * 1000 * 1000 + tl.tv_usec;
841 843 mutex_enter(&so->so_lock);
842 844 if (option_name == SO_RCVTIMEO)
843 845 so->so_rcvtimeo = drv_usectohz(t_usec);
844 846 else
845 847 so->so_sndtimeo = drv_usectohz(t_usec);
846 848 mutex_exit(&so->so_lock);
847 849 break;
848 850 }
849 851 case SO_RCVBUF:
850 852 /*
851 853 * XXX XPG 4.2 applications retrieve SO_RCVBUF from
852 854 * sockfs since the transport might adjust the value
853 855 * and not return exactly what was set by the
854 856 * application.
855 857 */
856 858 so->so_xpg_rcvbuf = *(int32_t *)optval;
857 859 break;
858 860 }
859 861 }
860 862 error = (*so->so_downcalls->sd_setsockopt)
861 863 (so->so_proto_handle, level, option_name, opt, optlen, cr);
862 864 done:
863 865 SO_UNBLOCK_FALLBACK(so);
864 866 return (error);
865 867 }
866 868
867 869 int
868 870 so_ioctl(struct sonode *so, int cmd, intptr_t arg, int mode,
869 871 struct cred *cr, int32_t *rvalp)
870 872 {
871 873 int error = 0;
872 874
873 875 SO_BLOCK_FALLBACK(so, SOP_IOCTL(so, cmd, arg, mode, cr, rvalp));
874 876
875 877 /*
876 878 * If there is a pending error, return error
877 879 * This can happen if a non blocking operation caused an error.
878 880 */
879 881 if (so->so_error != 0) {
880 882 mutex_enter(&so->so_lock);
881 883 error = sogeterr(so, B_TRUE);
882 884 mutex_exit(&so->so_lock);
883 885 if (error != 0)
884 886 goto done;
885 887 }
886 888
887 889 /*
888 890 * calling strioc can result in the socket falling back to TPI,
889 891 * if that is supported.
890 892 */
891 893 if ((so->so_filter_active == 0 ||
892 894 (error = sof_filter_ioctl(so, cmd, arg, mode,
893 895 rvalp, cr)) < 0) &&
894 896 (error = socket_ioctl_common(so, cmd, arg, mode, cr, rvalp)) < 0 &&
895 897 (error = socket_strioc_common(so, cmd, arg, mode, cr, rvalp)) < 0) {
896 898 error = (*so->so_downcalls->sd_ioctl)(so->so_proto_handle,
897 899 cmd, arg, mode, rvalp, cr);
898 900 }
899 901
900 902 done:
901 903 SO_UNBLOCK_FALLBACK(so);
902 904
903 905 return (error);
904 906 }
905 907
906 908 int
907 909 so_poll(struct sonode *so, short events, int anyyet, short *reventsp,
908 910 struct pollhead **phpp)
909 911 {
910 912 int state = so->so_state, mask;
911 913 *reventsp = 0;
912 914
913 915 /*
914 916 * In sockets the errors are represented as input/output events
915 917 */
916 918 if (so->so_error != 0 &&
917 919 ((POLLIN|POLLRDNORM|POLLOUT) & events) != 0) {
918 920 *reventsp = (POLLIN|POLLRDNORM|POLLOUT) & events;
919 921 return (0);
920 922 }
921 923
922 924 /*
923 925 * If the socket is in a state where it can send data
924 926 * turn on POLLWRBAND and POLLOUT events.
925 927 */
926 928 if ((so->so_mode & SM_CONNREQUIRED) == 0 || (state & SS_ISCONNECTED)) {
927 929 /*
928 930 * out of band data is allowed even if the connection
929 931 * is flow controlled
930 932 */
931 933 *reventsp |= POLLWRBAND & events;
932 934 if (!SO_SND_FLOWCTRLD(so)) {
933 935 /*
934 936 * As long as there is buffer to send data
935 937 * turn on POLLOUT events
936 938 */
937 939 *reventsp |= POLLOUT & events;
938 940 }
939 941 }
940 942
941 943 /*
942 944 * Turn on POLLIN whenever there is data on the receive queue,
943 945 * or the socket is in a state where no more data will be received.
944 946 * Also, if the socket is accepting connections, flip the bit if
945 947 * there is something on the queue.
946 948 *
947 949 * We do an initial check for events without holding locks. However,
948 950 * if there are no event available, then we redo the check for POLLIN
949 951 * events under the lock.
950 952 */
951 953
952 954 /* Pending connections */
953 955 if (!list_is_empty(&so->so_acceptq_list))
954 956 *reventsp |= (POLLIN|POLLRDNORM) & events;
955 957
956 958 /* Data */
957 959 /* so_downcalls is null for sctp */
958 960 if (so->so_downcalls != NULL && so->so_downcalls->sd_poll != NULL) {
959 961 *reventsp |= (*so->so_downcalls->sd_poll)
960 962 (so->so_proto_handle, events & SO_PROTO_POLLEV, anyyet,
961 963 CRED()) & events;
962 964 ASSERT((*reventsp & ~events) == 0);
963 965 /* do not recheck events */
964 966 events &= ~SO_PROTO_POLLEV;
965 967 } else {
966 968 if (SO_HAVE_DATA(so))
967 969 *reventsp |= (POLLIN|POLLRDNORM) & events;
968 970
969 971 /* Urgent data */
970 972 if ((state & SS_OOBPEND) != 0) {
971 973 *reventsp |= (POLLRDBAND | POLLPRI) & events;
972 974 }
973 975
974 976 /*
975 977 * If the socket has become disconnected, we set POLLHUP.
976 978 * Note that if we are in this state, we will have set POLLIN
977 979 * (SO_HAVE_DATA() is true on a disconnected socket), but not
978 980 * POLLOUT (SS_ISCONNECTED is false). This is in keeping with
979 981 * the semantics of POLLHUP, which is defined to be mutually
980 982 * exclusive with respect to POLLOUT but not POLLIN. We are
981 983 * therefore setting POLLHUP primarily for the benefit of
982 984 * those not polling on POLLIN, as they have no other way of
983 985 * knowing that the socket has been disconnected.
984 986 */
985 987 mask = SS_SENTLASTREADSIG | SS_SENTLASTWRITESIG;
986 988
987 989 if ((state & (mask | SS_ISCONNECTED)) == mask)
988 990 *reventsp |= POLLHUP;
989 991 }
990 992
991 993 if (!*reventsp && !anyyet) {
992 994 /* Check for read events again, but this time under lock */
993 995 if (events & (POLLIN|POLLRDNORM)) {
994 996 mutex_enter(&so->so_lock);
995 997 if (SO_HAVE_DATA(so) ||
996 998 !list_is_empty(&so->so_acceptq_list)) {
997 999 mutex_exit(&so->so_lock);
998 1000 *reventsp |= (POLLIN|POLLRDNORM) & events;
999 1001 return (0);
1000 1002 } else {
1001 1003 so->so_pollev |= SO_POLLEV_IN;
1002 1004 mutex_exit(&so->so_lock);
1003 1005 }
1004 1006 }
1005 1007 *phpp = &so->so_poll_list;
1006 1008 }
1007 1009 return (0);
1008 1010 }
1009 1011
1010 1012 /*
1011 1013 * Generic Upcalls
1012 1014 */
1013 1015 void
1014 1016 so_connected(sock_upper_handle_t sock_handle, sock_connid_t id,
1015 1017 cred_t *peer_cred, pid_t peer_cpid)
1016 1018 {
1017 1019 struct sonode *so = (struct sonode *)sock_handle;
1018 1020
1019 1021 mutex_enter(&so->so_lock);
1020 1022 ASSERT(so->so_proto_handle != NULL);
1021 1023
1022 1024 if (peer_cred != NULL) {
1023 1025 if (so->so_peercred != NULL)
1024 1026 crfree(so->so_peercred);
1025 1027 crhold(peer_cred);
1026 1028 so->so_peercred = peer_cred;
1027 1029 so->so_cpid = peer_cpid;
1028 1030 }
1029 1031
1030 1032 so->so_proto_connid = id;
1031 1033 soisconnected(so);
1032 1034 /*
1033 1035 * Wake ones who're waiting for conn to become established.
1034 1036 */
1035 1037 so_notify_connected(so);
1036 1038 }
1037 1039
1038 1040 int
1039 1041 so_disconnected(sock_upper_handle_t sock_handle, sock_connid_t id, int error)
1040 1042 {
1041 1043 struct sonode *so = (struct sonode *)sock_handle;
1042 1044 boolean_t connect_failed;
1043 1045
1044 1046 mutex_enter(&so->so_lock);
1045 1047
1046 1048 /*
1047 1049 * If we aren't currently connected, then this isn't a disconnect but
1048 1050 * rather a failure to connect.
1049 1051 */
1050 1052 connect_failed = !(so->so_state & SS_ISCONNECTED);
1051 1053
1052 1054 so->so_proto_connid = id;
1053 1055 soisdisconnected(so, error);
1054 1056 so_notify_disconnected(so, connect_failed, error);
1055 1057
1056 1058 return (0);
1057 1059 }
1058 1060
1059 1061 void
1060 1062 so_opctl(sock_upper_handle_t sock_handle, sock_opctl_action_t action,
1061 1063 uintptr_t arg)
1062 1064 {
1063 1065 struct sonode *so = (struct sonode *)sock_handle;
1064 1066
1065 1067 switch (action) {
1066 1068 case SOCK_OPCTL_SHUT_SEND:
1067 1069 mutex_enter(&so->so_lock);
1068 1070 socantsendmore(so);
1069 1071 so_notify_disconnecting(so);
1070 1072 break;
1071 1073 case SOCK_OPCTL_SHUT_RECV: {
1072 1074 mutex_enter(&so->so_lock);
1073 1075 socantrcvmore(so);
1074 1076 so_notify_eof(so);
1075 1077 break;
1076 1078 }
1077 1079 case SOCK_OPCTL_ENAB_ACCEPT:
1078 1080 mutex_enter(&so->so_lock);
1079 1081 so->so_state |= SS_ACCEPTCONN;
1080 1082 so->so_backlog = (unsigned int)arg;
1081 1083 /*
1082 1084 * The protocol can stop generating newconn upcalls when
1083 1085 * the backlog is full, so to make sure the listener does
1084 1086 * not end up with a queue full of deferred connections
1085 1087 * we reduce the backlog by one. Thus the listener will
1086 1088 * start closing deferred connections before the backlog
1087 1089 * is full.
1088 1090 */
1089 1091 if (so->so_filter_active > 0)
1090 1092 so->so_backlog = MAX(1, so->so_backlog - 1);
1091 1093 mutex_exit(&so->so_lock);
1092 1094 break;
1093 1095 default:
1094 1096 ASSERT(0);
1095 1097 break;
1096 1098 }
1097 1099 }
1098 1100
1099 1101 void
1100 1102 so_txq_full(sock_upper_handle_t sock_handle, boolean_t qfull)
1101 1103 {
1102 1104 struct sonode *so = (struct sonode *)sock_handle;
1103 1105
1104 1106 if (qfull) {
1105 1107 so_snd_qfull(so);
1106 1108 } else {
1107 1109 so_snd_qnotfull(so);
1108 1110 mutex_enter(&so->so_lock);
1109 1111 /* so_notify_writable drops so_lock */
1110 1112 so_notify_writable(so);
1111 1113 }
1112 1114 }
1113 1115
1114 1116 sock_upper_handle_t
1115 1117 so_newconn(sock_upper_handle_t parenthandle,
1116 1118 sock_lower_handle_t proto_handle, sock_downcalls_t *sock_downcalls,
1117 1119 struct cred *peer_cred, pid_t peer_cpid, sock_upcalls_t **sock_upcallsp)
1118 1120 {
1119 1121 struct sonode *so = (struct sonode *)parenthandle;
1120 1122 struct sonode *nso;
1121 1123 int error;
1122 1124
1123 1125 ASSERT(proto_handle != NULL);
1124 1126
1125 1127 if ((so->so_state & SS_ACCEPTCONN) == 0 ||
1126 1128 (so->so_acceptq_len >= so->so_backlog &&
1127 1129 (so->so_filter_active == 0 || !sof_sonode_drop_deferred(so)))) {
1128 1130 return (NULL);
1129 1131 }
1130 1132
1131 1133 nso = socket_newconn(so, proto_handle, sock_downcalls, SOCKET_NOSLEEP,
1132 1134 &error);
1133 1135 if (nso == NULL)
1134 1136 return (NULL);
1135 1137
1136 1138 if (peer_cred != NULL) {
1137 1139 crhold(peer_cred);
1138 1140 nso->so_peercred = peer_cred;
1139 1141 nso->so_cpid = peer_cpid;
1140 1142 }
1141 1143 nso->so_listener = so;
1142 1144
1143 1145 /*
1144 1146 * The new socket (nso), proto_handle and sock_upcallsp are all
1145 1147 * valid at this point. But as soon as nso is placed in the accept
1146 1148 * queue that can no longer be assumed (since an accept() thread may
1147 1149 * pull it off the queue and close the socket).
1148 1150 */
1149 1151 *sock_upcallsp = &so_upcalls;
1150 1152
1151 1153 mutex_enter(&so->so_acceptq_lock);
1152 1154 if (so->so_state & (SS_CLOSING|SS_FALLBACK_PENDING|SS_FALLBACK_COMP)) {
1153 1155 mutex_exit(&so->so_acceptq_lock);
1154 1156 ASSERT(nso->so_count == 1);
1155 1157 nso->so_count--;
1156 1158 nso->so_listener = NULL;
1157 1159 /* drop proto ref */
1158 1160 VN_RELE(SOTOV(nso));
1159 1161 socket_destroy(nso);
1160 1162 return (NULL);
1161 1163 } else {
1162 1164 so->so_acceptq_len++;
1163 1165 if (nso->so_state & SS_FIL_DEFER) {
1164 1166 list_insert_tail(&so->so_acceptq_defer, nso);
1165 1167 mutex_exit(&so->so_acceptq_lock);
1166 1168 } else {
1167 1169 list_insert_tail(&so->so_acceptq_list, nso);
1168 1170 cv_signal(&so->so_acceptq_cv);
1169 1171 mutex_exit(&so->so_acceptq_lock);
1170 1172 mutex_enter(&so->so_lock);
1171 1173 so_notify_newconn(so);
1172 1174 }
1173 1175
1174 1176 return ((sock_upper_handle_t)nso);
1175 1177 }
1176 1178 }
1177 1179
1178 1180 void
1179 1181 so_set_prop(sock_upper_handle_t sock_handle, struct sock_proto_props *soppp)
1180 1182 {
1181 1183 struct sonode *so;
1182 1184
1183 1185 so = (struct sonode *)sock_handle;
1184 1186
1185 1187 mutex_enter(&so->so_lock);
1186 1188
1187 1189 if (soppp->sopp_flags & SOCKOPT_MAXBLK)
1188 1190 so->so_proto_props.sopp_maxblk = soppp->sopp_maxblk;
1189 1191 if (soppp->sopp_flags & SOCKOPT_WROFF)
1190 1192 so->so_proto_props.sopp_wroff = soppp->sopp_wroff;
1191 1193 if (soppp->sopp_flags & SOCKOPT_TAIL)
1192 1194 so->so_proto_props.sopp_tail = soppp->sopp_tail;
1193 1195 if (soppp->sopp_flags & SOCKOPT_RCVHIWAT)
1194 1196 so->so_proto_props.sopp_rxhiwat = soppp->sopp_rxhiwat;
1195 1197 if (soppp->sopp_flags & SOCKOPT_RCVLOWAT)
1196 1198 so->so_proto_props.sopp_rxlowat = soppp->sopp_rxlowat;
1197 1199 if (soppp->sopp_flags & SOCKOPT_MAXPSZ)
1198 1200 so->so_proto_props.sopp_maxpsz = soppp->sopp_maxpsz;
1199 1201 if (soppp->sopp_flags & SOCKOPT_MINPSZ)
1200 1202 so->so_proto_props.sopp_minpsz = soppp->sopp_minpsz;
1201 1203 if (soppp->sopp_flags & SOCKOPT_ZCOPY) {
1202 1204 if (soppp->sopp_zcopyflag & ZCVMSAFE) {
1203 1205 so->so_proto_props.sopp_zcopyflag |= STZCVMSAFE;
1204 1206 so->so_proto_props.sopp_zcopyflag &= ~STZCVMUNSAFE;
1205 1207 } else if (soppp->sopp_zcopyflag & ZCVMUNSAFE) {
1206 1208 so->so_proto_props.sopp_zcopyflag |= STZCVMUNSAFE;
1207 1209 so->so_proto_props.sopp_zcopyflag &= ~STZCVMSAFE;
1208 1210 }
1209 1211
1210 1212 if (soppp->sopp_zcopyflag & COPYCACHED) {
1211 1213 so->so_proto_props.sopp_zcopyflag |= STRCOPYCACHED;
1212 1214 }
1213 1215 }
1214 1216 if (soppp->sopp_flags & SOCKOPT_OOBINLINE)
1215 1217 so->so_proto_props.sopp_oobinline = soppp->sopp_oobinline;
1216 1218 if (soppp->sopp_flags & SOCKOPT_RCVTIMER)
1217 1219 so->so_proto_props.sopp_rcvtimer = soppp->sopp_rcvtimer;
1218 1220 if (soppp->sopp_flags & SOCKOPT_RCVTHRESH)
1219 1221 so->so_proto_props.sopp_rcvthresh = soppp->sopp_rcvthresh;
1220 1222 if (soppp->sopp_flags & SOCKOPT_MAXADDRLEN)
1221 1223 so->so_proto_props.sopp_maxaddrlen = soppp->sopp_maxaddrlen;
1222 1224 if (soppp->sopp_flags & SOCKOPT_LOOPBACK)
1223 1225 so->so_proto_props.sopp_loopback = soppp->sopp_loopback;
1224 1226
1225 1227 mutex_exit(&so->so_lock);
1226 1228
1227 1229 if (so->so_filter_active > 0) {
1228 1230 sof_instance_t *inst;
1229 1231 ssize_t maxblk;
1230 1232 ushort_t wroff, tail;
1231 1233 maxblk = so->so_proto_props.sopp_maxblk;
1232 1234 wroff = so->so_proto_props.sopp_wroff;
1233 1235 tail = so->so_proto_props.sopp_tail;
1234 1236 for (inst = so->so_filter_bottom; inst != NULL;
1235 1237 inst = inst->sofi_prev) {
1236 1238 if (SOF_INTERESTED(inst, mblk_prop)) {
1237 1239 (*inst->sofi_ops->sofop_mblk_prop)(
1238 1240 (sof_handle_t)inst, inst->sofi_cookie,
1239 1241 &maxblk, &wroff, &tail);
1240 1242 }
1241 1243 }
1242 1244 mutex_enter(&so->so_lock);
1243 1245 so->so_proto_props.sopp_maxblk = maxblk;
1244 1246 so->so_proto_props.sopp_wroff = wroff;
1245 1247 so->so_proto_props.sopp_tail = tail;
1246 1248 mutex_exit(&so->so_lock);
1247 1249 }
1248 1250 #ifdef DEBUG
1249 1251 soppp->sopp_flags &= ~(SOCKOPT_MAXBLK | SOCKOPT_WROFF | SOCKOPT_TAIL |
1250 1252 SOCKOPT_RCVHIWAT | SOCKOPT_RCVLOWAT | SOCKOPT_MAXPSZ |
1251 1253 SOCKOPT_ZCOPY | SOCKOPT_OOBINLINE | SOCKOPT_RCVTIMER |
1252 1254 SOCKOPT_RCVTHRESH | SOCKOPT_MAXADDRLEN | SOCKOPT_MINPSZ |
1253 1255 SOCKOPT_LOOPBACK);
1254 1256 ASSERT(soppp->sopp_flags == 0);
1255 1257 #endif
1256 1258 }
1257 1259
1258 1260 /* ARGSUSED */
1259 1261 ssize_t
1260 1262 so_queue_msg_impl(struct sonode *so, mblk_t *mp,
1261 1263 size_t msg_size, int flags, int *errorp, boolean_t *force_pushp,
1262 1264 sof_instance_t *filter)
1263 1265 {
1264 1266 boolean_t force_push = B_TRUE;
1265 1267 int space_left;
1266 1268 sodirect_t *sodp = so->so_direct;
1267 1269
1268 1270 ASSERT(errorp != NULL);
1269 1271 *errorp = 0;
1270 1272 if (mp == NULL) {
1271 1273 if (so->so_downcalls->sd_recv_uio != NULL) {
1272 1274 mutex_enter(&so->so_lock);
1273 1275 /* the notify functions will drop the lock */
1274 1276 if (flags & MSG_OOB)
1275 1277 so_notify_oobdata(so, IS_SO_OOB_INLINE(so));
1276 1278 else
1277 1279 so_notify_data(so, msg_size);
1278 1280 return (0);
1279 1281 }
1280 1282 ASSERT(msg_size == 0);
1281 1283 mutex_enter(&so->so_lock);
1282 1284 goto space_check;
1283 1285 }
1284 1286
1285 1287 ASSERT(mp->b_next == NULL);
1286 1288 ASSERT(DB_TYPE(mp) == M_DATA || DB_TYPE(mp) == M_PROTO);
1287 1289 ASSERT(msg_size == msgdsize(mp));
1288 1290
1289 1291 if (DB_TYPE(mp) == M_PROTO && !__TPI_PRIM_ISALIGNED(mp->b_rptr)) {
1290 1292 /* The read pointer is not aligned correctly for TPI */
1291 1293 zcmn_err(getzoneid(), CE_WARN,
1292 1294 "sockfs: Unaligned TPI message received. rptr = %p\n",
1293 1295 (void *)mp->b_rptr);
1294 1296 freemsg(mp);
1295 1297 mutex_enter(&so->so_lock);
1296 1298 if (sodp != NULL)
1297 1299 SOD_UIOAFINI(sodp);
1298 1300 goto space_check;
1299 1301 }
1300 1302
1301 1303 if (so->so_filter_active > 0) {
1302 1304 for (; filter != NULL; filter = filter->sofi_prev) {
1303 1305 if (!SOF_INTERESTED(filter, data_in))
1304 1306 continue;
1305 1307 mp = (*filter->sofi_ops->sofop_data_in)(
1306 1308 (sof_handle_t)filter, filter->sofi_cookie, mp,
1307 1309 flags, &msg_size);
1308 1310 ASSERT(msgdsize(mp) == msg_size);
1309 1311 DTRACE_PROBE2(filter__data, (sof_instance_t), filter,
1310 1312 (mblk_t *), mp);
1311 1313 /* Data was consumed/dropped, just do space check */
1312 1314 if (msg_size == 0) {
1313 1315 mutex_enter(&so->so_lock);
1314 1316 goto space_check;
1315 1317 }
1316 1318 }
1317 1319 }
1318 1320
1319 1321 if (flags & MSG_OOB) {
1320 1322 so_queue_oob(so, mp, msg_size);
1321 1323 mutex_enter(&so->so_lock);
1322 1324 goto space_check;
1323 1325 }
1324 1326
1325 1327 if (force_pushp != NULL)
1326 1328 force_push = *force_pushp;
1327 1329
1328 1330 mutex_enter(&so->so_lock);
1329 1331 if (so->so_state & (SS_FALLBACK_DRAIN | SS_FALLBACK_COMP)) {
1330 1332 if (sodp != NULL)
1331 1333 SOD_DISABLE(sodp);
1332 1334 mutex_exit(&so->so_lock);
1333 1335 *errorp = EOPNOTSUPP;
1334 1336 return (-1);
1335 1337 }
1336 1338 if (so->so_state & (SS_CANTRCVMORE | SS_CLOSING)) {
1337 1339 freemsg(mp);
1338 1340 if (sodp != NULL)
1339 1341 SOD_DISABLE(sodp);
1340 1342 mutex_exit(&so->so_lock);
1341 1343 return (0);
1342 1344 }
1343 1345
1344 1346 /* process the mblk via I/OAT if capable */
1345 1347 if (sodp != NULL && sodp->sod_enabled) {
1346 1348 if (DB_TYPE(mp) == M_DATA) {
1347 1349 sod_uioa_mblk_init(sodp, mp, msg_size);
1348 1350 } else {
1349 1351 SOD_UIOAFINI(sodp);
1350 1352 }
1351 1353 }
1352 1354
1353 1355 if (mp->b_next == NULL) {
1354 1356 so_enqueue_msg(so, mp, msg_size);
1355 1357 } else {
1356 1358 do {
1357 1359 mblk_t *nmp;
1358 1360
1359 1361 if ((nmp = mp->b_next) != NULL) {
1360 1362 mp->b_next = NULL;
1361 1363 }
1362 1364 so_enqueue_msg(so, mp, msgdsize(mp));
1363 1365 mp = nmp;
1364 1366 } while (mp != NULL);
1365 1367 }
1366 1368
1367 1369 space_left = so->so_rcvbuf - so->so_rcv_queued;
1368 1370 if (space_left <= 0) {
1369 1371 so->so_flowctrld = B_TRUE;
1370 1372 *errorp = ENOSPC;
1371 1373 space_left = -1;
1372 1374 }
1373 1375
1374 1376 if (force_push || so->so_rcv_queued >= so->so_rcv_thresh ||
1375 1377 so->so_rcv_queued >= so->so_rcv_wanted) {
1376 1378 SOCKET_TIMER_CANCEL(so);
1377 1379 /*
1378 1380 * so_notify_data will release the lock
1379 1381 */
1380 1382 so_notify_data(so, so->so_rcv_queued);
1381 1383
1382 1384 if (force_pushp != NULL)
1383 1385 *force_pushp = B_TRUE;
1384 1386 goto done;
1385 1387 } else if (so->so_rcv_timer_tid == 0) {
1386 1388 /* Make sure the recv push timer is running */
1387 1389 SOCKET_TIMER_START(so);
1388 1390 }
1389 1391
1390 1392 done_unlock:
1391 1393 mutex_exit(&so->so_lock);
1392 1394 done:
1393 1395 return (space_left);
1394 1396
1395 1397 space_check:
1396 1398 space_left = so->so_rcvbuf - so->so_rcv_queued;
1397 1399 if (space_left <= 0) {
1398 1400 so->so_flowctrld = B_TRUE;
1399 1401 *errorp = ENOSPC;
1400 1402 space_left = -1;
1401 1403 }
1402 1404 goto done_unlock;
1403 1405 }
1404 1406
1405 1407 #pragma inline(so_queue_msg_impl)
1406 1408
1407 1409 ssize_t
1408 1410 so_queue_msg(sock_upper_handle_t sock_handle, mblk_t *mp,
1409 1411 size_t msg_size, int flags, int *errorp, boolean_t *force_pushp)
1410 1412 {
1411 1413 struct sonode *so = (struct sonode *)sock_handle;
1412 1414
1413 1415 return (so_queue_msg_impl(so, mp, msg_size, flags, errorp, force_pushp,
1414 1416 so->so_filter_bottom));
1415 1417 }
1416 1418
1417 1419 /*
1418 1420 * Set the offset of where the oob data is relative to the bytes in
1419 1421 * queued. Also generate SIGURG
1420 1422 */
1421 1423 void
1422 1424 so_signal_oob(sock_upper_handle_t sock_handle, ssize_t offset)
1423 1425 {
1424 1426 struct sonode *so;
1425 1427
1426 1428 ASSERT(offset >= 0);
1427 1429 so = (struct sonode *)sock_handle;
1428 1430 mutex_enter(&so->so_lock);
1429 1431 if (so->so_direct != NULL)
1430 1432 SOD_UIOAFINI(so->so_direct);
1431 1433
1432 1434 /*
1433 1435 * New urgent data on the way so forget about any old
1434 1436 * urgent data.
1435 1437 */
1436 1438 so->so_state &= ~(SS_HAVEOOBDATA|SS_HADOOBDATA);
1437 1439
1438 1440 /*
1439 1441 * Record that urgent data is pending.
1440 1442 */
1441 1443 so->so_state |= SS_OOBPEND;
1442 1444
1443 1445 if (so->so_oobmsg != NULL) {
1444 1446 dprintso(so, 1, ("sock: discarding old oob\n"));
1445 1447 freemsg(so->so_oobmsg);
1446 1448 so->so_oobmsg = NULL;
1447 1449 }
1448 1450
1449 1451 /*
1450 1452 * set the offset where the urgent byte is
1451 1453 */
1452 1454 so->so_oobmark = so->so_rcv_queued + offset;
1453 1455 if (so->so_oobmark == 0)
1454 1456 so->so_state |= SS_RCVATMARK;
1455 1457 else
1456 1458 so->so_state &= ~SS_RCVATMARK;
1457 1459
1458 1460 so_notify_oobsig(so);
1459 1461 }
1460 1462
1461 1463 /*
1462 1464 * Queue the OOB byte
1463 1465 */
1464 1466 static void
1465 1467 so_queue_oob(struct sonode *so, mblk_t *mp, size_t len)
1466 1468 {
1467 1469 mutex_enter(&so->so_lock);
1468 1470 if (so->so_direct != NULL)
1469 1471 SOD_UIOAFINI(so->so_direct);
1470 1472
1471 1473 ASSERT(mp != NULL);
1472 1474 if (!IS_SO_OOB_INLINE(so)) {
1473 1475 so->so_oobmsg = mp;
1474 1476 so->so_state |= SS_HAVEOOBDATA;
1475 1477 } else {
1476 1478 so_enqueue_msg(so, mp, len);
1477 1479 }
1478 1480
1479 1481 so_notify_oobdata(so, IS_SO_OOB_INLINE(so));
1480 1482 }
1481 1483
1482 1484 int
1483 1485 so_close(struct sonode *so, int flag, struct cred *cr)
1484 1486 {
1485 1487 int error;
1486 1488
1487 1489 /*
1488 1490 * No new data will be enqueued once the CLOSING flag is set.
1489 1491 */
1490 1492 mutex_enter(&so->so_lock);
1491 1493 so->so_state |= SS_CLOSING;
1492 1494 ASSERT(so_verify_oobstate(so));
1493 1495 so_rcv_flush(so);
1494 1496 mutex_exit(&so->so_lock);
1495 1497
1496 1498 if (so->so_filter_active > 0)
1497 1499 sof_sonode_closing(so);
1498 1500
1499 1501 if (so->so_state & SS_ACCEPTCONN) {
1500 1502 /*
1501 1503 * We grab and release the accept lock to ensure that any
1502 1504 * thread about to insert a socket in so_newconn completes
1503 1505 * before we flush the queue. Any thread calling so_newconn
1504 1506 * after we drop the lock will observe the SS_CLOSING flag,
1505 1507 * which will stop it from inserting the socket in the queue.
1506 1508 */
1507 1509 mutex_enter(&so->so_acceptq_lock);
1508 1510 mutex_exit(&so->so_acceptq_lock);
1509 1511
1510 1512 so_acceptq_flush(so, B_TRUE);
1511 1513 }
1512 1514
1513 1515 error = (*so->so_downcalls->sd_close)(so->so_proto_handle, flag, cr);
1514 1516 switch (error) {
1515 1517 default:
1516 1518 /* Protocol made a synchronous close; remove proto ref */
1517 1519 VN_RELE(SOTOV(so));
1518 1520 break;
1519 1521 case EINPROGRESS:
1520 1522 /*
1521 1523 * Protocol is in the process of closing, it will make a
1522 1524 * 'closed' upcall to remove the reference.
1523 1525 */
1524 1526 error = 0;
1525 1527 break;
1526 1528 }
1527 1529
1528 1530 return (error);
1529 1531 }
1530 1532
1531 1533 /*
1532 1534 * Upcall made by the protocol when it's doing an asynchronous close. It
↓ open down ↓ |
1163 lines elided |
↑ open up ↑ |
1533 1535 * will drop the protocol's reference on the socket.
1534 1536 */
1535 1537 void
1536 1538 so_closed(sock_upper_handle_t sock_handle)
1537 1539 {
1538 1540 struct sonode *so = (struct sonode *)sock_handle;
1539 1541
1540 1542 VN_RELE(SOTOV(so));
1541 1543 }
1542 1544
1545 +conn_pid_node_list_hdr_t *
1546 +so_get_sock_pid_list(sock_upper_handle_t sock_handle)
1547 +{
1548 + int sz, n = 0;
1549 + pid_node_t *pn;
1550 + conn_pid_node_t *cpn;
1551 + conn_pid_node_list_hdr_t *cph;
1552 + struct sonode *so = (struct sonode *)sock_handle;
1553 +
1554 + mutex_enter(&so->so_pid_list_lock);
1555 +
1556 + n = list_size(&so->so_pid_list);
1557 + sz = sizeof (conn_pid_node_list_hdr_t);
1558 + sz += (n > 1)?((n - 1) * sizeof (conn_pid_node_t)):0;
1559 + cph = kmem_zalloc(sz, KM_SLEEP);
1560 +
1561 + cph->cph_magic = CONN_PID_NODE_LIST_HDR_MAGIC;
1562 + cph->cph_contents = CONN_PID_NODE_LIST_HDR_SOC;
1563 + cph->cph_pn_cnt = n;
1564 + cph->cph_tot_size = sz;
1565 + cph->cph_flags = 0;
1566 + cph->cph_optional1 = 0;
1567 + cph->cph_optional2 = 0;
1568 +
1569 + if (cph->cph_pn_cnt > 0) {
1570 + cpn = cph->cph_cpns;
1571 + pn = list_head(&so->so_pid_list);
1572 + while (pn != NULL) {
1573 + PIDNODE2CONNPIDNODE(pn, cpn);
1574 + pn = list_next(&so->so_pid_list, pn);
1575 + cpn++;
1576 + }
1577 + }
1578 +
1579 + mutex_exit(&so->so_pid_list_lock);
1580 +
1581 + return (cph);
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_list
1944 1986 };
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX