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