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
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 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 ↓ |
622 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 + /* add curproc to the pid list associated with that file */
759 + if (fp->f_vnode != NULL)
760 + (void) VOP_IOCTL(fp->f_vnode, F_FORKED,
761 + (intptr_t)curproc, FKIOCTL, kcred, NULL, NULL);
762 +
769 763 if (AU_AUDITING())
770 764 audit_fdrecv(fd, fp);
771 765 dprint(1, ("fdbuf_extract: [%d] = %d, %p refcnt %d\n",
772 766 i, fd, (void *)fp, fp->f_count));
773 767 }
774 768 return (0);
775 769
776 770 cleanup:
777 771 /*
778 772 * Undo whatever partial work the loop above has done.
779 773 */
780 774 {
781 775 int j;
782 776
783 777 rp = (int *)rights;
784 778 for (j = 0; j < i; j++) {
785 779 dprint(0,
786 780 ("fdbuf_extract: cleanup[%d] = %d\n", j, *rp));
787 781 (void) closeandsetf(*rp++, NULL);
788 782 }
789 783 }
790 784
791 785 return (EMFILE);
792 786 }
793 787
794 788 /*
795 789 * Insert file descriptors into an fdbuf.
796 790 * Returns a kmem_alloc'ed fdbuf. The fdbuf should be freed
797 791 * by calling fdbuf_free().
798 792 */
799 793 int
800 794 fdbuf_create(void *rights, int rightslen, struct fdbuf **fdbufp)
801 795 {
802 796 int numfd, i;
803 797 int *fds;
804 798 struct file *fp;
805 799 struct fdbuf *fdbuf;
806 800 int fdbufsize;
807 801
808 802 dprint(1, ("fdbuf_create: len %d\n", rightslen));
809 803
810 804 numfd = rightslen / (int)sizeof (int);
811 805
812 806 fdbufsize = (int)FDBUF_HDRSIZE + (numfd * (int)sizeof (struct file *));
813 807 fdbuf = kmem_alloc(fdbufsize, KM_SLEEP);
814 808 fdbuf->fd_size = fdbufsize;
815 809 fdbuf->fd_numfd = 0;
816 810 fdbuf->fd_ebuf = NULL;
817 811 fdbuf->fd_ebuflen = 0;
818 812 fds = (int *)rights;
819 813 for (i = 0; i < numfd; i++) {
820 814 if ((fp = getf(fds[i])) == NULL) {
821 815 fdbuf_free(fdbuf);
822 816 return (EBADF);
823 817 }
824 818 dprint(1, ("fdbuf_create: [%d] = %d, %p refcnt %d\n",
825 819 i, fds[i], (void *)fp, fp->f_count));
826 820 mutex_enter(&fp->f_tlock);
827 821 fp->f_count++;
828 822 mutex_exit(&fp->f_tlock);
829 823 /*
830 824 * The maximum alignment for fdbuf (or any option header
831 825 * and its value) it 4 bytes. On a LP64 kernel, the alignment
832 826 * is not sufficient for pointers (fd_fds in this case). Since
833 827 * we just did a kmem_alloc (we get a double word alignment),
834 828 * we don't need to do anything on the send side (we loose
835 829 * the double word alignment because fdbuf goes after an
836 830 * option header (eg T_unitdata_req) which is only 4 byte
837 831 * aligned). We take care of this when we extract the file
838 832 * descriptor in fdbuf_extract or fdbuf_free.
839 833 */
840 834 fdbuf->fd_fds[i] = fp;
841 835 fdbuf->fd_numfd++;
842 836 releasef(fds[i]);
843 837 if (AU_AUDITING())
844 838 audit_fdsend(fds[i], fp, 0);
845 839 }
846 840 *fdbufp = fdbuf;
847 841 return (0);
848 842 }
849 843
850 844 static int
851 845 fdbuf_optlen(int rightslen)
852 846 {
853 847 int numfd;
854 848
855 849 numfd = rightslen / (int)sizeof (int);
856 850
857 851 return ((int)FDBUF_HDRSIZE + (numfd * (int)sizeof (struct file *)));
858 852 }
859 853
860 854 static t_uscalar_t
861 855 fdbuf_cmsglen(int fdbuflen)
862 856 {
863 857 return (t_uscalar_t)((fdbuflen - FDBUF_HDRSIZE) /
864 858 (int)sizeof (struct file *) * (int)sizeof (int));
865 859 }
866 860
867 861
868 862 /*
869 863 * Return non-zero if the mblk and fdbuf are consistent.
870 864 */
871 865 static int
872 866 fdbuf_verify(mblk_t *mp, struct fdbuf *fdbuf, int fdbuflen)
873 867 {
874 868 if (fdbuflen >= FDBUF_HDRSIZE &&
875 869 fdbuflen == fdbuf->fd_size) {
876 870 frtn_t *frp = mp->b_datap->db_frtnp;
877 871 /*
878 872 * Check that the SO_FILEP portion of the
879 873 * message has not been modified by
880 874 * the loopback transport. The sending sockfs generates
881 875 * a message that is esballoc'ed with the free function
882 876 * being fdbuf_free() and where free_arg contains the
883 877 * identical information as the SO_FILEP content.
884 878 *
885 879 * If any of these constraints are not satisfied we
886 880 * silently ignore the option.
887 881 */
888 882 ASSERT(mp);
889 883 if (frp != NULL &&
890 884 frp->free_func == fdbuf_free &&
891 885 frp->free_arg != NULL &&
892 886 bcmp(frp->free_arg, fdbuf, fdbuflen) == 0) {
893 887 dprint(1, ("fdbuf_verify: fdbuf %p len %d\n",
894 888 (void *)fdbuf, fdbuflen));
895 889 return (1);
896 890 } else {
897 891 zcmn_err(getzoneid(), CE_WARN,
898 892 "sockfs: mismatched fdbuf content (%p)",
899 893 (void *)mp);
900 894 return (0);
901 895 }
902 896 } else {
903 897 zcmn_err(getzoneid(), CE_WARN,
904 898 "sockfs: mismatched fdbuf len %d, %d\n",
905 899 fdbuflen, fdbuf->fd_size);
906 900 return (0);
907 901 }
908 902 }
909 903
910 904 /*
911 905 * When the file descriptors returned by sorecvmsg can not be passed
912 906 * to the application this routine will cleanup the references on
913 907 * the files. Start at startoff bytes into the buffer.
914 908 */
915 909 static void
916 910 close_fds(void *fdbuf, int fdbuflen, int startoff)
917 911 {
918 912 int *fds = (int *)fdbuf;
919 913 int numfd = fdbuflen / (int)sizeof (int);
920 914 int i;
921 915
922 916 dprint(1, ("close_fds(%p, %d, %d)\n", fdbuf, fdbuflen, startoff));
923 917
924 918 for (i = 0; i < numfd; i++) {
925 919 if (startoff < 0)
926 920 startoff = 0;
927 921 if (startoff < (int)sizeof (int)) {
928 922 /*
929 923 * This file descriptor is partially or fully after
930 924 * the offset
931 925 */
932 926 dprint(0,
933 927 ("close_fds: cleanup[%d] = %d\n", i, fds[i]));
934 928 (void) closeandsetf(fds[i], NULL);
935 929 }
936 930 startoff -= (int)sizeof (int);
937 931 }
938 932 }
939 933
940 934 /*
941 935 * Close all file descriptors contained in the control part starting at
942 936 * the startoffset.
943 937 */
944 938 void
945 939 so_closefds(void *control, t_uscalar_t controllen, int oldflg,
946 940 int startoff)
947 941 {
948 942 struct cmsghdr *cmsg;
949 943
950 944 if (control == NULL)
951 945 return;
952 946
953 947 if (oldflg) {
954 948 close_fds(control, controllen, startoff);
955 949 return;
956 950 }
957 951 /* Scan control part for file descriptors. */
958 952 for (cmsg = (struct cmsghdr *)control;
959 953 CMSG_VALID(cmsg, control, (uintptr_t)control + controllen);
960 954 cmsg = CMSG_NEXT(cmsg)) {
961 955 if (cmsg->cmsg_level == SOL_SOCKET &&
962 956 cmsg->cmsg_type == SCM_RIGHTS) {
963 957 close_fds(CMSG_CONTENT(cmsg),
964 958 (int)CMSG_CONTENTLEN(cmsg),
965 959 startoff - (int)sizeof (struct cmsghdr));
966 960 }
967 961 startoff -= cmsg->cmsg_len;
968 962 }
969 963 }
970 964
971 965 /*
972 966 * Returns a pointer/length for the file descriptors contained
973 967 * in the control buffer. Returns with *fdlenp == -1 if there are no
974 968 * file descriptor options present. This is different than there being
975 969 * a zero-length file descriptor option.
976 970 * Fail if there are multiple SCM_RIGHT cmsgs.
977 971 */
978 972 int
979 973 so_getfdopt(void *control, t_uscalar_t controllen, int oldflg,
980 974 void **fdsp, int *fdlenp)
981 975 {
982 976 struct cmsghdr *cmsg;
983 977 void *fds;
984 978 int fdlen;
985 979
986 980 if (control == NULL) {
987 981 *fdsp = NULL;
988 982 *fdlenp = -1;
989 983 return (0);
990 984 }
991 985
992 986 if (oldflg) {
993 987 *fdsp = control;
994 988 if (controllen == 0)
995 989 *fdlenp = -1;
996 990 else
997 991 *fdlenp = controllen;
998 992 dprint(1, ("so_getfdopt: old %d\n", *fdlenp));
999 993 return (0);
1000 994 }
1001 995
1002 996 fds = NULL;
1003 997 fdlen = 0;
1004 998
1005 999 for (cmsg = (struct cmsghdr *)control;
1006 1000 CMSG_VALID(cmsg, control, (uintptr_t)control + controllen);
1007 1001 cmsg = CMSG_NEXT(cmsg)) {
1008 1002 if (cmsg->cmsg_level == SOL_SOCKET &&
1009 1003 cmsg->cmsg_type == SCM_RIGHTS) {
1010 1004 if (fds != NULL)
1011 1005 return (EINVAL);
1012 1006 fds = CMSG_CONTENT(cmsg);
1013 1007 fdlen = (int)CMSG_CONTENTLEN(cmsg);
1014 1008 dprint(1, ("so_getfdopt: new %lu\n",
1015 1009 (size_t)CMSG_CONTENTLEN(cmsg)));
1016 1010 }
1017 1011 }
1018 1012 if (fds == NULL) {
1019 1013 dprint(1, ("so_getfdopt: NONE\n"));
1020 1014 *fdlenp = -1;
1021 1015 } else
1022 1016 *fdlenp = fdlen;
1023 1017 *fdsp = fds;
1024 1018 return (0);
1025 1019 }
1026 1020
1027 1021 /*
1028 1022 * Return the length of the options including any file descriptor options.
1029 1023 */
1030 1024 t_uscalar_t
1031 1025 so_optlen(void *control, t_uscalar_t controllen, int oldflg)
1032 1026 {
1033 1027 struct cmsghdr *cmsg;
1034 1028 t_uscalar_t optlen = 0;
1035 1029 t_uscalar_t len;
1036 1030
1037 1031 if (control == NULL)
1038 1032 return (0);
1039 1033
1040 1034 if (oldflg)
1041 1035 return ((t_uscalar_t)(sizeof (struct T_opthdr) +
1042 1036 fdbuf_optlen(controllen)));
1043 1037
1044 1038 for (cmsg = (struct cmsghdr *)control;
1045 1039 CMSG_VALID(cmsg, control, (uintptr_t)control + controllen);
1046 1040 cmsg = CMSG_NEXT(cmsg)) {
1047 1041 if (cmsg->cmsg_level == SOL_SOCKET &&
1048 1042 cmsg->cmsg_type == SCM_RIGHTS) {
1049 1043 len = fdbuf_optlen((int)CMSG_CONTENTLEN(cmsg));
1050 1044 } else {
1051 1045 len = (t_uscalar_t)CMSG_CONTENTLEN(cmsg);
1052 1046 }
1053 1047 optlen += (t_uscalar_t)(_TPI_ALIGN_TOPT(len) +
1054 1048 sizeof (struct T_opthdr));
1055 1049 }
1056 1050 dprint(1, ("so_optlen: controllen %d, flg %d -> optlen %d\n",
1057 1051 controllen, oldflg, optlen));
1058 1052 return (optlen);
1059 1053 }
1060 1054
1061 1055 /*
1062 1056 * Copy options from control to the mblk. Skip any file descriptor options.
1063 1057 */
1064 1058 void
1065 1059 so_cmsg2opt(void *control, t_uscalar_t controllen, int oldflg, mblk_t *mp)
1066 1060 {
1067 1061 struct T_opthdr toh;
1068 1062 struct cmsghdr *cmsg;
1069 1063
1070 1064 if (control == NULL)
1071 1065 return;
1072 1066
1073 1067 if (oldflg) {
1074 1068 /* No real options - caller has handled file descriptors */
1075 1069 return;
1076 1070 }
1077 1071 for (cmsg = (struct cmsghdr *)control;
1078 1072 CMSG_VALID(cmsg, control, (uintptr_t)control + controllen);
1079 1073 cmsg = CMSG_NEXT(cmsg)) {
1080 1074 /*
1081 1075 * Note: The caller handles file descriptors prior
1082 1076 * to calling this function.
1083 1077 */
1084 1078 t_uscalar_t len;
1085 1079
1086 1080 if (cmsg->cmsg_level == SOL_SOCKET &&
1087 1081 cmsg->cmsg_type == SCM_RIGHTS)
1088 1082 continue;
1089 1083
1090 1084 len = (t_uscalar_t)CMSG_CONTENTLEN(cmsg);
1091 1085 toh.level = cmsg->cmsg_level;
1092 1086 toh.name = cmsg->cmsg_type;
1093 1087 toh.len = len + (t_uscalar_t)sizeof (struct T_opthdr);
1094 1088 toh.status = 0;
1095 1089
1096 1090 soappendmsg(mp, &toh, sizeof (toh));
1097 1091 soappendmsg(mp, CMSG_CONTENT(cmsg), len);
1098 1092 mp->b_wptr += _TPI_ALIGN_TOPT(len) - len;
1099 1093 ASSERT(mp->b_wptr <= mp->b_datap->db_lim);
1100 1094 }
1101 1095 }
1102 1096
1103 1097 /*
1104 1098 * Return the length of the control message derived from the options.
1105 1099 * Exclude SO_SRCADDR and SO_UNIX_CLOSE options. Include SO_FILEP.
1106 1100 * When oldflg is set only include SO_FILEP.
1107 1101 * so_opt2cmsg and so_cmsglen are inter-related since so_cmsglen
1108 1102 * allocates the space that so_opt2cmsg fills. If one changes, the other should
1109 1103 * also be checked for any possible impacts.
1110 1104 */
1111 1105 t_uscalar_t
1112 1106 so_cmsglen(mblk_t *mp, void *opt, t_uscalar_t optlen, int oldflg)
1113 1107 {
1114 1108 t_uscalar_t cmsglen = 0;
1115 1109 struct T_opthdr *tohp;
1116 1110 t_uscalar_t len;
1117 1111 t_uscalar_t last_roundup = 0;
1118 1112
1119 1113 ASSERT(__TPI_TOPT_ISALIGNED(opt));
1120 1114
1121 1115 for (tohp = (struct T_opthdr *)opt;
1122 1116 tohp && _TPI_TOPT_VALID(tohp, opt, (uintptr_t)opt + optlen);
1123 1117 tohp = _TPI_TOPT_NEXTHDR(opt, optlen, tohp)) {
1124 1118 dprint(1, ("so_cmsglen: level 0x%x, name %d, len %d\n",
1125 1119 tohp->level, tohp->name, tohp->len));
1126 1120 if (tohp->level == SOL_SOCKET &&
1127 1121 (tohp->name == SO_SRCADDR ||
1128 1122 tohp->name == SO_UNIX_CLOSE)) {
1129 1123 continue;
1130 1124 }
1131 1125 if (tohp->level == SOL_SOCKET && tohp->name == SO_FILEP) {
1132 1126 struct fdbuf *fdbuf;
1133 1127 int fdbuflen;
1134 1128
1135 1129 fdbuf = (struct fdbuf *)_TPI_TOPT_DATA(tohp);
1136 1130 fdbuflen = (int)_TPI_TOPT_DATALEN(tohp);
1137 1131
1138 1132 if (!fdbuf_verify(mp, fdbuf, fdbuflen))
1139 1133 continue;
1140 1134 if (oldflg) {
1141 1135 cmsglen += fdbuf_cmsglen(fdbuflen);
1142 1136 continue;
1143 1137 }
1144 1138 len = fdbuf_cmsglen(fdbuflen);
1145 1139 } else if (tohp->level == SOL_SOCKET &&
1146 1140 tohp->name == SCM_TIMESTAMP) {
1147 1141 if (oldflg)
1148 1142 continue;
1149 1143
1150 1144 if (get_udatamodel() == DATAMODEL_NATIVE) {
1151 1145 len = sizeof (struct timeval);
1152 1146 } else {
1153 1147 len = sizeof (struct timeval32);
1154 1148 }
1155 1149 } else {
1156 1150 if (oldflg)
1157 1151 continue;
1158 1152 len = (t_uscalar_t)_TPI_TOPT_DATALEN(tohp);
1159 1153 }
1160 1154 /*
1161 1155 * Exclude roundup for last option to not set
1162 1156 * MSG_CTRUNC when the cmsg fits but the padding doesn't fit.
1163 1157 */
1164 1158 last_roundup = (t_uscalar_t)
1165 1159 (ROUNDUP_cmsglen(len + (int)sizeof (struct cmsghdr)) -
1166 1160 (len + (int)sizeof (struct cmsghdr)));
1167 1161 cmsglen += (t_uscalar_t)(len + (int)sizeof (struct cmsghdr)) +
1168 1162 last_roundup;
1169 1163 }
1170 1164 cmsglen -= last_roundup;
1171 1165 dprint(1, ("so_cmsglen: optlen %d, flg %d -> cmsglen %d\n",
1172 1166 optlen, oldflg, cmsglen));
1173 1167 return (cmsglen);
1174 1168 }
1175 1169
1176 1170 /*
1177 1171 * Copy options from options to the control. Convert SO_FILEP to
1178 1172 * file descriptors.
1179 1173 * Returns errno or zero.
1180 1174 * so_opt2cmsg and so_cmsglen are inter-related since so_cmsglen
1181 1175 * allocates the space that so_opt2cmsg fills. If one changes, the other should
1182 1176 * also be checked for any possible impacts.
1183 1177 */
1184 1178 int
1185 1179 so_opt2cmsg(mblk_t *mp, void *opt, t_uscalar_t optlen, int oldflg,
1186 1180 void *control, t_uscalar_t controllen)
1187 1181 {
1188 1182 struct T_opthdr *tohp;
1189 1183 struct cmsghdr *cmsg;
1190 1184 struct fdbuf *fdbuf;
1191 1185 int fdbuflen;
1192 1186 int error;
1193 1187 #if defined(DEBUG) || defined(__lint)
1194 1188 struct cmsghdr *cend = (struct cmsghdr *)
1195 1189 (((uint8_t *)control) + ROUNDUP_cmsglen(controllen));
1196 1190 #endif
1197 1191 cmsg = (struct cmsghdr *)control;
1198 1192
1199 1193 ASSERT(__TPI_TOPT_ISALIGNED(opt));
1200 1194
1201 1195 for (tohp = (struct T_opthdr *)opt;
1202 1196 tohp && _TPI_TOPT_VALID(tohp, opt, (uintptr_t)opt + optlen);
1203 1197 tohp = _TPI_TOPT_NEXTHDR(opt, optlen, tohp)) {
1204 1198 dprint(1, ("so_opt2cmsg: level 0x%x, name %d, len %d\n",
1205 1199 tohp->level, tohp->name, tohp->len));
1206 1200
1207 1201 if (tohp->level == SOL_SOCKET &&
1208 1202 (tohp->name == SO_SRCADDR ||
1209 1203 tohp->name == SO_UNIX_CLOSE)) {
1210 1204 continue;
1211 1205 }
1212 1206 ASSERT((uintptr_t)cmsg <= (uintptr_t)control + controllen);
1213 1207 if (tohp->level == SOL_SOCKET && tohp->name == SO_FILEP) {
1214 1208 fdbuf = (struct fdbuf *)_TPI_TOPT_DATA(tohp);
1215 1209 fdbuflen = (int)_TPI_TOPT_DATALEN(tohp);
1216 1210
1217 1211 if (!fdbuf_verify(mp, fdbuf, fdbuflen))
1218 1212 return (EPROTO);
1219 1213 if (oldflg) {
1220 1214 error = fdbuf_extract(fdbuf, control,
1221 1215 (int)controllen);
1222 1216 if (error != 0)
1223 1217 return (error);
1224 1218 continue;
1225 1219 } else {
1226 1220 int fdlen;
1227 1221
1228 1222 fdlen = (int)fdbuf_cmsglen(
1229 1223 (int)_TPI_TOPT_DATALEN(tohp));
1230 1224
1231 1225 cmsg->cmsg_level = tohp->level;
1232 1226 cmsg->cmsg_type = SCM_RIGHTS;
1233 1227 cmsg->cmsg_len = (socklen_t)(fdlen +
1234 1228 sizeof (struct cmsghdr));
1235 1229
1236 1230 error = fdbuf_extract(fdbuf,
1237 1231 CMSG_CONTENT(cmsg), fdlen);
1238 1232 if (error != 0)
1239 1233 return (error);
1240 1234 }
1241 1235 } else if (tohp->level == SOL_SOCKET &&
1242 1236 tohp->name == SCM_TIMESTAMP) {
1243 1237 timestruc_t *timestamp;
1244 1238
1245 1239 if (oldflg)
1246 1240 continue;
1247 1241
1248 1242 cmsg->cmsg_level = tohp->level;
1249 1243 cmsg->cmsg_type = tohp->name;
1250 1244
1251 1245 timestamp =
1252 1246 (timestruc_t *)P2ROUNDUP((intptr_t)&tohp[1],
1253 1247 sizeof (intptr_t));
1254 1248
1255 1249 if (get_udatamodel() == DATAMODEL_NATIVE) {
1256 1250 struct timeval tv;
1257 1251
1258 1252 cmsg->cmsg_len = sizeof (struct timeval) +
1259 1253 sizeof (struct cmsghdr);
1260 1254 tv.tv_sec = timestamp->tv_sec;
1261 1255 tv.tv_usec = timestamp->tv_nsec /
1262 1256 (NANOSEC / MICROSEC);
1263 1257 /*
1264 1258 * on LP64 systems, the struct timeval in
1265 1259 * the destination will not be 8-byte aligned,
1266 1260 * so use bcopy to avoid alignment trouble
1267 1261 */
1268 1262 bcopy(&tv, CMSG_CONTENT(cmsg), sizeof (tv));
1269 1263 } else {
1270 1264 struct timeval32 *time32;
1271 1265
1272 1266 cmsg->cmsg_len = sizeof (struct timeval32) +
1273 1267 sizeof (struct cmsghdr);
1274 1268 time32 = (struct timeval32 *)CMSG_CONTENT(cmsg);
1275 1269 time32->tv_sec = (time32_t)timestamp->tv_sec;
1276 1270 time32->tv_usec =
1277 1271 (int32_t)(timestamp->tv_nsec /
1278 1272 (NANOSEC / MICROSEC));
1279 1273 }
1280 1274
1281 1275 } else {
1282 1276 if (oldflg)
1283 1277 continue;
1284 1278
1285 1279 cmsg->cmsg_level = tohp->level;
1286 1280 cmsg->cmsg_type = tohp->name;
1287 1281 cmsg->cmsg_len = (socklen_t)(_TPI_TOPT_DATALEN(tohp) +
1288 1282 sizeof (struct cmsghdr));
1289 1283
1290 1284 /* copy content to control data part */
1291 1285 bcopy(&tohp[1], CMSG_CONTENT(cmsg),
1292 1286 CMSG_CONTENTLEN(cmsg));
1293 1287 }
1294 1288 /* move to next CMSG structure! */
1295 1289 cmsg = CMSG_NEXT(cmsg);
1296 1290 }
1297 1291 dprint(1, ("so_opt2cmsg: buf %p len %d; cend %p; final cmsg %p\n",
1298 1292 control, controllen, (void *)cend, (void *)cmsg));
1299 1293 ASSERT(cmsg <= cend);
1300 1294 return (0);
1301 1295 }
1302 1296
1303 1297 /*
1304 1298 * Extract the SO_SRCADDR option value if present.
1305 1299 */
1306 1300 void
1307 1301 so_getopt_srcaddr(void *opt, t_uscalar_t optlen, void **srcp,
1308 1302 t_uscalar_t *srclenp)
1309 1303 {
1310 1304 struct T_opthdr *tohp;
1311 1305
1312 1306 ASSERT(__TPI_TOPT_ISALIGNED(opt));
1313 1307
1314 1308 ASSERT(srcp != NULL && srclenp != NULL);
1315 1309 *srcp = NULL;
1316 1310 *srclenp = 0;
1317 1311
1318 1312 for (tohp = (struct T_opthdr *)opt;
1319 1313 tohp && _TPI_TOPT_VALID(tohp, opt, (uintptr_t)opt + optlen);
1320 1314 tohp = _TPI_TOPT_NEXTHDR(opt, optlen, tohp)) {
1321 1315 dprint(1, ("so_getopt_srcaddr: level 0x%x, name %d, len %d\n",
1322 1316 tohp->level, tohp->name, tohp->len));
1323 1317 if (tohp->level == SOL_SOCKET &&
1324 1318 tohp->name == SO_SRCADDR) {
1325 1319 *srcp = _TPI_TOPT_DATA(tohp);
1326 1320 *srclenp = (t_uscalar_t)_TPI_TOPT_DATALEN(tohp);
1327 1321 }
1328 1322 }
1329 1323 }
1330 1324
1331 1325 /*
1332 1326 * Verify if the SO_UNIX_CLOSE option is present.
1333 1327 */
1334 1328 int
1335 1329 so_getopt_unix_close(void *opt, t_uscalar_t optlen)
1336 1330 {
1337 1331 struct T_opthdr *tohp;
1338 1332
1339 1333 ASSERT(__TPI_TOPT_ISALIGNED(opt));
1340 1334
1341 1335 for (tohp = (struct T_opthdr *)opt;
1342 1336 tohp && _TPI_TOPT_VALID(tohp, opt, (uintptr_t)opt + optlen);
1343 1337 tohp = _TPI_TOPT_NEXTHDR(opt, optlen, tohp)) {
1344 1338 dprint(1,
1345 1339 ("so_getopt_unix_close: level 0x%x, name %d, len %d\n",
1346 1340 tohp->level, tohp->name, tohp->len));
1347 1341 if (tohp->level == SOL_SOCKET &&
1348 1342 tohp->name == SO_UNIX_CLOSE)
1349 1343 return (1);
1350 1344 }
1351 1345 return (0);
1352 1346 }
1353 1347
1354 1348 /*
1355 1349 * Allocate an M_PROTO message.
1356 1350 *
1357 1351 * If allocation fails the behavior depends on sleepflg:
1358 1352 * _ALLOC_NOSLEEP fail immediately
1359 1353 * _ALLOC_INTR sleep for memory until a signal is caught
1360 1354 * _ALLOC_SLEEP sleep forever. Don't return NULL.
1361 1355 */
1362 1356 mblk_t *
1363 1357 soallocproto(size_t size, int sleepflg, cred_t *cr)
1364 1358 {
1365 1359 mblk_t *mp;
1366 1360
1367 1361 /* Round up size for reuse */
1368 1362 size = MAX(size, 64);
1369 1363 if (cr != NULL)
1370 1364 mp = allocb_cred(size, cr, curproc->p_pid);
1371 1365 else
1372 1366 mp = allocb(size, BPRI_MED);
1373 1367
1374 1368 if (mp == NULL) {
1375 1369 int error; /* Dummy - error not returned to caller */
1376 1370
1377 1371 switch (sleepflg) {
1378 1372 case _ALLOC_SLEEP:
1379 1373 if (cr != NULL) {
1380 1374 mp = allocb_cred_wait(size, STR_NOSIG, &error,
1381 1375 cr, curproc->p_pid);
1382 1376 } else {
1383 1377 mp = allocb_wait(size, BPRI_MED, STR_NOSIG,
1384 1378 &error);
1385 1379 }
1386 1380 ASSERT(mp);
1387 1381 break;
1388 1382 case _ALLOC_INTR:
1389 1383 if (cr != NULL) {
1390 1384 mp = allocb_cred_wait(size, 0, &error, cr,
1391 1385 curproc->p_pid);
1392 1386 } else {
1393 1387 mp = allocb_wait(size, BPRI_MED, 0, &error);
1394 1388 }
1395 1389 if (mp == NULL) {
1396 1390 /* Caught signal while sleeping for memory */
1397 1391 eprintline(ENOBUFS);
1398 1392 return (NULL);
1399 1393 }
1400 1394 break;
1401 1395 case _ALLOC_NOSLEEP:
1402 1396 default:
1403 1397 eprintline(ENOBUFS);
1404 1398 return (NULL);
1405 1399 }
1406 1400 }
1407 1401 DB_TYPE(mp) = M_PROTO;
1408 1402 return (mp);
1409 1403 }
1410 1404
1411 1405 /*
1412 1406 * Allocate an M_PROTO message with a single component.
1413 1407 * len is the length of buf. size is the amount to allocate.
1414 1408 *
1415 1409 * buf can be NULL with a non-zero len.
1416 1410 * This results in a bzero'ed chunk being placed the message.
1417 1411 */
1418 1412 mblk_t *
1419 1413 soallocproto1(const void *buf, ssize_t len, ssize_t size, int sleepflg,
1420 1414 cred_t *cr)
1421 1415 {
1422 1416 mblk_t *mp;
1423 1417
1424 1418 if (size == 0)
1425 1419 size = len;
1426 1420
1427 1421 ASSERT(size >= len);
1428 1422 /* Round up size for reuse */
1429 1423 size = MAX(size, 64);
1430 1424 mp = soallocproto(size, sleepflg, cr);
1431 1425 if (mp == NULL)
1432 1426 return (NULL);
1433 1427 mp->b_datap->db_type = M_PROTO;
1434 1428 if (len != 0) {
1435 1429 if (buf != NULL)
1436 1430 bcopy(buf, mp->b_wptr, len);
1437 1431 else
1438 1432 bzero(mp->b_wptr, len);
1439 1433 mp->b_wptr += len;
1440 1434 }
1441 1435 return (mp);
1442 1436 }
1443 1437
1444 1438 /*
1445 1439 * Append buf/len to mp.
1446 1440 * The caller has to ensure that there is enough room in the mblk.
1447 1441 *
1448 1442 * buf can be NULL with a non-zero len.
1449 1443 * This results in a bzero'ed chunk being placed the message.
1450 1444 */
1451 1445 void
1452 1446 soappendmsg(mblk_t *mp, const void *buf, ssize_t len)
1453 1447 {
1454 1448 ASSERT(mp);
1455 1449
1456 1450 if (len != 0) {
1457 1451 /* Assert for room left */
1458 1452 ASSERT(mp->b_datap->db_lim - mp->b_wptr >= len);
1459 1453 if (buf != NULL)
1460 1454 bcopy(buf, mp->b_wptr, len);
1461 1455 else
1462 1456 bzero(mp->b_wptr, len);
1463 1457 }
1464 1458 mp->b_wptr += len;
1465 1459 }
1466 1460
1467 1461 /*
1468 1462 * Create a message using two kernel buffers.
1469 1463 * If size is set that will determine the allocation size (e.g. for future
1470 1464 * soappendmsg calls). If size is zero it is derived from the buffer
1471 1465 * lengths.
1472 1466 */
1473 1467 mblk_t *
1474 1468 soallocproto2(const void *buf1, ssize_t len1, const void *buf2, ssize_t len2,
1475 1469 ssize_t size, int sleepflg, cred_t *cr)
1476 1470 {
1477 1471 mblk_t *mp;
1478 1472
1479 1473 if (size == 0)
1480 1474 size = len1 + len2;
1481 1475 ASSERT(size >= len1 + len2);
1482 1476
1483 1477 mp = soallocproto1(buf1, len1, size, sleepflg, cr);
1484 1478 if (mp)
1485 1479 soappendmsg(mp, buf2, len2);
1486 1480 return (mp);
1487 1481 }
1488 1482
1489 1483 /*
1490 1484 * Create a message using three kernel buffers.
1491 1485 * If size is set that will determine the allocation size (for future
1492 1486 * soappendmsg calls). If size is zero it is derived from the buffer
1493 1487 * lengths.
1494 1488 */
1495 1489 mblk_t *
1496 1490 soallocproto3(const void *buf1, ssize_t len1, const void *buf2, ssize_t len2,
1497 1491 const void *buf3, ssize_t len3, ssize_t size, int sleepflg, cred_t *cr)
1498 1492 {
1499 1493 mblk_t *mp;
1500 1494
1501 1495 if (size == 0)
1502 1496 size = len1 + len2 +len3;
1503 1497 ASSERT(size >= len1 + len2 + len3);
1504 1498
1505 1499 mp = soallocproto1(buf1, len1, size, sleepflg, cr);
1506 1500 if (mp != NULL) {
1507 1501 soappendmsg(mp, buf2, len2);
1508 1502 soappendmsg(mp, buf3, len3);
1509 1503 }
1510 1504 return (mp);
1511 1505 }
1512 1506
1513 1507 #ifdef DEBUG
1514 1508 char *
1515 1509 pr_state(uint_t state, uint_t mode)
1516 1510 {
1517 1511 static char buf[1024];
1518 1512
1519 1513 buf[0] = 0;
1520 1514 if (state & SS_ISCONNECTED)
1521 1515 (void) strcat(buf, "ISCONNECTED ");
1522 1516 if (state & SS_ISCONNECTING)
1523 1517 (void) strcat(buf, "ISCONNECTING ");
1524 1518 if (state & SS_ISDISCONNECTING)
1525 1519 (void) strcat(buf, "ISDISCONNECTING ");
1526 1520 if (state & SS_CANTSENDMORE)
1527 1521 (void) strcat(buf, "CANTSENDMORE ");
1528 1522
1529 1523 if (state & SS_CANTRCVMORE)
1530 1524 (void) strcat(buf, "CANTRCVMORE ");
1531 1525 if (state & SS_ISBOUND)
1532 1526 (void) strcat(buf, "ISBOUND ");
1533 1527 if (state & SS_NDELAY)
1534 1528 (void) strcat(buf, "NDELAY ");
1535 1529 if (state & SS_NONBLOCK)
1536 1530 (void) strcat(buf, "NONBLOCK ");
1537 1531
1538 1532 if (state & SS_ASYNC)
1539 1533 (void) strcat(buf, "ASYNC ");
1540 1534 if (state & SS_ACCEPTCONN)
1541 1535 (void) strcat(buf, "ACCEPTCONN ");
1542 1536 if (state & SS_SAVEDEOR)
1543 1537 (void) strcat(buf, "SAVEDEOR ");
1544 1538
1545 1539 if (state & SS_RCVATMARK)
1546 1540 (void) strcat(buf, "RCVATMARK ");
1547 1541 if (state & SS_OOBPEND)
1548 1542 (void) strcat(buf, "OOBPEND ");
1549 1543 if (state & SS_HAVEOOBDATA)
1550 1544 (void) strcat(buf, "HAVEOOBDATA ");
1551 1545 if (state & SS_HADOOBDATA)
1552 1546 (void) strcat(buf, "HADOOBDATA ");
1553 1547
1554 1548 if (mode & SM_PRIV)
1555 1549 (void) strcat(buf, "PRIV ");
1556 1550 if (mode & SM_ATOMIC)
1557 1551 (void) strcat(buf, "ATOMIC ");
1558 1552 if (mode & SM_ADDR)
1559 1553 (void) strcat(buf, "ADDR ");
1560 1554 if (mode & SM_CONNREQUIRED)
1561 1555 (void) strcat(buf, "CONNREQUIRED ");
1562 1556
1563 1557 if (mode & SM_FDPASSING)
1564 1558 (void) strcat(buf, "FDPASSING ");
1565 1559 if (mode & SM_EXDATA)
1566 1560 (void) strcat(buf, "EXDATA ");
1567 1561 if (mode & SM_OPTDATA)
1568 1562 (void) strcat(buf, "OPTDATA ");
1569 1563 if (mode & SM_BYTESTREAM)
1570 1564 (void) strcat(buf, "BYTESTREAM ");
1571 1565 return (buf);
1572 1566 }
1573 1567
1574 1568 char *
1575 1569 pr_addr(int family, struct sockaddr *addr, t_uscalar_t addrlen)
1576 1570 {
1577 1571 static char buf[1024];
1578 1572
1579 1573 if (addr == NULL || addrlen == 0) {
1580 1574 (void) sprintf(buf, "(len %d) %p", addrlen, (void *)addr);
1581 1575 return (buf);
1582 1576 }
1583 1577 switch (family) {
1584 1578 case AF_INET: {
1585 1579 struct sockaddr_in sin;
1586 1580
1587 1581 bcopy(addr, &sin, sizeof (sin));
1588 1582
1589 1583 (void) sprintf(buf, "(len %d) %x/%d",
1590 1584 addrlen, ntohl(sin.sin_addr.s_addr), ntohs(sin.sin_port));
1591 1585 break;
1592 1586 }
1593 1587 case AF_INET6: {
1594 1588 struct sockaddr_in6 sin6;
1595 1589 uint16_t *piece = (uint16_t *)&sin6.sin6_addr;
1596 1590
1597 1591 bcopy((char *)addr, (char *)&sin6, sizeof (sin6));
1598 1592 (void) sprintf(buf, "(len %d) %x:%x:%x:%x:%x:%x:%x:%x/%d",
1599 1593 addrlen,
1600 1594 ntohs(piece[0]), ntohs(piece[1]),
1601 1595 ntohs(piece[2]), ntohs(piece[3]),
1602 1596 ntohs(piece[4]), ntohs(piece[5]),
1603 1597 ntohs(piece[6]), ntohs(piece[7]),
1604 1598 ntohs(sin6.sin6_port));
1605 1599 break;
1606 1600 }
1607 1601 case AF_UNIX: {
1608 1602 struct sockaddr_un *soun = (struct sockaddr_un *)addr;
1609 1603
1610 1604 (void) sprintf(buf, "(len %d) %s", addrlen,
1611 1605 (soun == NULL) ? "(none)" : soun->sun_path);
1612 1606 break;
1613 1607 }
1614 1608 default:
1615 1609 (void) sprintf(buf, "(unknown af %d)", family);
1616 1610 break;
1617 1611 }
1618 1612 return (buf);
1619 1613 }
1620 1614
1621 1615 /* The logical equivalence operator (a if-and-only-if b) */
1622 1616 #define EQUIVALENT(a, b) (((a) && (b)) || (!(a) && (!(b))))
1623 1617
1624 1618 /*
1625 1619 * Verify limitations and invariants on oob state.
1626 1620 * Return 1 if OK, otherwise 0 so that it can be used as
1627 1621 * ASSERT(verify_oobstate(so));
1628 1622 */
1629 1623 int
1630 1624 so_verify_oobstate(struct sonode *so)
1631 1625 {
1632 1626 boolean_t havemark;
1633 1627
1634 1628 ASSERT(MUTEX_HELD(&so->so_lock));
1635 1629
1636 1630 /*
1637 1631 * The possible state combinations are:
1638 1632 * 0
1639 1633 * SS_OOBPEND
1640 1634 * SS_OOBPEND|SS_HAVEOOBDATA
1641 1635 * SS_OOBPEND|SS_HADOOBDATA
1642 1636 * SS_HADOOBDATA
1643 1637 */
1644 1638 switch (so->so_state & (SS_OOBPEND|SS_HAVEOOBDATA|SS_HADOOBDATA)) {
1645 1639 case 0:
1646 1640 case SS_OOBPEND:
1647 1641 case SS_OOBPEND|SS_HAVEOOBDATA:
1648 1642 case SS_OOBPEND|SS_HADOOBDATA:
1649 1643 case SS_HADOOBDATA:
1650 1644 break;
1651 1645 default:
1652 1646 printf("Bad oob state 1 (%p): state %s\n",
1653 1647 (void *)so, pr_state(so->so_state, so->so_mode));
1654 1648 return (0);
1655 1649 }
1656 1650
1657 1651 /* SS_RCVATMARK should only be set when SS_OOBPEND is set */
1658 1652 if ((so->so_state & (SS_RCVATMARK|SS_OOBPEND)) == SS_RCVATMARK) {
1659 1653 printf("Bad oob state 2 (%p): state %s\n",
1660 1654 (void *)so, pr_state(so->so_state, so->so_mode));
1661 1655 return (0);
1662 1656 }
1663 1657
1664 1658 /*
1665 1659 * (havemark != 0 or SS_RCVATMARK) iff SS_OOBPEND
1666 1660 * For TPI, the presence of a "mark" is indicated by sti_oobsigcnt.
1667 1661 */
1668 1662 havemark = (SOCK_IS_NONSTR(so)) ? so->so_oobmark > 0 :
1669 1663 SOTOTPI(so)->sti_oobsigcnt > 0;
1670 1664
1671 1665 if (!EQUIVALENT(havemark || (so->so_state & SS_RCVATMARK),
1672 1666 so->so_state & SS_OOBPEND)) {
1673 1667 printf("Bad oob state 3 (%p): state %s\n",
1674 1668 (void *)so, pr_state(so->so_state, so->so_mode));
1675 1669 return (0);
1676 1670 }
1677 1671
1678 1672 /*
1679 1673 * Unless SO_OOBINLINE we have so_oobmsg != NULL iff SS_HAVEOOBDATA
1680 1674 */
1681 1675 if (!(so->so_options & SO_OOBINLINE) &&
1682 1676 !EQUIVALENT(so->so_oobmsg != NULL, so->so_state & SS_HAVEOOBDATA)) {
1683 1677 printf("Bad oob state 4 (%p): state %s\n",
1684 1678 (void *)so, pr_state(so->so_state, so->so_mode));
1685 1679 return (0);
1686 1680 }
1687 1681
1688 1682 if (!SOCK_IS_NONSTR(so) &&
1689 1683 SOTOTPI(so)->sti_oobsigcnt < SOTOTPI(so)->sti_oobcnt) {
1690 1684 printf("Bad oob state 5 (%p): counts %d/%d state %s\n",
1691 1685 (void *)so, SOTOTPI(so)->sti_oobsigcnt,
1692 1686 SOTOTPI(so)->sti_oobcnt,
1693 1687 pr_state(so->so_state, so->so_mode));
1694 1688 return (0);
1695 1689 }
1696 1690
1697 1691 return (1);
1698 1692 }
1699 1693 #undef EQUIVALENT
1700 1694 #endif /* DEBUG */
1701 1695
1702 1696 /* initialize sockfs zone specific kstat related items */
1703 1697 void *
1704 1698 sock_kstat_init(zoneid_t zoneid)
1705 1699 {
1706 1700 kstat_t *ksp;
1707 1701
1708 1702 ksp = kstat_create_zone("sockfs", 0, "sock_unix_list", "misc",
1709 1703 KSTAT_TYPE_RAW, 0, KSTAT_FLAG_VAR_SIZE|KSTAT_FLAG_VIRTUAL, zoneid);
1710 1704
1711 1705 if (ksp != NULL) {
1712 1706 ksp->ks_update = sockfs_update;
1713 1707 ksp->ks_snapshot = sockfs_snapshot;
1714 1708 ksp->ks_lock = &socklist.sl_lock;
1715 1709 ksp->ks_private = (void *)(uintptr_t)zoneid;
1716 1710 kstat_install(ksp);
1717 1711 }
1718 1712
1719 1713 return (ksp);
1720 1714 }
1721 1715
1722 1716 /* tear down sockfs zone specific kstat related items */
1723 1717 /*ARGSUSED*/
1724 1718 void
1725 1719 sock_kstat_fini(zoneid_t zoneid, void *arg)
1726 1720 {
1727 1721 kstat_t *ksp = (kstat_t *)arg;
1728 1722
1729 1723 if (ksp != NULL) {
1730 1724 ASSERT(zoneid == (zoneid_t)(uintptr_t)ksp->ks_private);
1731 1725 kstat_delete(ksp);
1732 1726 }
1733 1727 }
1734 1728
1735 1729 /*
↓ open down ↓ |
957 lines elided |
↑ open up ↑ |
1736 1730 * Zones:
1737 1731 * Note that nactive is going to be different for each zone.
1738 1732 * This means we require kstat to call sockfs_update and then sockfs_snapshot
1739 1733 * for the same zone, or sockfs_snapshot will be taken into the wrong size
1740 1734 * buffer. This is safe, but if the buffer is too small, user will not be
1741 1735 * given details of all sockets. However, as this kstat has a ks_lock, kstat
1742 1736 * driver will keep it locked between the update and the snapshot, so no
1743 1737 * other process (zone) can currently get inbetween resulting in a wrong size
1744 1738 * buffer allocation.
1745 1739 */
1740 +
1746 1741 static int
1747 1742 sockfs_update(kstat_t *ksp, int rw)
1748 1743 {
1749 - uint_t nactive = 0; /* # of active AF_UNIX sockets */
1744 + uint_t n, nactive = 0; /* # of active AF_UNIX sockets */
1745 + uint_t tsze, sze;
1750 1746 struct sonode *so; /* current sonode on socklist */
1751 1747 zoneid_t myzoneid = (zoneid_t)(uintptr_t)ksp->ks_private;
1752 1748
1749 + tsze = sze = 0;
1750 +
1753 1751 ASSERT((zoneid_t)(uintptr_t)ksp->ks_private == getzoneid());
1754 1752
1755 1753 if (rw == KSTAT_WRITE) { /* bounce all writes */
1756 1754 return (EACCES);
1757 1755 }
1758 1756
1759 1757 for (so = socklist.sl_list; so != NULL; so = SOTOTPI(so)->sti_next_so) {
1760 1758 if (so->so_count != 0 && so->so_zoneid == myzoneid) {
1759 +
1761 1760 nactive++;
1761 +
1762 + mutex_enter(&so->so_pid_list_lock);
1763 + n = list_size(&so->so_pid_list);
1764 + mutex_exit(&so->so_pid_list_lock);
1765 +
1766 + sze = sizeof (struct sockinfo);
1767 + sze += (n > 1)?((n - 1) * sizeof (conn_pid_node_t)):0;
1768 + tsze += sze;
1769 +
1762 1770 }
1763 1771 }
1764 1772 ksp->ks_ndata = nactive;
1765 - ksp->ks_data_size = nactive * sizeof (struct k_sockinfo);
1773 + ksp->ks_data_size = tsze;
1766 1774
1767 1775 return (0);
1768 1776 }
1769 1777
1770 1778 static int
1771 1779 sockfs_snapshot(kstat_t *ksp, void *buf, int rw)
1772 1780 {
1773 1781 int ns; /* # of sonodes we've copied */
1774 1782 struct sonode *so; /* current sonode on socklist */
1775 - struct k_sockinfo *pksi; /* where we put sockinfo data */
1783 + struct sockinfo *psi; /* where we put sockinfo data */
1776 1784 t_uscalar_t sn_len; /* soa_len */
1777 1785 zoneid_t myzoneid = (zoneid_t)(uintptr_t)ksp->ks_private;
1778 1786 sotpi_info_t *sti;
1779 1787
1788 + uint_t sze;
1789 + conn_pid_node_list_hdr_t *cph;
1790 +
1780 1791 ASSERT((zoneid_t)(uintptr_t)ksp->ks_private == getzoneid());
1781 1792
1782 1793 ksp->ks_snaptime = gethrtime();
1783 1794
1784 1795 if (rw == KSTAT_WRITE) { /* bounce all writes */
1785 1796 return (EACCES);
1786 1797 }
1787 1798
1788 1799 /*
1789 1800 * for each sonode on the socklist, we massage the important
1790 1801 * info into buf, in k_sockinfo format.
1791 1802 */
1792 - pksi = (struct k_sockinfo *)buf;
1803 + psi = (struct sockinfo *)buf;
1793 1804 ns = 0;
1794 1805 for (so = socklist.sl_list; so != NULL; so = SOTOTPI(so)->sti_next_so) {
1795 1806 /* only stuff active sonodes and the same zone: */
1796 1807 if (so->so_count == 0 || so->so_zoneid != myzoneid) {
1797 1808 continue;
1798 1809 }
1799 1810
1811 + /* get the pidnode list associated with this sonode */
1812 + cph = so_get_sock_pid_list((sock_upper_handle_t)so);
1813 +
1814 + /* calculate the size of this sockinfo structure */
1815 + sze = sizeof (struct sockinfo);
1816 + sze += (cph->cph_pn_cnt > 1)?
1817 + ((cph->cph_pn_cnt - 1) * sizeof (conn_pid_node_t)):0;
1818 +
1800 1819 /*
1801 1820 * If the sonode was activated between the update and the
1802 - * snapshot, we're done - as this is only a snapshot.
1821 + * snapshot, we're done - as this is only a snapshot. We need
1822 + * to make sure that we have space for this sockinfo.
1803 1823 */
1804 - if ((caddr_t)(pksi) >= (caddr_t)buf + ksp->ks_data_size) {
1824 + if (((caddr_t)(psi) + sze) >
1825 + ((caddr_t)buf + ksp->ks_data_size)) {
1805 1826 break;
1806 1827 }
1807 1828
1808 1829 sti = SOTOTPI(so);
1809 1830 /* 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 =
1831 + psi->si_size = sze;
1832 + psi->si_family = so->so_family;
1833 + psi->si_type = so->so_type;
1834 + psi->si_flag = so->so_flag;
1835 + psi->si_state = so->so_state;
1836 + psi->si_serv_type = sti->sti_serv_type;
1837 + psi->si_ux_laddr_sou_magic =
1817 1838 sti->sti_ux_laddr.soua_magic;
1818 - pksi->ks_si.si_ux_faddr_sou_magic =
1839 + psi->si_ux_faddr_sou_magic =
1819 1840 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;
1841 + psi->si_laddr_soa_len = sti->sti_laddr.soa_len;
1842 + psi->si_faddr_soa_len = sti->sti_faddr.soa_len;
1843 + psi->si_szoneid = so->so_zoneid;
1844 + psi->si_faddr_noxlate = sti->sti_faddr_noxlate;
1845 +
1824 1846
1825 1847 mutex_enter(&so->so_lock);
1826 1848
1827 1849 if (sti->sti_laddr_sa != NULL) {
1828 1850 ASSERT(sti->sti_laddr_sa->sa_data != NULL);
1829 1851 sn_len = sti->sti_laddr_len;
1830 1852 ASSERT(sn_len <= sizeof (short) +
1831 - sizeof (pksi->ks_si.si_laddr_sun_path));
1853 + sizeof (psi->si_laddr_sun_path));
1832 1854
1833 - pksi->ks_si.si_laddr_family =
1855 + psi->si_laddr_family =
1834 1856 sti->sti_laddr_sa->sa_family;
1835 1857 if (sn_len != 0) {
1836 1858 /* AF_UNIX socket names are NULL terminated */
1837 - (void) strncpy(pksi->ks_si.si_laddr_sun_path,
1859 + (void) strncpy(psi->si_laddr_sun_path,
1838 1860 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);
1861 + sizeof (psi->si_laddr_sun_path));
1862 + sn_len = strlen(psi->si_laddr_sun_path);
1841 1863 }
1842 - pksi->ks_si.si_laddr_sun_path[sn_len] = 0;
1864 + psi->si_laddr_sun_path[sn_len] = 0;
1843 1865 }
1844 1866
1845 1867 if (sti->sti_faddr_sa != NULL) {
1846 1868 ASSERT(sti->sti_faddr_sa->sa_data != NULL);
1847 1869 sn_len = sti->sti_faddr_len;
1848 1870 ASSERT(sn_len <= sizeof (short) +
1849 - sizeof (pksi->ks_si.si_faddr_sun_path));
1871 + sizeof (psi->si_faddr_sun_path));
1850 1872
1851 - pksi->ks_si.si_faddr_family =
1873 + psi->si_faddr_family =
1852 1874 sti->sti_faddr_sa->sa_family;
1853 1875 if (sn_len != 0) {
1854 - (void) strncpy(pksi->ks_si.si_faddr_sun_path,
1876 + (void) strncpy(psi->si_faddr_sun_path,
1855 1877 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);
1878 + sizeof (psi->si_faddr_sun_path));
1879 + sn_len = strlen(psi->si_faddr_sun_path);
1858 1880 }
1859 - pksi->ks_si.si_faddr_sun_path[sn_len] = 0;
1881 + psi->si_faddr_sun_path[sn_len] = 0;
1860 1882 }
1861 1883
1862 1884 mutex_exit(&so->so_lock);
1863 1885
1864 - (void) sprintf(pksi->ks_straddr[0], "%p", (void *)so);
1865 - (void) sprintf(pksi->ks_straddr[1], "%p",
1886 + (void) sprintf(psi->si_son_straddr, "%p", (void *)so);
1887 + (void) sprintf(psi->si_lvn_straddr, "%p",
1866 1888 (void *)sti->sti_ux_laddr.soua_vp);
1867 - (void) sprintf(pksi->ks_straddr[2], "%p",
1889 + (void) sprintf(psi->si_fvn_straddr, "%p",
1868 1890 (void *)sti->sti_ux_faddr.soua_vp);
1869 1891
1892 + if ((psi->si_pn_cnt = cph->cph_pn_cnt) > 0)
1893 + (void) memcpy(psi->si_pns, cph->cph_cpns,
1894 + psi->si_pn_cnt * sizeof (conn_pid_node_t));
1895 +
1896 + kmem_free(cph, cph->cph_tot_size);
1897 +
1898 + psi = (struct sockinfo *)((char *)psi + psi->si_size);
1870 1899 ns++;
1871 - pksi++;
1872 1900 }
1873 1901
1874 1902 ksp->ks_ndata = ns;
1875 1903 return (0);
1876 1904 }
1877 1905
1878 1906 ssize_t
1879 1907 soreadfile(file_t *fp, uchar_t *buf, u_offset_t fileoff, int *err, size_t size)
1880 1908 {
1881 1909 struct uio auio;
1882 1910 struct iovec aiov[MSG_MAXIOVLEN];
1883 1911 register vnode_t *vp;
1884 1912 int ioflag, rwflag;
1885 1913 ssize_t cnt;
1886 1914 int error = 0;
1887 1915 int iovcnt = 0;
1888 1916 short fflag;
1889 1917
1890 1918 vp = fp->f_vnode;
1891 1919 fflag = fp->f_flag;
1892 1920
1893 1921 rwflag = 0;
1894 1922 aiov[0].iov_base = (caddr_t)buf;
1895 1923 aiov[0].iov_len = size;
1896 1924 iovcnt = 1;
1897 1925 cnt = (ssize_t)size;
1898 1926 (void) VOP_RWLOCK(vp, rwflag, NULL);
1899 1927
1900 1928 auio.uio_loffset = fileoff;
1901 1929 auio.uio_iov = aiov;
1902 1930 auio.uio_iovcnt = iovcnt;
1903 1931 auio.uio_resid = cnt;
1904 1932 auio.uio_segflg = UIO_SYSSPACE;
1905 1933 auio.uio_llimit = MAXOFFSET_T;
1906 1934 auio.uio_fmode = fflag;
1907 1935 auio.uio_extflg = UIO_COPY_CACHED;
1908 1936
1909 1937 ioflag = auio.uio_fmode & (FAPPEND|FSYNC|FDSYNC|FRSYNC);
1910 1938
1911 1939 /* If read sync is not asked for, filter sync flags */
1912 1940 if ((ioflag & FRSYNC) == 0)
1913 1941 ioflag &= ~(FSYNC|FDSYNC);
1914 1942 error = VOP_READ(vp, &auio, ioflag, fp->f_cred, NULL);
1915 1943 cnt -= auio.uio_resid;
1916 1944
1917 1945 VOP_RWUNLOCK(vp, rwflag, NULL);
1918 1946
1919 1947 if (error == EINTR && cnt != 0)
1920 1948 error = 0;
1921 1949 out:
1922 1950 if (error != 0) {
1923 1951 *err = error;
1924 1952 return (0);
1925 1953 } else {
1926 1954 *err = 0;
1927 1955 return (cnt);
1928 1956 }
1929 1957 }
1930 1958
1931 1959 int
1932 1960 so_copyin(const void *from, void *to, size_t size, int fromkernel)
1933 1961 {
1934 1962 if (fromkernel) {
1935 1963 bcopy(from, to, size);
1936 1964 return (0);
1937 1965 }
1938 1966 return (xcopyin(from, to, size));
1939 1967 }
1940 1968
1941 1969 int
1942 1970 so_copyout(const void *from, void *to, size_t size, int tokernel)
1943 1971 {
1944 1972 if (tokernel) {
1945 1973 bcopy(from, to, size);
1946 1974 return (0);
1947 1975 }
1948 1976 return (xcopyout(from, to, size));
1949 1977 }
↓ open down ↓ |
68 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX