Print this page
4770 soconfig(1M) needs an option to print the in-kernel socket configuration table
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/common/fs/sockfs/socksyscalls.c
+++ new/usr/src/uts/common/fs/sockfs/socksyscalls.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
↓ open down ↓ |
16 lines elided |
↑ open up ↑ |
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) 1995, 2010, Oracle and/or its affiliates. All rights reserved.
24 24 */
25 25
26 26 /* Copyright (c) 2013, OmniTI Computer Consulting, Inc. All rights reserved. */
27 +/*
28 + * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
29 + */
27 30
28 31 #include <sys/types.h>
29 32 #include <sys/t_lock.h>
30 33 #include <sys/param.h>
31 34 #include <sys/systm.h>
32 35 #include <sys/buf.h>
33 36 #include <sys/conf.h>
34 37 #include <sys/cred.h>
35 38 #include <sys/kmem.h>
36 39 #include <sys/sysmacros.h>
37 40 #include <sys/vfs.h>
38 41 #include <sys/vnode.h>
39 42 #include <sys/debug.h>
40 43 #include <sys/errno.h>
41 44 #include <sys/time.h>
42 45 #include <sys/file.h>
43 46 #include <sys/user.h>
44 47 #include <sys/stream.h>
45 48 #include <sys/strsubr.h>
46 49 #include <sys/strsun.h>
47 50 #include <sys/sunddi.h>
48 51 #include <sys/esunddi.h>
49 52 #include <sys/flock.h>
50 53 #include <sys/modctl.h>
51 54 #include <sys/cmn_err.h>
52 55 #include <sys/vmsystm.h>
53 56 #include <sys/policy.h>
54 57
55 58 #include <sys/socket.h>
56 59 #include <sys/socketvar.h>
57 60
58 61 #include <sys/isa_defs.h>
59 62 #include <sys/inttypes.h>
60 63 #include <sys/systm.h>
61 64 #include <sys/cpuvar.h>
62 65 #include <sys/filio.h>
63 66 #include <sys/sendfile.h>
64 67 #include <sys/ddi.h>
65 68 #include <vm/seg.h>
66 69 #include <vm/seg_map.h>
67 70 #include <vm/seg_kpm.h>
68 71
69 72 #include <fs/sockfs/nl7c.h>
70 73 #include <fs/sockfs/sockcommon.h>
71 74 #include <fs/sockfs/sockfilter_impl.h>
72 75 #include <fs/sockfs/socktpi.h>
73 76
74 77 #ifdef SOCK_TEST
75 78 int do_useracc = 1; /* Controlled by setting SO_DEBUG to 4 */
76 79 #else
77 80 #define do_useracc 1
78 81 #endif /* SOCK_TEST */
79 82
80 83 extern int xnet_truncate_print;
81 84
82 85 extern void nl7c_init(void);
83 86 extern int sockfs_defer_nl7c_init;
84 87
85 88 /*
86 89 * Note: DEF_IOV_MAX is defined and used as it is in "fs/vncalls.c"
87 90 * as there isn't a formal definition of IOV_MAX ???
88 91 */
89 92 #define MSG_MAXIOVLEN 16
90 93
91 94 /*
92 95 * Kernel component of socket creation.
93 96 *
94 97 * The socket library determines which version number to use.
95 98 * First the library calls this with a NULL devpath. If this fails
96 99 * to find a transport (using solookup) the library will look in /etc/netconfig
97 100 * for the appropriate transport. If one is found it will pass in the
98 101 * devpath for the kernel to use.
99 102 */
100 103 int
101 104 so_socket(int family, int type_w_flags, int protocol, char *devpath,
102 105 int version)
103 106 {
104 107 struct sonode *so;
105 108 vnode_t *vp;
106 109 struct file *fp;
107 110 int fd;
108 111 int error;
109 112 int type;
110 113
111 114 type = type_w_flags & SOCK_TYPE_MASK;
112 115 type_w_flags &= ~SOCK_TYPE_MASK;
113 116 if (type_w_flags & ~(SOCK_CLOEXEC|SOCK_NDELAY|SOCK_NONBLOCK))
114 117 return (set_errno(EINVAL));
115 118
116 119 if (devpath != NULL) {
117 120 char *buf;
118 121 size_t kdevpathlen = 0;
119 122
120 123 buf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
121 124 if ((error = copyinstr(devpath, buf,
122 125 MAXPATHLEN, &kdevpathlen)) != 0) {
123 126 kmem_free(buf, MAXPATHLEN);
124 127 return (set_errno(error));
125 128 }
126 129 so = socket_create(family, type, protocol, buf, NULL,
127 130 SOCKET_SLEEP, version, CRED(), &error);
128 131 kmem_free(buf, MAXPATHLEN);
129 132 } else {
130 133 so = socket_create(family, type, protocol, NULL, NULL,
131 134 SOCKET_SLEEP, version, CRED(), &error);
132 135 }
133 136 if (so == NULL)
134 137 return (set_errno(error));
135 138
136 139 /* Allocate a file descriptor for the socket */
137 140 vp = SOTOV(so);
138 141 if (error = falloc(vp, FWRITE|FREAD, &fp, &fd)) {
139 142 (void) socket_close(so, 0, CRED());
140 143 socket_destroy(so);
141 144 return (set_errno(error));
142 145 }
143 146
144 147 /*
145 148 * Now fill in the entries that falloc reserved
146 149 */
147 150 if (type_w_flags & SOCK_NDELAY) {
148 151 so->so_state |= SS_NDELAY;
149 152 fp->f_flag |= FNDELAY;
150 153 }
151 154 if (type_w_flags & SOCK_NONBLOCK) {
152 155 so->so_state |= SS_NONBLOCK;
153 156 fp->f_flag |= FNONBLOCK;
154 157 }
155 158 mutex_exit(&fp->f_tlock);
156 159 setf(fd, fp);
157 160 if ((type_w_flags & SOCK_CLOEXEC) != 0) {
158 161 f_setfd(fd, FD_CLOEXEC);
159 162 }
160 163
161 164 return (fd);
162 165 }
163 166
164 167 /*
165 168 * Map from a file descriptor to a socket node.
166 169 * Returns with the file descriptor held i.e. the caller has to
167 170 * use releasef when done with the file descriptor.
168 171 */
169 172 struct sonode *
170 173 getsonode(int sock, int *errorp, file_t **fpp)
171 174 {
172 175 file_t *fp;
173 176 vnode_t *vp;
174 177 struct sonode *so;
175 178
176 179 if ((fp = getf(sock)) == NULL) {
177 180 *errorp = EBADF;
178 181 eprintline(*errorp);
179 182 return (NULL);
180 183 }
181 184 vp = fp->f_vnode;
182 185 /* Check if it is a socket */
183 186 if (vp->v_type != VSOCK) {
184 187 releasef(sock);
185 188 *errorp = ENOTSOCK;
186 189 eprintline(*errorp);
187 190 return (NULL);
188 191 }
189 192 /*
190 193 * Use the stream head to find the real socket vnode.
191 194 * This is needed when namefs sits above sockfs.
192 195 */
193 196 if (vp->v_stream) {
194 197 ASSERT(vp->v_stream->sd_vnode);
195 198 vp = vp->v_stream->sd_vnode;
196 199
197 200 so = VTOSO(vp);
198 201 if (so->so_version == SOV_STREAM) {
199 202 releasef(sock);
200 203 *errorp = ENOTSOCK;
201 204 eprintsoline(so, *errorp);
202 205 return (NULL);
203 206 }
204 207 } else {
205 208 so = VTOSO(vp);
206 209 }
207 210 if (fpp)
208 211 *fpp = fp;
209 212 return (so);
210 213 }
211 214
212 215 /*
213 216 * Allocate and copyin a sockaddr.
214 217 * Ensures NULL termination for AF_UNIX addresses by extending them
215 218 * with one NULL byte if need be. Verifies that the length is not
216 219 * excessive to prevent an application from consuming all of kernel
217 220 * memory. Returns NULL when an error occurred.
218 221 */
219 222 static struct sockaddr *
220 223 copyin_name(struct sonode *so, struct sockaddr *name, socklen_t *namelenp,
221 224 int *errorp)
222 225 {
223 226 char *faddr;
224 227 size_t namelen = (size_t)*namelenp;
225 228
226 229 ASSERT(namelen != 0);
227 230 if (namelen > SO_MAXARGSIZE) {
228 231 *errorp = EINVAL;
229 232 eprintsoline(so, *errorp);
230 233 return (NULL);
231 234 }
232 235
233 236 faddr = (char *)kmem_alloc(namelen, KM_SLEEP);
234 237 if (copyin(name, faddr, namelen)) {
235 238 kmem_free(faddr, namelen);
236 239 *errorp = EFAULT;
237 240 eprintsoline(so, *errorp);
238 241 return (NULL);
239 242 }
240 243
241 244 /*
242 245 * Add space for NULL termination if needed.
243 246 * Do a quick check if the last byte is NUL.
244 247 */
245 248 if (so->so_family == AF_UNIX && faddr[namelen - 1] != '\0') {
246 249 /* Check if there is any NULL termination */
247 250 size_t i;
248 251 int foundnull = 0;
249 252
250 253 for (i = sizeof (name->sa_family); i < namelen; i++) {
251 254 if (faddr[i] == '\0') {
252 255 foundnull = 1;
253 256 break;
254 257 }
255 258 }
256 259 if (!foundnull) {
257 260 /* Add extra byte for NUL padding */
258 261 char *nfaddr;
259 262
260 263 nfaddr = (char *)kmem_alloc(namelen + 1, KM_SLEEP);
261 264 bcopy(faddr, nfaddr, namelen);
262 265 kmem_free(faddr, namelen);
263 266
264 267 /* NUL terminate */
265 268 nfaddr[namelen] = '\0';
266 269 namelen++;
267 270 ASSERT((socklen_t)namelen == namelen);
268 271 *namelenp = (socklen_t)namelen;
269 272 faddr = nfaddr;
270 273 }
271 274 }
272 275 return ((struct sockaddr *)faddr);
273 276 }
274 277
275 278 /*
276 279 * Copy from kaddr/klen to uaddr/ulen. Updates ulenp if non-NULL.
277 280 */
278 281 static int
279 282 copyout_arg(void *uaddr, socklen_t ulen, void *ulenp,
280 283 void *kaddr, socklen_t klen)
281 284 {
282 285 if (uaddr != NULL) {
283 286 if (ulen > klen)
284 287 ulen = klen;
285 288
286 289 if (ulen != 0) {
287 290 if (copyout(kaddr, uaddr, ulen))
288 291 return (EFAULT);
289 292 }
290 293 } else
291 294 ulen = 0;
292 295
293 296 if (ulenp != NULL) {
294 297 if (copyout(&ulen, ulenp, sizeof (ulen)))
295 298 return (EFAULT);
296 299 }
297 300 return (0);
298 301 }
299 302
300 303 /*
301 304 * Copy from kaddr/klen to uaddr/ulen. Updates ulenp if non-NULL.
302 305 * If klen is greater than ulen it still uses the non-truncated
303 306 * klen to update ulenp.
304 307 */
305 308 static int
306 309 copyout_name(void *uaddr, socklen_t ulen, void *ulenp,
307 310 void *kaddr, socklen_t klen)
308 311 {
309 312 if (uaddr != NULL) {
310 313 if (ulen >= klen)
311 314 ulen = klen;
312 315 else if (ulen != 0 && xnet_truncate_print) {
313 316 printf("sockfs: truncating copyout of address using "
314 317 "XNET semantics for pid = %d. Lengths %d, %d\n",
315 318 curproc->p_pid, klen, ulen);
316 319 }
317 320
318 321 if (ulen != 0) {
319 322 if (copyout(kaddr, uaddr, ulen))
320 323 return (EFAULT);
321 324 } else
322 325 klen = 0;
323 326 } else
324 327 klen = 0;
325 328
326 329 if (ulenp != NULL) {
327 330 if (copyout(&klen, ulenp, sizeof (klen)))
328 331 return (EFAULT);
329 332 }
330 333 return (0);
331 334 }
332 335
333 336 /*
334 337 * The socketpair() code in libsocket creates two sockets (using
335 338 * the /etc/netconfig fallback if needed) before calling this routine
336 339 * to connect the two sockets together.
337 340 *
338 341 * For a SOCK_STREAM socketpair a listener is needed - in that case this
339 342 * routine will create a new file descriptor as part of accepting the
340 343 * connection. The library socketpair() will check if svs[2] has changed
341 344 * in which case it will close the changed fd.
342 345 *
343 346 * Note that this code could use the TPI feature of accepting the connection
344 347 * on the listening endpoint. However, that would require significant changes
345 348 * to soaccept.
346 349 */
347 350 int
348 351 so_socketpair(int sv[2])
349 352 {
350 353 int svs[2];
351 354 struct sonode *so1, *so2;
352 355 int error;
353 356 int orig_flags;
354 357 struct sockaddr_ux *name;
355 358 size_t namelen;
356 359 sotpi_info_t *sti1;
357 360 sotpi_info_t *sti2;
358 361
359 362 dprint(1, ("so_socketpair(%p)\n", (void *)sv));
360 363
361 364 error = useracc(sv, sizeof (svs), B_WRITE);
362 365 if (error && do_useracc)
363 366 return (set_errno(EFAULT));
364 367
365 368 if (copyin(sv, svs, sizeof (svs)))
366 369 return (set_errno(EFAULT));
367 370
368 371 if ((so1 = getsonode(svs[0], &error, NULL)) == NULL)
369 372 return (set_errno(error));
370 373
371 374 if ((so2 = getsonode(svs[1], &error, NULL)) == NULL) {
372 375 releasef(svs[0]);
373 376 return (set_errno(error));
374 377 }
375 378
376 379 if (so1->so_family != AF_UNIX || so2->so_family != AF_UNIX) {
377 380 error = EOPNOTSUPP;
378 381 goto done;
379 382 }
380 383
381 384 sti1 = SOTOTPI(so1);
382 385 sti2 = SOTOTPI(so2);
383 386
384 387 /*
385 388 * The code below makes assumptions about the "sockfs" implementation.
386 389 * So make sure that the correct implementation is really used.
387 390 */
388 391 ASSERT(so1->so_ops == &sotpi_sonodeops);
389 392 ASSERT(so2->so_ops == &sotpi_sonodeops);
390 393
391 394 if (so1->so_type == SOCK_DGRAM) {
392 395 /*
393 396 * Bind both sockets and connect them with each other.
394 397 * Need to allocate name/namelen for soconnect.
395 398 */
396 399 error = socket_bind(so1, NULL, 0, _SOBIND_UNSPEC, CRED());
397 400 if (error) {
398 401 eprintsoline(so1, error);
399 402 goto done;
400 403 }
401 404 error = socket_bind(so2, NULL, 0, _SOBIND_UNSPEC, CRED());
402 405 if (error) {
403 406 eprintsoline(so2, error);
404 407 goto done;
405 408 }
406 409 namelen = sizeof (struct sockaddr_ux);
407 410 name = kmem_alloc(namelen, KM_SLEEP);
408 411 name->sou_family = AF_UNIX;
409 412 name->sou_addr = sti2->sti_ux_laddr;
410 413 error = socket_connect(so1,
411 414 (struct sockaddr *)name,
412 415 (socklen_t)namelen,
413 416 0, _SOCONNECT_NOXLATE, CRED());
414 417 if (error) {
415 418 kmem_free(name, namelen);
416 419 eprintsoline(so1, error);
417 420 goto done;
418 421 }
419 422 name->sou_addr = sti1->sti_ux_laddr;
420 423 error = socket_connect(so2,
421 424 (struct sockaddr *)name,
422 425 (socklen_t)namelen,
423 426 0, _SOCONNECT_NOXLATE, CRED());
424 427 kmem_free(name, namelen);
425 428 if (error) {
426 429 eprintsoline(so2, error);
427 430 goto done;
428 431 }
429 432 releasef(svs[0]);
430 433 releasef(svs[1]);
431 434 } else {
432 435 /*
433 436 * Bind both sockets, with so1 being a listener.
434 437 * Connect so2 to so1 - nonblocking to avoid waiting for
435 438 * soaccept to complete.
436 439 * Accept a connection on so1. Pass out the new fd as sv[0].
437 440 * The library will detect the changed fd and close
438 441 * the original one.
439 442 */
440 443 struct sonode *nso;
441 444 struct vnode *nvp;
442 445 struct file *nfp;
443 446 int nfd;
444 447
445 448 /*
446 449 * We could simply call socket_listen() here (which would do the
447 450 * binding automatically) if the code didn't rely on passing
448 451 * _SOBIND_NOXLATE to the TPI implementation of socket_bind().
449 452 */
450 453 error = socket_bind(so1, NULL, 0, _SOBIND_UNSPEC|
451 454 _SOBIND_NOXLATE|_SOBIND_LISTEN|_SOBIND_SOCKETPAIR,
452 455 CRED());
453 456 if (error) {
454 457 eprintsoline(so1, error);
455 458 goto done;
456 459 }
457 460 error = socket_bind(so2, NULL, 0, _SOBIND_UNSPEC, CRED());
458 461 if (error) {
459 462 eprintsoline(so2, error);
460 463 goto done;
461 464 }
462 465
463 466 namelen = sizeof (struct sockaddr_ux);
464 467 name = kmem_alloc(namelen, KM_SLEEP);
465 468 name->sou_family = AF_UNIX;
466 469 name->sou_addr = sti1->sti_ux_laddr;
467 470 error = socket_connect(so2,
468 471 (struct sockaddr *)name,
469 472 (socklen_t)namelen,
470 473 FNONBLOCK, _SOCONNECT_NOXLATE, CRED());
471 474 kmem_free(name, namelen);
472 475 if (error) {
473 476 if (error != EINPROGRESS) {
474 477 eprintsoline(so2, error); goto done;
475 478 }
476 479 }
477 480
478 481 error = socket_accept(so1, 0, CRED(), &nso);
479 482 if (error) {
480 483 eprintsoline(so1, error);
481 484 goto done;
482 485 }
483 486
484 487 /* wait for so2 being SS_CONNECTED ignoring signals */
485 488 mutex_enter(&so2->so_lock);
486 489 error = sowaitconnected(so2, 0, 1);
487 490 mutex_exit(&so2->so_lock);
488 491 if (error != 0) {
489 492 (void) socket_close(nso, 0, CRED());
490 493 socket_destroy(nso);
491 494 eprintsoline(so2, error);
492 495 goto done;
493 496 }
494 497
495 498 nvp = SOTOV(nso);
496 499 if (error = falloc(nvp, FWRITE|FREAD, &nfp, &nfd)) {
497 500 (void) socket_close(nso, 0, CRED());
498 501 socket_destroy(nso);
499 502 eprintsoline(nso, error);
500 503 goto done;
501 504 }
502 505 /*
503 506 * copy over FNONBLOCK and FNDELAY flags should they exist
504 507 */
505 508 if (so1->so_state & SS_NONBLOCK)
506 509 nfp->f_flag |= FNONBLOCK;
507 510 if (so1->so_state & SS_NDELAY)
508 511 nfp->f_flag |= FNDELAY;
509 512
510 513 /*
511 514 * fill in the entries that falloc reserved
512 515 */
513 516 mutex_exit(&nfp->f_tlock);
514 517 setf(nfd, nfp);
515 518
516 519 /*
517 520 * get the original flags before we release
518 521 */
519 522 VERIFY(f_getfd_error(svs[0], &orig_flags) == 0);
520 523
521 524 releasef(svs[0]);
522 525 releasef(svs[1]);
523 526
524 527 /*
525 528 * If FD_CLOEXEC was set on the filedescriptor we're
526 529 * swapping out, we should set it on the new one too.
527 530 */
528 531 if (orig_flags & FD_CLOEXEC) {
529 532 f_setfd(nfd, FD_CLOEXEC);
530 533 }
531 534
532 535 /*
533 536 * The socketpair library routine will close the original
534 537 * svs[0] when this code passes out a different file
535 538 * descriptor.
536 539 */
537 540 svs[0] = nfd;
538 541
539 542 if (copyout(svs, sv, sizeof (svs))) {
540 543 (void) closeandsetf(nfd, NULL);
541 544 eprintline(EFAULT);
542 545 return (set_errno(EFAULT));
543 546 }
544 547 }
545 548 return (0);
546 549
547 550 done:
548 551 releasef(svs[0]);
549 552 releasef(svs[1]);
550 553 return (set_errno(error));
551 554 }
552 555
553 556 int
554 557 bind(int sock, struct sockaddr *name, socklen_t namelen, int version)
555 558 {
556 559 struct sonode *so;
557 560 int error;
558 561
559 562 dprint(1, ("bind(%d, %p, %d)\n",
560 563 sock, (void *)name, namelen));
561 564
562 565 if ((so = getsonode(sock, &error, NULL)) == NULL)
563 566 return (set_errno(error));
564 567
565 568 /* Allocate and copyin name */
566 569 /*
567 570 * X/Open test does not expect EFAULT with NULL name and non-zero
568 571 * namelen.
569 572 */
570 573 if (name != NULL && namelen != 0) {
571 574 ASSERT(MUTEX_NOT_HELD(&so->so_lock));
572 575 name = copyin_name(so, name, &namelen, &error);
573 576 if (name == NULL) {
574 577 releasef(sock);
575 578 return (set_errno(error));
576 579 }
577 580 } else {
578 581 name = NULL;
579 582 namelen = 0;
580 583 }
581 584
582 585 switch (version) {
583 586 default:
584 587 error = socket_bind(so, name, namelen, 0, CRED());
585 588 break;
586 589 case SOV_XPG4_2:
587 590 error = socket_bind(so, name, namelen, _SOBIND_XPG4_2, CRED());
588 591 break;
589 592 case SOV_SOCKBSD:
590 593 error = socket_bind(so, name, namelen, _SOBIND_SOCKBSD, CRED());
591 594 break;
592 595 }
593 596 done:
594 597 releasef(sock);
595 598 if (name != NULL)
596 599 kmem_free(name, (size_t)namelen);
597 600
598 601 if (error)
599 602 return (set_errno(error));
600 603 return (0);
601 604 }
602 605
603 606 /* ARGSUSED2 */
604 607 int
605 608 listen(int sock, int backlog, int version)
606 609 {
607 610 struct sonode *so;
608 611 int error;
609 612
610 613 dprint(1, ("listen(%d, %d)\n",
611 614 sock, backlog));
612 615
613 616 if ((so = getsonode(sock, &error, NULL)) == NULL)
614 617 return (set_errno(error));
615 618
616 619 error = socket_listen(so, backlog, CRED());
617 620
618 621 releasef(sock);
619 622 if (error)
620 623 return (set_errno(error));
621 624 return (0);
622 625 }
623 626
624 627 /*ARGSUSED3*/
625 628 int
626 629 accept(int sock, struct sockaddr *name, socklen_t *namelenp, int version,
627 630 int flags)
628 631 {
629 632 struct sonode *so;
630 633 file_t *fp;
631 634 int error;
632 635 socklen_t namelen;
633 636 struct sonode *nso;
634 637 struct vnode *nvp;
635 638 struct file *nfp;
636 639 int nfd;
637 640 int ssflags;
638 641 struct sockaddr *addrp;
639 642 socklen_t addrlen;
640 643
641 644 dprint(1, ("accept(%d, %p, %p)\n",
642 645 sock, (void *)name, (void *)namelenp));
643 646
644 647 if (flags & ~(SOCK_CLOEXEC|SOCK_NONBLOCK|SOCK_NDELAY)) {
645 648 return (set_errno(EINVAL));
646 649 }
647 650
648 651 /* Translate SOCK_ flags to their SS_ variant */
649 652 ssflags = 0;
650 653 if (flags & SOCK_NONBLOCK)
651 654 ssflags |= SS_NONBLOCK;
652 655 if (flags & SOCK_NDELAY)
653 656 ssflags |= SS_NDELAY;
654 657
655 658 if ((so = getsonode(sock, &error, &fp)) == NULL)
656 659 return (set_errno(error));
657 660
658 661 if (name != NULL) {
659 662 ASSERT(MUTEX_NOT_HELD(&so->so_lock));
660 663 if (copyin(namelenp, &namelen, sizeof (namelen))) {
661 664 releasef(sock);
662 665 return (set_errno(EFAULT));
663 666 }
664 667 if (namelen != 0) {
665 668 error = useracc(name, (size_t)namelen, B_WRITE);
666 669 if (error && do_useracc) {
667 670 releasef(sock);
668 671 return (set_errno(EFAULT));
669 672 }
670 673 } else
671 674 name = NULL;
672 675 } else {
673 676 namelen = 0;
674 677 }
675 678
676 679 /*
677 680 * Allocate the user fd before socket_accept() in order to
678 681 * catch EMFILE errors before calling socket_accept().
679 682 */
680 683 if ((nfd = ufalloc(0)) == -1) {
681 684 eprintsoline(so, EMFILE);
682 685 releasef(sock);
683 686 return (set_errno(EMFILE));
684 687 }
685 688 error = socket_accept(so, fp->f_flag, CRED(), &nso);
686 689 if (error) {
687 690 setf(nfd, NULL);
688 691 releasef(sock);
689 692 return (set_errno(error));
690 693 }
691 694
692 695 nvp = SOTOV(nso);
693 696
694 697 ASSERT(MUTEX_NOT_HELD(&nso->so_lock));
695 698 if (namelen != 0) {
696 699 addrlen = so->so_max_addr_len;
697 700 addrp = (struct sockaddr *)kmem_alloc(addrlen, KM_SLEEP);
698 701
699 702 if ((error = socket_getpeername(nso, (struct sockaddr *)addrp,
700 703 &addrlen, B_TRUE, CRED())) == 0) {
701 704 error = copyout_name(name, namelen, namelenp,
702 705 addrp, addrlen);
703 706 } else {
704 707 ASSERT(error == EINVAL || error == ENOTCONN);
705 708 error = ECONNABORTED;
706 709 }
707 710 kmem_free(addrp, so->so_max_addr_len);
708 711 }
709 712
710 713 if (error) {
711 714 setf(nfd, NULL);
712 715 (void) socket_close(nso, 0, CRED());
713 716 socket_destroy(nso);
714 717 releasef(sock);
715 718 return (set_errno(error));
716 719 }
717 720 if (error = falloc(NULL, FWRITE|FREAD, &nfp, NULL)) {
718 721 setf(nfd, NULL);
719 722 (void) socket_close(nso, 0, CRED());
720 723 socket_destroy(nso);
721 724 eprintsoline(so, error);
722 725 releasef(sock);
723 726 return (set_errno(error));
724 727 }
725 728 /*
726 729 * fill in the entries that falloc reserved
727 730 */
728 731 nfp->f_vnode = nvp;
729 732 mutex_exit(&nfp->f_tlock);
730 733 setf(nfd, nfp);
731 734
732 735 /*
733 736 * Act on SOCK_CLOEXEC from flags
734 737 */
735 738 if (flags & SOCK_CLOEXEC) {
736 739 f_setfd(nfd, FD_CLOEXEC);
737 740 }
738 741
739 742 /*
740 743 * Copy FNDELAY and FNONBLOCK from listener to acceptor
741 744 * and from ssflags
742 745 */
743 746 if ((ssflags | so->so_state) & (SS_NDELAY|SS_NONBLOCK)) {
744 747 uint_t oflag = nfp->f_flag;
745 748 int arg = 0;
746 749
747 750 if ((ssflags | so->so_state) & SS_NONBLOCK)
748 751 arg |= FNONBLOCK;
749 752 else if ((ssflags | so->so_state) & SS_NDELAY)
750 753 arg |= FNDELAY;
751 754
752 755 /*
753 756 * This code is a simplification of the F_SETFL code in fcntl()
754 757 * Ignore any errors from VOP_SETFL.
755 758 */
756 759 if ((error = VOP_SETFL(nvp, oflag, arg, nfp->f_cred, NULL))
757 760 != 0) {
758 761 eprintsoline(so, error);
759 762 error = 0;
760 763 } else {
761 764 mutex_enter(&nfp->f_tlock);
762 765 nfp->f_flag &= ~FMASK | (FREAD|FWRITE);
763 766 nfp->f_flag |= arg;
764 767 mutex_exit(&nfp->f_tlock);
765 768 }
766 769 }
767 770 releasef(sock);
768 771 return (nfd);
769 772 }
770 773
771 774 int
772 775 connect(int sock, struct sockaddr *name, socklen_t namelen, int version)
773 776 {
774 777 struct sonode *so;
775 778 file_t *fp;
776 779 int error;
777 780
778 781 dprint(1, ("connect(%d, %p, %d)\n",
779 782 sock, (void *)name, namelen));
780 783
781 784 if ((so = getsonode(sock, &error, &fp)) == NULL)
782 785 return (set_errno(error));
783 786
784 787 /* Allocate and copyin name */
785 788 if (namelen != 0) {
786 789 ASSERT(MUTEX_NOT_HELD(&so->so_lock));
787 790 name = copyin_name(so, name, &namelen, &error);
788 791 if (name == NULL) {
789 792 releasef(sock);
790 793 return (set_errno(error));
791 794 }
792 795 } else
793 796 name = NULL;
794 797
795 798 error = socket_connect(so, name, namelen, fp->f_flag,
796 799 (version != SOV_XPG4_2) ? 0 : _SOCONNECT_XPG4_2, CRED());
797 800 releasef(sock);
798 801 if (name)
799 802 kmem_free(name, (size_t)namelen);
800 803 if (error)
801 804 return (set_errno(error));
802 805 return (0);
803 806 }
804 807
805 808 /*ARGSUSED2*/
806 809 int
807 810 shutdown(int sock, int how, int version)
808 811 {
809 812 struct sonode *so;
810 813 int error;
811 814
812 815 dprint(1, ("shutdown(%d, %d)\n",
813 816 sock, how));
814 817
815 818 if ((so = getsonode(sock, &error, NULL)) == NULL)
816 819 return (set_errno(error));
817 820
818 821 error = socket_shutdown(so, how, CRED());
819 822
820 823 releasef(sock);
821 824 if (error)
822 825 return (set_errno(error));
823 826 return (0);
824 827 }
825 828
826 829 /*
827 830 * Common receive routine.
828 831 */
829 832 static ssize_t
830 833 recvit(int sock,
831 834 struct nmsghdr *msg,
832 835 struct uio *uiop,
833 836 int flags,
834 837 socklen_t *namelenp,
835 838 socklen_t *controllenp,
836 839 int *flagsp)
837 840 {
838 841 struct sonode *so;
839 842 file_t *fp;
840 843 void *name;
841 844 socklen_t namelen;
842 845 void *control;
843 846 socklen_t controllen;
844 847 ssize_t len;
845 848 int error;
846 849
847 850 if ((so = getsonode(sock, &error, &fp)) == NULL)
848 851 return (set_errno(error));
849 852
850 853 len = uiop->uio_resid;
851 854 uiop->uio_fmode = fp->f_flag;
852 855 uiop->uio_extflg = UIO_COPY_CACHED;
853 856
854 857 name = msg->msg_name;
855 858 namelen = msg->msg_namelen;
856 859 control = msg->msg_control;
857 860 controllen = msg->msg_controllen;
858 861
859 862 msg->msg_flags = flags & (MSG_OOB | MSG_PEEK | MSG_WAITALL |
860 863 MSG_DONTWAIT | MSG_XPG4_2);
861 864
862 865 error = socket_recvmsg(so, msg, uiop, CRED());
863 866 if (error) {
864 867 releasef(sock);
865 868 return (set_errno(error));
866 869 }
867 870 lwp_stat_update(LWP_STAT_MSGRCV, 1);
868 871 releasef(sock);
869 872
870 873 error = copyout_name(name, namelen, namelenp,
871 874 msg->msg_name, msg->msg_namelen);
872 875 if (error)
873 876 goto err;
874 877
875 878 if (flagsp != NULL) {
876 879 /*
877 880 * Clear internal flag.
878 881 */
879 882 msg->msg_flags &= ~MSG_XPG4_2;
880 883
881 884 /*
882 885 * Determine MSG_CTRUNC. sorecvmsg sets MSG_CTRUNC only
883 886 * when controllen is zero and there is control data to
884 887 * copy out.
885 888 */
886 889 if (controllen != 0 &&
887 890 (msg->msg_controllen > controllen || control == NULL)) {
888 891 dprint(1, ("recvit: CTRUNC %d %d %p\n",
889 892 msg->msg_controllen, controllen, control));
890 893
891 894 msg->msg_flags |= MSG_CTRUNC;
892 895 }
893 896 if (copyout(&msg->msg_flags, flagsp,
894 897 sizeof (msg->msg_flags))) {
895 898 error = EFAULT;
896 899 goto err;
897 900 }
898 901 }
899 902 /*
900 903 * Note: This MUST be done last. There can be no "goto err" after this
901 904 * point since it could make so_closefds run twice on some part
902 905 * of the file descriptor array.
903 906 */
904 907 if (controllen != 0) {
905 908 if (!(flags & MSG_XPG4_2)) {
906 909 /*
907 910 * Good old msg_accrights can only return a multiple
908 911 * of 4 bytes.
909 912 */
910 913 controllen &= ~((int)sizeof (uint32_t) - 1);
911 914 }
912 915 error = copyout_arg(control, controllen, controllenp,
913 916 msg->msg_control, msg->msg_controllen);
914 917 if (error)
915 918 goto err;
916 919
917 920 if (msg->msg_controllen > controllen || control == NULL) {
918 921 if (control == NULL)
919 922 controllen = 0;
920 923 so_closefds(msg->msg_control, msg->msg_controllen,
921 924 !(flags & MSG_XPG4_2), controllen);
922 925 }
923 926 }
924 927 if (msg->msg_namelen != 0)
925 928 kmem_free(msg->msg_name, (size_t)msg->msg_namelen);
926 929 if (msg->msg_controllen != 0)
927 930 kmem_free(msg->msg_control, (size_t)msg->msg_controllen);
928 931 return (len - uiop->uio_resid);
929 932
930 933 err:
931 934 /*
932 935 * If we fail and the control part contains file descriptors
933 936 * we have to close the fd's.
934 937 */
935 938 if (msg->msg_controllen != 0)
936 939 so_closefds(msg->msg_control, msg->msg_controllen,
937 940 !(flags & MSG_XPG4_2), 0);
938 941 if (msg->msg_namelen != 0)
939 942 kmem_free(msg->msg_name, (size_t)msg->msg_namelen);
940 943 if (msg->msg_controllen != 0)
941 944 kmem_free(msg->msg_control, (size_t)msg->msg_controllen);
942 945 return (set_errno(error));
943 946 }
944 947
945 948 /*
946 949 * Native system call
947 950 */
948 951 ssize_t
949 952 recv(int sock, void *buffer, size_t len, int flags)
950 953 {
951 954 struct nmsghdr lmsg;
952 955 struct uio auio;
953 956 struct iovec aiov[1];
954 957
955 958 dprint(1, ("recv(%d, %p, %ld, %d)\n",
956 959 sock, buffer, len, flags));
957 960
958 961 if ((ssize_t)len < 0) {
959 962 return (set_errno(EINVAL));
960 963 }
961 964
962 965 aiov[0].iov_base = buffer;
963 966 aiov[0].iov_len = len;
964 967 auio.uio_loffset = 0;
965 968 auio.uio_iov = aiov;
966 969 auio.uio_iovcnt = 1;
967 970 auio.uio_resid = len;
968 971 auio.uio_segflg = UIO_USERSPACE;
969 972 auio.uio_limit = 0;
970 973
971 974 lmsg.msg_namelen = 0;
972 975 lmsg.msg_controllen = 0;
973 976 lmsg.msg_flags = 0;
974 977 return (recvit(sock, &lmsg, &auio, flags, NULL, NULL, NULL));
975 978 }
976 979
977 980 ssize_t
978 981 recvfrom(int sock, void *buffer, size_t len, int flags,
979 982 struct sockaddr *name, socklen_t *namelenp)
980 983 {
981 984 struct nmsghdr lmsg;
982 985 struct uio auio;
983 986 struct iovec aiov[1];
984 987
985 988 dprint(1, ("recvfrom(%d, %p, %ld, %d, %p, %p)\n",
986 989 sock, buffer, len, flags, (void *)name, (void *)namelenp));
987 990
988 991 if ((ssize_t)len < 0) {
989 992 return (set_errno(EINVAL));
990 993 }
991 994
992 995 aiov[0].iov_base = buffer;
993 996 aiov[0].iov_len = len;
994 997 auio.uio_loffset = 0;
995 998 auio.uio_iov = aiov;
996 999 auio.uio_iovcnt = 1;
997 1000 auio.uio_resid = len;
998 1001 auio.uio_segflg = UIO_USERSPACE;
999 1002 auio.uio_limit = 0;
1000 1003
1001 1004 lmsg.msg_name = (char *)name;
1002 1005 if (namelenp != NULL) {
1003 1006 if (copyin(namelenp, &lmsg.msg_namelen,
1004 1007 sizeof (lmsg.msg_namelen)))
1005 1008 return (set_errno(EFAULT));
1006 1009 } else {
1007 1010 lmsg.msg_namelen = 0;
1008 1011 }
1009 1012 lmsg.msg_controllen = 0;
1010 1013 lmsg.msg_flags = 0;
1011 1014
1012 1015 return (recvit(sock, &lmsg, &auio, flags, namelenp, NULL, NULL));
1013 1016 }
1014 1017
1015 1018 /*
1016 1019 * Uses the MSG_XPG4_2 flag to determine if the caller is using
1017 1020 * struct omsghdr or struct nmsghdr.
1018 1021 */
1019 1022 ssize_t
1020 1023 recvmsg(int sock, struct nmsghdr *msg, int flags)
1021 1024 {
1022 1025 STRUCT_DECL(nmsghdr, u_lmsg);
1023 1026 STRUCT_HANDLE(nmsghdr, umsgptr);
1024 1027 struct nmsghdr lmsg;
1025 1028 struct uio auio;
1026 1029 struct iovec aiov[MSG_MAXIOVLEN];
1027 1030 int iovcnt;
1028 1031 ssize_t len;
1029 1032 int i;
1030 1033 int *flagsp;
1031 1034 model_t model;
1032 1035
1033 1036 dprint(1, ("recvmsg(%d, %p, %d)\n",
1034 1037 sock, (void *)msg, flags));
1035 1038
1036 1039 model = get_udatamodel();
1037 1040 STRUCT_INIT(u_lmsg, model);
1038 1041 STRUCT_SET_HANDLE(umsgptr, model, msg);
1039 1042
1040 1043 if (flags & MSG_XPG4_2) {
1041 1044 if (copyin(msg, STRUCT_BUF(u_lmsg), STRUCT_SIZE(u_lmsg)))
1042 1045 return (set_errno(EFAULT));
1043 1046 flagsp = STRUCT_FADDR(umsgptr, msg_flags);
1044 1047 } else {
1045 1048 /*
1046 1049 * Assumes that nmsghdr and omsghdr are identically shaped
1047 1050 * except for the added msg_flags field.
1048 1051 */
1049 1052 if (copyin(msg, STRUCT_BUF(u_lmsg),
1050 1053 SIZEOF_STRUCT(omsghdr, model)))
1051 1054 return (set_errno(EFAULT));
1052 1055 STRUCT_FSET(u_lmsg, msg_flags, 0);
1053 1056 flagsp = NULL;
1054 1057 }
1055 1058
1056 1059 /*
1057 1060 * Code below us will kmem_alloc memory and hang it
1058 1061 * off msg_control and msg_name fields. This forces
1059 1062 * us to copy the structure to its native form.
1060 1063 */
1061 1064 lmsg.msg_name = STRUCT_FGETP(u_lmsg, msg_name);
1062 1065 lmsg.msg_namelen = STRUCT_FGET(u_lmsg, msg_namelen);
1063 1066 lmsg.msg_iov = STRUCT_FGETP(u_lmsg, msg_iov);
1064 1067 lmsg.msg_iovlen = STRUCT_FGET(u_lmsg, msg_iovlen);
1065 1068 lmsg.msg_control = STRUCT_FGETP(u_lmsg, msg_control);
1066 1069 lmsg.msg_controllen = STRUCT_FGET(u_lmsg, msg_controllen);
1067 1070 lmsg.msg_flags = STRUCT_FGET(u_lmsg, msg_flags);
1068 1071
1069 1072 iovcnt = lmsg.msg_iovlen;
1070 1073
1071 1074 if (iovcnt <= 0 || iovcnt > MSG_MAXIOVLEN) {
1072 1075 return (set_errno(EMSGSIZE));
1073 1076 }
1074 1077
1075 1078 #ifdef _SYSCALL32_IMPL
1076 1079 /*
1077 1080 * 32-bit callers need to have their iovec expanded, while ensuring
1078 1081 * that they can't move more than 2Gbytes of data in a single call.
1079 1082 */
1080 1083 if (model == DATAMODEL_ILP32) {
1081 1084 struct iovec32 aiov32[MSG_MAXIOVLEN];
1082 1085 ssize32_t count32;
1083 1086
1084 1087 if (copyin((struct iovec32 *)lmsg.msg_iov, aiov32,
1085 1088 iovcnt * sizeof (struct iovec32)))
1086 1089 return (set_errno(EFAULT));
1087 1090
1088 1091 count32 = 0;
1089 1092 for (i = 0; i < iovcnt; i++) {
1090 1093 ssize32_t iovlen32;
1091 1094
1092 1095 iovlen32 = aiov32[i].iov_len;
1093 1096 count32 += iovlen32;
1094 1097 if (iovlen32 < 0 || count32 < 0)
1095 1098 return (set_errno(EINVAL));
1096 1099 aiov[i].iov_len = iovlen32;
1097 1100 aiov[i].iov_base =
1098 1101 (caddr_t)(uintptr_t)aiov32[i].iov_base;
1099 1102 }
1100 1103 } else
1101 1104 #endif /* _SYSCALL32_IMPL */
1102 1105 if (copyin(lmsg.msg_iov, aiov, iovcnt * sizeof (struct iovec))) {
1103 1106 return (set_errno(EFAULT));
1104 1107 }
1105 1108 len = 0;
1106 1109 for (i = 0; i < iovcnt; i++) {
1107 1110 ssize_t iovlen = aiov[i].iov_len;
1108 1111 len += iovlen;
1109 1112 if (iovlen < 0 || len < 0) {
1110 1113 return (set_errno(EINVAL));
1111 1114 }
1112 1115 }
1113 1116 auio.uio_loffset = 0;
1114 1117 auio.uio_iov = aiov;
1115 1118 auio.uio_iovcnt = iovcnt;
1116 1119 auio.uio_resid = len;
1117 1120 auio.uio_segflg = UIO_USERSPACE;
1118 1121 auio.uio_limit = 0;
1119 1122
1120 1123 if (lmsg.msg_control != NULL &&
1121 1124 (do_useracc == 0 ||
1122 1125 useracc(lmsg.msg_control, lmsg.msg_controllen,
1123 1126 B_WRITE) != 0)) {
1124 1127 return (set_errno(EFAULT));
1125 1128 }
1126 1129
1127 1130 return (recvit(sock, &lmsg, &auio, flags,
1128 1131 STRUCT_FADDR(umsgptr, msg_namelen),
1129 1132 STRUCT_FADDR(umsgptr, msg_controllen), flagsp));
1130 1133 }
1131 1134
1132 1135 /*
1133 1136 * Common send function.
1134 1137 */
1135 1138 static ssize_t
1136 1139 sendit(int sock, struct nmsghdr *msg, struct uio *uiop, int flags)
1137 1140 {
1138 1141 struct sonode *so;
1139 1142 file_t *fp;
1140 1143 void *name;
1141 1144 socklen_t namelen;
1142 1145 void *control;
1143 1146 socklen_t controllen;
1144 1147 ssize_t len;
1145 1148 int error;
1146 1149
1147 1150 if ((so = getsonode(sock, &error, &fp)) == NULL)
1148 1151 return (set_errno(error));
1149 1152
1150 1153 uiop->uio_fmode = fp->f_flag;
1151 1154
1152 1155 if (so->so_family == AF_UNIX)
1153 1156 uiop->uio_extflg = UIO_COPY_CACHED;
1154 1157 else
1155 1158 uiop->uio_extflg = UIO_COPY_DEFAULT;
1156 1159
1157 1160 /* Allocate and copyin name and control */
1158 1161 name = msg->msg_name;
1159 1162 namelen = msg->msg_namelen;
1160 1163 if (name != NULL && namelen != 0) {
1161 1164 ASSERT(MUTEX_NOT_HELD(&so->so_lock));
1162 1165 name = copyin_name(so,
1163 1166 (struct sockaddr *)name,
1164 1167 &namelen, &error);
1165 1168 if (name == NULL)
1166 1169 goto done3;
1167 1170 /* copyin_name null terminates addresses for AF_UNIX */
1168 1171 msg->msg_namelen = namelen;
1169 1172 msg->msg_name = name;
1170 1173 } else {
1171 1174 msg->msg_name = name = NULL;
1172 1175 msg->msg_namelen = namelen = 0;
1173 1176 }
1174 1177
1175 1178 control = msg->msg_control;
1176 1179 controllen = msg->msg_controllen;
1177 1180 if ((control != NULL) && (controllen != 0)) {
1178 1181 /*
1179 1182 * Verify that the length is not excessive to prevent
1180 1183 * an application from consuming all of kernel memory.
1181 1184 */
1182 1185 if (controllen > SO_MAXARGSIZE) {
1183 1186 error = EINVAL;
1184 1187 goto done2;
1185 1188 }
1186 1189 control = kmem_alloc(controllen, KM_SLEEP);
1187 1190
1188 1191 ASSERT(MUTEX_NOT_HELD(&so->so_lock));
1189 1192 if (copyin(msg->msg_control, control, controllen)) {
1190 1193 error = EFAULT;
1191 1194 goto done1;
1192 1195 }
1193 1196 msg->msg_control = control;
1194 1197 } else {
1195 1198 msg->msg_control = control = NULL;
1196 1199 msg->msg_controllen = controllen = 0;
1197 1200 }
1198 1201
1199 1202 len = uiop->uio_resid;
1200 1203 msg->msg_flags = flags;
1201 1204
1202 1205 error = socket_sendmsg(so, msg, uiop, CRED());
1203 1206 done1:
1204 1207 if (control != NULL)
1205 1208 kmem_free(control, controllen);
1206 1209 done2:
1207 1210 if (name != NULL)
1208 1211 kmem_free(name, namelen);
1209 1212 done3:
1210 1213 if (error != 0) {
1211 1214 releasef(sock);
1212 1215 return (set_errno(error));
1213 1216 }
1214 1217 lwp_stat_update(LWP_STAT_MSGSND, 1);
1215 1218 releasef(sock);
1216 1219 return (len - uiop->uio_resid);
1217 1220 }
1218 1221
1219 1222 /*
1220 1223 * Native system call
1221 1224 */
1222 1225 ssize_t
1223 1226 send(int sock, void *buffer, size_t len, int flags)
1224 1227 {
1225 1228 struct nmsghdr lmsg;
1226 1229 struct uio auio;
1227 1230 struct iovec aiov[1];
1228 1231
1229 1232 dprint(1, ("send(%d, %p, %ld, %d)\n",
1230 1233 sock, buffer, len, flags));
1231 1234
1232 1235 if ((ssize_t)len < 0) {
1233 1236 return (set_errno(EINVAL));
1234 1237 }
1235 1238
1236 1239 aiov[0].iov_base = buffer;
1237 1240 aiov[0].iov_len = len;
1238 1241 auio.uio_loffset = 0;
1239 1242 auio.uio_iov = aiov;
1240 1243 auio.uio_iovcnt = 1;
1241 1244 auio.uio_resid = len;
1242 1245 auio.uio_segflg = UIO_USERSPACE;
1243 1246 auio.uio_limit = 0;
1244 1247
1245 1248 lmsg.msg_name = NULL;
1246 1249 lmsg.msg_control = NULL;
1247 1250 if (!(flags & MSG_XPG4_2)) {
1248 1251 /*
1249 1252 * In order to be compatible with the libsocket/sockmod
1250 1253 * implementation we set EOR for all send* calls.
1251 1254 */
1252 1255 flags |= MSG_EOR;
1253 1256 }
1254 1257 return (sendit(sock, &lmsg, &auio, flags));
1255 1258 }
1256 1259
1257 1260 /*
1258 1261 * Uses the MSG_XPG4_2 flag to determine if the caller is using
1259 1262 * struct omsghdr or struct nmsghdr.
1260 1263 */
1261 1264 ssize_t
1262 1265 sendmsg(int sock, struct nmsghdr *msg, int flags)
1263 1266 {
1264 1267 struct nmsghdr lmsg;
1265 1268 STRUCT_DECL(nmsghdr, u_lmsg);
1266 1269 struct uio auio;
1267 1270 struct iovec aiov[MSG_MAXIOVLEN];
1268 1271 int iovcnt;
1269 1272 ssize_t len;
1270 1273 int i;
1271 1274 model_t model;
1272 1275
1273 1276 dprint(1, ("sendmsg(%d, %p, %d)\n", sock, (void *)msg, flags));
1274 1277
1275 1278 model = get_udatamodel();
1276 1279 STRUCT_INIT(u_lmsg, model);
1277 1280
1278 1281 if (flags & MSG_XPG4_2) {
1279 1282 if (copyin(msg, (char *)STRUCT_BUF(u_lmsg),
1280 1283 STRUCT_SIZE(u_lmsg)))
1281 1284 return (set_errno(EFAULT));
1282 1285 } else {
1283 1286 /*
1284 1287 * Assumes that nmsghdr and omsghdr are identically shaped
1285 1288 * except for the added msg_flags field.
1286 1289 */
1287 1290 if (copyin(msg, (char *)STRUCT_BUF(u_lmsg),
1288 1291 SIZEOF_STRUCT(omsghdr, model)))
1289 1292 return (set_errno(EFAULT));
1290 1293 /*
1291 1294 * In order to be compatible with the libsocket/sockmod
1292 1295 * implementation we set EOR for all send* calls.
1293 1296 */
1294 1297 flags |= MSG_EOR;
1295 1298 }
1296 1299
1297 1300 /*
1298 1301 * Code below us will kmem_alloc memory and hang it
1299 1302 * off msg_control and msg_name fields. This forces
1300 1303 * us to copy the structure to its native form.
1301 1304 */
1302 1305 lmsg.msg_name = STRUCT_FGETP(u_lmsg, msg_name);
1303 1306 lmsg.msg_namelen = STRUCT_FGET(u_lmsg, msg_namelen);
1304 1307 lmsg.msg_iov = STRUCT_FGETP(u_lmsg, msg_iov);
1305 1308 lmsg.msg_iovlen = STRUCT_FGET(u_lmsg, msg_iovlen);
1306 1309 lmsg.msg_control = STRUCT_FGETP(u_lmsg, msg_control);
1307 1310 lmsg.msg_controllen = STRUCT_FGET(u_lmsg, msg_controllen);
1308 1311 lmsg.msg_flags = STRUCT_FGET(u_lmsg, msg_flags);
1309 1312
1310 1313 iovcnt = lmsg.msg_iovlen;
1311 1314
1312 1315 if (iovcnt <= 0 || iovcnt > MSG_MAXIOVLEN) {
1313 1316 /*
1314 1317 * Unless this is XPG 4.2 we allow iovcnt == 0 to
1315 1318 * be compatible with SunOS 4.X and 4.4BSD.
1316 1319 */
1317 1320 if (iovcnt != 0 || (flags & MSG_XPG4_2))
1318 1321 return (set_errno(EMSGSIZE));
1319 1322 }
1320 1323
1321 1324 #ifdef _SYSCALL32_IMPL
1322 1325 /*
1323 1326 * 32-bit callers need to have their iovec expanded, while ensuring
1324 1327 * that they can't move more than 2Gbytes of data in a single call.
1325 1328 */
1326 1329 if (model == DATAMODEL_ILP32) {
1327 1330 struct iovec32 aiov32[MSG_MAXIOVLEN];
1328 1331 ssize32_t count32;
1329 1332
1330 1333 if (iovcnt != 0 &&
1331 1334 copyin((struct iovec32 *)lmsg.msg_iov, aiov32,
1332 1335 iovcnt * sizeof (struct iovec32)))
1333 1336 return (set_errno(EFAULT));
1334 1337
1335 1338 count32 = 0;
1336 1339 for (i = 0; i < iovcnt; i++) {
1337 1340 ssize32_t iovlen32;
1338 1341
1339 1342 iovlen32 = aiov32[i].iov_len;
1340 1343 count32 += iovlen32;
1341 1344 if (iovlen32 < 0 || count32 < 0)
1342 1345 return (set_errno(EINVAL));
1343 1346 aiov[i].iov_len = iovlen32;
1344 1347 aiov[i].iov_base =
1345 1348 (caddr_t)(uintptr_t)aiov32[i].iov_base;
1346 1349 }
1347 1350 } else
1348 1351 #endif /* _SYSCALL32_IMPL */
1349 1352 if (iovcnt != 0 &&
1350 1353 copyin(lmsg.msg_iov, aiov,
1351 1354 (unsigned)iovcnt * sizeof (struct iovec))) {
1352 1355 return (set_errno(EFAULT));
1353 1356 }
1354 1357 len = 0;
1355 1358 for (i = 0; i < iovcnt; i++) {
1356 1359 ssize_t iovlen = aiov[i].iov_len;
1357 1360 len += iovlen;
1358 1361 if (iovlen < 0 || len < 0) {
1359 1362 return (set_errno(EINVAL));
1360 1363 }
1361 1364 }
1362 1365 auio.uio_loffset = 0;
1363 1366 auio.uio_iov = aiov;
1364 1367 auio.uio_iovcnt = iovcnt;
1365 1368 auio.uio_resid = len;
1366 1369 auio.uio_segflg = UIO_USERSPACE;
1367 1370 auio.uio_limit = 0;
1368 1371
1369 1372 return (sendit(sock, &lmsg, &auio, flags));
1370 1373 }
1371 1374
1372 1375 ssize_t
1373 1376 sendto(int sock, void *buffer, size_t len, int flags,
1374 1377 struct sockaddr *name, socklen_t namelen)
1375 1378 {
1376 1379 struct nmsghdr lmsg;
1377 1380 struct uio auio;
1378 1381 struct iovec aiov[1];
1379 1382
1380 1383 dprint(1, ("sendto(%d, %p, %ld, %d, %p, %d)\n",
1381 1384 sock, buffer, len, flags, (void *)name, namelen));
1382 1385
1383 1386 if ((ssize_t)len < 0) {
1384 1387 return (set_errno(EINVAL));
1385 1388 }
1386 1389
1387 1390 aiov[0].iov_base = buffer;
1388 1391 aiov[0].iov_len = len;
1389 1392 auio.uio_loffset = 0;
1390 1393 auio.uio_iov = aiov;
1391 1394 auio.uio_iovcnt = 1;
1392 1395 auio.uio_resid = len;
1393 1396 auio.uio_segflg = UIO_USERSPACE;
1394 1397 auio.uio_limit = 0;
1395 1398
1396 1399 lmsg.msg_name = (char *)name;
1397 1400 lmsg.msg_namelen = namelen;
1398 1401 lmsg.msg_control = NULL;
1399 1402 if (!(flags & MSG_XPG4_2)) {
1400 1403 /*
1401 1404 * In order to be compatible with the libsocket/sockmod
1402 1405 * implementation we set EOR for all send* calls.
1403 1406 */
1404 1407 flags |= MSG_EOR;
1405 1408 }
1406 1409 return (sendit(sock, &lmsg, &auio, flags));
1407 1410 }
1408 1411
1409 1412 /*ARGSUSED3*/
1410 1413 int
1411 1414 getpeername(int sock, struct sockaddr *name, socklen_t *namelenp, int version)
1412 1415 {
1413 1416 struct sonode *so;
1414 1417 int error;
1415 1418 socklen_t namelen;
1416 1419 socklen_t sock_addrlen;
1417 1420 struct sockaddr *sock_addrp;
1418 1421
1419 1422 dprint(1, ("getpeername(%d, %p, %p)\n",
1420 1423 sock, (void *)name, (void *)namelenp));
1421 1424
1422 1425 if ((so = getsonode(sock, &error, NULL)) == NULL)
1423 1426 goto bad;
1424 1427
1425 1428 ASSERT(MUTEX_NOT_HELD(&so->so_lock));
1426 1429 if (copyin(namelenp, &namelen, sizeof (namelen)) ||
1427 1430 (name == NULL && namelen != 0)) {
1428 1431 error = EFAULT;
1429 1432 goto rel_out;
1430 1433 }
1431 1434 sock_addrlen = so->so_max_addr_len;
1432 1435 sock_addrp = (struct sockaddr *)kmem_alloc(sock_addrlen, KM_SLEEP);
1433 1436
1434 1437 if ((error = socket_getpeername(so, sock_addrp, &sock_addrlen,
1435 1438 B_FALSE, CRED())) == 0) {
1436 1439 ASSERT(sock_addrlen <= so->so_max_addr_len);
1437 1440 error = copyout_name(name, namelen, namelenp,
1438 1441 (void *)sock_addrp, sock_addrlen);
1439 1442 }
1440 1443 kmem_free(sock_addrp, so->so_max_addr_len);
1441 1444 rel_out:
1442 1445 releasef(sock);
1443 1446 bad: return (error != 0 ? set_errno(error) : 0);
1444 1447 }
1445 1448
1446 1449 /*ARGSUSED3*/
1447 1450 int
1448 1451 getsockname(int sock, struct sockaddr *name,
1449 1452 socklen_t *namelenp, int version)
1450 1453 {
1451 1454 struct sonode *so;
1452 1455 int error;
1453 1456 socklen_t namelen, sock_addrlen;
1454 1457 struct sockaddr *sock_addrp;
1455 1458
1456 1459 dprint(1, ("getsockname(%d, %p, %p)\n",
1457 1460 sock, (void *)name, (void *)namelenp));
1458 1461
1459 1462 if ((so = getsonode(sock, &error, NULL)) == NULL)
1460 1463 goto bad;
1461 1464
1462 1465 ASSERT(MUTEX_NOT_HELD(&so->so_lock));
1463 1466 if (copyin(namelenp, &namelen, sizeof (namelen)) ||
1464 1467 (name == NULL && namelen != 0)) {
1465 1468 error = EFAULT;
1466 1469 goto rel_out;
1467 1470 }
1468 1471
1469 1472 sock_addrlen = so->so_max_addr_len;
1470 1473 sock_addrp = (struct sockaddr *)kmem_alloc(sock_addrlen, KM_SLEEP);
1471 1474 if ((error = socket_getsockname(so, sock_addrp, &sock_addrlen,
1472 1475 CRED())) == 0) {
1473 1476 ASSERT(MUTEX_NOT_HELD(&so->so_lock));
1474 1477 ASSERT(sock_addrlen <= so->so_max_addr_len);
1475 1478 error = copyout_name(name, namelen, namelenp,
1476 1479 (void *)sock_addrp, sock_addrlen);
1477 1480 }
1478 1481 kmem_free(sock_addrp, so->so_max_addr_len);
1479 1482 rel_out:
1480 1483 releasef(sock);
1481 1484 bad: return (error != 0 ? set_errno(error) : 0);
1482 1485 }
1483 1486
1484 1487 /*ARGSUSED5*/
1485 1488 int
1486 1489 getsockopt(int sock,
1487 1490 int level,
1488 1491 int option_name,
1489 1492 void *option_value,
1490 1493 socklen_t *option_lenp,
1491 1494 int version)
1492 1495 {
1493 1496 struct sonode *so;
1494 1497 socklen_t optlen, optlen_res;
1495 1498 void *optval;
1496 1499 int error;
1497 1500
1498 1501 dprint(1, ("getsockopt(%d, %d, %d, %p, %p)\n",
1499 1502 sock, level, option_name, option_value, (void *)option_lenp));
1500 1503
1501 1504 if ((so = getsonode(sock, &error, NULL)) == NULL)
1502 1505 return (set_errno(error));
1503 1506
1504 1507 ASSERT(MUTEX_NOT_HELD(&so->so_lock));
1505 1508 if (copyin(option_lenp, &optlen, sizeof (optlen))) {
1506 1509 releasef(sock);
1507 1510 return (set_errno(EFAULT));
1508 1511 }
1509 1512 /*
1510 1513 * Verify that the length is not excessive to prevent
1511 1514 * an application from consuming all of kernel memory.
1512 1515 */
1513 1516 if (optlen > SO_MAXARGSIZE) {
1514 1517 error = EINVAL;
1515 1518 releasef(sock);
1516 1519 return (set_errno(error));
1517 1520 }
1518 1521 optval = kmem_alloc(optlen, KM_SLEEP);
1519 1522 optlen_res = optlen;
1520 1523 error = socket_getsockopt(so, level, option_name, optval,
1521 1524 &optlen_res, (version != SOV_XPG4_2) ? 0 : _SOGETSOCKOPT_XPG4_2,
1522 1525 CRED());
1523 1526 releasef(sock);
1524 1527 if (error) {
1525 1528 kmem_free(optval, optlen);
1526 1529 return (set_errno(error));
1527 1530 }
1528 1531 error = copyout_arg(option_value, optlen, option_lenp,
1529 1532 optval, optlen_res);
1530 1533 kmem_free(optval, optlen);
1531 1534 if (error)
1532 1535 return (set_errno(error));
1533 1536 return (0);
1534 1537 }
1535 1538
1536 1539 /*ARGSUSED5*/
1537 1540 int
1538 1541 setsockopt(int sock,
1539 1542 int level,
1540 1543 int option_name,
1541 1544 void *option_value,
1542 1545 socklen_t option_len,
1543 1546 int version)
1544 1547 {
1545 1548 struct sonode *so;
1546 1549 intptr_t buffer[2];
1547 1550 void *optval = NULL;
1548 1551 int error;
1549 1552
1550 1553 dprint(1, ("setsockopt(%d, %d, %d, %p, %d)\n",
1551 1554 sock, level, option_name, option_value, option_len));
1552 1555
1553 1556 if ((so = getsonode(sock, &error, NULL)) == NULL)
1554 1557 return (set_errno(error));
1555 1558
1556 1559 if (option_value != NULL) {
1557 1560 if (option_len != 0) {
1558 1561 /*
1559 1562 * Verify that the length is not excessive to prevent
1560 1563 * an application from consuming all of kernel memory.
1561 1564 */
1562 1565 if (option_len > SO_MAXARGSIZE) {
1563 1566 error = EINVAL;
1564 1567 goto done2;
1565 1568 }
1566 1569 optval = option_len <= sizeof (buffer) ?
1567 1570 &buffer : kmem_alloc((size_t)option_len, KM_SLEEP);
1568 1571 ASSERT(MUTEX_NOT_HELD(&so->so_lock));
1569 1572 if (copyin(option_value, optval, (size_t)option_len)) {
1570 1573 error = EFAULT;
1571 1574 goto done1;
1572 1575 }
1573 1576 }
1574 1577 } else
1575 1578 option_len = 0;
1576 1579
1577 1580 error = socket_setsockopt(so, level, option_name, optval,
1578 1581 (t_uscalar_t)option_len, CRED());
1579 1582 done1:
1580 1583 if (optval != buffer)
1581 1584 kmem_free(optval, (size_t)option_len);
1582 1585 done2:
1583 1586 releasef(sock);
1584 1587 if (error)
1585 1588 return (set_errno(error));
1586 1589 return (0);
1587 1590 }
1588 1591
1589 1592 static int
1590 1593 sockconf_add_sock(int family, int type, int protocol, char *name)
1591 1594 {
1592 1595 int error = 0;
1593 1596 char *kdevpath = NULL;
1594 1597 char *kmodule = NULL;
1595 1598 char *buf = NULL;
1596 1599 size_t pathlen = 0;
1597 1600 struct sockparams *sp;
1598 1601
1599 1602 if (name == NULL)
1600 1603 return (EINVAL);
1601 1604 /*
1602 1605 * Copyin the name.
1603 1606 * This also makes it possible to check for too long pathnames.
1604 1607 * Compress the space needed for the name before passing it
1605 1608 * to soconfig - soconfig will store the string until
1606 1609 * the configuration is removed.
1607 1610 */
1608 1611 buf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1609 1612 if ((error = copyinstr(name, buf, MAXPATHLEN, &pathlen)) != 0) {
1610 1613 kmem_free(buf, MAXPATHLEN);
1611 1614 return (error);
1612 1615 }
1613 1616 if (strncmp(buf, "/dev", strlen("/dev")) == 0) {
1614 1617 /* For device */
1615 1618
1616 1619 /*
1617 1620 * Special handling for NCA:
1618 1621 *
1619 1622 * DEV_NCA is never opened even if an application
1620 1623 * requests for AF_NCA. The device opened is instead a
1621 1624 * predefined AF_INET transport (NCA_INET_DEV).
1622 1625 *
1623 1626 * Prior to Volo (PSARC/2007/587) NCA would determine
1624 1627 * the device using a lookup, which worked then because
1625 1628 * all protocols were based on TPI. Since TPI is no
1626 1629 * longer the default, we have to explicitly state
1627 1630 * which device to use.
1628 1631 */
1629 1632 if (strcmp(buf, NCA_DEV) == 0) {
1630 1633 /* only support entry <28, 2, 0> */
1631 1634 if (family != AF_NCA || type != SOCK_STREAM ||
1632 1635 protocol != 0) {
1633 1636 kmem_free(buf, MAXPATHLEN);
1634 1637 return (EINVAL);
1635 1638 }
1636 1639
1637 1640 pathlen = strlen(NCA_INET_DEV) + 1;
1638 1641 kdevpath = kmem_alloc(pathlen, KM_SLEEP);
1639 1642 bcopy(NCA_INET_DEV, kdevpath, pathlen);
1640 1643 kdevpath[pathlen - 1] = '\0';
1641 1644 } else {
1642 1645 kdevpath = kmem_alloc(pathlen, KM_SLEEP);
1643 1646 bcopy(buf, kdevpath, pathlen);
1644 1647 kdevpath[pathlen - 1] = '\0';
1645 1648 }
1646 1649 } else {
1647 1650 /* For socket module */
1648 1651 kmodule = kmem_alloc(pathlen, KM_SLEEP);
1649 1652 bcopy(buf, kmodule, pathlen);
1650 1653 kmodule[pathlen - 1] = '\0';
1651 1654 pathlen = 0;
1652 1655 }
1653 1656 kmem_free(buf, MAXPATHLEN);
1654 1657
1655 1658 /* sockparams_create frees mod name and devpath upon failure */
1656 1659 sp = sockparams_create(family, type, protocol, kmodule,
1657 1660 kdevpath, pathlen, 0, KM_SLEEP, &error);
1658 1661 if (sp != NULL) {
1659 1662 error = sockparams_add(sp);
1660 1663 if (error != 0)
1661 1664 sockparams_destroy(sp);
1662 1665 }
1663 1666
1664 1667 return (error);
1665 1668 }
1666 1669
1667 1670 static int
1668 1671 sockconf_remove_sock(int family, int type, int protocol)
1669 1672 {
1670 1673 return (sockparams_delete(family, type, protocol));
1671 1674 }
1672 1675
1673 1676 static int
1674 1677 sockconfig_remove_filter(const char *uname)
1675 1678 {
1676 1679 char kname[SOF_MAXNAMELEN];
1677 1680 size_t len;
1678 1681 int error;
1679 1682 sof_entry_t *ent;
1680 1683
1681 1684 if ((error = copyinstr(uname, kname, SOF_MAXNAMELEN, &len)) != 0)
1682 1685 return (error);
1683 1686
1684 1687 ent = sof_entry_remove_by_name(kname);
1685 1688 if (ent == NULL)
1686 1689 return (ENXIO);
1687 1690
1688 1691 mutex_enter(&ent->sofe_lock);
1689 1692 ASSERT(!(ent->sofe_flags & SOFEF_CONDEMED));
1690 1693 if (ent->sofe_refcnt == 0) {
1691 1694 mutex_exit(&ent->sofe_lock);
1692 1695 sof_entry_free(ent);
1693 1696 } else {
1694 1697 /* let the last socket free the filter */
1695 1698 ent->sofe_flags |= SOFEF_CONDEMED;
1696 1699 mutex_exit(&ent->sofe_lock);
1697 1700 }
1698 1701
1699 1702 return (0);
1700 1703 }
1701 1704
1702 1705 static int
1703 1706 sockconfig_add_filter(const char *uname, void *ufilpropp)
1704 1707 {
1705 1708 struct sockconfig_filter_props filprop;
1706 1709 sof_entry_t *ent;
1707 1710 int error;
1708 1711 size_t tuplesz, len;
1709 1712 char hintbuf[SOF_MAXNAMELEN];
1710 1713
1711 1714 ent = kmem_zalloc(sizeof (sof_entry_t), KM_SLEEP);
1712 1715 mutex_init(&ent->sofe_lock, NULL, MUTEX_DEFAULT, NULL);
1713 1716
1714 1717 if ((error = copyinstr(uname, ent->sofe_name, SOF_MAXNAMELEN,
1715 1718 &len)) != 0) {
1716 1719 sof_entry_free(ent);
1717 1720 return (error);
1718 1721 }
1719 1722
1720 1723 if (get_udatamodel() == DATAMODEL_NATIVE) {
1721 1724 if (copyin(ufilpropp, &filprop, sizeof (filprop)) != 0) {
1722 1725 sof_entry_free(ent);
1723 1726 return (EFAULT);
1724 1727 }
1725 1728 }
1726 1729 #ifdef _SYSCALL32_IMPL
1727 1730 else {
1728 1731 struct sockconfig_filter_props32 filprop32;
1729 1732
1730 1733 if (copyin(ufilpropp, &filprop32, sizeof (filprop32)) != 0) {
1731 1734 sof_entry_free(ent);
1732 1735 return (EFAULT);
1733 1736 }
1734 1737 filprop.sfp_modname = (char *)(uintptr_t)filprop32.sfp_modname;
1735 1738 filprop.sfp_autoattach = filprop32.sfp_autoattach;
1736 1739 filprop.sfp_hint = filprop32.sfp_hint;
1737 1740 filprop.sfp_hintarg = (char *)(uintptr_t)filprop32.sfp_hintarg;
1738 1741 filprop.sfp_socktuple_cnt = filprop32.sfp_socktuple_cnt;
1739 1742 filprop.sfp_socktuple =
1740 1743 (sof_socktuple_t *)(uintptr_t)filprop32.sfp_socktuple;
1741 1744 }
1742 1745 #endif /* _SYSCALL32_IMPL */
1743 1746
1744 1747 if ((error = copyinstr(filprop.sfp_modname, ent->sofe_modname,
1745 1748 sizeof (ent->sofe_modname), &len)) != 0) {
1746 1749 sof_entry_free(ent);
1747 1750 return (error);
1748 1751 }
1749 1752
1750 1753 /*
1751 1754 * A filter must specify at least one socket tuple.
1752 1755 */
1753 1756 if (filprop.sfp_socktuple_cnt == 0 ||
1754 1757 filprop.sfp_socktuple_cnt > SOF_MAXSOCKTUPLECNT) {
1755 1758 sof_entry_free(ent);
1756 1759 return (EINVAL);
1757 1760 }
1758 1761 ent->sofe_flags = filprop.sfp_autoattach ? SOFEF_AUTO : SOFEF_PROG;
1759 1762 ent->sofe_hint = filprop.sfp_hint;
1760 1763
1761 1764 /*
1762 1765 * Verify the hint, and copy in the hint argument, if necessary.
1763 1766 */
1764 1767 switch (ent->sofe_hint) {
1765 1768 case SOF_HINT_BEFORE:
1766 1769 case SOF_HINT_AFTER:
1767 1770 if ((error = copyinstr(filprop.sfp_hintarg, hintbuf,
1768 1771 sizeof (hintbuf), &len)) != 0) {
1769 1772 sof_entry_free(ent);
1770 1773 return (error);
1771 1774 }
1772 1775 ent->sofe_hintarg = kmem_alloc(len, KM_SLEEP);
1773 1776 bcopy(hintbuf, ent->sofe_hintarg, len);
1774 1777 /* FALLTHRU */
1775 1778 case SOF_HINT_TOP:
1776 1779 case SOF_HINT_BOTTOM:
1777 1780 /* hints cannot be used with programmatic filters */
1778 1781 if (ent->sofe_flags & SOFEF_PROG) {
1779 1782 sof_entry_free(ent);
1780 1783 return (EINVAL);
1781 1784 }
1782 1785 break;
1783 1786 case SOF_HINT_NONE:
1784 1787 break;
1785 1788 default:
1786 1789 /* bad hint value */
1787 1790 sof_entry_free(ent);
1788 1791 return (EINVAL);
1789 1792 }
1790 1793
1791 1794 ent->sofe_socktuple_cnt = filprop.sfp_socktuple_cnt;
1792 1795 tuplesz = sizeof (sof_socktuple_t) * ent->sofe_socktuple_cnt;
1793 1796 ent->sofe_socktuple = kmem_alloc(tuplesz, KM_SLEEP);
1794 1797
1795 1798 if (get_udatamodel() == DATAMODEL_NATIVE) {
1796 1799 if (copyin(filprop.sfp_socktuple, ent->sofe_socktuple,
1797 1800 tuplesz)) {
1798 1801 sof_entry_free(ent);
1799 1802 return (EFAULT);
1800 1803 }
1801 1804 }
1802 1805 #ifdef _SYSCALL32_IMPL
1803 1806 else {
1804 1807 int i;
1805 1808 caddr_t data = (caddr_t)filprop.sfp_socktuple;
1806 1809 sof_socktuple_t *tup = ent->sofe_socktuple;
1807 1810 sof_socktuple32_t tup32;
1808 1811
1809 1812 tup = ent->sofe_socktuple;
1810 1813 for (i = 0; i < ent->sofe_socktuple_cnt; i++, tup++) {
1811 1814 ASSERT(tup < ent->sofe_socktuple + tuplesz);
1812 1815
1813 1816 if (copyin(data, &tup32, sizeof (tup32)) != 0) {
1814 1817 sof_entry_free(ent);
1815 1818 return (EFAULT);
1816 1819 }
1817 1820 tup->sofst_family = tup32.sofst_family;
1818 1821 tup->sofst_type = tup32.sofst_type;
1819 1822 tup->sofst_protocol = tup32.sofst_protocol;
1820 1823
1821 1824 data += sizeof (tup32);
1822 1825 }
1823 1826 }
1824 1827 #endif /* _SYSCALL32_IMPL */
1825 1828
1826 1829 /* Sockets can start using the filter as soon as the filter is added */
1827 1830 if ((error = sof_entry_add(ent)) != 0)
1828 1831 sof_entry_free(ent);
1829 1832
1830 1833 return (error);
1831 1834 }
1832 1835
1833 1836 /*
1834 1837 * Socket configuration system call. It is used to add and remove
1835 1838 * socket types.
1836 1839 */
1837 1840 int
1838 1841 sockconfig(int cmd, void *arg1, void *arg2, void *arg3, void *arg4)
1839 1842 {
1840 1843 int error = 0;
1841 1844
1842 1845 if (secpolicy_net_config(CRED(), B_FALSE) != 0)
1843 1846 return (set_errno(EPERM));
1844 1847
1845 1848 if (sockfs_defer_nl7c_init) {
1846 1849 nl7c_init();
1847 1850 sockfs_defer_nl7c_init = 0;
1848 1851 }
1849 1852
1850 1853 switch (cmd) {
1851 1854 case SOCKCONFIG_ADD_SOCK:
1852 1855 error = sockconf_add_sock((int)(uintptr_t)arg1,
1853 1856 (int)(uintptr_t)arg2, (int)(uintptr_t)arg3, arg4);
1854 1857 break;
↓ open down ↓ |
1818 lines elided |
↑ open up ↑ |
1855 1858 case SOCKCONFIG_REMOVE_SOCK:
1856 1859 error = sockconf_remove_sock((int)(uintptr_t)arg1,
1857 1860 (int)(uintptr_t)arg2, (int)(uintptr_t)arg3);
1858 1861 break;
1859 1862 case SOCKCONFIG_ADD_FILTER:
1860 1863 error = sockconfig_add_filter((const char *)arg1, arg2);
1861 1864 break;
1862 1865 case SOCKCONFIG_REMOVE_FILTER:
1863 1866 error = sockconfig_remove_filter((const char *)arg1);
1864 1867 break;
1868 + case SOCKCONFIG_GET_SOCKTABLE:
1869 + error = sockparams_copyout_socktable((int)(uintptr_t)arg1);
1870 + break;
1865 1871 default:
1866 1872 #ifdef DEBUG
1867 1873 cmn_err(CE_NOTE, "sockconfig: unkonwn subcommand %d", cmd);
1868 1874 #endif
1869 1875 error = EINVAL;
1870 1876 break;
1871 1877 }
1872 1878
1873 1879 if (error != 0) {
1874 1880 eprintline(error);
1875 1881 return (set_errno(error));
1876 1882 }
1877 1883 return (0);
1878 1884 }
1879 1885
1880 1886
1881 1887 /*
1882 1888 * Sendfile is implemented through two schemes, direct I/O or by
1883 1889 * caching in the filesystem page cache. We cache the input file by
1884 1890 * default and use direct I/O only if sendfile_max_size is set
1885 1891 * appropriately as explained below. Note that this logic is consistent
1886 1892 * with other filesystems where caching is turned on by default
1887 1893 * unless explicitly turned off by using the DIRECTIO ioctl.
1888 1894 *
1889 1895 * We choose a slightly different scheme here. One can turn off
1890 1896 * caching by setting sendfile_max_size to 0. One can also enable
1891 1897 * caching of files <= sendfile_max_size by setting sendfile_max_size
1892 1898 * to an appropriate value. By default sendfile_max_size is set to the
1893 1899 * maximum value so that all files are cached. In future, we may provide
1894 1900 * better interfaces for caching the file.
1895 1901 *
1896 1902 * Sendfile through Direct I/O (Zero copy)
1897 1903 * --------------------------------------
1898 1904 *
1899 1905 * As disks are normally slower than the network, we can't have a
1900 1906 * single thread that reads the disk and writes to the network. We
1901 1907 * need to have parallelism. This is done by having the sendfile
1902 1908 * thread create another thread that reads from the filesystem
1903 1909 * and queues it for network processing. In this scheme, the data
1904 1910 * is never copied anywhere i.e it is zero copy unlike the other
1905 1911 * scheme.
1906 1912 *
1907 1913 * We have a sendfile queue (snfq) where each sendfile
1908 1914 * request (snf_req_t) is queued for processing by a thread. Number
1909 1915 * of threads is dynamically allocated and they exit if they are idling
1910 1916 * beyond a specified amount of time. When each request (snf_req_t) is
1911 1917 * processed by a thread, it produces a number of mblk_t structures to
1912 1918 * be consumed by the sendfile thread. snf_deque and snf_enque are
1913 1919 * used for consuming and producing mblks. Size of the filesystem
1914 1920 * read is determined by the tunable (sendfile_read_size). A single
1915 1921 * mblk holds sendfile_read_size worth of data (except the last
1916 1922 * read of the file) which is sent down as a whole to the network.
1917 1923 * sendfile_read_size is set to 1 MB as this seems to be the optimal
1918 1924 * value for the UFS filesystem backed by a striped storage array.
1919 1925 *
1920 1926 * Synchronisation between read (producer) and write (consumer) threads.
1921 1927 * --------------------------------------------------------------------
1922 1928 *
1923 1929 * sr_lock protects sr_ib_head and sr_ib_tail. The lock is held while
1924 1930 * adding and deleting items in this list. Error can happen anytime
1925 1931 * during read or write. There could be unprocessed mblks in the
1926 1932 * sr_ib_XXX list when a read or write error occurs. Whenever error
1927 1933 * is encountered, we need two things to happen :
1928 1934 *
1929 1935 * a) One of the threads need to clean the mblks.
1930 1936 * b) When one thread encounters an error, the other should stop.
1931 1937 *
1932 1938 * For (a), we don't want to penalize the reader thread as it could do
1933 1939 * some useful work processing other requests. For (b), the error can
1934 1940 * be detected by examining sr_read_error or sr_write_error.
1935 1941 * sr_lock protects sr_read_error and sr_write_error. If both reader and
1936 1942 * writer encounters error, we need to report the write error back to
1937 1943 * the application as that's what would have happened if the operations
1938 1944 * were done sequentially. With this in mind, following should work :
1939 1945 *
1940 1946 * - Check for errors before read or write.
1941 1947 * - If the reader encounters error, set the error in sr_read_error.
1942 1948 * Check sr_write_error, if it is set, send cv_signal as it is
1943 1949 * waiting for reader to complete. If it is not set, the writer
1944 1950 * is either running sinking data to the network or blocked
1945 1951 * because of flow control. For handling the latter case, we
1946 1952 * always send a signal. In any case, it will examine sr_read_error
1947 1953 * and return. sr_read_error is marked with SR_READ_DONE to tell
1948 1954 * the writer that the reader is done in all the cases.
1949 1955 * - If the writer encounters error, set the error in sr_write_error.
1950 1956 * The reader thread is either blocked because of flow control or
1951 1957 * running reading data from the disk. For the former, we need to
1952 1958 * wakeup the thread. Again to keep it simple, we always wake up
1953 1959 * the reader thread. Then, wait for the read thread to complete
1954 1960 * if it is not done yet. Cleanup and return.
1955 1961 *
1956 1962 * High and low water marks for the read thread.
1957 1963 * --------------------------------------------
1958 1964 *
1959 1965 * If sendfile() is used to send data over a slow network, we need to
1960 1966 * make sure that the read thread does not produce data at a faster
1961 1967 * rate than the network. This can happen if the disk is faster than
1962 1968 * the network. In such a case, we don't want to build a very large queue.
1963 1969 * But we would still like to get all of the network throughput possible.
1964 1970 * This implies that network should never block waiting for data.
1965 1971 * As there are lot of disk throughput/network throughput combinations
1966 1972 * possible, it is difficult to come up with an accurate number.
1967 1973 * A typical 10K RPM disk has a max seek latency 17ms and rotational
1968 1974 * latency of 3ms for reading a disk block. Thus, the total latency to
1969 1975 * initiate a new read, transfer data from the disk and queue for
1970 1976 * transmission would take about a max of 25ms. Todays max transfer rate
1971 1977 * for network is 100MB/sec. If the thread is blocked because of flow
1972 1978 * control, it would take 25ms to get new data ready for transmission.
1973 1979 * We have to make sure that network is not idling, while we are initiating
1974 1980 * new transfers. So, at 100MB/sec, to keep network busy we would need
1975 1981 * 2.5MB of data. Rounding off, we keep the low water mark to be 3MB of data.
1976 1982 * We need to pick a high water mark so that the woken up thread would
1977 1983 * do considerable work before blocking again to prevent thrashing. Currently,
1978 1984 * we pick this to be 10 times that of the low water mark.
1979 1985 *
1980 1986 * Sendfile with segmap caching (One copy from page cache to mblks).
1981 1987 * ----------------------------------------------------------------
1982 1988 *
1983 1989 * We use the segmap cache for caching the file, if the size of file
1984 1990 * is <= sendfile_max_size. In this case we don't use threads as VM
1985 1991 * is reasonably fast enough to keep up with the network. If the underlying
1986 1992 * transport allows, we call segmap_getmapflt() to map MAXBSIZE (8K) worth
1987 1993 * of data into segmap space, and use the virtual address from segmap
1988 1994 * directly through desballoc() to avoid copy. Once the transport is done
1989 1995 * with the data, the mapping will be released through segmap_release()
1990 1996 * called by the call-back routine.
1991 1997 *
1992 1998 * If zero-copy is not allowed by the transport, we simply call VOP_READ()
1993 1999 * to copy the data from the filesystem into our temporary network buffer.
1994 2000 *
1995 2001 * To disable caching, set sendfile_max_size to 0.
1996 2002 */
1997 2003
1998 2004 uint_t sendfile_read_size = 1024 * 1024;
1999 2005 #define SENDFILE_REQ_LOWAT 3 * 1024 * 1024
2000 2006 uint_t sendfile_req_lowat = SENDFILE_REQ_LOWAT;
2001 2007 uint_t sendfile_req_hiwat = 10 * SENDFILE_REQ_LOWAT;
2002 2008 struct sendfile_stats sf_stats;
2003 2009 struct sendfile_queue *snfq;
2004 2010 clock_t snfq_timeout;
2005 2011 off64_t sendfile_max_size;
2006 2012
2007 2013 static void snf_enque(snf_req_t *, mblk_t *);
2008 2014 static mblk_t *snf_deque(snf_req_t *);
2009 2015
2010 2016 void
2011 2017 sendfile_init(void)
2012 2018 {
2013 2019 snfq = kmem_zalloc(sizeof (struct sendfile_queue), KM_SLEEP);
2014 2020
2015 2021 mutex_init(&snfq->snfq_lock, NULL, MUTEX_DEFAULT, NULL);
2016 2022 cv_init(&snfq->snfq_cv, NULL, CV_DEFAULT, NULL);
2017 2023 snfq->snfq_max_threads = max_ncpus;
2018 2024 snfq_timeout = SNFQ_TIMEOUT;
2019 2025 /* Cache all files by default. */
2020 2026 sendfile_max_size = MAXOFFSET_T;
2021 2027 }
2022 2028
2023 2029 /*
2024 2030 * Queues a mblk_t for network processing.
2025 2031 */
2026 2032 static void
2027 2033 snf_enque(snf_req_t *sr, mblk_t *mp)
2028 2034 {
2029 2035 mp->b_next = NULL;
2030 2036 mutex_enter(&sr->sr_lock);
2031 2037 if (sr->sr_mp_head == NULL) {
2032 2038 sr->sr_mp_head = sr->sr_mp_tail = mp;
2033 2039 cv_signal(&sr->sr_cv);
2034 2040 } else {
2035 2041 sr->sr_mp_tail->b_next = mp;
2036 2042 sr->sr_mp_tail = mp;
2037 2043 }
2038 2044 sr->sr_qlen += MBLKL(mp);
2039 2045 while ((sr->sr_qlen > sr->sr_hiwat) &&
2040 2046 (sr->sr_write_error == 0)) {
2041 2047 sf_stats.ss_full_waits++;
2042 2048 cv_wait(&sr->sr_cv, &sr->sr_lock);
2043 2049 }
2044 2050 mutex_exit(&sr->sr_lock);
2045 2051 }
2046 2052
2047 2053 /*
2048 2054 * De-queues a mblk_t for network processing.
2049 2055 */
2050 2056 static mblk_t *
2051 2057 snf_deque(snf_req_t *sr)
2052 2058 {
2053 2059 mblk_t *mp;
2054 2060
2055 2061 mutex_enter(&sr->sr_lock);
2056 2062 /*
2057 2063 * If we have encountered an error on read or read is
2058 2064 * completed and no more mblks, return NULL.
2059 2065 * We need to check for NULL sr_mp_head also as
2060 2066 * the reads could have completed and there is
2061 2067 * nothing more to come.
2062 2068 */
2063 2069 if (((sr->sr_read_error & ~SR_READ_DONE) != 0) ||
2064 2070 ((sr->sr_read_error & SR_READ_DONE) &&
2065 2071 sr->sr_mp_head == NULL)) {
2066 2072 mutex_exit(&sr->sr_lock);
2067 2073 return (NULL);
2068 2074 }
2069 2075 /*
2070 2076 * To start with neither SR_READ_DONE is marked nor
2071 2077 * the error is set. When we wake up from cv_wait,
2072 2078 * following are the possibilities :
2073 2079 *
2074 2080 * a) sr_read_error is zero and mblks are queued.
2075 2081 * b) sr_read_error is set to SR_READ_DONE
2076 2082 * and mblks are queued.
2077 2083 * c) sr_read_error is set to SR_READ_DONE
2078 2084 * and no mblks.
2079 2085 * d) sr_read_error is set to some error other
2080 2086 * than SR_READ_DONE.
2081 2087 */
2082 2088
2083 2089 while ((sr->sr_read_error == 0) && (sr->sr_mp_head == NULL)) {
2084 2090 sf_stats.ss_empty_waits++;
2085 2091 cv_wait(&sr->sr_cv, &sr->sr_lock);
2086 2092 }
2087 2093 /* Handle (a) and (b) first - the normal case. */
2088 2094 if (((sr->sr_read_error & ~SR_READ_DONE) == 0) &&
2089 2095 (sr->sr_mp_head != NULL)) {
2090 2096 mp = sr->sr_mp_head;
2091 2097 sr->sr_mp_head = mp->b_next;
2092 2098 sr->sr_qlen -= MBLKL(mp);
2093 2099 if (sr->sr_qlen < sr->sr_lowat)
2094 2100 cv_signal(&sr->sr_cv);
2095 2101 mutex_exit(&sr->sr_lock);
2096 2102 mp->b_next = NULL;
2097 2103 return (mp);
2098 2104 }
2099 2105 /* Handle (c) and (d). */
2100 2106 mutex_exit(&sr->sr_lock);
2101 2107 return (NULL);
2102 2108 }
2103 2109
2104 2110 /*
2105 2111 * Reads data from the filesystem and queues it for network processing.
2106 2112 */
2107 2113 void
2108 2114 snf_async_read(snf_req_t *sr)
2109 2115 {
2110 2116 size_t iosize;
2111 2117 u_offset_t fileoff;
2112 2118 u_offset_t size;
2113 2119 int ret_size;
2114 2120 int error;
2115 2121 file_t *fp;
2116 2122 mblk_t *mp;
2117 2123 struct vnode *vp;
2118 2124 int extra = 0;
2119 2125 int maxblk = 0;
2120 2126 int wroff = 0;
2121 2127 struct sonode *so;
2122 2128
2123 2129 fp = sr->sr_fp;
2124 2130 size = sr->sr_file_size;
2125 2131 fileoff = sr->sr_file_off;
2126 2132
2127 2133 /*
2128 2134 * Ignore the error for filesystems that doesn't support DIRECTIO.
2129 2135 */
2130 2136 (void) VOP_IOCTL(fp->f_vnode, _FIODIRECTIO, DIRECTIO_ON, 0,
2131 2137 kcred, NULL, NULL);
2132 2138
2133 2139 vp = sr->sr_vp;
2134 2140 if (vp->v_type == VSOCK) {
2135 2141 stdata_t *stp;
2136 2142
2137 2143 /*
2138 2144 * Get the extra space to insert a header and a trailer.
2139 2145 */
2140 2146 so = VTOSO(vp);
2141 2147 stp = vp->v_stream;
2142 2148 if (stp == NULL) {
2143 2149 wroff = so->so_proto_props.sopp_wroff;
2144 2150 maxblk = so->so_proto_props.sopp_maxblk;
2145 2151 extra = wroff + so->so_proto_props.sopp_tail;
2146 2152 } else {
2147 2153 wroff = (int)(stp->sd_wroff);
2148 2154 maxblk = (int)(stp->sd_maxblk);
2149 2155 extra = wroff + (int)(stp->sd_tail);
2150 2156 }
2151 2157 }
2152 2158
2153 2159 while ((size != 0) && (sr->sr_write_error == 0)) {
2154 2160
2155 2161 iosize = (int)MIN(sr->sr_maxpsz, size);
2156 2162
2157 2163 /*
2158 2164 * Socket filters can limit the mblk size,
2159 2165 * so limit reads to maxblk if there are
2160 2166 * filters present.
2161 2167 */
2162 2168 if (vp->v_type == VSOCK &&
2163 2169 so->so_filter_active > 0 && maxblk != INFPSZ)
2164 2170 iosize = (int)MIN(iosize, maxblk);
2165 2171
2166 2172 if (is_system_labeled()) {
2167 2173 mp = allocb_cred(iosize + extra, CRED(),
2168 2174 curproc->p_pid);
2169 2175 } else {
2170 2176 mp = allocb(iosize + extra, BPRI_MED);
2171 2177 }
2172 2178 if (mp == NULL) {
2173 2179 error = EAGAIN;
2174 2180 break;
2175 2181 }
2176 2182
2177 2183 mp->b_rptr += wroff;
2178 2184
2179 2185 ret_size = soreadfile(fp, mp->b_rptr, fileoff, &error, iosize);
2180 2186
2181 2187 /* Error or Reached EOF ? */
2182 2188 if ((error != 0) || (ret_size == 0)) {
2183 2189 freeb(mp);
2184 2190 break;
2185 2191 }
2186 2192 mp->b_wptr = mp->b_rptr + ret_size;
2187 2193
2188 2194 snf_enque(sr, mp);
2189 2195 size -= ret_size;
2190 2196 fileoff += ret_size;
2191 2197 }
2192 2198 (void) VOP_IOCTL(fp->f_vnode, _FIODIRECTIO, DIRECTIO_OFF, 0,
2193 2199 kcred, NULL, NULL);
2194 2200 mutex_enter(&sr->sr_lock);
2195 2201 sr->sr_read_error = error;
2196 2202 sr->sr_read_error |= SR_READ_DONE;
2197 2203 cv_signal(&sr->sr_cv);
2198 2204 mutex_exit(&sr->sr_lock);
2199 2205 }
2200 2206
2201 2207 void
2202 2208 snf_async_thread(void)
2203 2209 {
2204 2210 snf_req_t *sr;
2205 2211 callb_cpr_t cprinfo;
2206 2212 clock_t time_left = 1;
2207 2213
2208 2214 CALLB_CPR_INIT(&cprinfo, &snfq->snfq_lock, callb_generic_cpr, "snfq");
2209 2215
2210 2216 mutex_enter(&snfq->snfq_lock);
2211 2217 for (;;) {
2212 2218 /*
2213 2219 * If we didn't find a entry, then block until woken up
2214 2220 * again and then look through the queues again.
2215 2221 */
2216 2222 while ((sr = snfq->snfq_req_head) == NULL) {
2217 2223 CALLB_CPR_SAFE_BEGIN(&cprinfo);
2218 2224 if (time_left <= 0) {
2219 2225 snfq->snfq_svc_threads--;
2220 2226 CALLB_CPR_EXIT(&cprinfo);
2221 2227 thread_exit();
2222 2228 /* NOTREACHED */
2223 2229 }
2224 2230 snfq->snfq_idle_cnt++;
2225 2231
2226 2232 time_left = cv_reltimedwait(&snfq->snfq_cv,
2227 2233 &snfq->snfq_lock, snfq_timeout, TR_CLOCK_TICK);
2228 2234 snfq->snfq_idle_cnt--;
2229 2235
2230 2236 CALLB_CPR_SAFE_END(&cprinfo, &snfq->snfq_lock);
2231 2237 }
2232 2238 snfq->snfq_req_head = sr->sr_next;
2233 2239 snfq->snfq_req_cnt--;
2234 2240 mutex_exit(&snfq->snfq_lock);
2235 2241 snf_async_read(sr);
2236 2242 mutex_enter(&snfq->snfq_lock);
2237 2243 }
2238 2244 }
2239 2245
2240 2246
2241 2247 snf_req_t *
2242 2248 create_thread(int operation, struct vnode *vp, file_t *fp,
2243 2249 u_offset_t fileoff, u_offset_t size)
2244 2250 {
2245 2251 snf_req_t *sr;
2246 2252 stdata_t *stp;
2247 2253
2248 2254 sr = (snf_req_t *)kmem_zalloc(sizeof (snf_req_t), KM_SLEEP);
2249 2255
2250 2256 sr->sr_vp = vp;
2251 2257 sr->sr_fp = fp;
2252 2258 stp = vp->v_stream;
2253 2259
2254 2260 /*
2255 2261 * store sd_qn_maxpsz into sr_maxpsz while we have stream head.
2256 2262 * stream might be closed before thread returns from snf_async_read.
2257 2263 */
2258 2264 if (stp != NULL && stp->sd_qn_maxpsz > 0) {
2259 2265 sr->sr_maxpsz = MIN(MAXBSIZE, stp->sd_qn_maxpsz);
2260 2266 } else {
2261 2267 sr->sr_maxpsz = MAXBSIZE;
2262 2268 }
2263 2269
2264 2270 sr->sr_operation = operation;
2265 2271 sr->sr_file_off = fileoff;
2266 2272 sr->sr_file_size = size;
2267 2273 sr->sr_hiwat = sendfile_req_hiwat;
2268 2274 sr->sr_lowat = sendfile_req_lowat;
2269 2275 mutex_init(&sr->sr_lock, NULL, MUTEX_DEFAULT, NULL);
2270 2276 cv_init(&sr->sr_cv, NULL, CV_DEFAULT, NULL);
2271 2277 /*
2272 2278 * See whether we need another thread for servicing this
2273 2279 * request. If there are already enough requests queued
2274 2280 * for the threads, create one if not exceeding
2275 2281 * snfq_max_threads.
2276 2282 */
2277 2283 mutex_enter(&snfq->snfq_lock);
2278 2284 if (snfq->snfq_req_cnt >= snfq->snfq_idle_cnt &&
2279 2285 snfq->snfq_svc_threads < snfq->snfq_max_threads) {
2280 2286 (void) thread_create(NULL, 0, &snf_async_thread, 0, 0, &p0,
2281 2287 TS_RUN, minclsyspri);
2282 2288 snfq->snfq_svc_threads++;
2283 2289 }
2284 2290 if (snfq->snfq_req_head == NULL) {
2285 2291 snfq->snfq_req_head = snfq->snfq_req_tail = sr;
2286 2292 cv_signal(&snfq->snfq_cv);
2287 2293 } else {
2288 2294 snfq->snfq_req_tail->sr_next = sr;
2289 2295 snfq->snfq_req_tail = sr;
2290 2296 }
2291 2297 snfq->snfq_req_cnt++;
2292 2298 mutex_exit(&snfq->snfq_lock);
2293 2299 return (sr);
2294 2300 }
2295 2301
2296 2302 int
2297 2303 snf_direct_io(file_t *fp, file_t *rfp, u_offset_t fileoff, u_offset_t size,
2298 2304 ssize_t *count)
2299 2305 {
2300 2306 snf_req_t *sr;
2301 2307 mblk_t *mp;
2302 2308 int iosize;
2303 2309 int error = 0;
2304 2310 short fflag;
2305 2311 struct vnode *vp;
2306 2312 int ksize;
2307 2313 struct nmsghdr msg;
2308 2314
2309 2315 ksize = 0;
2310 2316 *count = 0;
2311 2317 bzero(&msg, sizeof (msg));
2312 2318
2313 2319 vp = fp->f_vnode;
2314 2320 fflag = fp->f_flag;
2315 2321 if ((sr = create_thread(READ_OP, vp, rfp, fileoff, size)) == NULL)
2316 2322 return (EAGAIN);
2317 2323
2318 2324 /*
2319 2325 * We check for read error in snf_deque. It has to check
2320 2326 * for successful READ_DONE and return NULL, and we might
2321 2327 * as well make an additional check there.
2322 2328 */
2323 2329 while ((mp = snf_deque(sr)) != NULL) {
2324 2330
2325 2331 if (ISSIG(curthread, JUSTLOOKING)) {
2326 2332 freeb(mp);
2327 2333 error = EINTR;
2328 2334 break;
2329 2335 }
2330 2336 iosize = MBLKL(mp);
2331 2337
2332 2338 error = socket_sendmblk(VTOSO(vp), &msg, fflag, CRED(), &mp);
2333 2339
2334 2340 if (error != 0) {
2335 2341 if (mp != NULL)
2336 2342 freeb(mp);
2337 2343 break;
2338 2344 }
2339 2345 ksize += iosize;
2340 2346 }
2341 2347 *count = ksize;
2342 2348
2343 2349 mutex_enter(&sr->sr_lock);
2344 2350 sr->sr_write_error = error;
2345 2351 /* Look at the big comments on why we cv_signal here. */
2346 2352 cv_signal(&sr->sr_cv);
2347 2353
2348 2354 /* Wait for the reader to complete always. */
2349 2355 while (!(sr->sr_read_error & SR_READ_DONE)) {
2350 2356 cv_wait(&sr->sr_cv, &sr->sr_lock);
2351 2357 }
2352 2358 /* If there is no write error, check for read error. */
2353 2359 if (error == 0)
2354 2360 error = (sr->sr_read_error & ~SR_READ_DONE);
2355 2361
2356 2362 if (error != 0) {
2357 2363 mblk_t *next_mp;
2358 2364
2359 2365 mp = sr->sr_mp_head;
2360 2366 while (mp != NULL) {
2361 2367 next_mp = mp->b_next;
2362 2368 mp->b_next = NULL;
2363 2369 freeb(mp);
2364 2370 mp = next_mp;
2365 2371 }
2366 2372 }
2367 2373 mutex_exit(&sr->sr_lock);
2368 2374 kmem_free(sr, sizeof (snf_req_t));
2369 2375 return (error);
2370 2376 }
2371 2377
2372 2378 /* Maximum no.of pages allocated by vpm for sendfile at a time */
2373 2379 #define SNF_VPMMAXPGS (VPMMAXPGS/2)
2374 2380
2375 2381 /*
2376 2382 * Maximum no.of elements in the list returned by vpm, including
2377 2383 * NULL for the last entry
2378 2384 */
2379 2385 #define SNF_MAXVMAPS (SNF_VPMMAXPGS + 1)
2380 2386
2381 2387 typedef struct {
2382 2388 unsigned int snfv_ref;
2383 2389 frtn_t snfv_frtn;
2384 2390 vnode_t *snfv_vp;
2385 2391 struct vmap snfv_vml[SNF_MAXVMAPS];
2386 2392 } snf_vmap_desbinfo;
2387 2393
2388 2394 typedef struct {
2389 2395 frtn_t snfi_frtn;
2390 2396 caddr_t snfi_base;
2391 2397 uint_t snfi_mapoff;
2392 2398 size_t snfi_len;
2393 2399 vnode_t *snfi_vp;
2394 2400 } snf_smap_desbinfo;
2395 2401
2396 2402 /*
2397 2403 * The callback function used for vpm mapped mblks called when the last ref of
2398 2404 * the mblk is dropped which normally occurs when TCP receives the ack. But it
2399 2405 * can be the driver too due to lazy reclaim.
2400 2406 */
2401 2407 void
2402 2408 snf_vmap_desbfree(snf_vmap_desbinfo *snfv)
2403 2409 {
2404 2410 ASSERT(snfv->snfv_ref != 0);
2405 2411 if (atomic_dec_32_nv(&snfv->snfv_ref) == 0) {
2406 2412 vpm_unmap_pages(snfv->snfv_vml, S_READ);
2407 2413 VN_RELE(snfv->snfv_vp);
2408 2414 kmem_free(snfv, sizeof (snf_vmap_desbinfo));
2409 2415 }
2410 2416 }
2411 2417
2412 2418 /*
2413 2419 * The callback function used for segmap'ped mblks called when the last ref of
2414 2420 * the mblk is dropped which normally occurs when TCP receives the ack. But it
2415 2421 * can be the driver too due to lazy reclaim.
2416 2422 */
2417 2423 void
2418 2424 snf_smap_desbfree(snf_smap_desbinfo *snfi)
2419 2425 {
2420 2426 if (! IS_KPM_ADDR(snfi->snfi_base)) {
2421 2427 /*
2422 2428 * We don't need to call segmap_fault(F_SOFTUNLOCK) for
2423 2429 * segmap_kpm as long as the latter never falls back to
2424 2430 * "use_segmap_range". (See segmap_getmapflt().)
2425 2431 *
2426 2432 * Using S_OTHER saves an redundant hat_setref() in
2427 2433 * segmap_unlock()
2428 2434 */
2429 2435 (void) segmap_fault(kas.a_hat, segkmap,
2430 2436 (caddr_t)(uintptr_t)(((uintptr_t)snfi->snfi_base +
2431 2437 snfi->snfi_mapoff) & PAGEMASK), snfi->snfi_len,
2432 2438 F_SOFTUNLOCK, S_OTHER);
2433 2439 }
2434 2440 (void) segmap_release(segkmap, snfi->snfi_base, SM_DONTNEED);
2435 2441 VN_RELE(snfi->snfi_vp);
2436 2442 kmem_free(snfi, sizeof (*snfi));
2437 2443 }
2438 2444
2439 2445 /*
2440 2446 * Use segmap or vpm instead of bcopy to send down a desballoca'ed, mblk.
2441 2447 * When segmap is used, the mblk contains a segmap slot of no more
2442 2448 * than MAXBSIZE.
2443 2449 *
2444 2450 * With vpm, a maximum of SNF_MAXVMAPS page-sized mappings can be obtained
2445 2451 * in each iteration and sent by socket_sendmblk until an error occurs or
2446 2452 * the requested size has been transferred. An mblk is esballoca'ed from
2447 2453 * each mapped page and a chain of these mblk is sent to the transport layer.
2448 2454 * vpm will be called to unmap the pages when all mblks have been freed by
2449 2455 * free_func.
2450 2456 *
2451 2457 * At the end of the whole sendfile() operation, we wait till the data from
2452 2458 * the last mblk is ack'ed by the transport before returning so that the
2453 2459 * caller of sendfile() can safely modify the file content.
2454 2460 */
2455 2461 int
2456 2462 snf_segmap(file_t *fp, vnode_t *fvp, u_offset_t fileoff, u_offset_t total_size,
2457 2463 ssize_t *count, boolean_t nowait)
2458 2464 {
2459 2465 caddr_t base;
2460 2466 int mapoff;
2461 2467 vnode_t *vp;
2462 2468 mblk_t *mp = NULL;
2463 2469 int chain_size;
2464 2470 int error;
2465 2471 clock_t deadlk_wait;
2466 2472 short fflag;
2467 2473 int ksize;
2468 2474 struct vattr va;
2469 2475 boolean_t dowait = B_FALSE;
2470 2476 struct nmsghdr msg;
2471 2477
2472 2478 vp = fp->f_vnode;
2473 2479 fflag = fp->f_flag;
2474 2480 ksize = 0;
2475 2481 bzero(&msg, sizeof (msg));
2476 2482
2477 2483 for (;;) {
2478 2484 if (ISSIG(curthread, JUSTLOOKING)) {
2479 2485 error = EINTR;
2480 2486 break;
2481 2487 }
2482 2488
2483 2489 if (vpm_enable) {
2484 2490 snf_vmap_desbinfo *snfv;
2485 2491 mblk_t *nmp;
2486 2492 int mblk_size;
2487 2493 int maxsize;
2488 2494 int i;
2489 2495
2490 2496 mapoff = fileoff & PAGEOFFSET;
2491 2497 maxsize = MIN((SNF_VPMMAXPGS * PAGESIZE), total_size);
2492 2498
2493 2499 snfv = kmem_zalloc(sizeof (snf_vmap_desbinfo),
2494 2500 KM_SLEEP);
2495 2501
2496 2502 /*
2497 2503 * Get vpm mappings for maxsize with read access.
2498 2504 * If the pages aren't available yet, we get
2499 2505 * DEADLK, so wait and try again a little later using
2500 2506 * an increasing wait. We might be here a long time.
2501 2507 *
2502 2508 * If delay_sig returns EINTR, be sure to exit and
2503 2509 * pass it up to the caller.
2504 2510 */
2505 2511 deadlk_wait = 0;
2506 2512 while ((error = vpm_map_pages(fvp, fileoff,
2507 2513 (size_t)maxsize, (VPM_FETCHPAGE), snfv->snfv_vml,
2508 2514 SNF_MAXVMAPS, NULL, S_READ)) == EDEADLK) {
2509 2515 deadlk_wait += (deadlk_wait < 5) ? 1 : 4;
2510 2516 if ((error = delay_sig(deadlk_wait)) != 0) {
2511 2517 break;
2512 2518 }
2513 2519 }
2514 2520 if (error != 0) {
2515 2521 kmem_free(snfv, sizeof (snf_vmap_desbinfo));
2516 2522 error = (error == EINTR) ? EINTR : EIO;
2517 2523 goto out;
2518 2524 }
2519 2525 snfv->snfv_frtn.free_func = snf_vmap_desbfree;
2520 2526 snfv->snfv_frtn.free_arg = (caddr_t)snfv;
2521 2527
2522 2528 /* Construct the mblk chain from the page mappings */
2523 2529 chain_size = 0;
2524 2530 for (i = 0; (snfv->snfv_vml[i].vs_addr != NULL) &&
2525 2531 total_size > 0; i++) {
2526 2532 ASSERT(chain_size < maxsize);
2527 2533 mblk_size = MIN(snfv->snfv_vml[i].vs_len -
2528 2534 mapoff, total_size);
2529 2535 nmp = esballoca(
2530 2536 (uchar_t *)snfv->snfv_vml[i].vs_addr +
2531 2537 mapoff, mblk_size, BPRI_HI,
2532 2538 &snfv->snfv_frtn);
2533 2539
2534 2540 /*
2535 2541 * We return EAGAIN after unmapping the pages
2536 2542 * if we cannot allocate the the head of the
2537 2543 * chain. Otherwise, we continue sending the
2538 2544 * mblks constructed so far.
2539 2545 */
2540 2546 if (nmp == NULL) {
2541 2547 if (i == 0) {
2542 2548 vpm_unmap_pages(snfv->snfv_vml,
2543 2549 S_READ);
2544 2550 kmem_free(snfv,
2545 2551 sizeof (snf_vmap_desbinfo));
2546 2552 error = EAGAIN;
2547 2553 goto out;
2548 2554 }
2549 2555 break;
2550 2556 }
2551 2557 /* Mark this dblk with the zero-copy flag */
2552 2558 nmp->b_datap->db_struioflag |= STRUIO_ZC;
2553 2559 nmp->b_wptr += mblk_size;
2554 2560 chain_size += mblk_size;
2555 2561 fileoff += mblk_size;
2556 2562 total_size -= mblk_size;
2557 2563 snfv->snfv_ref++;
2558 2564 mapoff = 0;
2559 2565 if (i > 0)
2560 2566 linkb(mp, nmp);
2561 2567 else
2562 2568 mp = nmp;
2563 2569 }
2564 2570 VN_HOLD(fvp);
2565 2571 snfv->snfv_vp = fvp;
2566 2572 } else {
2567 2573 /* vpm not supported. fallback to segmap */
2568 2574 snf_smap_desbinfo *snfi;
2569 2575
2570 2576 mapoff = fileoff & MAXBOFFSET;
2571 2577 chain_size = MAXBSIZE - mapoff;
2572 2578 if (chain_size > total_size)
2573 2579 chain_size = total_size;
2574 2580 /*
2575 2581 * we don't forcefault because we'll call
2576 2582 * segmap_fault(F_SOFTLOCK) next.
2577 2583 *
2578 2584 * S_READ will get the ref bit set (by either
2579 2585 * segmap_getmapflt() or segmap_fault()) and page
2580 2586 * shared locked.
2581 2587 */
2582 2588 base = segmap_getmapflt(segkmap, fvp, fileoff,
2583 2589 chain_size, segmap_kpm ? SM_FAULT : 0, S_READ);
2584 2590
2585 2591 snfi = kmem_alloc(sizeof (*snfi), KM_SLEEP);
2586 2592 snfi->snfi_len = (size_t)roundup(mapoff+chain_size,
2587 2593 PAGESIZE)- (mapoff & PAGEMASK);
2588 2594 /*
2589 2595 * We must call segmap_fault() even for segmap_kpm
2590 2596 * because that's how error gets returned.
2591 2597 * (segmap_getmapflt() never fails but segmap_fault()
2592 2598 * does.)
2593 2599 *
2594 2600 * If the pages aren't available yet, we get
2595 2601 * DEADLK, so wait and try again a little later using
2596 2602 * an increasing wait. We might be here a long time.
2597 2603 *
2598 2604 * If delay_sig returns EINTR, be sure to exit and
2599 2605 * pass it up to the caller.
2600 2606 */
2601 2607 deadlk_wait = 0;
2602 2608 while ((error = FC_ERRNO(segmap_fault(kas.a_hat,
2603 2609 segkmap, (caddr_t)(uintptr_t)(((uintptr_t)base +
2604 2610 mapoff) & PAGEMASK), snfi->snfi_len, F_SOFTLOCK,
2605 2611 S_READ))) == EDEADLK) {
2606 2612 deadlk_wait += (deadlk_wait < 5) ? 1 : 4;
2607 2613 if ((error = delay_sig(deadlk_wait)) != 0) {
2608 2614 break;
2609 2615 }
2610 2616 }
2611 2617 if (error != 0) {
2612 2618 (void) segmap_release(segkmap, base, 0);
2613 2619 kmem_free(snfi, sizeof (*snfi));
2614 2620 error = (error == EINTR) ? EINTR : EIO;
2615 2621 goto out;
2616 2622 }
2617 2623 snfi->snfi_frtn.free_func = snf_smap_desbfree;
2618 2624 snfi->snfi_frtn.free_arg = (caddr_t)snfi;
2619 2625 snfi->snfi_base = base;
2620 2626 snfi->snfi_mapoff = mapoff;
2621 2627 mp = esballoca((uchar_t *)base + mapoff, chain_size,
2622 2628 BPRI_HI, &snfi->snfi_frtn);
2623 2629
2624 2630 if (mp == NULL) {
2625 2631 (void) segmap_fault(kas.a_hat, segkmap,
2626 2632 (caddr_t)(uintptr_t)(((uintptr_t)base +
2627 2633 mapoff) & PAGEMASK), snfi->snfi_len,
2628 2634 F_SOFTUNLOCK, S_OTHER);
2629 2635 (void) segmap_release(segkmap, base, 0);
2630 2636 kmem_free(snfi, sizeof (*snfi));
2631 2637 freemsg(mp);
2632 2638 error = EAGAIN;
2633 2639 goto out;
2634 2640 }
2635 2641 VN_HOLD(fvp);
2636 2642 snfi->snfi_vp = fvp;
2637 2643 mp->b_wptr += chain_size;
2638 2644
2639 2645 /* Mark this dblk with the zero-copy flag */
2640 2646 mp->b_datap->db_struioflag |= STRUIO_ZC;
2641 2647 fileoff += chain_size;
2642 2648 total_size -= chain_size;
2643 2649 }
2644 2650
2645 2651 if (total_size == 0 && !nowait) {
2646 2652 ASSERT(!dowait);
2647 2653 dowait = B_TRUE;
2648 2654 mp->b_datap->db_struioflag |= STRUIO_ZCNOTIFY;
2649 2655 }
2650 2656 VOP_RWUNLOCK(fvp, V_WRITELOCK_FALSE, NULL);
2651 2657 error = socket_sendmblk(VTOSO(vp), &msg, fflag, CRED(), &mp);
2652 2658 if (error != 0) {
2653 2659 /*
2654 2660 * mp contains the mblks that were not sent by
2655 2661 * socket_sendmblk. Use its size to update *count
2656 2662 */
2657 2663 *count = ksize + (chain_size - msgdsize(mp));
2658 2664 if (mp != NULL)
2659 2665 freemsg(mp);
2660 2666 return (error);
2661 2667 }
2662 2668 ksize += chain_size;
2663 2669 if (total_size == 0)
2664 2670 goto done;
2665 2671
2666 2672 (void) VOP_RWLOCK(fvp, V_WRITELOCK_FALSE, NULL);
2667 2673 va.va_mask = AT_SIZE;
2668 2674 error = VOP_GETATTR(fvp, &va, 0, kcred, NULL);
2669 2675 if (error)
2670 2676 break;
2671 2677 /* Read as much as possible. */
2672 2678 if (fileoff >= va.va_size)
2673 2679 break;
2674 2680 if (total_size + fileoff > va.va_size)
2675 2681 total_size = va.va_size - fileoff;
2676 2682 }
2677 2683 out:
2678 2684 VOP_RWUNLOCK(fvp, V_WRITELOCK_FALSE, NULL);
2679 2685 done:
2680 2686 *count = ksize;
2681 2687 if (dowait) {
2682 2688 stdata_t *stp;
2683 2689
2684 2690 stp = vp->v_stream;
2685 2691 if (stp == NULL) {
2686 2692 struct sonode *so;
2687 2693 so = VTOSO(vp);
2688 2694 error = so_zcopy_wait(so);
2689 2695 } else {
2690 2696 mutex_enter(&stp->sd_lock);
2691 2697 while (!(stp->sd_flag & STZCNOTIFY)) {
2692 2698 if (cv_wait_sig(&stp->sd_zcopy_wait,
2693 2699 &stp->sd_lock) == 0) {
2694 2700 error = EINTR;
2695 2701 break;
2696 2702 }
2697 2703 }
2698 2704 stp->sd_flag &= ~STZCNOTIFY;
2699 2705 mutex_exit(&stp->sd_lock);
2700 2706 }
2701 2707 }
2702 2708 return (error);
2703 2709 }
2704 2710
2705 2711 int
2706 2712 snf_cache(file_t *fp, vnode_t *fvp, u_offset_t fileoff, u_offset_t size,
2707 2713 uint_t maxpsz, ssize_t *count)
2708 2714 {
2709 2715 struct vnode *vp;
2710 2716 mblk_t *mp;
2711 2717 int iosize;
2712 2718 int extra = 0;
2713 2719 int error;
2714 2720 short fflag;
2715 2721 int ksize;
2716 2722 int ioflag;
2717 2723 struct uio auio;
2718 2724 struct iovec aiov;
2719 2725 struct vattr va;
2720 2726 int maxblk = 0;
2721 2727 int wroff = 0;
2722 2728 struct sonode *so;
2723 2729 struct nmsghdr msg;
2724 2730
2725 2731 vp = fp->f_vnode;
2726 2732 if (vp->v_type == VSOCK) {
2727 2733 stdata_t *stp;
2728 2734
2729 2735 /*
2730 2736 * Get the extra space to insert a header and a trailer.
2731 2737 */
2732 2738 so = VTOSO(vp);
2733 2739 stp = vp->v_stream;
2734 2740 if (stp == NULL) {
2735 2741 wroff = so->so_proto_props.sopp_wroff;
2736 2742 maxblk = so->so_proto_props.sopp_maxblk;
2737 2743 extra = wroff + so->so_proto_props.sopp_tail;
2738 2744 } else {
2739 2745 wroff = (int)(stp->sd_wroff);
2740 2746 maxblk = (int)(stp->sd_maxblk);
2741 2747 extra = wroff + (int)(stp->sd_tail);
2742 2748 }
2743 2749 }
2744 2750 bzero(&msg, sizeof (msg));
2745 2751 fflag = fp->f_flag;
2746 2752 ksize = 0;
2747 2753 auio.uio_iov = &aiov;
2748 2754 auio.uio_iovcnt = 1;
2749 2755 auio.uio_segflg = UIO_SYSSPACE;
2750 2756 auio.uio_llimit = MAXOFFSET_T;
2751 2757 auio.uio_fmode = fflag;
2752 2758 auio.uio_extflg = UIO_COPY_CACHED;
2753 2759 ioflag = auio.uio_fmode & (FSYNC|FDSYNC|FRSYNC);
2754 2760 /* If read sync is not asked for, filter sync flags */
2755 2761 if ((ioflag & FRSYNC) == 0)
2756 2762 ioflag &= ~(FSYNC|FDSYNC);
2757 2763 for (;;) {
2758 2764 if (ISSIG(curthread, JUSTLOOKING)) {
2759 2765 error = EINTR;
2760 2766 break;
2761 2767 }
2762 2768 iosize = (int)MIN(maxpsz, size);
2763 2769
2764 2770 /*
2765 2771 * Socket filters can limit the mblk size,
2766 2772 * so limit reads to maxblk if there are
2767 2773 * filters present.
2768 2774 */
2769 2775 if (vp->v_type == VSOCK &&
2770 2776 so->so_filter_active > 0 && maxblk != INFPSZ)
2771 2777 iosize = (int)MIN(iosize, maxblk);
2772 2778
2773 2779 if (is_system_labeled()) {
2774 2780 mp = allocb_cred(iosize + extra, CRED(),
2775 2781 curproc->p_pid);
2776 2782 } else {
2777 2783 mp = allocb(iosize + extra, BPRI_MED);
2778 2784 }
2779 2785 if (mp == NULL) {
2780 2786 error = EAGAIN;
2781 2787 break;
2782 2788 }
2783 2789
2784 2790 mp->b_rptr += wroff;
2785 2791
2786 2792 aiov.iov_base = (caddr_t)mp->b_rptr;
2787 2793 aiov.iov_len = iosize;
2788 2794 auio.uio_loffset = fileoff;
2789 2795 auio.uio_resid = iosize;
2790 2796
2791 2797 error = VOP_READ(fvp, &auio, ioflag, fp->f_cred, NULL);
2792 2798 iosize -= auio.uio_resid;
2793 2799
2794 2800 if (error == EINTR && iosize != 0)
2795 2801 error = 0;
2796 2802
2797 2803 if (error != 0 || iosize == 0) {
2798 2804 freeb(mp);
2799 2805 break;
2800 2806 }
2801 2807 mp->b_wptr = mp->b_rptr + iosize;
2802 2808
2803 2809 VOP_RWUNLOCK(fvp, V_WRITELOCK_FALSE, NULL);
2804 2810
2805 2811 error = socket_sendmblk(VTOSO(vp), &msg, fflag, CRED(), &mp);
2806 2812
2807 2813 if (error != 0) {
2808 2814 *count = ksize;
2809 2815 if (mp != NULL)
2810 2816 freeb(mp);
2811 2817 return (error);
2812 2818 }
2813 2819 ksize += iosize;
2814 2820 size -= iosize;
2815 2821 if (size == 0)
2816 2822 goto done;
2817 2823
2818 2824 fileoff += iosize;
2819 2825 (void) VOP_RWLOCK(fvp, V_WRITELOCK_FALSE, NULL);
2820 2826 va.va_mask = AT_SIZE;
2821 2827 error = VOP_GETATTR(fvp, &va, 0, kcred, NULL);
2822 2828 if (error)
2823 2829 break;
2824 2830 /* Read as much as possible. */
2825 2831 if (fileoff >= va.va_size)
2826 2832 size = 0;
2827 2833 else if (size + fileoff > va.va_size)
2828 2834 size = va.va_size - fileoff;
2829 2835 }
2830 2836 VOP_RWUNLOCK(fvp, V_WRITELOCK_FALSE, NULL);
2831 2837 done:
2832 2838 *count = ksize;
2833 2839 return (error);
2834 2840 }
2835 2841
2836 2842 #if defined(_SYSCALL32_IMPL) || defined(_ILP32)
2837 2843 /*
2838 2844 * Largefile support for 32 bit applications only.
2839 2845 */
2840 2846 int
2841 2847 sosendfile64(file_t *fp, file_t *rfp, const struct ksendfilevec64 *sfv,
2842 2848 ssize32_t *count32)
2843 2849 {
2844 2850 ssize32_t sfv_len;
2845 2851 u_offset_t sfv_off, va_size;
2846 2852 struct vnode *vp, *fvp, *realvp;
2847 2853 struct vattr va;
2848 2854 stdata_t *stp;
2849 2855 ssize_t count = 0;
2850 2856 int error = 0;
2851 2857 boolean_t dozcopy = B_FALSE;
2852 2858 uint_t maxpsz;
2853 2859
2854 2860 sfv_len = (ssize32_t)sfv->sfv_len;
2855 2861 if (sfv_len < 0) {
2856 2862 error = EINVAL;
2857 2863 goto out;
2858 2864 }
2859 2865
2860 2866 if (sfv_len == 0) goto out;
2861 2867
2862 2868 sfv_off = (u_offset_t)sfv->sfv_off;
2863 2869
2864 2870 /* Same checks as in pread */
2865 2871 if (sfv_off > MAXOFFSET_T) {
2866 2872 error = EINVAL;
2867 2873 goto out;
2868 2874 }
2869 2875 if (sfv_off + sfv_len > MAXOFFSET_T)
2870 2876 sfv_len = (ssize32_t)(MAXOFFSET_T - sfv_off);
2871 2877
2872 2878 /*
2873 2879 * There are no more checks on sfv_len. So, we cast it to
2874 2880 * u_offset_t and share the snf_direct_io/snf_cache code between
2875 2881 * 32 bit and 64 bit.
2876 2882 *
2877 2883 * TODO: should do nbl_need_check() like read()?
2878 2884 */
2879 2885 if (sfv_len > sendfile_max_size) {
2880 2886 sf_stats.ss_file_not_cached++;
2881 2887 error = snf_direct_io(fp, rfp, sfv_off, (u_offset_t)sfv_len,
2882 2888 &count);
2883 2889 goto out;
2884 2890 }
2885 2891 fvp = rfp->f_vnode;
2886 2892 if (VOP_REALVP(fvp, &realvp, NULL) == 0)
2887 2893 fvp = realvp;
2888 2894 /*
2889 2895 * Grab the lock as a reader to prevent the file size
2890 2896 * from changing underneath.
2891 2897 */
2892 2898 (void) VOP_RWLOCK(fvp, V_WRITELOCK_FALSE, NULL);
2893 2899 va.va_mask = AT_SIZE;
2894 2900 error = VOP_GETATTR(fvp, &va, 0, kcred, NULL);
2895 2901 va_size = va.va_size;
2896 2902 if ((error != 0) || (va_size == 0) || (sfv_off >= va_size)) {
2897 2903 VOP_RWUNLOCK(fvp, V_WRITELOCK_FALSE, NULL);
2898 2904 goto out;
2899 2905 }
2900 2906 /* Read as much as possible. */
2901 2907 if (sfv_off + sfv_len > va_size)
2902 2908 sfv_len = va_size - sfv_off;
2903 2909
2904 2910 vp = fp->f_vnode;
2905 2911 stp = vp->v_stream;
2906 2912 /*
2907 2913 * When the NOWAIT flag is not set, we enable zero-copy only if the
2908 2914 * transfer size is large enough. This prevents performance loss
2909 2915 * when the caller sends the file piece by piece.
2910 2916 */
2911 2917 if (sfv_len >= MAXBSIZE && (sfv_len >= (va_size >> 1) ||
2912 2918 (sfv->sfv_flag & SFV_NOWAIT) || sfv_len >= 0x1000000) &&
2913 2919 !vn_has_flocks(fvp) && !(fvp->v_flag & VNOMAP)) {
2914 2920 uint_t copyflag;
2915 2921 copyflag = stp != NULL ? stp->sd_copyflag :
2916 2922 VTOSO(vp)->so_proto_props.sopp_zcopyflag;
2917 2923 if ((copyflag & (STZCVMSAFE|STZCVMUNSAFE)) == 0) {
2918 2924 int on = 1;
2919 2925
2920 2926 if (socket_setsockopt(VTOSO(vp), SOL_SOCKET,
2921 2927 SO_SND_COPYAVOID, &on, sizeof (on), CRED()) == 0)
2922 2928 dozcopy = B_TRUE;
2923 2929 } else {
2924 2930 dozcopy = copyflag & STZCVMSAFE;
2925 2931 }
2926 2932 }
2927 2933 if (dozcopy) {
2928 2934 sf_stats.ss_file_segmap++;
2929 2935 error = snf_segmap(fp, fvp, sfv_off, (u_offset_t)sfv_len,
2930 2936 &count, ((sfv->sfv_flag & SFV_NOWAIT) != 0));
2931 2937 } else {
2932 2938 if (vp->v_type == VSOCK && stp == NULL) {
2933 2939 sonode_t *so = VTOSO(vp);
2934 2940 maxpsz = so->so_proto_props.sopp_maxpsz;
2935 2941 } else if (stp != NULL) {
2936 2942 maxpsz = stp->sd_qn_maxpsz;
2937 2943 } else {
2938 2944 maxpsz = maxphys;
2939 2945 }
2940 2946
2941 2947 if (maxpsz == INFPSZ)
2942 2948 maxpsz = maxphys;
2943 2949 else
2944 2950 maxpsz = roundup(maxpsz, MAXBSIZE);
2945 2951 sf_stats.ss_file_cached++;
2946 2952 error = snf_cache(fp, fvp, sfv_off, (u_offset_t)sfv_len,
2947 2953 maxpsz, &count);
2948 2954 }
2949 2955 out:
2950 2956 releasef(sfv->sfv_fd);
2951 2957 *count32 = (ssize32_t)count;
2952 2958 return (error);
2953 2959 }
2954 2960 #endif
2955 2961
2956 2962 #ifdef _SYSCALL32_IMPL
2957 2963 /*
2958 2964 * recv32(), recvfrom32(), send32(), sendto32(): intentionally return a
2959 2965 * ssize_t rather than ssize32_t; see the comments above read32 for details.
2960 2966 */
2961 2967
2962 2968 ssize_t
2963 2969 recv32(int32_t sock, caddr32_t buffer, size32_t len, int32_t flags)
2964 2970 {
2965 2971 return (recv(sock, (void *)(uintptr_t)buffer, (ssize32_t)len, flags));
2966 2972 }
2967 2973
2968 2974 ssize_t
2969 2975 recvfrom32(int32_t sock, caddr32_t buffer, size32_t len, int32_t flags,
2970 2976 caddr32_t name, caddr32_t namelenp)
2971 2977 {
2972 2978 return (recvfrom(sock, (void *)(uintptr_t)buffer, (ssize32_t)len, flags,
2973 2979 (void *)(uintptr_t)name, (void *)(uintptr_t)namelenp));
2974 2980 }
2975 2981
2976 2982 ssize_t
2977 2983 send32(int32_t sock, caddr32_t buffer, size32_t len, int32_t flags)
2978 2984 {
2979 2985 return (send(sock, (void *)(uintptr_t)buffer, (ssize32_t)len, flags));
2980 2986 }
2981 2987
2982 2988 ssize_t
2983 2989 sendto32(int32_t sock, caddr32_t buffer, size32_t len, int32_t flags,
2984 2990 caddr32_t name, socklen_t namelen)
2985 2991 {
2986 2992 return (sendto(sock, (void *)(uintptr_t)buffer, (ssize32_t)len, flags,
2987 2993 (void *)(uintptr_t)name, namelen));
2988 2994 }
2989 2995 #endif /* _SYSCALL32_IMPL */
2990 2996
2991 2997 /*
2992 2998 * Function wrappers (mostly around the sonode switch) for
2993 2999 * backward compatibility.
2994 3000 */
2995 3001
2996 3002 int
2997 3003 soaccept(struct sonode *so, int fflag, struct sonode **nsop)
2998 3004 {
2999 3005 return (socket_accept(so, fflag, CRED(), nsop));
3000 3006 }
3001 3007
3002 3008 int
3003 3009 sobind(struct sonode *so, struct sockaddr *name, socklen_t namelen,
3004 3010 int backlog, int flags)
3005 3011 {
3006 3012 int error;
3007 3013
3008 3014 error = socket_bind(so, name, namelen, flags, CRED());
3009 3015 if (error == 0 && backlog != 0)
3010 3016 return (socket_listen(so, backlog, CRED()));
3011 3017
3012 3018 return (error);
3013 3019 }
3014 3020
3015 3021 int
3016 3022 solisten(struct sonode *so, int backlog)
3017 3023 {
3018 3024 return (socket_listen(so, backlog, CRED()));
3019 3025 }
3020 3026
3021 3027 int
3022 3028 soconnect(struct sonode *so, struct sockaddr *name, socklen_t namelen,
3023 3029 int fflag, int flags)
3024 3030 {
3025 3031 return (socket_connect(so, name, namelen, fflag, flags, CRED()));
3026 3032 }
3027 3033
3028 3034 int
3029 3035 sorecvmsg(struct sonode *so, struct nmsghdr *msg, struct uio *uiop)
3030 3036 {
3031 3037 return (socket_recvmsg(so, msg, uiop, CRED()));
3032 3038 }
3033 3039
3034 3040 int
3035 3041 sosendmsg(struct sonode *so, struct nmsghdr *msg, struct uio *uiop)
3036 3042 {
3037 3043 return (socket_sendmsg(so, msg, uiop, CRED()));
3038 3044 }
3039 3045
3040 3046 int
3041 3047 soshutdown(struct sonode *so, int how)
3042 3048 {
3043 3049 return (socket_shutdown(so, how, CRED()));
3044 3050 }
3045 3051
3046 3052 int
3047 3053 sogetsockopt(struct sonode *so, int level, int option_name, void *optval,
3048 3054 socklen_t *optlenp, int flags)
3049 3055 {
3050 3056 return (socket_getsockopt(so, level, option_name, optval, optlenp,
3051 3057 flags, CRED()));
3052 3058 }
3053 3059
3054 3060 int
3055 3061 sosetsockopt(struct sonode *so, int level, int option_name, const void *optval,
3056 3062 t_uscalar_t optlen)
3057 3063 {
3058 3064 return (socket_setsockopt(so, level, option_name, optval, optlen,
3059 3065 CRED()));
3060 3066 }
3061 3067
3062 3068 /*
3063 3069 * Because this is backward compatibility interface it only needs to be
3064 3070 * able to handle the creation of TPI sockfs sockets.
3065 3071 */
3066 3072 struct sonode *
3067 3073 socreate(struct sockparams *sp, int family, int type, int protocol, int version,
3068 3074 int *errorp)
3069 3075 {
3070 3076 struct sonode *so;
3071 3077
3072 3078 ASSERT(sp != NULL);
3073 3079
3074 3080 so = sp->sp_smod_info->smod_sock_create_func(sp, family, type, protocol,
3075 3081 version, SOCKET_SLEEP, errorp, CRED());
3076 3082 if (so == NULL) {
3077 3083 SOCKPARAMS_DEC_REF(sp);
3078 3084 } else {
3079 3085 if ((*errorp = SOP_INIT(so, NULL, CRED(), SOCKET_SLEEP)) == 0) {
3080 3086 /* Cannot fail, only bumps so_count */
3081 3087 (void) VOP_OPEN(&SOTOV(so), FREAD|FWRITE, CRED(), NULL);
3082 3088 } else {
3083 3089 socket_destroy(so);
3084 3090 so = NULL;
3085 3091 }
3086 3092 }
3087 3093 return (so);
3088 3094 }
↓ open down ↓ |
1214 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX