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