Print this page
5880 Increase IOV_MAX to at least 1024
Portions contributed by: Jerry Jelinek <jerry.jelinek@joyent.com>
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/common/fs/sockfs/socksubr.c
+++ new/usr/src/uts/common/fs/sockfs/socksubr.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
↓ open down ↓ |
15 lines elided |
↑ open up ↑ |
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 23 * Copyright (c) 1995, 2010, Oracle and/or its affiliates. All rights reserved.
24 24 * Copyright 2016 Nexenta Systems, Inc. All rights reserved.
25 25 * Copyright 2019 OmniOS Community Edition (OmniOSce) Association.
26 + * Copyright 2015, Joyent, Inc. All rights reserved.
26 27 */
27 28
28 29 #include <sys/types.h>
29 30 #include <sys/t_lock.h>
30 31 #include <sys/param.h>
31 32 #include <sys/systm.h>
32 33 #include <sys/buf.h>
33 34 #include <sys/conf.h>
34 35 #include <sys/cred.h>
35 36 #include <sys/kmem.h>
36 37 #include <sys/sysmacros.h>
37 38 #include <sys/vfs.h>
38 39 #include <sys/vfs_opreg.h>
39 40 #include <sys/vnode.h>
40 41 #include <sys/debug.h>
41 42 #include <sys/errno.h>
42 43 #include <sys/time.h>
43 44 #include <sys/file.h>
44 45 #include <sys/open.h>
45 46 #include <sys/user.h>
46 47 #include <sys/termios.h>
47 48 #include <sys/stream.h>
48 49 #include <sys/strsubr.h>
49 50 #include <sys/strsun.h>
50 51 #include <sys/esunddi.h>
51 52 #include <sys/flock.h>
52 53 #include <sys/modctl.h>
53 54 #include <sys/cmn_err.h>
54 55 #include <sys/mkdev.h>
55 56 #include <sys/pathname.h>
56 57 #include <sys/ddi.h>
57 58 #include <sys/stat.h>
58 59 #include <sys/fs/snode.h>
59 60 #include <sys/fs/dv_node.h>
60 61 #include <sys/zone.h>
61 62
62 63 #include <sys/socket.h>
63 64 #include <sys/socketvar.h>
64 65 #include <netinet/in.h>
65 66 #include <sys/un.h>
66 67 #include <sys/ucred.h>
67 68
68 69 #include <sys/tiuser.h>
69 70 #define _SUN_TPI_VERSION 2
70 71 #include <sys/tihdr.h>
71 72
72 73 #include <c2/audit.h>
73 74
74 75 #include <fs/sockfs/nl7c.h>
75 76 #include <fs/sockfs/sockcommon.h>
76 77 #include <fs/sockfs/sockfilter_impl.h>
77 78 #include <fs/sockfs/socktpi.h>
78 79 #include <fs/sockfs/socktpi_impl.h>
79 80 #include <fs/sockfs/sodirect.h>
80 81
81 82 /*
82 83 * Macros that operate on struct cmsghdr.
83 84 * The CMSG_VALID macro does not assume that the last option buffer is padded.
84 85 */
85 86 #define CMSG_CONTENT(cmsg) (&((cmsg)[1]))
86 87 #define CMSG_CONTENTLEN(cmsg) ((cmsg)->cmsg_len - sizeof (struct cmsghdr))
87 88 #define CMSG_VALID(cmsg, start, end) \
88 89 (ISALIGNED_cmsghdr(cmsg) && \
89 90 ((uintptr_t)(cmsg) >= (uintptr_t)(start)) && \
90 91 ((uintptr_t)(cmsg) < (uintptr_t)(end)) && \
91 92 ((ssize_t)(cmsg)->cmsg_len >= sizeof (struct cmsghdr)) && \
92 93 ((uintptr_t)(cmsg) + (cmsg)->cmsg_len <= (uintptr_t)(end)))
93 94 #define SO_LOCK_WAKEUP_TIME 3000 /* Wakeup time in milliseconds */
94 95
95 96 dev_t sockdev; /* For fsid in getattr */
96 97 int sockfs_defer_nl7c_init = 0;
97 98
98 99 struct socklist socklist;
99 100
100 101 struct kmem_cache *socket_cache;
101 102
102 103 /*
103 104 * sockconf_lock protects the socket configuration (socket types and
104 105 * socket filters) which is changed via the sockconfig system call.
105 106 */
106 107 krwlock_t sockconf_lock;
107 108
108 109 static int sockfs_update(kstat_t *, int);
109 110 static int sockfs_snapshot(kstat_t *, void *, int);
110 111 extern smod_info_t *sotpi_smod_create(void);
111 112
112 113 extern void sendfile_init();
113 114
114 115 extern void nl7c_init(void);
115 116
116 117 extern int modrootloaded;
117 118
118 119 /*
119 120 * Translate from a device pathname (e.g. "/dev/tcp") to a vnode.
120 121 * Returns with the vnode held.
121 122 */
122 123 int
123 124 sogetvp(char *devpath, vnode_t **vpp, int uioflag)
124 125 {
125 126 struct snode *csp;
126 127 vnode_t *vp, *dvp;
127 128 major_t maj;
128 129 int error;
129 130
130 131 ASSERT(uioflag == UIO_SYSSPACE || uioflag == UIO_USERSPACE);
131 132
132 133 /*
133 134 * Lookup the underlying filesystem vnode.
134 135 */
135 136 error = lookupname(devpath, uioflag, FOLLOW, NULLVPP, &vp);
136 137 if (error)
137 138 return (error);
138 139
139 140 /* Check that it is the correct vnode */
140 141 if (vp->v_type != VCHR) {
141 142 VN_RELE(vp);
142 143 return (ENOTSOCK);
143 144 }
144 145
145 146 /*
146 147 * If devpath went through devfs, the device should already
147 148 * be configured. If devpath is a mknod file, however, we
148 149 * need to make sure the device is properly configured.
149 150 * To do this, we do something similar to spec_open()
150 151 * except that we resolve to the minor/leaf level since
151 152 * we need to return a vnode.
152 153 */
153 154 csp = VTOS(VTOS(vp)->s_commonvp);
154 155 if (!(csp->s_flag & SDIPSET)) {
155 156 char *pathname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
156 157 error = ddi_dev_pathname(vp->v_rdev, S_IFCHR, pathname);
157 158 if (error == 0)
158 159 error = devfs_lookupname(pathname, NULLVPP, &dvp);
159 160 VN_RELE(vp);
160 161 kmem_free(pathname, MAXPATHLEN);
161 162 if (error != 0)
162 163 return (ENXIO);
163 164 vp = dvp; /* use the devfs vp */
164 165 }
165 166
166 167 /* device is configured at this point */
167 168 maj = getmajor(vp->v_rdev);
168 169 if (!STREAMSTAB(maj)) {
169 170 VN_RELE(vp);
170 171 return (ENOSTR);
171 172 }
172 173
173 174 *vpp = vp;
174 175 return (0);
175 176 }
176 177
177 178 /*
178 179 * Update the accessed, updated, or changed times in an sonode
179 180 * with the current time.
180 181 *
181 182 * Note that both SunOS 4.X and 4.4BSD sockets do not present reasonable
182 183 * attributes in a fstat call. (They return the current time and 0 for
183 184 * all timestamps, respectively.) We maintain the current timestamps
184 185 * here primarily so that should sockmod be popped the resulting
185 186 * file descriptor will behave like a stream w.r.t. the timestamps.
186 187 */
187 188 void
188 189 so_update_attrs(struct sonode *so, int flag)
189 190 {
190 191 time_t now = gethrestime_sec();
191 192
192 193 if (SOCK_IS_NONSTR(so))
193 194 return;
194 195
195 196 mutex_enter(&so->so_lock);
196 197 so->so_flag |= flag;
197 198 if (flag & SOACC)
198 199 SOTOTPI(so)->sti_atime = now;
199 200 if (flag & SOMOD)
200 201 SOTOTPI(so)->sti_mtime = now;
201 202 mutex_exit(&so->so_lock);
202 203 }
203 204
204 205 extern so_create_func_t sock_comm_create_function;
205 206 extern so_destroy_func_t sock_comm_destroy_function;
206 207 /*
207 208 * Init function called when sockfs is loaded.
208 209 */
209 210 int
210 211 sockinit(int fstype, char *name)
211 212 {
212 213 static const fs_operation_def_t sock_vfsops_template[] = {
213 214 NULL, NULL
214 215 };
215 216 int error;
216 217 major_t dev;
217 218 char *err_str;
218 219
219 220 error = vfs_setfsops(fstype, sock_vfsops_template, NULL);
220 221 if (error != 0) {
221 222 zcmn_err(GLOBAL_ZONEID, CE_WARN,
222 223 "sockinit: bad vfs ops template");
223 224 return (error);
224 225 }
225 226
226 227 error = vn_make_ops(name, socket_vnodeops_template,
227 228 &socket_vnodeops);
228 229 if (error != 0) {
229 230 err_str = "sockinit: bad socket vnode ops template";
230 231 /* vn_make_ops() does not reset socktpi_vnodeops on failure. */
231 232 socket_vnodeops = NULL;
232 233 goto failure;
233 234 }
234 235
235 236 socket_cache = kmem_cache_create("socket_cache",
236 237 sizeof (struct sonode), 0, sonode_constructor,
237 238 sonode_destructor, NULL, NULL, NULL, 0);
238 239
239 240 rw_init(&sockconf_lock, NULL, RW_DEFAULT, NULL);
240 241
241 242 error = socktpi_init();
242 243 if (error != 0) {
243 244 err_str = NULL;
244 245 goto failure;
245 246 }
246 247
247 248 error = sod_init();
248 249 if (error != 0) {
249 250 err_str = NULL;
250 251 goto failure;
251 252 }
252 253
253 254 /*
254 255 * Set up the default create and destroy functions
255 256 */
256 257 sock_comm_create_function = socket_sonode_create;
257 258 sock_comm_destroy_function = socket_sonode_destroy;
258 259
259 260 /*
260 261 * Build initial list mapping socket parameters to vnode.
261 262 */
262 263 smod_init();
263 264 smod_add(sotpi_smod_create());
264 265
265 266 sockparams_init();
266 267
267 268 /*
268 269 * If sockets are needed before init runs /sbin/soconfig
269 270 * it is possible to preload the sockparams list here using
270 271 * calls like:
271 272 * sockconfig(1,2,3, "/dev/tcp", 0);
272 273 */
273 274
274 275 /*
275 276 * Create a unique dev_t for use in so_fsid.
276 277 */
277 278
278 279 if ((dev = getudev()) == (major_t)-1)
279 280 dev = 0;
280 281 sockdev = makedevice(dev, 0);
281 282
282 283 mutex_init(&socklist.sl_lock, NULL, MUTEX_DEFAULT, NULL);
283 284 sendfile_init();
284 285 if (!modrootloaded) {
285 286 sockfs_defer_nl7c_init = 1;
286 287 } else {
287 288 nl7c_init();
288 289 }
289 290
290 291 /* Initialize socket filters */
291 292 sof_init();
292 293
293 294 return (0);
294 295
295 296 failure:
296 297 (void) vfs_freevfsops_by_type(fstype);
297 298 if (socket_vnodeops != NULL)
298 299 vn_freevnodeops(socket_vnodeops);
299 300 if (err_str != NULL)
300 301 zcmn_err(GLOBAL_ZONEID, CE_WARN, err_str);
301 302 return (error);
302 303 }
303 304
304 305 /*
305 306 * Caller must hold the mutex. Used to set SOLOCKED.
306 307 */
307 308 void
308 309 so_lock_single(struct sonode *so)
309 310 {
310 311 ASSERT(MUTEX_HELD(&so->so_lock));
311 312
312 313 while (so->so_flag & (SOLOCKED | SOASYNC_UNBIND)) {
313 314 cv_wait_stop(&so->so_single_cv, &so->so_lock,
314 315 SO_LOCK_WAKEUP_TIME);
315 316 }
316 317 so->so_flag |= SOLOCKED;
317 318 }
318 319
319 320 /*
320 321 * Caller must hold the mutex and pass in SOLOCKED or SOASYNC_UNBIND.
321 322 * Used to clear SOLOCKED or SOASYNC_UNBIND.
322 323 */
323 324 void
324 325 so_unlock_single(struct sonode *so, int flag)
325 326 {
326 327 ASSERT(MUTEX_HELD(&so->so_lock));
327 328 ASSERT(flag & (SOLOCKED|SOASYNC_UNBIND));
328 329 ASSERT((flag & ~(SOLOCKED|SOASYNC_UNBIND)) == 0);
329 330 ASSERT(so->so_flag & flag);
330 331 /*
331 332 * Process the T_DISCON_IND on sti_discon_ind_mp.
332 333 *
333 334 * Call to so_drain_discon_ind will result in so_lock
334 335 * being dropped and re-acquired later.
335 336 */
336 337 if (!SOCK_IS_NONSTR(so)) {
337 338 sotpi_info_t *sti = SOTOTPI(so);
338 339
339 340 if (sti->sti_discon_ind_mp != NULL)
340 341 so_drain_discon_ind(so);
341 342 }
342 343
343 344 cv_signal(&so->so_single_cv);
344 345 so->so_flag &= ~flag;
345 346 }
346 347
347 348 /*
348 349 * Caller must hold the mutex. Used to set SOREADLOCKED.
349 350 * If the caller wants nonblocking behavior it should set fmode.
350 351 */
351 352 int
352 353 so_lock_read(struct sonode *so, int fmode)
353 354 {
354 355 ASSERT(MUTEX_HELD(&so->so_lock));
355 356
356 357 while (so->so_flag & SOREADLOCKED) {
357 358 if (fmode & (FNDELAY|FNONBLOCK))
358 359 return (EWOULDBLOCK);
359 360 cv_wait_stop(&so->so_read_cv, &so->so_lock,
360 361 SO_LOCK_WAKEUP_TIME);
361 362 }
362 363 so->so_flag |= SOREADLOCKED;
363 364 return (0);
364 365 }
365 366
366 367 /*
367 368 * Like so_lock_read above but allows signals.
368 369 */
369 370 int
370 371 so_lock_read_intr(struct sonode *so, int fmode)
371 372 {
372 373 ASSERT(MUTEX_HELD(&so->so_lock));
373 374
374 375 while (so->so_flag & SOREADLOCKED) {
375 376 if (fmode & (FNDELAY|FNONBLOCK))
376 377 return (EWOULDBLOCK);
377 378 if (!cv_wait_sig(&so->so_read_cv, &so->so_lock))
378 379 return (EINTR);
379 380 }
380 381 so->so_flag |= SOREADLOCKED;
381 382 return (0);
382 383 }
383 384
384 385 /*
385 386 * Caller must hold the mutex. Used to clear SOREADLOCKED,
386 387 * set in so_lock_read() or so_lock_read_intr().
387 388 */
388 389 void
389 390 so_unlock_read(struct sonode *so)
390 391 {
391 392 ASSERT(MUTEX_HELD(&so->so_lock));
392 393 ASSERT(so->so_flag & SOREADLOCKED);
393 394
394 395 cv_signal(&so->so_read_cv);
395 396 so->so_flag &= ~SOREADLOCKED;
396 397 }
397 398
398 399 /*
399 400 * Verify that the specified offset falls within the mblk and
400 401 * that the resulting pointer is aligned.
401 402 * Returns NULL if not.
402 403 */
403 404 void *
404 405 sogetoff(mblk_t *mp, t_uscalar_t offset,
405 406 t_uscalar_t length, uint_t align_size)
406 407 {
407 408 uintptr_t ptr1, ptr2;
408 409
409 410 ASSERT(mp && mp->b_wptr >= mp->b_rptr);
410 411 ptr1 = (uintptr_t)mp->b_rptr + offset;
411 412 ptr2 = (uintptr_t)ptr1 + length;
412 413 if (ptr1 < (uintptr_t)mp->b_rptr || ptr2 > (uintptr_t)mp->b_wptr) {
413 414 eprintline(0);
414 415 return (NULL);
415 416 }
416 417 if ((ptr1 & (align_size - 1)) != 0) {
417 418 eprintline(0);
418 419 return (NULL);
419 420 }
420 421 return ((void *)ptr1);
421 422 }
422 423
423 424 /*
424 425 * Return the AF_UNIX underlying filesystem vnode matching a given name.
425 426 * Makes sure the sending and the destination sonodes are compatible.
426 427 * The vnode is returned held.
427 428 *
428 429 * The underlying filesystem VSOCK vnode has a v_stream pointer that
429 430 * references the actual stream head (hence indirectly the actual sonode).
430 431 */
431 432 static int
432 433 so_ux_lookup(struct sonode *so, struct sockaddr_un *soun, int checkaccess,
433 434 vnode_t **vpp)
434 435 {
435 436 vnode_t *vp; /* Underlying filesystem vnode */
436 437 vnode_t *rvp; /* real vnode */
437 438 vnode_t *svp; /* sockfs vnode */
438 439 struct sonode *so2;
439 440 int error;
440 441
441 442 dprintso(so, 1, ("so_ux_lookup(%p) name <%s>\n", (void *)so,
442 443 soun->sun_path));
443 444
444 445 error = lookupname(soun->sun_path, UIO_SYSSPACE, FOLLOW, NULLVPP, &vp);
445 446 if (error) {
446 447 eprintsoline(so, error);
447 448 return (error);
448 449 }
449 450
450 451 /*
451 452 * Traverse lofs mounts get the real vnode
452 453 */
453 454 if (VOP_REALVP(vp, &rvp, NULL) == 0) {
454 455 VN_HOLD(rvp); /* hold the real vnode */
455 456 VN_RELE(vp); /* release hold from lookup */
456 457 vp = rvp;
457 458 }
458 459
459 460 if (vp->v_type != VSOCK) {
460 461 error = ENOTSOCK;
461 462 eprintsoline(so, error);
462 463 goto done2;
463 464 }
464 465
465 466 if (checkaccess) {
466 467 /*
467 468 * Check that we have permissions to access the destination
468 469 * vnode. This check is not done in BSD but it is required
469 470 * by X/Open.
470 471 */
471 472 if (error = VOP_ACCESS(vp, VREAD|VWRITE, 0, CRED(), NULL)) {
472 473 eprintsoline(so, error);
473 474 goto done2;
474 475 }
475 476 }
476 477
477 478 /*
478 479 * Check if the remote socket has been closed.
479 480 *
480 481 * Synchronize with vn_rele_stream by holding v_lock while traversing
481 482 * v_stream->sd_vnode.
482 483 */
483 484 mutex_enter(&vp->v_lock);
484 485 if (vp->v_stream == NULL) {
485 486 mutex_exit(&vp->v_lock);
486 487 if (so->so_type == SOCK_DGRAM)
487 488 error = EDESTADDRREQ;
488 489 else
489 490 error = ECONNREFUSED;
490 491
491 492 eprintsoline(so, error);
492 493 goto done2;
493 494 }
494 495 ASSERT(vp->v_stream->sd_vnode);
495 496 svp = vp->v_stream->sd_vnode;
496 497 /*
497 498 * holding v_lock on underlying filesystem vnode and acquiring
498 499 * it on sockfs vnode. Assumes that no code ever attempts to
499 500 * acquire these locks in the reverse order.
500 501 */
501 502 VN_HOLD(svp);
502 503 mutex_exit(&vp->v_lock);
503 504
504 505 if (svp->v_type != VSOCK) {
505 506 error = ENOTSOCK;
506 507 eprintsoline(so, error);
507 508 goto done;
508 509 }
509 510
510 511 so2 = VTOSO(svp);
511 512
512 513 if (so->so_type != so2->so_type) {
513 514 error = EPROTOTYPE;
514 515 eprintsoline(so, error);
515 516 goto done;
516 517 }
517 518
518 519 VN_RELE(svp);
519 520 *vpp = vp;
520 521 return (0);
521 522
522 523 done:
523 524 VN_RELE(svp);
524 525 done2:
525 526 VN_RELE(vp);
526 527 return (error);
527 528 }
528 529
529 530 /*
530 531 * Verify peer address for connect and sendto/sendmsg.
531 532 * Since sendto/sendmsg would not get synchronous errors from the transport
532 533 * provider we have to do these ugly checks in the socket layer to
533 534 * preserve compatibility with SunOS 4.X.
534 535 */
535 536 int
536 537 so_addr_verify(struct sonode *so, const struct sockaddr *name,
537 538 socklen_t namelen)
538 539 {
539 540 int family;
540 541
541 542 dprintso(so, 1, ("so_addr_verify(%p, %p, %d)\n",
542 543 (void *)so, (void *)name, namelen));
543 544
544 545 ASSERT(name != NULL);
545 546
546 547 family = so->so_family;
547 548 switch (family) {
548 549 case AF_INET:
549 550 if (name->sa_family != family) {
550 551 eprintsoline(so, EAFNOSUPPORT);
551 552 return (EAFNOSUPPORT);
552 553 }
553 554 if (namelen != (socklen_t)sizeof (struct sockaddr_in)) {
554 555 eprintsoline(so, EINVAL);
555 556 return (EINVAL);
556 557 }
557 558 break;
558 559 case AF_INET6: {
559 560 #ifdef DEBUG
560 561 struct sockaddr_in6 *sin6;
561 562 #endif /* DEBUG */
562 563
563 564 if (name->sa_family != family) {
564 565 eprintsoline(so, EAFNOSUPPORT);
565 566 return (EAFNOSUPPORT);
566 567 }
567 568 if (namelen != (socklen_t)sizeof (struct sockaddr_in6)) {
568 569 eprintsoline(so, EINVAL);
569 570 return (EINVAL);
570 571 }
571 572 #ifdef DEBUG
572 573 /* Verify that apps don't forget to clear sin6_scope_id etc */
573 574 sin6 = (struct sockaddr_in6 *)name;
574 575 if (sin6->sin6_scope_id != 0 &&
575 576 !IN6_IS_ADDR_LINKSCOPE(&sin6->sin6_addr)) {
576 577 zcmn_err(getzoneid(), CE_WARN,
577 578 "connect/send* with uninitialized sin6_scope_id "
578 579 "(%d) on socket. Pid = %d\n",
579 580 (int)sin6->sin6_scope_id, (int)curproc->p_pid);
580 581 }
581 582 #endif /* DEBUG */
582 583 break;
583 584 }
584 585 case AF_UNIX:
585 586 if (SOTOTPI(so)->sti_faddr_noxlate) {
586 587 return (0);
587 588 }
588 589 if (namelen < (socklen_t)sizeof (short)) {
589 590 eprintsoline(so, ENOENT);
590 591 return (ENOENT);
591 592 }
592 593 if (name->sa_family != family) {
593 594 eprintsoline(so, EAFNOSUPPORT);
594 595 return (EAFNOSUPPORT);
595 596 }
596 597 /* MAXPATHLEN + soun_family + nul termination */
597 598 if (namelen > (socklen_t)(MAXPATHLEN + sizeof (short) + 1)) {
598 599 eprintsoline(so, ENAMETOOLONG);
599 600 return (ENAMETOOLONG);
600 601 }
601 602
602 603 break;
603 604
604 605 default:
605 606 /*
606 607 * Default is don't do any length or sa_family check
607 608 * to allow non-sockaddr style addresses.
608 609 */
609 610 break;
610 611 }
611 612
612 613 return (0);
613 614 }
614 615
615 616
616 617 /*
617 618 * Translate an AF_UNIX sockaddr_un to the transport internal name.
618 619 * Assumes caller has called so_addr_verify first. The translated
619 620 * (internal form) address is stored in sti->sti_ux_taddr.
620 621 */
621 622 /*ARGSUSED*/
622 623 int
623 624 so_ux_addr_xlate(struct sonode *so, struct sockaddr *name,
624 625 socklen_t namelen, int checkaccess,
625 626 void **addrp, socklen_t *addrlenp)
626 627 {
627 628 int error;
628 629 struct sockaddr_un *soun;
629 630 vnode_t *vp;
630 631 void *addr;
631 632 socklen_t addrlen;
632 633 sotpi_info_t *sti = SOTOTPI(so);
633 634
634 635 dprintso(so, 1, ("so_ux_addr_xlate(%p, %p, %d, %d)\n",
635 636 (void *)so, (void *)name, namelen, checkaccess));
636 637
637 638 ASSERT(name != NULL);
638 639 ASSERT(so->so_family == AF_UNIX);
639 640 ASSERT(!sti->sti_faddr_noxlate);
640 641 ASSERT(namelen >= (socklen_t)sizeof (short));
641 642 ASSERT(name->sa_family == AF_UNIX);
642 643 soun = (struct sockaddr_un *)name;
643 644 /*
644 645 * Lookup vnode for the specified path name and verify that
645 646 * it is a socket.
646 647 */
647 648 error = so_ux_lookup(so, soun, checkaccess, &vp);
648 649 if (error) {
649 650 eprintsoline(so, error);
650 651 return (error);
651 652 }
652 653 /*
653 654 * Use the address of the peer vnode as the address to send
654 655 * to. We release the peer vnode here. In case it has been
655 656 * closed by the time the T_CONN_REQ or T_UNITDATA_REQ reaches the
656 657 * transport the message will get an error or be dropped.
657 658 * Note that that soua_vp is never dereferenced; it's just a
658 659 * convenient value by which we can identify the peer.
659 660 */
660 661 sti->sti_ux_taddr.soua_vp = vp;
661 662 sti->sti_ux_taddr.soua_magic = SOU_MAGIC_EXPLICIT;
662 663 addr = &sti->sti_ux_taddr;
663 664 addrlen = (socklen_t)sizeof (sti->sti_ux_taddr);
664 665 dprintso(so, 1, ("ux_xlate UNIX: addrlen %d, vp %p\n",
665 666 addrlen, (void *)vp));
666 667 VN_RELE(vp);
667 668 *addrp = addr;
668 669 *addrlenp = (socklen_t)addrlen;
669 670 return (0);
670 671 }
671 672
672 673 /*
673 674 * Esballoc free function for messages that contain SO_FILEP option.
674 675 * Decrement the reference count on the file pointers using closef.
675 676 */
676 677 void
677 678 fdbuf_free(struct fdbuf *fdbuf)
678 679 {
679 680 int i;
680 681 struct file *fp;
681 682
682 683 dprint(1, ("fdbuf_free: %d fds\n", fdbuf->fd_numfd));
683 684 for (i = 0; i < fdbuf->fd_numfd; i++) {
684 685 /*
685 686 * We need pointer size alignment for fd_fds. On a LP64
686 687 * kernel, the required alignment is 8 bytes while
687 688 * the option headers and values are only 4 bytes
688 689 * aligned. So its safer to do a bcopy compared to
689 690 * assigning fdbuf->fd_fds[i] to fp.
690 691 */
691 692 bcopy((char *)&fdbuf->fd_fds[i], (char *)&fp, sizeof (fp));
692 693 dprint(1, ("fdbuf_free: [%d] = %p\n", i, (void *)fp));
693 694 (void) closef(fp);
694 695 }
695 696 if (fdbuf->fd_ebuf != NULL)
696 697 kmem_free(fdbuf->fd_ebuf, fdbuf->fd_ebuflen);
697 698 kmem_free(fdbuf, fdbuf->fd_size);
698 699 }
699 700
700 701 /*
701 702 * Allocate an esballoc'ed message for AF_UNIX file descriptor passing.
702 703 * Waits if memory is not available.
703 704 */
704 705 mblk_t *
705 706 fdbuf_allocmsg(int size, struct fdbuf *fdbuf)
706 707 {
707 708 uchar_t *buf;
708 709 mblk_t *mp;
709 710
710 711 dprint(1, ("fdbuf_allocmsg: size %d, %d fds\n", size, fdbuf->fd_numfd));
711 712 buf = kmem_alloc(size, KM_SLEEP);
712 713 fdbuf->fd_ebuf = (caddr_t)buf;
713 714 fdbuf->fd_ebuflen = size;
714 715 fdbuf->fd_frtn.free_func = fdbuf_free;
715 716 fdbuf->fd_frtn.free_arg = (caddr_t)fdbuf;
716 717
717 718 mp = esballoc_wait(buf, size, BPRI_MED, &fdbuf->fd_frtn);
718 719 mp->b_datap->db_type = M_PROTO;
719 720 return (mp);
720 721 }
721 722
722 723 /*
723 724 * Extract file descriptors from a fdbuf.
724 725 * Return list in rights/rightslen.
725 726 */
726 727 /*ARGSUSED*/
727 728 static int
728 729 fdbuf_extract(struct fdbuf *fdbuf, void *rights, int rightslen)
729 730 {
730 731 int i, fd;
731 732 int *rp;
732 733 struct file *fp;
733 734 int numfd;
734 735
735 736 dprint(1, ("fdbuf_extract: %d fds, len %d\n",
736 737 fdbuf->fd_numfd, rightslen));
737 738
738 739 numfd = fdbuf->fd_numfd;
739 740 ASSERT(rightslen == numfd * (int)sizeof (int));
740 741
741 742 /*
742 743 * Allocate a file descriptor and increment the f_count.
743 744 * The latter is needed since we always call fdbuf_free
744 745 * which performs a closef.
745 746 */
746 747 rp = (int *)rights;
747 748 for (i = 0; i < numfd; i++) {
748 749 if ((fd = ufalloc(0)) == -1)
749 750 goto cleanup;
750 751 /*
751 752 * We need pointer size alignment for fd_fds. On a LP64
752 753 * kernel, the required alignment is 8 bytes while
753 754 * the option headers and values are only 4 bytes
754 755 * aligned. So its safer to do a bcopy compared to
755 756 * assigning fdbuf->fd_fds[i] to fp.
756 757 */
757 758 bcopy((char *)&fdbuf->fd_fds[i], (char *)&fp, sizeof (fp));
758 759 mutex_enter(&fp->f_tlock);
759 760 fp->f_count++;
760 761 mutex_exit(&fp->f_tlock);
761 762 setf(fd, fp);
762 763 *rp++ = fd;
763 764 if (AU_AUDITING())
764 765 audit_fdrecv(fd, fp);
765 766 dprint(1, ("fdbuf_extract: [%d] = %d, %p refcnt %d\n",
766 767 i, fd, (void *)fp, fp->f_count));
767 768 }
768 769 return (0);
769 770
770 771 cleanup:
771 772 /*
772 773 * Undo whatever partial work the loop above has done.
773 774 */
774 775 {
775 776 int j;
776 777
777 778 rp = (int *)rights;
778 779 for (j = 0; j < i; j++) {
779 780 dprint(0,
780 781 ("fdbuf_extract: cleanup[%d] = %d\n", j, *rp));
781 782 (void) closeandsetf(*rp++, NULL);
782 783 }
783 784 }
784 785
785 786 return (EMFILE);
786 787 }
787 788
788 789 /*
789 790 * Insert file descriptors into an fdbuf.
790 791 * Returns a kmem_alloc'ed fdbuf. The fdbuf should be freed
791 792 * by calling fdbuf_free().
792 793 */
793 794 int
794 795 fdbuf_create(void *rights, int rightslen, struct fdbuf **fdbufp)
795 796 {
796 797 int numfd, i;
797 798 int *fds;
798 799 struct file *fp;
799 800 struct fdbuf *fdbuf;
800 801 int fdbufsize;
801 802
802 803 dprint(1, ("fdbuf_create: len %d\n", rightslen));
803 804
804 805 numfd = rightslen / (int)sizeof (int);
805 806
806 807 fdbufsize = (int)FDBUF_HDRSIZE + (numfd * (int)sizeof (struct file *));
807 808 fdbuf = kmem_alloc(fdbufsize, KM_SLEEP);
808 809 fdbuf->fd_size = fdbufsize;
809 810 fdbuf->fd_numfd = 0;
810 811 fdbuf->fd_ebuf = NULL;
811 812 fdbuf->fd_ebuflen = 0;
812 813 fds = (int *)rights;
813 814 for (i = 0; i < numfd; i++) {
814 815 if ((fp = getf(fds[i])) == NULL) {
815 816 fdbuf_free(fdbuf);
816 817 return (EBADF);
817 818 }
818 819 dprint(1, ("fdbuf_create: [%d] = %d, %p refcnt %d\n",
819 820 i, fds[i], (void *)fp, fp->f_count));
820 821 mutex_enter(&fp->f_tlock);
821 822 fp->f_count++;
822 823 mutex_exit(&fp->f_tlock);
823 824 /*
824 825 * The maximum alignment for fdbuf (or any option header
825 826 * and its value) it 4 bytes. On a LP64 kernel, the alignment
826 827 * is not sufficient for pointers (fd_fds in this case). Since
827 828 * we just did a kmem_alloc (we get a double word alignment),
828 829 * we don't need to do anything on the send side (we loose
829 830 * the double word alignment because fdbuf goes after an
830 831 * option header (eg T_unitdata_req) which is only 4 byte
831 832 * aligned). We take care of this when we extract the file
832 833 * descriptor in fdbuf_extract or fdbuf_free.
833 834 */
834 835 fdbuf->fd_fds[i] = fp;
835 836 fdbuf->fd_numfd++;
836 837 releasef(fds[i]);
837 838 if (AU_AUDITING())
838 839 audit_fdsend(fds[i], fp, 0);
839 840 }
840 841 *fdbufp = fdbuf;
841 842 return (0);
842 843 }
843 844
844 845 static int
845 846 fdbuf_optlen(int rightslen)
846 847 {
847 848 int numfd;
848 849
849 850 numfd = rightslen / (int)sizeof (int);
850 851
851 852 return ((int)FDBUF_HDRSIZE + (numfd * (int)sizeof (struct file *)));
852 853 }
853 854
854 855 static t_uscalar_t
855 856 fdbuf_cmsglen(int fdbuflen)
856 857 {
857 858 return (t_uscalar_t)((fdbuflen - FDBUF_HDRSIZE) /
858 859 (int)sizeof (struct file *) * (int)sizeof (int));
859 860 }
860 861
861 862
862 863 /*
863 864 * Return non-zero if the mblk and fdbuf are consistent.
864 865 */
865 866 static int
866 867 fdbuf_verify(mblk_t *mp, struct fdbuf *fdbuf, int fdbuflen)
867 868 {
868 869 if (fdbuflen >= FDBUF_HDRSIZE &&
869 870 fdbuflen == fdbuf->fd_size) {
870 871 frtn_t *frp = mp->b_datap->db_frtnp;
871 872 /*
872 873 * Check that the SO_FILEP portion of the
873 874 * message has not been modified by
874 875 * the loopback transport. The sending sockfs generates
875 876 * a message that is esballoc'ed with the free function
876 877 * being fdbuf_free() and where free_arg contains the
877 878 * identical information as the SO_FILEP content.
878 879 *
879 880 * If any of these constraints are not satisfied we
880 881 * silently ignore the option.
881 882 */
882 883 ASSERT(mp);
883 884 if (frp != NULL &&
884 885 frp->free_func == fdbuf_free &&
885 886 frp->free_arg != NULL &&
886 887 bcmp(frp->free_arg, fdbuf, fdbuflen) == 0) {
887 888 dprint(1, ("fdbuf_verify: fdbuf %p len %d\n",
888 889 (void *)fdbuf, fdbuflen));
889 890 return (1);
890 891 } else {
891 892 zcmn_err(getzoneid(), CE_WARN,
892 893 "sockfs: mismatched fdbuf content (%p)",
893 894 (void *)mp);
894 895 return (0);
895 896 }
896 897 } else {
897 898 zcmn_err(getzoneid(), CE_WARN,
898 899 "sockfs: mismatched fdbuf len %d, %d\n",
899 900 fdbuflen, fdbuf->fd_size);
900 901 return (0);
901 902 }
902 903 }
903 904
904 905 /*
905 906 * When the file descriptors returned by sorecvmsg can not be passed
906 907 * to the application this routine will cleanup the references on
907 908 * the files. Start at startoff bytes into the buffer.
908 909 */
909 910 static void
910 911 close_fds(void *fdbuf, int fdbuflen, int startoff)
911 912 {
912 913 int *fds = (int *)fdbuf;
913 914 int numfd = fdbuflen / (int)sizeof (int);
914 915 int i;
915 916
916 917 dprint(1, ("close_fds(%p, %d, %d)\n", fdbuf, fdbuflen, startoff));
917 918
918 919 for (i = 0; i < numfd; i++) {
919 920 if (startoff < 0)
920 921 startoff = 0;
921 922 if (startoff < (int)sizeof (int)) {
922 923 /*
923 924 * This file descriptor is partially or fully after
924 925 * the offset
925 926 */
926 927 dprint(0,
927 928 ("close_fds: cleanup[%d] = %d\n", i, fds[i]));
928 929 (void) closeandsetf(fds[i], NULL);
929 930 }
930 931 startoff -= (int)sizeof (int);
931 932 }
932 933 }
933 934
934 935 /*
935 936 * Close all file descriptors contained in the control part starting at
936 937 * the startoffset.
937 938 */
938 939 void
939 940 so_closefds(void *control, t_uscalar_t controllen, int oldflg,
940 941 int startoff)
941 942 {
942 943 struct cmsghdr *cmsg;
943 944
944 945 if (control == NULL)
945 946 return;
946 947
947 948 if (oldflg) {
948 949 close_fds(control, controllen, startoff);
949 950 return;
950 951 }
951 952 /* Scan control part for file descriptors. */
952 953 for (cmsg = (struct cmsghdr *)control;
953 954 CMSG_VALID(cmsg, control, (uintptr_t)control + controllen);
954 955 cmsg = CMSG_NEXT(cmsg)) {
955 956 if (cmsg->cmsg_level == SOL_SOCKET &&
956 957 cmsg->cmsg_type == SCM_RIGHTS) {
957 958 close_fds(CMSG_CONTENT(cmsg),
958 959 (int)CMSG_CONTENTLEN(cmsg),
959 960 startoff - (int)sizeof (struct cmsghdr));
960 961 }
961 962 startoff -= cmsg->cmsg_len;
962 963 }
963 964 }
964 965
965 966 /*
966 967 * Returns a pointer/length for the file descriptors contained
967 968 * in the control buffer. Returns with *fdlenp == -1 if there are no
968 969 * file descriptor options present. This is different than there being
969 970 * a zero-length file descriptor option.
970 971 * Fail if there are multiple SCM_RIGHT cmsgs.
971 972 */
972 973 int
973 974 so_getfdopt(void *control, t_uscalar_t controllen, int oldflg,
974 975 void **fdsp, int *fdlenp)
975 976 {
976 977 struct cmsghdr *cmsg;
977 978 void *fds;
978 979 int fdlen;
979 980
980 981 if (control == NULL) {
981 982 *fdsp = NULL;
982 983 *fdlenp = -1;
983 984 return (0);
984 985 }
985 986
986 987 if (oldflg) {
987 988 *fdsp = control;
988 989 if (controllen == 0)
989 990 *fdlenp = -1;
990 991 else
991 992 *fdlenp = controllen;
992 993 dprint(1, ("so_getfdopt: old %d\n", *fdlenp));
993 994 return (0);
994 995 }
995 996
996 997 fds = NULL;
997 998 fdlen = 0;
998 999
999 1000 for (cmsg = (struct cmsghdr *)control;
1000 1001 CMSG_VALID(cmsg, control, (uintptr_t)control + controllen);
1001 1002 cmsg = CMSG_NEXT(cmsg)) {
1002 1003 if (cmsg->cmsg_level == SOL_SOCKET &&
1003 1004 cmsg->cmsg_type == SCM_RIGHTS) {
1004 1005 if (fds != NULL)
1005 1006 return (EINVAL);
1006 1007 fds = CMSG_CONTENT(cmsg);
1007 1008 fdlen = (int)CMSG_CONTENTLEN(cmsg);
1008 1009 dprint(1, ("so_getfdopt: new %lu\n",
1009 1010 (size_t)CMSG_CONTENTLEN(cmsg)));
1010 1011 }
1011 1012 }
1012 1013 if (fds == NULL) {
1013 1014 dprint(1, ("so_getfdopt: NONE\n"));
1014 1015 *fdlenp = -1;
1015 1016 } else
1016 1017 *fdlenp = fdlen;
1017 1018 *fdsp = fds;
1018 1019 return (0);
1019 1020 }
1020 1021
1021 1022 /*
1022 1023 * Return the length of the options including any file descriptor options.
1023 1024 */
1024 1025 t_uscalar_t
1025 1026 so_optlen(void *control, t_uscalar_t controllen, int oldflg)
1026 1027 {
1027 1028 struct cmsghdr *cmsg;
1028 1029 t_uscalar_t optlen = 0;
1029 1030 t_uscalar_t len;
1030 1031
1031 1032 if (control == NULL)
1032 1033 return (0);
1033 1034
1034 1035 if (oldflg)
1035 1036 return ((t_uscalar_t)(sizeof (struct T_opthdr) +
1036 1037 fdbuf_optlen(controllen)));
1037 1038
1038 1039 for (cmsg = (struct cmsghdr *)control;
1039 1040 CMSG_VALID(cmsg, control, (uintptr_t)control + controllen);
1040 1041 cmsg = CMSG_NEXT(cmsg)) {
1041 1042 if (cmsg->cmsg_level == SOL_SOCKET &&
1042 1043 cmsg->cmsg_type == SCM_RIGHTS) {
1043 1044 len = fdbuf_optlen((int)CMSG_CONTENTLEN(cmsg));
1044 1045 } else {
1045 1046 len = (t_uscalar_t)CMSG_CONTENTLEN(cmsg);
1046 1047 }
1047 1048 optlen += (t_uscalar_t)(_TPI_ALIGN_TOPT(len) +
1048 1049 sizeof (struct T_opthdr));
1049 1050 }
1050 1051 dprint(1, ("so_optlen: controllen %d, flg %d -> optlen %d\n",
1051 1052 controllen, oldflg, optlen));
1052 1053 return (optlen);
1053 1054 }
1054 1055
1055 1056 /*
1056 1057 * Copy options from control to the mblk. Skip any file descriptor options.
1057 1058 */
1058 1059 void
1059 1060 so_cmsg2opt(void *control, t_uscalar_t controllen, int oldflg, mblk_t *mp)
1060 1061 {
1061 1062 struct T_opthdr toh;
1062 1063 struct cmsghdr *cmsg;
1063 1064
1064 1065 if (control == NULL)
1065 1066 return;
1066 1067
1067 1068 if (oldflg) {
1068 1069 /* No real options - caller has handled file descriptors */
1069 1070 return;
1070 1071 }
1071 1072 for (cmsg = (struct cmsghdr *)control;
1072 1073 CMSG_VALID(cmsg, control, (uintptr_t)control + controllen);
1073 1074 cmsg = CMSG_NEXT(cmsg)) {
1074 1075 /*
1075 1076 * Note: The caller handles file descriptors prior
1076 1077 * to calling this function.
1077 1078 */
1078 1079 t_uscalar_t len;
1079 1080
1080 1081 if (cmsg->cmsg_level == SOL_SOCKET &&
1081 1082 cmsg->cmsg_type == SCM_RIGHTS)
1082 1083 continue;
1083 1084
1084 1085 len = (t_uscalar_t)CMSG_CONTENTLEN(cmsg);
1085 1086 toh.level = cmsg->cmsg_level;
1086 1087 toh.name = cmsg->cmsg_type;
1087 1088 toh.len = len + (t_uscalar_t)sizeof (struct T_opthdr);
1088 1089 toh.status = 0;
1089 1090
1090 1091 soappendmsg(mp, &toh, sizeof (toh));
1091 1092 soappendmsg(mp, CMSG_CONTENT(cmsg), len);
1092 1093 mp->b_wptr += _TPI_ALIGN_TOPT(len) - len;
1093 1094 ASSERT(mp->b_wptr <= mp->b_datap->db_lim);
1094 1095 }
1095 1096 }
1096 1097
1097 1098 /*
1098 1099 * Return the length of the control message derived from the options.
1099 1100 * Exclude SO_SRCADDR and SO_UNIX_CLOSE options. Include SO_FILEP.
1100 1101 * When oldflg is set only include SO_FILEP.
1101 1102 * so_opt2cmsg and so_cmsglen are inter-related since so_cmsglen
1102 1103 * allocates the space that so_opt2cmsg fills. If one changes, the other should
1103 1104 * also be checked for any possible impacts.
1104 1105 */
1105 1106 t_uscalar_t
1106 1107 so_cmsglen(mblk_t *mp, void *opt, t_uscalar_t optlen, int oldflg)
1107 1108 {
1108 1109 t_uscalar_t cmsglen = 0;
1109 1110 struct T_opthdr *tohp;
1110 1111 t_uscalar_t len;
1111 1112 t_uscalar_t last_roundup = 0;
1112 1113
1113 1114 ASSERT(__TPI_TOPT_ISALIGNED(opt));
1114 1115
1115 1116 for (tohp = (struct T_opthdr *)opt;
1116 1117 tohp && _TPI_TOPT_VALID(tohp, opt, (uintptr_t)opt + optlen);
1117 1118 tohp = _TPI_TOPT_NEXTHDR(opt, optlen, tohp)) {
1118 1119 dprint(1, ("so_cmsglen: level 0x%x, name %d, len %d\n",
1119 1120 tohp->level, tohp->name, tohp->len));
1120 1121 if (tohp->level == SOL_SOCKET &&
1121 1122 (tohp->name == SO_SRCADDR ||
1122 1123 tohp->name == SO_UNIX_CLOSE)) {
1123 1124 continue;
1124 1125 }
1125 1126 if (tohp->level == SOL_SOCKET && tohp->name == SO_FILEP) {
1126 1127 struct fdbuf *fdbuf;
1127 1128 int fdbuflen;
1128 1129
1129 1130 fdbuf = (struct fdbuf *)_TPI_TOPT_DATA(tohp);
1130 1131 fdbuflen = (int)_TPI_TOPT_DATALEN(tohp);
1131 1132
1132 1133 if (!fdbuf_verify(mp, fdbuf, fdbuflen))
1133 1134 continue;
1134 1135 if (oldflg) {
1135 1136 cmsglen += fdbuf_cmsglen(fdbuflen);
1136 1137 continue;
1137 1138 }
1138 1139 len = fdbuf_cmsglen(fdbuflen);
1139 1140 } else if (tohp->level == SOL_SOCKET &&
1140 1141 tohp->name == SCM_TIMESTAMP) {
1141 1142 if (oldflg)
1142 1143 continue;
1143 1144
1144 1145 if (get_udatamodel() == DATAMODEL_NATIVE) {
1145 1146 len = sizeof (struct timeval);
1146 1147 } else {
1147 1148 len = sizeof (struct timeval32);
1148 1149 }
1149 1150 } else {
1150 1151 if (oldflg)
1151 1152 continue;
1152 1153 len = (t_uscalar_t)_TPI_TOPT_DATALEN(tohp);
1153 1154 }
1154 1155 /*
1155 1156 * Exclude roundup for last option to not set
1156 1157 * MSG_CTRUNC when the cmsg fits but the padding doesn't fit.
1157 1158 */
1158 1159 last_roundup = (t_uscalar_t)
1159 1160 (ROUNDUP_cmsglen(len + (int)sizeof (struct cmsghdr)) -
1160 1161 (len + (int)sizeof (struct cmsghdr)));
1161 1162 cmsglen += (t_uscalar_t)(len + (int)sizeof (struct cmsghdr)) +
1162 1163 last_roundup;
1163 1164 }
1164 1165 cmsglen -= last_roundup;
1165 1166 dprint(1, ("so_cmsglen: optlen %d, flg %d -> cmsglen %d\n",
1166 1167 optlen, oldflg, cmsglen));
1167 1168 return (cmsglen);
1168 1169 }
1169 1170
1170 1171 /*
1171 1172 * Copy options from options to the control. Convert SO_FILEP to
1172 1173 * file descriptors.
1173 1174 * Returns errno or zero.
1174 1175 * so_opt2cmsg and so_cmsglen are inter-related since so_cmsglen
1175 1176 * allocates the space that so_opt2cmsg fills. If one changes, the other should
1176 1177 * also be checked for any possible impacts.
1177 1178 */
1178 1179 int
1179 1180 so_opt2cmsg(mblk_t *mp, void *opt, t_uscalar_t optlen, int oldflg,
1180 1181 void *control, t_uscalar_t controllen)
1181 1182 {
1182 1183 struct T_opthdr *tohp;
1183 1184 struct cmsghdr *cmsg;
1184 1185 struct fdbuf *fdbuf;
1185 1186 int fdbuflen;
1186 1187 int error;
1187 1188 #if defined(DEBUG) || defined(__lint)
1188 1189 struct cmsghdr *cend = (struct cmsghdr *)
1189 1190 (((uint8_t *)control) + ROUNDUP_cmsglen(controllen));
1190 1191 #endif
1191 1192 cmsg = (struct cmsghdr *)control;
1192 1193
1193 1194 ASSERT(__TPI_TOPT_ISALIGNED(opt));
1194 1195
1195 1196 for (tohp = (struct T_opthdr *)opt;
1196 1197 tohp && _TPI_TOPT_VALID(tohp, opt, (uintptr_t)opt + optlen);
1197 1198 tohp = _TPI_TOPT_NEXTHDR(opt, optlen, tohp)) {
1198 1199 dprint(1, ("so_opt2cmsg: level 0x%x, name %d, len %d\n",
1199 1200 tohp->level, tohp->name, tohp->len));
1200 1201
1201 1202 if (tohp->level == SOL_SOCKET &&
1202 1203 (tohp->name == SO_SRCADDR ||
1203 1204 tohp->name == SO_UNIX_CLOSE)) {
1204 1205 continue;
1205 1206 }
1206 1207 ASSERT((uintptr_t)cmsg <= (uintptr_t)control + controllen);
1207 1208 if (tohp->level == SOL_SOCKET && tohp->name == SO_FILEP) {
1208 1209 fdbuf = (struct fdbuf *)_TPI_TOPT_DATA(tohp);
1209 1210 fdbuflen = (int)_TPI_TOPT_DATALEN(tohp);
1210 1211
1211 1212 if (!fdbuf_verify(mp, fdbuf, fdbuflen))
1212 1213 return (EPROTO);
1213 1214 if (oldflg) {
1214 1215 error = fdbuf_extract(fdbuf, control,
1215 1216 (int)controllen);
1216 1217 if (error != 0)
1217 1218 return (error);
1218 1219 continue;
1219 1220 } else {
1220 1221 int fdlen;
1221 1222
1222 1223 fdlen = (int)fdbuf_cmsglen(
1223 1224 (int)_TPI_TOPT_DATALEN(tohp));
1224 1225
1225 1226 cmsg->cmsg_level = tohp->level;
1226 1227 cmsg->cmsg_type = SCM_RIGHTS;
1227 1228 cmsg->cmsg_len = (socklen_t)(fdlen +
1228 1229 sizeof (struct cmsghdr));
1229 1230
1230 1231 error = fdbuf_extract(fdbuf,
1231 1232 CMSG_CONTENT(cmsg), fdlen);
1232 1233 if (error != 0)
1233 1234 return (error);
1234 1235 }
1235 1236 } else if (tohp->level == SOL_SOCKET &&
1236 1237 tohp->name == SCM_TIMESTAMP) {
1237 1238 timestruc_t *timestamp;
1238 1239
1239 1240 if (oldflg)
1240 1241 continue;
1241 1242
1242 1243 cmsg->cmsg_level = tohp->level;
1243 1244 cmsg->cmsg_type = tohp->name;
1244 1245
1245 1246 timestamp =
1246 1247 (timestruc_t *)P2ROUNDUP((intptr_t)&tohp[1],
1247 1248 sizeof (intptr_t));
1248 1249
1249 1250 if (get_udatamodel() == DATAMODEL_NATIVE) {
1250 1251 struct timeval tv;
1251 1252
1252 1253 cmsg->cmsg_len = sizeof (struct timeval) +
1253 1254 sizeof (struct cmsghdr);
1254 1255 tv.tv_sec = timestamp->tv_sec;
1255 1256 tv.tv_usec = timestamp->tv_nsec /
1256 1257 (NANOSEC / MICROSEC);
1257 1258 /*
1258 1259 * on LP64 systems, the struct timeval in
1259 1260 * the destination will not be 8-byte aligned,
1260 1261 * so use bcopy to avoid alignment trouble
1261 1262 */
1262 1263 bcopy(&tv, CMSG_CONTENT(cmsg), sizeof (tv));
1263 1264 } else {
1264 1265 struct timeval32 *time32;
1265 1266
1266 1267 cmsg->cmsg_len = sizeof (struct timeval32) +
1267 1268 sizeof (struct cmsghdr);
1268 1269 time32 = (struct timeval32 *)CMSG_CONTENT(cmsg);
1269 1270 time32->tv_sec = (time32_t)timestamp->tv_sec;
1270 1271 time32->tv_usec =
1271 1272 (int32_t)(timestamp->tv_nsec /
1272 1273 (NANOSEC / MICROSEC));
1273 1274 }
1274 1275
1275 1276 } else {
1276 1277 if (oldflg)
1277 1278 continue;
1278 1279
1279 1280 cmsg->cmsg_level = tohp->level;
1280 1281 cmsg->cmsg_type = tohp->name;
1281 1282 cmsg->cmsg_len = (socklen_t)(_TPI_TOPT_DATALEN(tohp) +
1282 1283 sizeof (struct cmsghdr));
1283 1284
1284 1285 /* copy content to control data part */
1285 1286 bcopy(&tohp[1], CMSG_CONTENT(cmsg),
1286 1287 CMSG_CONTENTLEN(cmsg));
1287 1288 }
1288 1289 /* move to next CMSG structure! */
1289 1290 cmsg = CMSG_NEXT(cmsg);
1290 1291 }
1291 1292 dprint(1, ("so_opt2cmsg: buf %p len %d; cend %p; final cmsg %p\n",
1292 1293 control, controllen, (void *)cend, (void *)cmsg));
1293 1294 ASSERT(cmsg <= cend);
1294 1295 return (0);
1295 1296 }
1296 1297
1297 1298 /*
1298 1299 * Extract the SO_SRCADDR option value if present.
1299 1300 */
1300 1301 void
1301 1302 so_getopt_srcaddr(void *opt, t_uscalar_t optlen, void **srcp,
1302 1303 t_uscalar_t *srclenp)
1303 1304 {
1304 1305 struct T_opthdr *tohp;
1305 1306
1306 1307 ASSERT(__TPI_TOPT_ISALIGNED(opt));
1307 1308
1308 1309 ASSERT(srcp != NULL && srclenp != NULL);
1309 1310 *srcp = NULL;
1310 1311 *srclenp = 0;
1311 1312
1312 1313 for (tohp = (struct T_opthdr *)opt;
1313 1314 tohp && _TPI_TOPT_VALID(tohp, opt, (uintptr_t)opt + optlen);
1314 1315 tohp = _TPI_TOPT_NEXTHDR(opt, optlen, tohp)) {
1315 1316 dprint(1, ("so_getopt_srcaddr: level 0x%x, name %d, len %d\n",
1316 1317 tohp->level, tohp->name, tohp->len));
1317 1318 if (tohp->level == SOL_SOCKET &&
1318 1319 tohp->name == SO_SRCADDR) {
1319 1320 *srcp = _TPI_TOPT_DATA(tohp);
1320 1321 *srclenp = (t_uscalar_t)_TPI_TOPT_DATALEN(tohp);
1321 1322 }
1322 1323 }
1323 1324 }
1324 1325
1325 1326 /*
1326 1327 * Verify if the SO_UNIX_CLOSE option is present.
1327 1328 */
1328 1329 int
1329 1330 so_getopt_unix_close(void *opt, t_uscalar_t optlen)
1330 1331 {
1331 1332 struct T_opthdr *tohp;
1332 1333
1333 1334 ASSERT(__TPI_TOPT_ISALIGNED(opt));
1334 1335
1335 1336 for (tohp = (struct T_opthdr *)opt;
1336 1337 tohp && _TPI_TOPT_VALID(tohp, opt, (uintptr_t)opt + optlen);
1337 1338 tohp = _TPI_TOPT_NEXTHDR(opt, optlen, tohp)) {
1338 1339 dprint(1,
1339 1340 ("so_getopt_unix_close: level 0x%x, name %d, len %d\n",
1340 1341 tohp->level, tohp->name, tohp->len));
1341 1342 if (tohp->level == SOL_SOCKET &&
1342 1343 tohp->name == SO_UNIX_CLOSE)
1343 1344 return (1);
1344 1345 }
1345 1346 return (0);
1346 1347 }
1347 1348
1348 1349 /*
1349 1350 * Allocate an M_PROTO message.
1350 1351 *
1351 1352 * If allocation fails the behavior depends on sleepflg:
1352 1353 * _ALLOC_NOSLEEP fail immediately
1353 1354 * _ALLOC_INTR sleep for memory until a signal is caught
1354 1355 * _ALLOC_SLEEP sleep forever. Don't return NULL.
1355 1356 */
1356 1357 mblk_t *
1357 1358 soallocproto(size_t size, int sleepflg, cred_t *cr)
1358 1359 {
1359 1360 mblk_t *mp;
1360 1361
1361 1362 /* Round up size for reuse */
1362 1363 size = MAX(size, 64);
1363 1364 if (cr != NULL)
1364 1365 mp = allocb_cred(size, cr, curproc->p_pid);
1365 1366 else
1366 1367 mp = allocb(size, BPRI_MED);
1367 1368
1368 1369 if (mp == NULL) {
1369 1370 int error; /* Dummy - error not returned to caller */
1370 1371
1371 1372 switch (sleepflg) {
1372 1373 case _ALLOC_SLEEP:
1373 1374 if (cr != NULL) {
1374 1375 mp = allocb_cred_wait(size, STR_NOSIG, &error,
1375 1376 cr, curproc->p_pid);
1376 1377 } else {
1377 1378 mp = allocb_wait(size, BPRI_MED, STR_NOSIG,
1378 1379 &error);
1379 1380 }
1380 1381 ASSERT(mp);
1381 1382 break;
1382 1383 case _ALLOC_INTR:
1383 1384 if (cr != NULL) {
1384 1385 mp = allocb_cred_wait(size, 0, &error, cr,
1385 1386 curproc->p_pid);
1386 1387 } else {
1387 1388 mp = allocb_wait(size, BPRI_MED, 0, &error);
1388 1389 }
1389 1390 if (mp == NULL) {
1390 1391 /* Caught signal while sleeping for memory */
1391 1392 eprintline(ENOBUFS);
1392 1393 return (NULL);
1393 1394 }
1394 1395 break;
1395 1396 case _ALLOC_NOSLEEP:
1396 1397 default:
1397 1398 eprintline(ENOBUFS);
1398 1399 return (NULL);
1399 1400 }
1400 1401 }
1401 1402 DB_TYPE(mp) = M_PROTO;
1402 1403 return (mp);
1403 1404 }
1404 1405
1405 1406 /*
1406 1407 * Allocate an M_PROTO message with a single component.
1407 1408 * len is the length of buf. size is the amount to allocate.
1408 1409 *
1409 1410 * buf can be NULL with a non-zero len.
1410 1411 * This results in a bzero'ed chunk being placed the message.
1411 1412 */
1412 1413 mblk_t *
1413 1414 soallocproto1(const void *buf, ssize_t len, ssize_t size, int sleepflg,
1414 1415 cred_t *cr)
1415 1416 {
1416 1417 mblk_t *mp;
1417 1418
1418 1419 if (size == 0)
1419 1420 size = len;
1420 1421
1421 1422 ASSERT(size >= len);
1422 1423 /* Round up size for reuse */
1423 1424 size = MAX(size, 64);
1424 1425 mp = soallocproto(size, sleepflg, cr);
1425 1426 if (mp == NULL)
1426 1427 return (NULL);
1427 1428 mp->b_datap->db_type = M_PROTO;
1428 1429 if (len != 0) {
1429 1430 if (buf != NULL)
1430 1431 bcopy(buf, mp->b_wptr, len);
1431 1432 else
1432 1433 bzero(mp->b_wptr, len);
1433 1434 mp->b_wptr += len;
1434 1435 }
1435 1436 return (mp);
1436 1437 }
1437 1438
1438 1439 /*
1439 1440 * Append buf/len to mp.
1440 1441 * The caller has to ensure that there is enough room in the mblk.
1441 1442 *
1442 1443 * buf can be NULL with a non-zero len.
1443 1444 * This results in a bzero'ed chunk being placed the message.
1444 1445 */
1445 1446 void
1446 1447 soappendmsg(mblk_t *mp, const void *buf, ssize_t len)
1447 1448 {
1448 1449 ASSERT(mp);
1449 1450
1450 1451 if (len != 0) {
1451 1452 /* Assert for room left */
1452 1453 ASSERT(mp->b_datap->db_lim - mp->b_wptr >= len);
1453 1454 if (buf != NULL)
1454 1455 bcopy(buf, mp->b_wptr, len);
1455 1456 else
1456 1457 bzero(mp->b_wptr, len);
1457 1458 }
1458 1459 mp->b_wptr += len;
1459 1460 }
1460 1461
1461 1462 /*
1462 1463 * Create a message using two kernel buffers.
1463 1464 * If size is set that will determine the allocation size (e.g. for future
1464 1465 * soappendmsg calls). If size is zero it is derived from the buffer
1465 1466 * lengths.
1466 1467 */
1467 1468 mblk_t *
1468 1469 soallocproto2(const void *buf1, ssize_t len1, const void *buf2, ssize_t len2,
1469 1470 ssize_t size, int sleepflg, cred_t *cr)
1470 1471 {
1471 1472 mblk_t *mp;
1472 1473
1473 1474 if (size == 0)
1474 1475 size = len1 + len2;
1475 1476 ASSERT(size >= len1 + len2);
1476 1477
1477 1478 mp = soallocproto1(buf1, len1, size, sleepflg, cr);
1478 1479 if (mp)
1479 1480 soappendmsg(mp, buf2, len2);
1480 1481 return (mp);
1481 1482 }
1482 1483
1483 1484 /*
1484 1485 * Create a message using three kernel buffers.
1485 1486 * If size is set that will determine the allocation size (for future
1486 1487 * soappendmsg calls). If size is zero it is derived from the buffer
1487 1488 * lengths.
1488 1489 */
1489 1490 mblk_t *
1490 1491 soallocproto3(const void *buf1, ssize_t len1, const void *buf2, ssize_t len2,
1491 1492 const void *buf3, ssize_t len3, ssize_t size, int sleepflg, cred_t *cr)
1492 1493 {
1493 1494 mblk_t *mp;
1494 1495
1495 1496 if (size == 0)
1496 1497 size = len1 + len2 +len3;
1497 1498 ASSERT(size >= len1 + len2 + len3);
1498 1499
1499 1500 mp = soallocproto1(buf1, len1, size, sleepflg, cr);
1500 1501 if (mp != NULL) {
1501 1502 soappendmsg(mp, buf2, len2);
1502 1503 soappendmsg(mp, buf3, len3);
1503 1504 }
1504 1505 return (mp);
1505 1506 }
1506 1507
1507 1508 #ifdef DEBUG
1508 1509 char *
1509 1510 pr_state(uint_t state, uint_t mode)
1510 1511 {
1511 1512 static char buf[1024];
1512 1513
1513 1514 buf[0] = 0;
1514 1515 if (state & SS_ISCONNECTED)
1515 1516 (void) strcat(buf, "ISCONNECTED ");
1516 1517 if (state & SS_ISCONNECTING)
1517 1518 (void) strcat(buf, "ISCONNECTING ");
1518 1519 if (state & SS_ISDISCONNECTING)
1519 1520 (void) strcat(buf, "ISDISCONNECTING ");
1520 1521 if (state & SS_CANTSENDMORE)
1521 1522 (void) strcat(buf, "CANTSENDMORE ");
1522 1523
1523 1524 if (state & SS_CANTRCVMORE)
1524 1525 (void) strcat(buf, "CANTRCVMORE ");
1525 1526 if (state & SS_ISBOUND)
1526 1527 (void) strcat(buf, "ISBOUND ");
1527 1528 if (state & SS_NDELAY)
1528 1529 (void) strcat(buf, "NDELAY ");
1529 1530 if (state & SS_NONBLOCK)
1530 1531 (void) strcat(buf, "NONBLOCK ");
1531 1532
1532 1533 if (state & SS_ASYNC)
1533 1534 (void) strcat(buf, "ASYNC ");
1534 1535 if (state & SS_ACCEPTCONN)
1535 1536 (void) strcat(buf, "ACCEPTCONN ");
1536 1537 if (state & SS_SAVEDEOR)
1537 1538 (void) strcat(buf, "SAVEDEOR ");
1538 1539
1539 1540 if (state & SS_RCVATMARK)
1540 1541 (void) strcat(buf, "RCVATMARK ");
1541 1542 if (state & SS_OOBPEND)
1542 1543 (void) strcat(buf, "OOBPEND ");
1543 1544 if (state & SS_HAVEOOBDATA)
1544 1545 (void) strcat(buf, "HAVEOOBDATA ");
1545 1546 if (state & SS_HADOOBDATA)
1546 1547 (void) strcat(buf, "HADOOBDATA ");
1547 1548
1548 1549 if (mode & SM_PRIV)
1549 1550 (void) strcat(buf, "PRIV ");
1550 1551 if (mode & SM_ATOMIC)
1551 1552 (void) strcat(buf, "ATOMIC ");
1552 1553 if (mode & SM_ADDR)
1553 1554 (void) strcat(buf, "ADDR ");
1554 1555 if (mode & SM_CONNREQUIRED)
1555 1556 (void) strcat(buf, "CONNREQUIRED ");
1556 1557
1557 1558 if (mode & SM_FDPASSING)
1558 1559 (void) strcat(buf, "FDPASSING ");
1559 1560 if (mode & SM_EXDATA)
1560 1561 (void) strcat(buf, "EXDATA ");
1561 1562 if (mode & SM_OPTDATA)
1562 1563 (void) strcat(buf, "OPTDATA ");
1563 1564 if (mode & SM_BYTESTREAM)
1564 1565 (void) strcat(buf, "BYTESTREAM ");
1565 1566 return (buf);
1566 1567 }
1567 1568
1568 1569 char *
1569 1570 pr_addr(int family, struct sockaddr *addr, t_uscalar_t addrlen)
1570 1571 {
1571 1572 static char buf[1024];
1572 1573
1573 1574 if (addr == NULL || addrlen == 0) {
1574 1575 (void) sprintf(buf, "(len %d) %p", addrlen, (void *)addr);
1575 1576 return (buf);
1576 1577 }
1577 1578 switch (family) {
1578 1579 case AF_INET: {
1579 1580 struct sockaddr_in sin;
1580 1581
1581 1582 bcopy(addr, &sin, sizeof (sin));
1582 1583
1583 1584 (void) sprintf(buf, "(len %d) %x/%d",
1584 1585 addrlen, ntohl(sin.sin_addr.s_addr), ntohs(sin.sin_port));
1585 1586 break;
1586 1587 }
1587 1588 case AF_INET6: {
1588 1589 struct sockaddr_in6 sin6;
1589 1590 uint16_t *piece = (uint16_t *)&sin6.sin6_addr;
1590 1591
1591 1592 bcopy((char *)addr, (char *)&sin6, sizeof (sin6));
1592 1593 (void) sprintf(buf, "(len %d) %x:%x:%x:%x:%x:%x:%x:%x/%d",
1593 1594 addrlen,
1594 1595 ntohs(piece[0]), ntohs(piece[1]),
1595 1596 ntohs(piece[2]), ntohs(piece[3]),
1596 1597 ntohs(piece[4]), ntohs(piece[5]),
1597 1598 ntohs(piece[6]), ntohs(piece[7]),
1598 1599 ntohs(sin6.sin6_port));
1599 1600 break;
1600 1601 }
1601 1602 case AF_UNIX: {
1602 1603 struct sockaddr_un *soun = (struct sockaddr_un *)addr;
1603 1604
1604 1605 (void) sprintf(buf, "(len %d) %s", addrlen,
1605 1606 (soun == NULL) ? "(none)" : soun->sun_path);
1606 1607 break;
1607 1608 }
1608 1609 default:
1609 1610 (void) sprintf(buf, "(unknown af %d)", family);
1610 1611 break;
1611 1612 }
1612 1613 return (buf);
1613 1614 }
1614 1615
1615 1616 /* The logical equivalence operator (a if-and-only-if b) */
1616 1617 #define EQUIVALENT(a, b) (((a) && (b)) || (!(a) && (!(b))))
1617 1618
1618 1619 /*
1619 1620 * Verify limitations and invariants on oob state.
1620 1621 * Return 1 if OK, otherwise 0 so that it can be used as
1621 1622 * ASSERT(verify_oobstate(so));
1622 1623 */
1623 1624 int
1624 1625 so_verify_oobstate(struct sonode *so)
1625 1626 {
1626 1627 boolean_t havemark;
1627 1628
1628 1629 ASSERT(MUTEX_HELD(&so->so_lock));
1629 1630
1630 1631 /*
1631 1632 * The possible state combinations are:
1632 1633 * 0
1633 1634 * SS_OOBPEND
1634 1635 * SS_OOBPEND|SS_HAVEOOBDATA
1635 1636 * SS_OOBPEND|SS_HADOOBDATA
1636 1637 * SS_HADOOBDATA
1637 1638 */
1638 1639 switch (so->so_state & (SS_OOBPEND|SS_HAVEOOBDATA|SS_HADOOBDATA)) {
1639 1640 case 0:
1640 1641 case SS_OOBPEND:
1641 1642 case SS_OOBPEND|SS_HAVEOOBDATA:
1642 1643 case SS_OOBPEND|SS_HADOOBDATA:
1643 1644 case SS_HADOOBDATA:
1644 1645 break;
1645 1646 default:
1646 1647 printf("Bad oob state 1 (%p): state %s\n",
1647 1648 (void *)so, pr_state(so->so_state, so->so_mode));
1648 1649 return (0);
1649 1650 }
1650 1651
1651 1652 /* SS_RCVATMARK should only be set when SS_OOBPEND is set */
1652 1653 if ((so->so_state & (SS_RCVATMARK|SS_OOBPEND)) == SS_RCVATMARK) {
1653 1654 printf("Bad oob state 2 (%p): state %s\n",
1654 1655 (void *)so, pr_state(so->so_state, so->so_mode));
1655 1656 return (0);
1656 1657 }
1657 1658
1658 1659 /*
1659 1660 * (havemark != 0 or SS_RCVATMARK) iff SS_OOBPEND
1660 1661 * For TPI, the presence of a "mark" is indicated by sti_oobsigcnt.
1661 1662 */
1662 1663 havemark = (SOCK_IS_NONSTR(so)) ? so->so_oobmark > 0 :
1663 1664 SOTOTPI(so)->sti_oobsigcnt > 0;
1664 1665
1665 1666 if (!EQUIVALENT(havemark || (so->so_state & SS_RCVATMARK),
1666 1667 so->so_state & SS_OOBPEND)) {
1667 1668 printf("Bad oob state 3 (%p): state %s\n",
1668 1669 (void *)so, pr_state(so->so_state, so->so_mode));
1669 1670 return (0);
1670 1671 }
1671 1672
1672 1673 /*
1673 1674 * Unless SO_OOBINLINE we have so_oobmsg != NULL iff SS_HAVEOOBDATA
1674 1675 */
1675 1676 if (!(so->so_options & SO_OOBINLINE) &&
1676 1677 !EQUIVALENT(so->so_oobmsg != NULL, so->so_state & SS_HAVEOOBDATA)) {
1677 1678 printf("Bad oob state 4 (%p): state %s\n",
1678 1679 (void *)so, pr_state(so->so_state, so->so_mode));
1679 1680 return (0);
1680 1681 }
1681 1682
1682 1683 if (!SOCK_IS_NONSTR(so) &&
1683 1684 SOTOTPI(so)->sti_oobsigcnt < SOTOTPI(so)->sti_oobcnt) {
1684 1685 printf("Bad oob state 5 (%p): counts %d/%d state %s\n",
1685 1686 (void *)so, SOTOTPI(so)->sti_oobsigcnt,
1686 1687 SOTOTPI(so)->sti_oobcnt,
1687 1688 pr_state(so->so_state, so->so_mode));
1688 1689 return (0);
1689 1690 }
1690 1691
1691 1692 return (1);
1692 1693 }
1693 1694 #undef EQUIVALENT
1694 1695 #endif /* DEBUG */
1695 1696
1696 1697 /* initialize sockfs zone specific kstat related items */
1697 1698 void *
1698 1699 sock_kstat_init(zoneid_t zoneid)
1699 1700 {
1700 1701 kstat_t *ksp;
1701 1702
1702 1703 ksp = kstat_create_zone("sockfs", 0, "sock_unix_list", "misc",
1703 1704 KSTAT_TYPE_RAW, 0, KSTAT_FLAG_VAR_SIZE|KSTAT_FLAG_VIRTUAL, zoneid);
1704 1705
1705 1706 if (ksp != NULL) {
1706 1707 ksp->ks_update = sockfs_update;
1707 1708 ksp->ks_snapshot = sockfs_snapshot;
1708 1709 ksp->ks_lock = &socklist.sl_lock;
1709 1710 ksp->ks_private = (void *)(uintptr_t)zoneid;
1710 1711 kstat_install(ksp);
1711 1712 }
1712 1713
1713 1714 return (ksp);
1714 1715 }
1715 1716
1716 1717 /* tear down sockfs zone specific kstat related items */
1717 1718 /*ARGSUSED*/
1718 1719 void
1719 1720 sock_kstat_fini(zoneid_t zoneid, void *arg)
1720 1721 {
1721 1722 kstat_t *ksp = (kstat_t *)arg;
1722 1723
1723 1724 if (ksp != NULL) {
1724 1725 ASSERT(zoneid == (zoneid_t)(uintptr_t)ksp->ks_private);
1725 1726 kstat_delete(ksp);
1726 1727 }
1727 1728 }
1728 1729
1729 1730 /*
1730 1731 * Zones:
1731 1732 * Note that nactive is going to be different for each zone.
1732 1733 * This means we require kstat to call sockfs_update and then sockfs_snapshot
1733 1734 * for the same zone, or sockfs_snapshot will be taken into the wrong size
1734 1735 * buffer. This is safe, but if the buffer is too small, user will not be
1735 1736 * given details of all sockets. However, as this kstat has a ks_lock, kstat
1736 1737 * driver will keep it locked between the update and the snapshot, so no
1737 1738 * other process (zone) can currently get inbetween resulting in a wrong size
1738 1739 * buffer allocation.
1739 1740 */
1740 1741 static int
1741 1742 sockfs_update(kstat_t *ksp, int rw)
1742 1743 {
1743 1744 uint_t nactive = 0; /* # of active AF_UNIX sockets */
1744 1745 struct sonode *so; /* current sonode on socklist */
1745 1746 zoneid_t myzoneid = (zoneid_t)(uintptr_t)ksp->ks_private;
1746 1747
1747 1748 ASSERT((zoneid_t)(uintptr_t)ksp->ks_private == getzoneid());
1748 1749
1749 1750 if (rw == KSTAT_WRITE) { /* bounce all writes */
1750 1751 return (EACCES);
1751 1752 }
1752 1753
1753 1754 for (so = socklist.sl_list; so != NULL; so = SOTOTPI(so)->sti_next_so) {
1754 1755 if (so->so_count != 0 && so->so_zoneid == myzoneid) {
1755 1756 nactive++;
1756 1757 }
1757 1758 }
1758 1759 ksp->ks_ndata = nactive;
1759 1760 ksp->ks_data_size = nactive * sizeof (struct sockinfo);
1760 1761
1761 1762 return (0);
1762 1763 }
1763 1764
1764 1765 static int
1765 1766 sockfs_snapshot(kstat_t *ksp, void *buf, int rw)
1766 1767 {
1767 1768 int ns; /* # of sonodes we've copied */
1768 1769 struct sonode *so; /* current sonode on socklist */
1769 1770 struct sockinfo *psi; /* where we put sockinfo data */
1770 1771 t_uscalar_t sn_len; /* soa_len */
1771 1772 zoneid_t myzoneid = (zoneid_t)(uintptr_t)ksp->ks_private;
1772 1773 sotpi_info_t *sti;
1773 1774
1774 1775 ASSERT((zoneid_t)(uintptr_t)ksp->ks_private == getzoneid());
1775 1776
1776 1777 ksp->ks_snaptime = gethrtime();
1777 1778
1778 1779 if (rw == KSTAT_WRITE) { /* bounce all writes */
1779 1780 return (EACCES);
1780 1781 }
1781 1782
1782 1783 /*
1783 1784 * For each sonode on the socklist, we massage the important
1784 1785 * info into buf, in sockinfo format.
1785 1786 */
1786 1787 psi = (struct sockinfo *)buf;
1787 1788 ns = 0;
1788 1789 for (so = socklist.sl_list; so != NULL; so = SOTOTPI(so)->sti_next_so) {
1789 1790 vattr_t attr;
1790 1791
1791 1792 /* only stuff active sonodes and the same zone: */
1792 1793 if (so->so_count == 0 || so->so_zoneid != myzoneid) {
1793 1794 continue;
1794 1795 }
1795 1796
1796 1797 /*
1797 1798 * If the sonode was activated between the update and the
1798 1799 * snapshot, we're done - as this is only a snapshot.
1799 1800 */
1800 1801 if ((caddr_t)(psi) >= (caddr_t)buf + ksp->ks_data_size) {
1801 1802 break;
1802 1803 }
1803 1804
1804 1805 sti = SOTOTPI(so);
1805 1806 /* copy important info into buf: */
1806 1807 psi->si_size = sizeof (struct sockinfo);
1807 1808 psi->si_family = so->so_family;
1808 1809 psi->si_type = so->so_type;
1809 1810 psi->si_flag = so->so_flag;
1810 1811 psi->si_state = so->so_state;
1811 1812 psi->si_serv_type = sti->sti_serv_type;
1812 1813 psi->si_ux_laddr_sou_magic = sti->sti_ux_laddr.soua_magic;
1813 1814 psi->si_ux_faddr_sou_magic = sti->sti_ux_faddr.soua_magic;
1814 1815 psi->si_laddr_soa_len = sti->sti_laddr.soa_len;
1815 1816 psi->si_faddr_soa_len = sti->sti_faddr.soa_len;
1816 1817 psi->si_szoneid = so->so_zoneid;
1817 1818 psi->si_faddr_noxlate = sti->sti_faddr_noxlate;
1818 1819
1819 1820 /*
1820 1821 * Grab the inode, if possible.
1821 1822 * This must be done before entering so_lock as VOP_GETATTR
1822 1823 * will acquire it.
1823 1824 */
1824 1825 if (so->so_vnode == NULL ||
1825 1826 VOP_GETATTR(so->so_vnode, &attr, 0, CRED(), NULL) != 0)
1826 1827 attr.va_nodeid = 0;
1827 1828
1828 1829 psi->si_inode = attr.va_nodeid;
1829 1830
1830 1831 mutex_enter(&so->so_lock);
1831 1832
1832 1833 if (sti->sti_laddr_sa != NULL) {
1833 1834 ASSERT(sti->sti_laddr_sa->sa_data != NULL);
1834 1835 sn_len = sti->sti_laddr_len;
1835 1836 ASSERT(sn_len <= sizeof (short) +
1836 1837 sizeof (psi->si_laddr_sun_path));
1837 1838
1838 1839 psi->si_laddr_family =
1839 1840 sti->sti_laddr_sa->sa_family;
1840 1841 if (sn_len != 0) {
1841 1842 /* AF_UNIX socket names are NULL terminated */
1842 1843 (void) strncpy(psi->si_laddr_sun_path,
1843 1844 sti->sti_laddr_sa->sa_data,
1844 1845 sizeof (psi->si_laddr_sun_path));
1845 1846 sn_len = strlen(psi->si_laddr_sun_path);
1846 1847 }
1847 1848 psi->si_laddr_sun_path[sn_len] = 0;
1848 1849 }
1849 1850
1850 1851 if (sti->sti_faddr_sa != NULL) {
1851 1852 ASSERT(sti->sti_faddr_sa->sa_data != NULL);
1852 1853 sn_len = sti->sti_faddr_len;
1853 1854 ASSERT(sn_len <= sizeof (short) +
1854 1855 sizeof (psi->si_faddr_sun_path));
1855 1856
1856 1857 psi->si_faddr_family =
1857 1858 sti->sti_faddr_sa->sa_family;
1858 1859 if (sn_len != 0) {
1859 1860 (void) strncpy(psi->si_faddr_sun_path,
1860 1861 sti->sti_faddr_sa->sa_data,
1861 1862 sizeof (psi->si_faddr_sun_path));
1862 1863 sn_len = strlen(psi->si_faddr_sun_path);
1863 1864 }
1864 1865 psi->si_faddr_sun_path[sn_len] = 0;
1865 1866 }
1866 1867
1867 1868 mutex_exit(&so->so_lock);
1868 1869
1869 1870 (void) snprintf(psi->si_son_straddr,
1870 1871 sizeof (psi->si_son_straddr), "%p", (void *)so);
1871 1872 (void) snprintf(psi->si_lvn_straddr,
1872 1873 sizeof (psi->si_lvn_straddr), "%p",
1873 1874 (void *)sti->sti_ux_laddr.soua_vp);
1874 1875 (void) snprintf(psi->si_fvn_straddr,
1875 1876 sizeof (psi->si_fvn_straddr), "%p",
1876 1877 (void *)sti->sti_ux_faddr.soua_vp);
1877 1878
1878 1879 ns++;
1879 1880 psi++;
↓ open down ↓ |
1844 lines elided |
↑ open up ↑ |
1880 1881 }
1881 1882
1882 1883 ksp->ks_ndata = ns;
1883 1884 return (0);
1884 1885 }
1885 1886
1886 1887 ssize_t
1887 1888 soreadfile(file_t *fp, uchar_t *buf, u_offset_t fileoff, int *err, size_t size)
1888 1889 {
1889 1890 struct uio auio;
1890 - struct iovec aiov[MSG_MAXIOVLEN];
1891 + struct iovec aiov[1];
1891 1892 register vnode_t *vp;
1892 1893 int ioflag, rwflag;
1893 1894 ssize_t cnt;
1894 1895 int error = 0;
1895 1896 int iovcnt = 0;
1896 1897 short fflag;
1897 1898
1898 1899 vp = fp->f_vnode;
1899 1900 fflag = fp->f_flag;
1900 1901
1901 1902 rwflag = 0;
1902 1903 aiov[0].iov_base = (caddr_t)buf;
1903 1904 aiov[0].iov_len = size;
1904 1905 iovcnt = 1;
1905 1906 cnt = (ssize_t)size;
1906 1907 (void) VOP_RWLOCK(vp, rwflag, NULL);
1907 1908
1908 1909 auio.uio_loffset = fileoff;
1909 1910 auio.uio_iov = aiov;
1910 1911 auio.uio_iovcnt = iovcnt;
1911 1912 auio.uio_resid = cnt;
1912 1913 auio.uio_segflg = UIO_SYSSPACE;
1913 1914 auio.uio_llimit = MAXOFFSET_T;
1914 1915 auio.uio_fmode = fflag;
1915 1916 auio.uio_extflg = UIO_COPY_CACHED;
1916 1917
1917 1918 ioflag = auio.uio_fmode & (FAPPEND|FSYNC|FDSYNC|FRSYNC);
1918 1919
1919 1920 /* If read sync is not asked for, filter sync flags */
1920 1921 if ((ioflag & FRSYNC) == 0)
1921 1922 ioflag &= ~(FSYNC|FDSYNC);
1922 1923 error = VOP_READ(vp, &auio, ioflag, fp->f_cred, NULL);
1923 1924 cnt -= auio.uio_resid;
1924 1925
1925 1926 VOP_RWUNLOCK(vp, rwflag, NULL);
1926 1927
1927 1928 if (error == EINTR && cnt != 0)
1928 1929 error = 0;
1929 1930 out:
1930 1931 if (error != 0) {
1931 1932 *err = error;
1932 1933 return (0);
1933 1934 } else {
1934 1935 *err = 0;
1935 1936 return (cnt);
1936 1937 }
1937 1938 }
1938 1939
1939 1940 int
1940 1941 so_copyin(const void *from, void *to, size_t size, int fromkernel)
1941 1942 {
1942 1943 if (fromkernel) {
1943 1944 bcopy(from, to, size);
1944 1945 return (0);
1945 1946 }
1946 1947 return (xcopyin(from, to, size));
1947 1948 }
1948 1949
1949 1950 int
1950 1951 so_copyout(const void *from, void *to, size_t size, int tokernel)
1951 1952 {
1952 1953 if (tokernel) {
1953 1954 bcopy(from, to, size);
1954 1955 return (0);
1955 1956 }
1956 1957 return (xcopyout(from, to, size));
1957 1958 }
↓ open down ↓ |
57 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX