| 1 | /* |
| 2 | * Copyright (c) 1982, 1986, 1989, 1991, 1993 |
| 3 | * The Regents of the University of California. All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * 3. All advertising materials mentioning features or use of this software |
| 14 | * must display the following acknowledgement: |
| 15 | * This product includes software developed by the University of |
| 16 | * California, Berkeley and its contributors. |
| 17 | * 4. Neither the name of the University nor the names of its contributors |
| 18 | * may be used to endorse or promote products derived from this software |
| 19 | * without specific prior written permission. |
| 20 | * |
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 31 | * SUCH DAMAGE. |
| 32 | * |
| 33 | * From: @(#)uipc_usrreq.c 8.3 (Berkeley) 1/4/94 |
| 34 | * $FreeBSD: src/sys/kern/uipc_usrreq.c,v 1.54.2.10 2003/03/04 17:28:09 nectar Exp $ |
| 35 | * $DragonFly: src/sys/kern/uipc_usrreq.c,v 1.30 2006/10/27 04:56:31 dillon Exp $ |
| 36 | */ |
| 37 | |
| 38 | #include <sys/param.h> |
| 39 | #include <sys/systm.h> |
| 40 | #include <sys/kernel.h> |
| 41 | #include <sys/domain.h> |
| 42 | #include <sys/fcntl.h> |
| 43 | #include <sys/malloc.h> /* XXX must be before <sys/file.h> */ |
| 44 | #include <sys/proc.h> |
| 45 | #include <sys/file.h> |
| 46 | #include <sys/filedesc.h> |
| 47 | #include <sys/mbuf.h> |
| 48 | #include <sys/nlookup.h> |
| 49 | #include <sys/protosw.h> |
| 50 | #include <sys/socket.h> |
| 51 | #include <sys/socketvar.h> |
| 52 | #include <sys/resourcevar.h> |
| 53 | #include <sys/stat.h> |
| 54 | #include <sys/mount.h> |
| 55 | #include <sys/sysctl.h> |
| 56 | #include <sys/un.h> |
| 57 | #include <sys/unpcb.h> |
| 58 | #include <sys/vnode.h> |
| 59 | #include <sys/file2.h> |
| 60 | #include <sys/spinlock2.h> |
| 61 | |
| 62 | #include <vm/vm_zone.h> |
| 63 | |
| 64 | static struct vm_zone *unp_zone; |
| 65 | static unp_gen_t unp_gencnt; |
| 66 | static u_int unp_count; |
| 67 | |
| 68 | static struct unp_head unp_shead, unp_dhead; |
| 69 | |
| 70 | /* |
| 71 | * Unix communications domain. |
| 72 | * |
| 73 | * TODO: |
| 74 | * SEQPACKET, RDM |
| 75 | * rethink name space problems |
| 76 | * need a proper out-of-band |
| 77 | * lock pushdown |
| 78 | */ |
| 79 | static struct sockaddr sun_noname = { sizeof(sun_noname), AF_LOCAL }; |
| 80 | static ino_t unp_ino; /* prototype for fake inode numbers */ |
| 81 | |
| 82 | static int unp_attach (struct socket *, struct pru_attach_info *); |
| 83 | static void unp_detach (struct unpcb *); |
| 84 | static int unp_bind (struct unpcb *,struct sockaddr *, struct thread *); |
| 85 | static int unp_connect (struct socket *,struct sockaddr *, |
| 86 | struct thread *); |
| 87 | static void unp_disconnect (struct unpcb *); |
| 88 | static void unp_shutdown (struct unpcb *); |
| 89 | static void unp_drop (struct unpcb *, int); |
| 90 | static void unp_gc (void); |
| 91 | static int unp_gc_clearmarks(struct file *, void *); |
| 92 | static int unp_gc_checkmarks(struct file *, void *); |
| 93 | static int unp_gc_checkrefs(struct file *, void *); |
| 94 | static void unp_scan (struct mbuf *, void (*)(struct file *, void *), |
| 95 | void *data); |
| 96 | static void unp_mark (struct file *, void *data); |
| 97 | static void unp_discard (struct file *, void *); |
| 98 | static int unp_internalize (struct mbuf *, struct thread *); |
| 99 | static int unp_listen (struct unpcb *, struct thread *); |
| 100 | |
| 101 | static int |
| 102 | uipc_abort(struct socket *so) |
| 103 | { |
| 104 | struct unpcb *unp = so->so_pcb; |
| 105 | |
| 106 | if (unp == NULL) |
| 107 | return EINVAL; |
| 108 | unp_drop(unp, ECONNABORTED); |
| 109 | unp_detach(unp); |
| 110 | sofree(so); |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | static int |
| 115 | uipc_accept(struct socket *so, struct sockaddr **nam) |
| 116 | { |
| 117 | struct unpcb *unp = so->so_pcb; |
| 118 | |
| 119 | if (unp == NULL) |
| 120 | return EINVAL; |
| 121 | |
| 122 | /* |
| 123 | * Pass back name of connected socket, |
| 124 | * if it was bound and we are still connected |
| 125 | * (our peer may have closed already!). |
| 126 | */ |
| 127 | if (unp->unp_conn && unp->unp_conn->unp_addr) { |
| 128 | *nam = dup_sockaddr((struct sockaddr *)unp->unp_conn->unp_addr); |
| 129 | } else { |
| 130 | *nam = dup_sockaddr((struct sockaddr *)&sun_noname); |
| 131 | } |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | static int |
| 136 | uipc_attach(struct socket *so, int proto, struct pru_attach_info *ai) |
| 137 | { |
| 138 | struct unpcb *unp = so->so_pcb; |
| 139 | |
| 140 | if (unp != NULL) |
| 141 | return EISCONN; |
| 142 | return unp_attach(so, ai); |
| 143 | } |
| 144 | |
| 145 | static int |
| 146 | uipc_bind(struct socket *so, struct sockaddr *nam, struct thread *td) |
| 147 | { |
| 148 | struct unpcb *unp = so->so_pcb; |
| 149 | |
| 150 | if (unp == NULL) |
| 151 | return EINVAL; |
| 152 | return unp_bind(unp, nam, td); |
| 153 | } |
| 154 | |
| 155 | static int |
| 156 | uipc_connect(struct socket *so, struct sockaddr *nam, struct thread *td) |
| 157 | { |
| 158 | struct unpcb *unp = so->so_pcb; |
| 159 | |
| 160 | if (unp == NULL) |
| 161 | return EINVAL; |
| 162 | return unp_connect(so, nam, td); |
| 163 | } |
| 164 | |
| 165 | static int |
| 166 | uipc_connect2(struct socket *so1, struct socket *so2) |
| 167 | { |
| 168 | struct unpcb *unp = so1->so_pcb; |
| 169 | |
| 170 | if (unp == NULL) |
| 171 | return EINVAL; |
| 172 | |
| 173 | return unp_connect2(so1, so2); |
| 174 | } |
| 175 | |
| 176 | /* control is EOPNOTSUPP */ |
| 177 | |
| 178 | static int |
| 179 | uipc_detach(struct socket *so) |
| 180 | { |
| 181 | struct unpcb *unp = so->so_pcb; |
| 182 | |
| 183 | if (unp == NULL) |
| 184 | return EINVAL; |
| 185 | |
| 186 | unp_detach(unp); |
| 187 | return 0; |
| 188 | } |
| 189 | |
| 190 | static int |
| 191 | uipc_disconnect(struct socket *so) |
| 192 | { |
| 193 | struct unpcb *unp = so->so_pcb; |
| 194 | |
| 195 | if (unp == NULL) |
| 196 | return EINVAL; |
| 197 | unp_disconnect(unp); |
| 198 | return 0; |
| 199 | } |
| 200 | |
| 201 | static int |
| 202 | uipc_listen(struct socket *so, struct thread *td) |
| 203 | { |
| 204 | struct unpcb *unp = so->so_pcb; |
| 205 | |
| 206 | if (unp == NULL || unp->unp_vnode == NULL) |
| 207 | return EINVAL; |
| 208 | return unp_listen(unp, td); |
| 209 | } |
| 210 | |
| 211 | static int |
| 212 | uipc_peeraddr(struct socket *so, struct sockaddr **nam) |
| 213 | { |
| 214 | struct unpcb *unp = so->so_pcb; |
| 215 | |
| 216 | if (unp == NULL) |
| 217 | return EINVAL; |
| 218 | if (unp->unp_conn && unp->unp_conn->unp_addr) |
| 219 | *nam = dup_sockaddr((struct sockaddr *)unp->unp_conn->unp_addr); |
| 220 | else { |
| 221 | /* |
| 222 | * XXX: It seems that this test always fails even when |
| 223 | * connection is established. So, this else clause is |
| 224 | * added as workaround to return PF_LOCAL sockaddr. |
| 225 | */ |
| 226 | *nam = dup_sockaddr((struct sockaddr *)&sun_noname); |
| 227 | } |
| 228 | return 0; |
| 229 | } |
| 230 | |
| 231 | static int |
| 232 | uipc_rcvd(struct socket *so, int flags) |
| 233 | { |
| 234 | struct unpcb *unp = so->so_pcb; |
| 235 | struct socket *so2; |
| 236 | u_long newhiwat; |
| 237 | |
| 238 | if (unp == NULL) |
| 239 | return EINVAL; |
| 240 | switch (so->so_type) { |
| 241 | case SOCK_DGRAM: |
| 242 | panic("uipc_rcvd DGRAM?"); |
| 243 | /*NOTREACHED*/ |
| 244 | |
| 245 | case SOCK_STREAM: |
| 246 | if (unp->unp_conn == NULL) |
| 247 | break; |
| 248 | so2 = unp->unp_conn->unp_socket; |
| 249 | /* |
| 250 | * Adjust backpressure on sender |
| 251 | * and wakeup any waiting to write. |
| 252 | */ |
| 253 | so2->so_snd.sb_mbmax += unp->unp_mbcnt - so->so_rcv.sb_mbcnt; |
| 254 | unp->unp_mbcnt = so->so_rcv.sb_mbcnt; |
| 255 | newhiwat = |
| 256 | so2->so_snd.sb_hiwat + unp->unp_cc - so->so_rcv.sb_cc; |
| 257 | chgsbsize(so2->so_cred->cr_uidinfo, &so2->so_snd.sb_hiwat, |
| 258 | newhiwat, RLIM_INFINITY); |
| 259 | unp->unp_cc = so->so_rcv.sb_cc; |
| 260 | sowwakeup(so2); |
| 261 | break; |
| 262 | |
| 263 | default: |
| 264 | panic("uipc_rcvd unknown socktype"); |
| 265 | } |
| 266 | return 0; |
| 267 | } |
| 268 | |
| 269 | /* pru_rcvoob is EOPNOTSUPP */ |
| 270 | |
| 271 | static int |
| 272 | uipc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, |
| 273 | struct mbuf *control, struct thread *td) |
| 274 | { |
| 275 | int error = 0; |
| 276 | struct unpcb *unp = so->so_pcb; |
| 277 | struct socket *so2; |
| 278 | u_long newhiwat; |
| 279 | |
| 280 | if (unp == NULL) { |
| 281 | error = EINVAL; |
| 282 | goto release; |
| 283 | } |
| 284 | if (flags & PRUS_OOB) { |
| 285 | error = EOPNOTSUPP; |
| 286 | goto release; |
| 287 | } |
| 288 | |
| 289 | if (control && (error = unp_internalize(control, td))) |
| 290 | goto release; |
| 291 | |
| 292 | switch (so->so_type) { |
| 293 | case SOCK_DGRAM: |
| 294 | { |
| 295 | struct sockaddr *from; |
| 296 | |
| 297 | if (nam) { |
| 298 | if (unp->unp_conn) { |
| 299 | error = EISCONN; |
| 300 | break; |
| 301 | } |
| 302 | error = unp_connect(so, nam, td); |
| 303 | if (error) |
| 304 | break; |
| 305 | } else { |
| 306 | if (unp->unp_conn == NULL) { |
| 307 | error = ENOTCONN; |
| 308 | break; |
| 309 | } |
| 310 | } |
| 311 | so2 = unp->unp_conn->unp_socket; |
| 312 | if (unp->unp_addr) |
| 313 | from = (struct sockaddr *)unp->unp_addr; |
| 314 | else |
| 315 | from = &sun_noname; |
| 316 | if (sbappendaddr(&so2->so_rcv, from, m, control)) { |
| 317 | sorwakeup(so2); |
| 318 | m = NULL; |
| 319 | control = NULL; |
| 320 | } else |
| 321 | error = ENOBUFS; |
| 322 | if (nam) |
| 323 | unp_disconnect(unp); |
| 324 | break; |
| 325 | } |
| 326 | |
| 327 | case SOCK_STREAM: |
| 328 | /* Connect if not connected yet. */ |
| 329 | /* |
| 330 | * Note: A better implementation would complain |
| 331 | * if not equal to the peer's address. |
| 332 | */ |
| 333 | if (!(so->so_state & SS_ISCONNECTED)) { |
| 334 | if (nam) { |
| 335 | error = unp_connect(so, nam, td); |
| 336 | if (error) |
| 337 | break; /* XXX */ |
| 338 | } else { |
| 339 | error = ENOTCONN; |
| 340 | break; |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | if (so->so_state & SS_CANTSENDMORE) { |
| 345 | error = EPIPE; |
| 346 | break; |
| 347 | } |
| 348 | if (unp->unp_conn == NULL) |
| 349 | panic("uipc_send connected but no connection?"); |
| 350 | so2 = unp->unp_conn->unp_socket; |
| 351 | /* |
| 352 | * Send to paired receive port, and then reduce |
| 353 | * send buffer hiwater marks to maintain backpressure. |
| 354 | * Wake up readers. |
| 355 | */ |
| 356 | if (control) { |
| 357 | if (sbappendcontrol(&so2->so_rcv, m, control)) |
| 358 | control = NULL; |
| 359 | } else |
| 360 | sbappend(&so2->so_rcv, m); |
| 361 | so->so_snd.sb_mbmax -= |
| 362 | so2->so_rcv.sb_mbcnt - unp->unp_conn->unp_mbcnt; |
| 363 | unp->unp_conn->unp_mbcnt = so2->so_rcv.sb_mbcnt; |
| 364 | newhiwat = so->so_snd.sb_hiwat - |
| 365 | (so2->so_rcv.sb_cc - unp->unp_conn->unp_cc); |
| 366 | chgsbsize(so->so_cred->cr_uidinfo, &so->so_snd.sb_hiwat, |
| 367 | newhiwat, RLIM_INFINITY); |
| 368 | unp->unp_conn->unp_cc = so2->so_rcv.sb_cc; |
| 369 | sorwakeup(so2); |
| 370 | m = NULL; |
| 371 | break; |
| 372 | |
| 373 | default: |
| 374 | panic("uipc_send unknown socktype"); |
| 375 | } |
| 376 | |
| 377 | /* |
| 378 | * SEND_EOF is equivalent to a SEND followed by a SHUTDOWN. |
| 379 | */ |
| 380 | if (flags & PRUS_EOF) { |
| 381 | socantsendmore(so); |
| 382 | unp_shutdown(unp); |
| 383 | } |
| 384 | |
| 385 | if (control && error != 0) |
| 386 | unp_dispose(control); |
| 387 | |
| 388 | release: |
| 389 | if (control) |
| 390 | m_freem(control); |
| 391 | if (m) |
| 392 | m_freem(m); |
| 393 | return error; |
| 394 | } |
| 395 | |
| 396 | static int |
| 397 | uipc_sense(struct socket *so, struct stat *sb) |
| 398 | { |
| 399 | struct unpcb *unp = so->so_pcb; |
| 400 | struct socket *so2; |
| 401 | |
| 402 | if (unp == NULL) |
| 403 | return EINVAL; |
| 404 | sb->st_blksize = so->so_snd.sb_hiwat; |
| 405 | if (so->so_type == SOCK_STREAM && unp->unp_conn != NULL) { |
| 406 | so2 = unp->unp_conn->unp_socket; |
| 407 | sb->st_blksize += so2->so_rcv.sb_cc; |
| 408 | } |
| 409 | sb->st_dev = NOUDEV; |
| 410 | if (unp->unp_ino == 0) /* make up a non-zero inode number */ |
| 411 | unp->unp_ino = (++unp_ino == 0) ? ++unp_ino : unp_ino; |
| 412 | sb->st_ino = unp->unp_ino; |
| 413 | return (0); |
| 414 | } |
| 415 | |
| 416 | static int |
| 417 | uipc_shutdown(struct socket *so) |
| 418 | { |
| 419 | struct unpcb *unp = so->so_pcb; |
| 420 | |
| 421 | if (unp == NULL) |
| 422 | return EINVAL; |
| 423 | socantsendmore(so); |
| 424 | unp_shutdown(unp); |
| 425 | return 0; |
| 426 | } |
| 427 | |
| 428 | static int |
| 429 | uipc_sockaddr(struct socket *so, struct sockaddr **nam) |
| 430 | { |
| 431 | struct unpcb *unp = so->so_pcb; |
| 432 | |
| 433 | if (unp == NULL) |
| 434 | return EINVAL; |
| 435 | if (unp->unp_addr) |
| 436 | *nam = dup_sockaddr((struct sockaddr *)unp->unp_addr); |
| 437 | return 0; |
| 438 | } |
| 439 | |
| 440 | struct pr_usrreqs uipc_usrreqs = { |
| 441 | uipc_abort, uipc_accept, uipc_attach, uipc_bind, uipc_connect, |
| 442 | uipc_connect2, pru_control_notsupp, uipc_detach, uipc_disconnect, |
| 443 | uipc_listen, uipc_peeraddr, uipc_rcvd, pru_rcvoob_notsupp, |
| 444 | uipc_send, uipc_sense, uipc_shutdown, uipc_sockaddr, |
| 445 | sosend, soreceive, sopoll |
| 446 | }; |
| 447 | |
| 448 | int |
| 449 | uipc_ctloutput(struct socket *so, struct sockopt *sopt) |
| 450 | { |
| 451 | struct unpcb *unp = so->so_pcb; |
| 452 | int error; |
| 453 | |
| 454 | switch (sopt->sopt_dir) { |
| 455 | case SOPT_GET: |
| 456 | switch (sopt->sopt_name) { |
| 457 | case LOCAL_PEERCRED: |
| 458 | if (unp->unp_flags & UNP_HAVEPC) |
| 459 | error = sooptcopyout(sopt, &unp->unp_peercred, |
| 460 | sizeof(unp->unp_peercred)); |
| 461 | else { |
| 462 | if (so->so_type == SOCK_STREAM) |
| 463 | error = ENOTCONN; |
| 464 | else |
| 465 | error = EINVAL; |
| 466 | } |
| 467 | break; |
| 468 | default: |
| 469 | error = EOPNOTSUPP; |
| 470 | break; |
| 471 | } |
| 472 | break; |
| 473 | case SOPT_SET: |
| 474 | default: |
| 475 | error = EOPNOTSUPP; |
| 476 | break; |
| 477 | } |
| 478 | return (error); |
| 479 | } |
| 480 | |
| 481 | /* |
| 482 | * Both send and receive buffers are allocated PIPSIZ bytes of buffering |
| 483 | * for stream sockets, although the total for sender and receiver is |
| 484 | * actually only PIPSIZ. |
| 485 | * Datagram sockets really use the sendspace as the maximum datagram size, |
| 486 | * and don't really want to reserve the sendspace. Their recvspace should |
| 487 | * be large enough for at least one max-size datagram plus address. |
| 488 | */ |
| 489 | #ifndef PIPSIZ |
| 490 | #define PIPSIZ 8192 |
| 491 | #endif |
| 492 | static u_long unpst_sendspace = PIPSIZ; |
| 493 | static u_long unpst_recvspace = PIPSIZ; |
| 494 | static u_long unpdg_sendspace = 2*1024; /* really max datagram size */ |
| 495 | static u_long unpdg_recvspace = 4*1024; |
| 496 | |
| 497 | static int unp_rights; /* file descriptors in flight */ |
| 498 | static struct spinlock unp_spin = SPINLOCK_INITIALIZER(&unp_spin); |
| 499 | |
| 500 | SYSCTL_DECL(_net_local_stream); |
| 501 | SYSCTL_INT(_net_local_stream, OID_AUTO, sendspace, CTLFLAG_RW, |
| 502 | &unpst_sendspace, 0, ""); |
| 503 | SYSCTL_INT(_net_local_stream, OID_AUTO, recvspace, CTLFLAG_RW, |
| 504 | &unpst_recvspace, 0, ""); |
| 505 | |
| 506 | SYSCTL_DECL(_net_local_dgram); |
| 507 | SYSCTL_INT(_net_local_dgram, OID_AUTO, maxdgram, CTLFLAG_RW, |
| 508 | &unpdg_sendspace, 0, ""); |
| 509 | SYSCTL_INT(_net_local_dgram, OID_AUTO, recvspace, CTLFLAG_RW, |
| 510 | &unpdg_recvspace, 0, ""); |
| 511 | |
| 512 | SYSCTL_DECL(_net_local); |
| 513 | SYSCTL_INT(_net_local, OID_AUTO, inflight, CTLFLAG_RD, &unp_rights, 0, ""); |
| 514 | |
| 515 | static int |
| 516 | unp_attach(struct socket *so, struct pru_attach_info *ai) |
| 517 | { |
| 518 | struct unpcb *unp; |
| 519 | int error; |
| 520 | |
| 521 | if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { |
| 522 | switch (so->so_type) { |
| 523 | |
| 524 | case SOCK_STREAM: |
| 525 | error = soreserve(so, unpst_sendspace, unpst_recvspace, |
| 526 | ai->sb_rlimit); |
| 527 | break; |
| 528 | |
| 529 | case SOCK_DGRAM: |
| 530 | error = soreserve(so, unpdg_sendspace, unpdg_recvspace, |
| 531 | ai->sb_rlimit); |
| 532 | break; |
| 533 | |
| 534 | default: |
| 535 | panic("unp_attach"); |
| 536 | } |
| 537 | if (error) |
| 538 | return (error); |
| 539 | } |
| 540 | unp = zalloc(unp_zone); |
| 541 | if (unp == NULL) |
| 542 | return (ENOBUFS); |
| 543 | bzero(unp, sizeof *unp); |
| 544 | unp->unp_gencnt = ++unp_gencnt; |
| 545 | unp_count++; |
| 546 | LIST_INIT(&unp->unp_refs); |
| 547 | unp->unp_socket = so; |
| 548 | unp->unp_rvnode = ai->fd_rdir; /* jail cruft XXX JH */ |
| 549 | LIST_INSERT_HEAD(so->so_type == SOCK_DGRAM ? &unp_dhead |
| 550 | : &unp_shead, unp, unp_link); |
| 551 | so->so_pcb = (caddr_t)unp; |
| 552 | return (0); |
| 553 | } |
| 554 | |
| 555 | static void |
| 556 | unp_detach(struct unpcb *unp) |
| 557 | { |
| 558 | LIST_REMOVE(unp, unp_link); |
| 559 | unp->unp_gencnt = ++unp_gencnt; |
| 560 | --unp_count; |
| 561 | if (unp->unp_vnode) { |
| 562 | unp->unp_vnode->v_socket = NULL; |
| 563 | vrele(unp->unp_vnode); |
| 564 | unp->unp_vnode = NULL; |
| 565 | } |
| 566 | if (unp->unp_conn) |
| 567 | unp_disconnect(unp); |
| 568 | while (!LIST_EMPTY(&unp->unp_refs)) |
| 569 | unp_drop(LIST_FIRST(&unp->unp_refs), ECONNRESET); |
| 570 | soisdisconnected(unp->unp_socket); |
| 571 | unp->unp_socket->so_pcb = NULL; |
| 572 | if (unp_rights) { |
| 573 | /* |
| 574 | * Normally the receive buffer is flushed later, |
| 575 | * in sofree, but if our receive buffer holds references |
| 576 | * to descriptors that are now garbage, we will dispose |
| 577 | * of those descriptor references after the garbage collector |
| 578 | * gets them (resulting in a "panic: closef: count < 0"). |
| 579 | */ |
| 580 | sorflush(unp->unp_socket); |
| 581 | unp_gc(); |
| 582 | } |
| 583 | if (unp->unp_addr) |
| 584 | kfree(unp->unp_addr, M_SONAME); |
| 585 | zfree(unp_zone, unp); |
| 586 | } |
| 587 | |
| 588 | static int |
| 589 | unp_bind(struct unpcb *unp, struct sockaddr *nam, struct thread *td) |
| 590 | { |
| 591 | struct proc *p = td->td_proc; |
| 592 | struct sockaddr_un *soun = (struct sockaddr_un *)nam; |
| 593 | struct vnode *vp; |
| 594 | struct vattr vattr; |
| 595 | int error, namelen; |
| 596 | struct nlookupdata nd; |
| 597 | char buf[SOCK_MAXADDRLEN]; |
| 598 | |
| 599 | if (unp->unp_vnode != NULL) |
| 600 | return (EINVAL); |
| 601 | namelen = soun->sun_len - offsetof(struct sockaddr_un, sun_path); |
| 602 | if (namelen <= 0) |
| 603 | return (EINVAL); |
| 604 | strncpy(buf, soun->sun_path, namelen); |
| 605 | buf[namelen] = 0; /* null-terminate the string */ |
| 606 | error = nlookup_init(&nd, buf, UIO_SYSSPACE, NLC_LOCKVP|NLC_CREATE); |
| 607 | if (error == 0) |
| 608 | error = nlookup(&nd); |
| 609 | if (error == 0 && nd.nl_nch.ncp->nc_vp != NULL) |
| 610 | error = EADDRINUSE; |
| 611 | if (error) |
| 612 | goto done; |
| 613 | |
| 614 | VATTR_NULL(&vattr); |
| 615 | vattr.va_type = VSOCK; |
| 616 | vattr.va_mode = (ACCESSPERMS & ~p->p_fd->fd_cmask); |
| 617 | error = VOP_NCREATE(&nd.nl_nch, &vp, nd.nl_cred, &vattr); |
| 618 | if (error == 0) { |
| 619 | vp->v_socket = unp->unp_socket; |
| 620 | unp->unp_vnode = vp; |
| 621 | unp->unp_addr = (struct sockaddr_un *)dup_sockaddr(nam); |
| 622 | vn_unlock(vp); |
| 623 | } |
| 624 | done: |
| 625 | nlookup_done(&nd); |
| 626 | return (error); |
| 627 | } |
| 628 | |
| 629 | static int |
| 630 | unp_connect(struct socket *so, struct sockaddr *nam, struct thread *td) |
| 631 | { |
| 632 | struct proc *p = td->td_proc; |
| 633 | struct sockaddr_un *soun = (struct sockaddr_un *)nam; |
| 634 | struct vnode *vp; |
| 635 | struct socket *so2, *so3; |
| 636 | struct unpcb *unp, *unp2, *unp3; |
| 637 | int error, len; |
| 638 | struct nlookupdata nd; |
| 639 | char buf[SOCK_MAXADDRLEN]; |
| 640 | |
| 641 | KKASSERT(p); |
| 642 | |
| 643 | len = nam->sa_len - offsetof(struct sockaddr_un, sun_path); |
| 644 | if (len <= 0) |
| 645 | return EINVAL; |
| 646 | strncpy(buf, soun->sun_path, len); |
| 647 | buf[len] = 0; |
| 648 | |
| 649 | vp = NULL; |
| 650 | error = nlookup_init(&nd, buf, UIO_SYSSPACE, NLC_FOLLOW); |
| 651 | if (error == 0) |
| 652 | error = nlookup(&nd); |
| 653 | if (error == 0) |
| 654 | error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp); |
| 655 | nlookup_done(&nd); |
| 656 | if (error) |
| 657 | return (error); |
| 658 | |
| 659 | if (vp->v_type != VSOCK) { |
| 660 | error = ENOTSOCK; |
| 661 | goto bad; |
| 662 | } |
| 663 | error = VOP_ACCESS(vp, VWRITE, p->p_ucred); |
| 664 | if (error) |
| 665 | goto bad; |
| 666 | so2 = vp->v_socket; |
| 667 | if (so2 == NULL) { |
| 668 | error = ECONNREFUSED; |
| 669 | goto bad; |
| 670 | } |
| 671 | if (so->so_type != so2->so_type) { |
| 672 | error = EPROTOTYPE; |
| 673 | goto bad; |
| 674 | } |
| 675 | if (so->so_proto->pr_flags & PR_CONNREQUIRED) { |
| 676 | if (!(so2->so_options & SO_ACCEPTCONN) || |
| 677 | (so3 = sonewconn(so2, 0)) == NULL) { |
| 678 | error = ECONNREFUSED; |
| 679 | goto bad; |
| 680 | } |
| 681 | unp = so->so_pcb; |
| 682 | unp2 = so2->so_pcb; |
| 683 | unp3 = so3->so_pcb; |
| 684 | if (unp2->unp_addr) |
| 685 | unp3->unp_addr = (struct sockaddr_un *) |
| 686 | dup_sockaddr((struct sockaddr *)unp2->unp_addr); |
| 687 | |
| 688 | /* |
| 689 | * unp_peercred management: |
| 690 | * |
| 691 | * The connecter's (client's) credentials are copied |
| 692 | * from its process structure at the time of connect() |
| 693 | * (which is now). |
| 694 | */ |
| 695 | cru2x(p->p_ucred, &unp3->unp_peercred); |
| 696 | unp3->unp_flags |= UNP_HAVEPC; |
| 697 | /* |
| 698 | * The receiver's (server's) credentials are copied |
| 699 | * from the unp_peercred member of socket on which the |
| 700 | * former called listen(); unp_listen() cached that |
| 701 | * process's credentials at that time so we can use |
| 702 | * them now. |
| 703 | */ |
| 704 | KASSERT(unp2->unp_flags & UNP_HAVEPCCACHED, |
| 705 | ("unp_connect: listener without cached peercred")); |
| 706 | memcpy(&unp->unp_peercred, &unp2->unp_peercred, |
| 707 | sizeof(unp->unp_peercred)); |
| 708 | unp->unp_flags |= UNP_HAVEPC; |
| 709 | |
| 710 | so2 = so3; |
| 711 | } |
| 712 | error = unp_connect2(so, so2); |
| 713 | bad: |
| 714 | vput(vp); |
| 715 | return (error); |
| 716 | } |
| 717 | |
| 718 | int |
| 719 | unp_connect2(struct socket *so, struct socket *so2) |
| 720 | { |
| 721 | struct unpcb *unp = so->so_pcb; |
| 722 | struct unpcb *unp2; |
| 723 | |
| 724 | if (so2->so_type != so->so_type) |
| 725 | return (EPROTOTYPE); |
| 726 | unp2 = so2->so_pcb; |
| 727 | unp->unp_conn = unp2; |
| 728 | switch (so->so_type) { |
| 729 | |
| 730 | case SOCK_DGRAM: |
| 731 | LIST_INSERT_HEAD(&unp2->unp_refs, unp, unp_reflink); |
| 732 | soisconnected(so); |
| 733 | break; |
| 734 | |
| 735 | case SOCK_STREAM: |
| 736 | unp2->unp_conn = unp; |
| 737 | soisconnected(so); |
| 738 | soisconnected(so2); |
| 739 | break; |
| 740 | |
| 741 | default: |
| 742 | panic("unp_connect2"); |
| 743 | } |
| 744 | return (0); |
| 745 | } |
| 746 | |
| 747 | static void |
| 748 | unp_disconnect(struct unpcb *unp) |
| 749 | { |
| 750 | struct unpcb *unp2 = unp->unp_conn; |
| 751 | |
| 752 | if (unp2 == NULL) |
| 753 | return; |
| 754 | |
| 755 | unp->unp_conn = NULL; |
| 756 | |
| 757 | switch (unp->unp_socket->so_type) { |
| 758 | case SOCK_DGRAM: |
| 759 | LIST_REMOVE(unp, unp_reflink); |
| 760 | unp->unp_socket->so_state &= ~SS_ISCONNECTED; |
| 761 | break; |
| 762 | case SOCK_STREAM: |
| 763 | soisdisconnected(unp->unp_socket); |
| 764 | unp2->unp_conn = NULL; |
| 765 | soisdisconnected(unp2->unp_socket); |
| 766 | break; |
| 767 | } |
| 768 | } |
| 769 | |
| 770 | #ifdef notdef |
| 771 | void |
| 772 | unp_abort(struct unpcb *unp) |
| 773 | { |
| 774 | |
| 775 | unp_detach(unp); |
| 776 | } |
| 777 | #endif |
| 778 | |
| 779 | static int |
| 780 | prison_unpcb(struct thread *td, struct unpcb *unp) |
| 781 | { |
| 782 | struct proc *p; |
| 783 | |
| 784 | if (td == NULL) |
| 785 | return (0); |
| 786 | if ((p = td->td_proc) == NULL) |
| 787 | return (0); |
| 788 | if (!p->p_ucred->cr_prison) |
| 789 | return (0); |
| 790 | if (p->p_fd->fd_rdir == unp->unp_rvnode) |
| 791 | return (0); |
| 792 | return (1); |
| 793 | } |
| 794 | |
| 795 | static int |
| 796 | unp_pcblist(SYSCTL_HANDLER_ARGS) |
| 797 | { |
| 798 | int error, i, n; |
| 799 | struct unpcb *unp, **unp_list; |
| 800 | unp_gen_t gencnt; |
| 801 | struct unp_head *head; |
| 802 | |
| 803 | head = ((intptr_t)arg1 == SOCK_DGRAM ? &unp_dhead : &unp_shead); |
| 804 | |
| 805 | KKASSERT(curproc != NULL); |
| 806 | |
| 807 | /* |
| 808 | * The process of preparing the PCB list is too time-consuming and |
| 809 | * resource-intensive to repeat twice on every request. |
| 810 | */ |
| 811 | if (req->oldptr == NULL) { |
| 812 | n = unp_count; |
| 813 | req->oldidx = (n + n/8) * sizeof(struct xunpcb); |
| 814 | return 0; |
| 815 | } |
| 816 | |
| 817 | if (req->newptr != NULL) |
| 818 | return EPERM; |
| 819 | |
| 820 | /* |
| 821 | * OK, now we're committed to doing something. |
| 822 | */ |
| 823 | gencnt = unp_gencnt; |
| 824 | n = unp_count; |
| 825 | |
| 826 | unp_list = kmalloc(n * sizeof *unp_list, M_TEMP, M_WAITOK); |
| 827 | if (unp_list == NULL) |
| 828 | return ENOMEM; |
| 829 | |
| 830 | for (unp = LIST_FIRST(head), i = 0; unp && i < n; |
| 831 | unp = LIST_NEXT(unp, unp_link)) { |
| 832 | if (unp->unp_gencnt <= gencnt && !prison_unpcb(req->td, unp)) |
| 833 | unp_list[i++] = unp; |
| 834 | } |
| 835 | n = i; /* in case we lost some during malloc */ |
| 836 | |
| 837 | error = 0; |
| 838 | for (i = 0; i < n; i++) { |
| 839 | unp = unp_list[i]; |
| 840 | if (unp->unp_gencnt <= gencnt) { |
| 841 | struct xunpcb xu; |
| 842 | xu.xu_len = sizeof xu; |
| 843 | xu.xu_unpp = unp; |
| 844 | /* |
| 845 | * XXX - need more locking here to protect against |
| 846 | * connect/disconnect races for SMP. |
| 847 | */ |
| 848 | if (unp->unp_addr) |
| 849 | bcopy(unp->unp_addr, &xu.xu_addr, |
| 850 | unp->unp_addr->sun_len); |
| 851 | if (unp->unp_conn && unp->unp_conn->unp_addr) |
| 852 | bcopy(unp->unp_conn->unp_addr, |
| 853 | &xu.xu_caddr, |
| 854 | unp->unp_conn->unp_addr->sun_len); |
| 855 | bcopy(unp, &xu.xu_unp, sizeof *unp); |
| 856 | sotoxsocket(unp->unp_socket, &xu.xu_socket); |
| 857 | error = SYSCTL_OUT(req, &xu, sizeof xu); |
| 858 | } |
| 859 | } |
| 860 | kfree(unp_list, M_TEMP); |
| 861 | return error; |
| 862 | } |
| 863 | |
| 864 | SYSCTL_PROC(_net_local_dgram, OID_AUTO, pcblist, CTLFLAG_RD, |
| 865 | (caddr_t)(long)SOCK_DGRAM, 0, unp_pcblist, "S,xunpcb", |
| 866 | "List of active local datagram sockets"); |
| 867 | SYSCTL_PROC(_net_local_stream, OID_AUTO, pcblist, CTLFLAG_RD, |
| 868 | (caddr_t)(long)SOCK_STREAM, 0, unp_pcblist, "S,xunpcb", |
| 869 | "List of active local stream sockets"); |
| 870 | |
| 871 | static void |
| 872 | unp_shutdown(struct unpcb *unp) |
| 873 | { |
| 874 | struct socket *so; |
| 875 | |
| 876 | if (unp->unp_socket->so_type == SOCK_STREAM && unp->unp_conn != NULL && |
| 877 | (so = unp->unp_conn->unp_socket)) |
| 878 | socantrcvmore(so); |
| 879 | } |
| 880 | |
| 881 | static void |
| 882 | unp_drop(struct unpcb *unp, int errno) |
| 883 | { |
| 884 | struct socket *so = unp->unp_socket; |
| 885 | |
| 886 | so->so_error = errno; |
| 887 | unp_disconnect(unp); |
| 888 | } |
| 889 | |
| 890 | #ifdef notdef |
| 891 | void |
| 892 | unp_drain() |
| 893 | { |
| 894 | |
| 895 | } |
| 896 | #endif |
| 897 | |
| 898 | int |
| 899 | unp_externalize(struct mbuf *rights) |
| 900 | { |
| 901 | struct proc *p = curproc; /* XXX */ |
| 902 | int i; |
| 903 | struct cmsghdr *cm = mtod(rights, struct cmsghdr *); |
| 904 | int *fdp; |
| 905 | struct file **rp; |
| 906 | struct file *fp; |
| 907 | int newfds = (cm->cmsg_len - (CMSG_DATA(cm) - (u_char *)cm)) |
| 908 | / sizeof (struct file *); |
| 909 | int f; |
| 910 | |
| 911 | /* |
| 912 | * if the new FD's will not fit, then we free them all |
| 913 | */ |
| 914 | if (!fdavail(p, newfds)) { |
| 915 | rp = (struct file **)CMSG_DATA(cm); |
| 916 | for (i = 0; i < newfds; i++) { |
| 917 | fp = *rp; |
| 918 | /* |
| 919 | * zero the pointer before calling unp_discard, |
| 920 | * since it may end up in unp_gc().. |
| 921 | */ |
| 922 | *rp++ = 0; |
| 923 | unp_discard(fp, NULL); |
| 924 | } |
| 925 | return (EMSGSIZE); |
| 926 | } |
| 927 | /* |
| 928 | * now change each pointer to an fd in the global table to |
| 929 | * an integer that is the index to the local fd table entry |
| 930 | * that we set up to point to the global one we are transferring. |
| 931 | * If sizeof (struct file *) is bigger than or equal to sizeof int, |
| 932 | * then do it in forward order. In that case, an integer will |
| 933 | * always come in the same place or before its corresponding |
| 934 | * struct file pointer. |
| 935 | * If sizeof (struct file *) is smaller than sizeof int, then |
| 936 | * do it in reverse order. |
| 937 | */ |
| 938 | if (sizeof (struct file *) >= sizeof (int)) { |
| 939 | fdp = (int *)(cm + 1); |
| 940 | rp = (struct file **)CMSG_DATA(cm); |
| 941 | for (i = 0; i < newfds; i++) { |
| 942 | if (fdalloc(p, 0, &f)) |
| 943 | panic("unp_externalize"); |
| 944 | fp = *rp++; |
| 945 | fsetfd(p, fp, f); |
| 946 | fdrop(fp); |
| 947 | spin_lock_wr(&unp_spin); |
| 948 | fp->f_msgcount--; |
| 949 | unp_rights--; |
| 950 | spin_unlock_wr(&unp_spin); |
| 951 | *fdp++ = f; |
| 952 | } |
| 953 | } else { |
| 954 | fdp = (int *)(cm + 1) + newfds - 1; |
| 955 | rp = (struct file **)CMSG_DATA(cm) + newfds - 1; |
| 956 | for (i = 0; i < newfds; i++) { |
| 957 | if (fdalloc(p, 0, &f)) |
| 958 | panic("unp_externalize"); |
| 959 | fp = *rp--; |
| 960 | fsetfd(p, fp, f); |
| 961 | fdrop(fp); |
| 962 | spin_lock_wr(&unp_spin); |
| 963 | fp->f_msgcount--; |
| 964 | unp_rights--; |
| 965 | spin_unlock_wr(&unp_spin); |
| 966 | *fdp-- = f; |
| 967 | } |
| 968 | } |
| 969 | |
| 970 | /* |
| 971 | * Adjust length, in case sizeof(struct file *) and sizeof(int) |
| 972 | * differs. |
| 973 | */ |
| 974 | cm->cmsg_len = CMSG_LEN(newfds * sizeof(int)); |
| 975 | rights->m_len = cm->cmsg_len; |
| 976 | return (0); |
| 977 | } |
| 978 | |
| 979 | void |
| 980 | unp_init(void) |
| 981 | { |
| 982 | unp_zone = zinit("unpcb", sizeof(struct unpcb), nmbclusters, 0, 0); |
| 983 | if (unp_zone == NULL) |
| 984 | panic("unp_init"); |
| 985 | LIST_INIT(&unp_dhead); |
| 986 | LIST_INIT(&unp_shead); |
| 987 | spin_init(&unp_spin); |
| 988 | } |
| 989 | |
| 990 | static int |
| 991 | unp_internalize(struct mbuf *control, struct thread *td) |
| 992 | { |
| 993 | struct proc *p = td->td_proc; |
| 994 | struct filedesc *fdescp; |
| 995 | struct cmsghdr *cm = mtod(control, struct cmsghdr *); |
| 996 | struct file **rp; |
| 997 | struct file *fp; |
| 998 | int i, fd, *fdp; |
| 999 | struct cmsgcred *cmcred; |
| 1000 | int oldfds; |
| 1001 | u_int newlen; |
| 1002 | |
| 1003 | KKASSERT(p); |
| 1004 | fdescp = p->p_fd; |
| 1005 | if ((cm->cmsg_type != SCM_RIGHTS && cm->cmsg_type != SCM_CREDS) || |
| 1006 | cm->cmsg_level != SOL_SOCKET || cm->cmsg_len != control->m_len) |
| 1007 | return (EINVAL); |
| 1008 | |
| 1009 | /* |
| 1010 | * Fill in credential information. |
| 1011 | */ |
| 1012 | if (cm->cmsg_type == SCM_CREDS) { |
| 1013 | cmcred = (struct cmsgcred *)(cm + 1); |
| 1014 | cmcred->cmcred_pid = p->p_pid; |
| 1015 | cmcred->cmcred_uid = p->p_ucred->cr_ruid; |
| 1016 | cmcred->cmcred_gid = p->p_ucred->cr_rgid; |
| 1017 | cmcred->cmcred_euid = p->p_ucred->cr_uid; |
| 1018 | cmcred->cmcred_ngroups = MIN(p->p_ucred->cr_ngroups, |
| 1019 | CMGROUP_MAX); |
| 1020 | for (i = 0; i < cmcred->cmcred_ngroups; i++) |
| 1021 | cmcred->cmcred_groups[i] = p->p_ucred->cr_groups[i]; |
| 1022 | return(0); |
| 1023 | } |
| 1024 | |
| 1025 | oldfds = (cm->cmsg_len - sizeof (*cm)) / sizeof (int); |
| 1026 | /* |
| 1027 | * check that all the FDs passed in refer to legal OPEN files |
| 1028 | * If not, reject the entire operation. |
| 1029 | */ |
| 1030 | fdp = (int *)(cm + 1); |
| 1031 | for (i = 0; i < oldfds; i++) { |
| 1032 | fd = *fdp++; |
| 1033 | if ((unsigned)fd >= fdescp->fd_nfiles || |
| 1034 | fdescp->fd_files[fd].fp == NULL) |
| 1035 | return (EBADF); |
| 1036 | if (fdescp->fd_files[fd].fp->f_type == DTYPE_KQUEUE) |
| 1037 | return (EOPNOTSUPP); |
| 1038 | } |
| 1039 | /* |
| 1040 | * Now replace the integer FDs with pointers to |
| 1041 | * the associated global file table entry.. |
| 1042 | * Allocate a bigger buffer as necessary. But if an cluster is not |
| 1043 | * enough, return E2BIG. |
| 1044 | */ |
| 1045 | newlen = CMSG_LEN(oldfds * sizeof(struct file *)); |
| 1046 | if (newlen > MCLBYTES) |
| 1047 | return (E2BIG); |
| 1048 | if (newlen - control->m_len > M_TRAILINGSPACE(control)) { |
| 1049 | if (control->m_flags & M_EXT) |
| 1050 | return (E2BIG); |
| 1051 | MCLGET(control, MB_WAIT); |
| 1052 | if (!(control->m_flags & M_EXT)) |
| 1053 | return (ENOBUFS); |
| 1054 | |
| 1055 | /* copy the data to the cluster */ |
| 1056 | memcpy(mtod(control, char *), cm, cm->cmsg_len); |
| 1057 | cm = mtod(control, struct cmsghdr *); |
| 1058 | } |
| 1059 | |
| 1060 | /* |
| 1061 | * Adjust length, in case sizeof(struct file *) and sizeof(int) |
| 1062 | * differs. |
| 1063 | */ |
| 1064 | control->m_len = cm->cmsg_len = newlen; |
| 1065 | |
| 1066 | /* |
| 1067 | * Transform the file descriptors into struct file pointers. |
| 1068 | * If sizeof (struct file *) is bigger than or equal to sizeof int, |
| 1069 | * then do it in reverse order so that the int won't get until |
| 1070 | * we're done. |
| 1071 | * If sizeof (struct file *) is smaller than sizeof int, then |
| 1072 | * do it in forward order. |
| 1073 | */ |
| 1074 | if (sizeof (struct file *) >= sizeof (int)) { |
| 1075 | fdp = (int *)(cm + 1) + oldfds - 1; |
| 1076 | rp = (struct file **)CMSG_DATA(cm) + oldfds - 1; |
| 1077 | for (i = 0; i < oldfds; i++) { |
| 1078 | fp = fdescp->fd_files[*fdp--].fp; |
| 1079 | *rp-- = fp; |
| 1080 | fhold(fp); |
| 1081 | spin_lock_wr(&unp_spin); |
| 1082 | fp->f_msgcount++; |
| 1083 | unp_rights++; |
| 1084 | spin_unlock_wr(&unp_spin); |
| 1085 | } |
| 1086 | } else { |
| 1087 | fdp = (int *)(cm + 1); |
| 1088 | rp = (struct file **)CMSG_DATA(cm); |
| 1089 | for (i = 0; i < oldfds; i++) { |
| 1090 | fp = fdescp->fd_files[*fdp++].fp; |
| 1091 | *rp++ = fp; |
| 1092 | fhold(fp); |
| 1093 | spin_lock_wr(&unp_spin); |
| 1094 | fp->f_msgcount++; |
| 1095 | unp_rights++; |
| 1096 | spin_unlock_wr(&unp_spin); |
| 1097 | } |
| 1098 | } |
| 1099 | return (0); |
| 1100 | } |
| 1101 | |
| 1102 | /* |
| 1103 | * Garbage collect in-transit file descriptors that get lost due to |
| 1104 | * loops (i.e. when a socket is sent to another process over itself, |
| 1105 | * and more complex situations). |
| 1106 | * |
| 1107 | * NOT MPSAFE - TODO socket flush code and maybe closef. Rest is MPSAFE. |
| 1108 | */ |
| 1109 | |
| 1110 | struct unp_gc_info { |
| 1111 | struct file **extra_ref; |
| 1112 | struct file *locked_fp; |
| 1113 | int defer; |
| 1114 | int index; |
| 1115 | int maxindex; |
| 1116 | }; |
| 1117 | |
| 1118 | static void |
| 1119 | unp_gc() |
| 1120 | { |
| 1121 | struct unp_gc_info info; |
| 1122 | static boolean_t unp_gcing; |
| 1123 | struct file **fpp; |
| 1124 | int i; |
| 1125 | |
| 1126 | spin_lock_wr(&unp_spin); |
| 1127 | if (unp_gcing) { |
| 1128 | spin_unlock_wr(&unp_spin); |
| 1129 | return; |
| 1130 | } |
| 1131 | unp_gcing = TRUE; |
| 1132 | spin_unlock_wr(&unp_spin); |
| 1133 | |
| 1134 | /* |
| 1135 | * before going through all this, set all FDs to |
| 1136 | * be NOT defered and NOT externally accessible |
| 1137 | */ |
| 1138 | info.defer = 0; |
| 1139 | allfiles_scan_exclusive(unp_gc_clearmarks, NULL); |
| 1140 | do { |
| 1141 | allfiles_scan_exclusive(unp_gc_checkmarks, &info); |
| 1142 | } while (info.defer); |
| 1143 | |
| 1144 | /* |
| 1145 | * We grab an extra reference to each of the file table entries |
| 1146 | * that are not otherwise accessible and then free the rights |
| 1147 | * that are stored in messages on them. |
| 1148 | * |
| 1149 | * The bug in the orginal code is a little tricky, so I'll describe |
| 1150 | * what's wrong with it here. |
| 1151 | * |
| 1152 | * It is incorrect to simply unp_discard each entry for f_msgcount |
| 1153 | * times -- consider the case of sockets A and B that contain |
| 1154 | * references to each other. On a last close of some other socket, |
| 1155 | * we trigger a gc since the number of outstanding rights (unp_rights) |
| 1156 | * is non-zero. If during the sweep phase the gc code un_discards, |
| 1157 | * we end up doing a (full) closef on the descriptor. A closef on A |
| 1158 | * results in the following chain. Closef calls soo_close, which |
| 1159 | * calls soclose. Soclose calls first (through the switch |
| 1160 | * uipc_usrreq) unp_detach, which re-invokes unp_gc. Unp_gc simply |
| 1161 | * returns because the previous instance had set unp_gcing, and |
| 1162 | * we return all the way back to soclose, which marks the socket |
| 1163 | * with SS_NOFDREF, and then calls sofree. Sofree calls sorflush |
| 1164 | * to free up the rights that are queued in messages on the socket A, |
| 1165 | * i.e., the reference on B. The sorflush calls via the dom_dispose |
| 1166 | * switch unp_dispose, which unp_scans with unp_discard. This second |
| 1167 | * instance of unp_discard just calls closef on B. |
| 1168 | * |
| 1169 | * Well, a similar chain occurs on B, resulting in a sorflush on B, |
| 1170 | * which results in another closef on A. Unfortunately, A is already |
| 1171 | * being closed, and the descriptor has already been marked with |
| 1172 | * SS_NOFDREF, and soclose panics at this point. |
| 1173 | * |
| 1174 | * Here, we first take an extra reference to each inaccessible |
| 1175 | * descriptor. Then, we call sorflush ourself, since we know |
| 1176 | * it is a Unix domain socket anyhow. After we destroy all the |
| 1177 | * rights carried in messages, we do a last closef to get rid |
| 1178 | * of our extra reference. This is the last close, and the |
| 1179 | * unp_detach etc will shut down the socket. |
| 1180 | * |
| 1181 | * 91/09/19, bsy@cs.cmu.edu |
| 1182 | */ |
| 1183 | info.extra_ref = kmalloc(256 * sizeof(struct file *), M_FILE, M_WAITOK); |
| 1184 | info.maxindex = 256; |
| 1185 | |
| 1186 | do { |
| 1187 | /* |
| 1188 | * Look for matches |
| 1189 | */ |
| 1190 | info.index = 0; |
| 1191 | allfiles_scan_exclusive(unp_gc_checkrefs, &info); |
| 1192 | |
| 1193 | /* |
| 1194 | * For each FD on our hit list, do the following two things |
| 1195 | */ |
| 1196 | for (i = info.index, fpp = info.extra_ref; --i >= 0; ++fpp) { |
| 1197 | struct file *tfp = *fpp; |
| 1198 | if (tfp->f_type == DTYPE_SOCKET && tfp->f_data != NULL) |
| 1199 | sorflush((struct socket *)(tfp->f_data)); |
| 1200 | } |
| 1201 | for (i = info.index, fpp = info.extra_ref; --i >= 0; ++fpp) |
| 1202 | closef(*fpp, NULL); |
| 1203 | } while (info.index == info.maxindex); |
| 1204 | kfree((caddr_t)info.extra_ref, M_FILE); |
| 1205 | unp_gcing = FALSE; |
| 1206 | } |
| 1207 | |
| 1208 | /* |
| 1209 | * MPSAFE - NOTE: filehead list and file pointer spinlocked on entry |
| 1210 | */ |
| 1211 | static int |
| 1212 | unp_gc_checkrefs(struct file *fp, void *data) |
| 1213 | { |
| 1214 | struct unp_gc_info *info = data; |
| 1215 | |
| 1216 | if (fp->f_count == 0) |
| 1217 | return(0); |
| 1218 | if (info->index == info->maxindex) |
| 1219 | return(-1); |
| 1220 | |
| 1221 | /* |
| 1222 | * If all refs are from msgs, and it's not marked accessible |
| 1223 | * then it must be referenced from some unreachable cycle |
| 1224 | * of (shut-down) FDs, so include it in our |
| 1225 | * list of FDs to remove |
| 1226 | */ |
| 1227 | if (fp->f_count == fp->f_msgcount && !(fp->f_flag & FMARK)) { |
| 1228 | info->extra_ref[info->index++] = fp; |
| 1229 | fhold(fp); |
| 1230 | } |
| 1231 | return(0); |
| 1232 | } |
| 1233 | |
| 1234 | /* |
| 1235 | * MPSAFE - NOTE: filehead list and file pointer spinlocked on entry |
| 1236 | */ |
| 1237 | static int |
| 1238 | unp_gc_clearmarks(struct file *fp, void *data __unused) |
| 1239 | { |
| 1240 | fp->f_flag &= ~(FMARK|FDEFER); |
| 1241 | return(0); |
| 1242 | } |
| 1243 | |
| 1244 | /* |
| 1245 | * MPSAFE - NOTE: filehead list and file pointer spinlocked on entry |
| 1246 | */ |
| 1247 | static int |
| 1248 | unp_gc_checkmarks(struct file *fp, void *data) |
| 1249 | { |
| 1250 | struct unp_gc_info *info = data; |
| 1251 | struct socket *so; |
| 1252 | |
| 1253 | /* |
| 1254 | * If the file is not open, skip it |
| 1255 | */ |
| 1256 | if (fp->f_count == 0) |
| 1257 | return(0); |
| 1258 | /* |
| 1259 | * If we already marked it as 'defer' in a |
| 1260 | * previous pass, then try process it this time |
| 1261 | * and un-mark it |
| 1262 | */ |
| 1263 | if (fp->f_flag & FDEFER) { |
| 1264 | fp->f_flag &= ~FDEFER; |
| 1265 | --info->defer; |
| 1266 | } else { |
| 1267 | /* |
| 1268 | * if it's not defered, then check if it's |
| 1269 | * already marked.. if so skip it |
| 1270 | */ |
| 1271 | if (fp->f_flag & FMARK) |
| 1272 | return(0); |
| 1273 | /* |
| 1274 | * If all references are from messages |
| 1275 | * in transit, then skip it. it's not |
| 1276 | * externally accessible. |
| 1277 | */ |
| 1278 | if (fp->f_count == fp->f_msgcount) |
| 1279 | return(0); |
| 1280 | /* |
| 1281 | * If it got this far then it must be |
| 1282 | * externally accessible. |
| 1283 | */ |
| 1284 | fp->f_flag |= FMARK; |
| 1285 | } |
| 1286 | /* |
| 1287 | * either it was defered, or it is externally |
| 1288 | * accessible and not already marked so. |
| 1289 | * Now check if it is possibly one of OUR sockets. |
| 1290 | */ |
| 1291 | if (fp->f_type != DTYPE_SOCKET || |
| 1292 | (so = (struct socket *)fp->f_data) == NULL) |
| 1293 | return(0); |
| 1294 | if (so->so_proto->pr_domain != &localdomain || |
| 1295 | !(so->so_proto->pr_flags & PR_RIGHTS)) |
| 1296 | return(0); |
| 1297 | #ifdef notdef |
| 1298 | XXX note: exclusive fp->f_spin lock held |
| 1299 | if (so->so_rcv.sb_flags & SB_LOCK) { |
| 1300 | /* |
| 1301 | * This is problematical; it's not clear |
| 1302 | * we need to wait for the sockbuf to be |
| 1303 | * unlocked (on a uniprocessor, at least), |
| 1304 | * and it's also not clear what to do |
| 1305 | * if sbwait returns an error due to receipt |
| 1306 | * of a signal. If sbwait does return |
| 1307 | * an error, we'll go into an infinite |
| 1308 | * loop. Delete all of this for now. |
| 1309 | */ |
| 1310 | sbwait(&so->so_rcv); |
| 1311 | goto restart; |
| 1312 | } |
| 1313 | #endif |
| 1314 | /* |
| 1315 | * So, Ok, it's one of our sockets and it IS externally |
| 1316 | * accessible (or was defered). Now we look |
| 1317 | * to see if we hold any file descriptors in its |
| 1318 | * message buffers. Follow those links and mark them |
| 1319 | * as accessible too. |
| 1320 | */ |
| 1321 | info->locked_fp = fp; |
| 1322 | /* spin_lock_wr(&so->so_rcv.sb_spin); */ |
| 1323 | unp_scan(so->so_rcv.sb_mb, unp_mark, info); |
| 1324 | /* spin_unlock_wr(&so->so_rcv.sb_spin);*/ |
| 1325 | return (0); |
| 1326 | } |
| 1327 | |
| 1328 | void |
| 1329 | unp_dispose(struct mbuf *m) |
| 1330 | { |
| 1331 | if (m) |
| 1332 | unp_scan(m, unp_discard, NULL); |
| 1333 | } |
| 1334 | |
| 1335 | static int |
| 1336 | unp_listen(struct unpcb *unp, struct thread *td) |
| 1337 | { |
| 1338 | struct proc *p = td->td_proc; |
| 1339 | |
| 1340 | KKASSERT(p); |
| 1341 | cru2x(p->p_ucred, &unp->unp_peercred); |
| 1342 | unp->unp_flags |= UNP_HAVEPCCACHED; |
| 1343 | return (0); |
| 1344 | } |
| 1345 | |
| 1346 | static void |
| 1347 | unp_scan(struct mbuf *m0, void (*op)(struct file *, void *), void *data) |
| 1348 | { |
| 1349 | struct mbuf *m; |
| 1350 | struct file **rp; |
| 1351 | struct cmsghdr *cm; |
| 1352 | int i; |
| 1353 | int qfds; |
| 1354 | |
| 1355 | while (m0) { |
| 1356 | for (m = m0; m; m = m->m_next) { |
| 1357 | if (m->m_type == MT_CONTROL && |
| 1358 | m->m_len >= sizeof(*cm)) { |
| 1359 | cm = mtod(m, struct cmsghdr *); |
| 1360 | if (cm->cmsg_level != SOL_SOCKET || |
| 1361 | cm->cmsg_type != SCM_RIGHTS) |
| 1362 | continue; |
| 1363 | qfds = (cm->cmsg_len - |
| 1364 | (CMSG_DATA(cm) - (u_char *)cm)) |
| 1365 | / sizeof (struct file *); |
| 1366 | rp = (struct file **)CMSG_DATA(cm); |
| 1367 | for (i = 0; i < qfds; i++) |
| 1368 | (*op)(*rp++, data); |
| 1369 | break; /* XXX, but saves time */ |
| 1370 | } |
| 1371 | } |
| 1372 | m0 = m0->m_nextpkt; |
| 1373 | } |
| 1374 | } |
| 1375 | |
| 1376 | static void |
| 1377 | unp_mark(struct file *fp, void *data) |
| 1378 | { |
| 1379 | struct unp_gc_info *info = data; |
| 1380 | |
| 1381 | if (info->locked_fp != fp) |
| 1382 | spin_lock_wr(&fp->f_spin); |
| 1383 | if ((fp->f_flag & FMARK) == 0) { |
| 1384 | ++info->defer; |
| 1385 | fp->f_flag |= (FMARK|FDEFER); |
| 1386 | } |
| 1387 | if (info->locked_fp != fp) |
| 1388 | spin_unlock_wr(&fp->f_spin); |
| 1389 | } |
| 1390 | |
| 1391 | static void |
| 1392 | unp_discard(struct file *fp, void *data __unused) |
| 1393 | { |
| 1394 | spin_lock_wr(&unp_spin); |
| 1395 | fp->f_msgcount--; |
| 1396 | unp_rights--; |
| 1397 | spin_unlock_wr(&unp_spin); |
| 1398 | closef(fp, NULL); |
| 1399 | } |
| 1400 | |