1139743Simp/*- 243412Snewton * Copyright (c) 1998 Mark Newton 343412Snewton * Copyright (c) 1996 Christos Zoulas. 443412Snewton * All rights reserved. 543412Snewton * 643412Snewton * Redistribution and use in source and binary forms, with or without 743412Snewton * modification, are permitted provided that the following conditions 843412Snewton * are met: 943412Snewton * 1. Redistributions of source code must retain the above copyright 1043412Snewton * notice, this list of conditions and the following disclaimer. 1143412Snewton * 2. Redistributions in binary form must reproduce the above copyright 1243412Snewton * notice, this list of conditions and the following disclaimer in the 1343412Snewton * documentation and/or other materials provided with the distribution. 1443412Snewton * 3. All advertising materials mentioning features or use of this software 1543412Snewton * must display the following acknowledgement: 1643412Snewton * This product includes software developed by Christos Zoulas. 1743412Snewton * 4. The name of the author may not be used to endorse or promote products 1843412Snewton * derived from this software without specific prior written permission. 1943412Snewton * 2043412Snewton * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 2143412Snewton * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 2243412Snewton * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 2343412Snewton * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 2443412Snewton * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 2543412Snewton * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2643412Snewton * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2743412Snewton * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2843412Snewton * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 2943412Snewton * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 3043412Snewton */ 3143412Snewton 3243412Snewton/* 3343412Snewton * In SVR4 unix domain sockets are referenced sometimes 3443412Snewton * (in putmsg(2) for example) as a [device, inode] pair instead of a pathname. 3543412Snewton * Since there is no iname() routine in the kernel, and we need access to 3643412Snewton * a mapping from inode to pathname, we keep our own table. This is a simple 3743412Snewton * linked list that contains the pathname, the [device, inode] pair, the 3843412Snewton * file corresponding to that socket and the process. When the 3943412Snewton * socket gets closed we remove the item from the list. The list gets loaded 4043412Snewton * every time a stat(2) call finds a socket. 4143412Snewton */ 4243412Snewton 43116174Sobrien#include <sys/cdefs.h> 44116174Sobrien__FBSDID("$FreeBSD$"); 45116174Sobrien 4643412Snewton#include <sys/param.h> 4743412Snewton#include <sys/systm.h> 4843412Snewton#include <sys/queue.h> 49160558Sjhb#include <sys/eventhandler.h> 5043412Snewton#include <sys/file.h> 51160558Sjhb#include <sys/kernel.h> 52147819Sjhb#include <sys/lock.h> 53147819Sjhb#include <sys/mutex.h> 5443412Snewton#include <sys/socket.h> 5543412Snewton#include <sys/socketvar.h> 5643412Snewton#include <sys/sysproto.h> 5743412Snewton#include <sys/un.h> 5843412Snewton#include <sys/stat.h> 5943412Snewton#include <sys/proc.h> 6043412Snewton#include <sys/malloc.h> 6143412Snewton 6265302Sobrien#include <compat/svr4/svr4.h> 6365302Sobrien#include <compat/svr4/svr4_types.h> 6465302Sobrien#include <compat/svr4/svr4_util.h> 6565302Sobrien#include <compat/svr4/svr4_socket.h> 6665302Sobrien#include <compat/svr4/svr4_signal.h> 6765302Sobrien#include <compat/svr4/svr4_sockmod.h> 6865302Sobrien#include <compat/svr4/svr4_proto.h> 69299215Sdchagin#include <compat/svr4/svr4_stropts.h> 7043412Snewton 71160558Sjhbstruct svr4_sockcache_entry { 72160558Sjhb struct proc *p; /* Process for the socket */ 73160558Sjhb void *cookie; /* Internal cookie used for matching */ 74160558Sjhb struct sockaddr_un sock;/* Pathname for the socket */ 75160558Sjhb dev_t dev; /* Device where the socket lives on */ 76160558Sjhb ino_t ino; /* Inode where the socket lives on */ 77160558Sjhb TAILQ_ENTRY(svr4_sockcache_entry) entries; 78160558Sjhb}; 79160558Sjhb 80160558Sjhbstatic TAILQ_HEAD(, svr4_sockcache_entry) svr4_head; 81160558Sjhbstatic struct mtx svr4_sockcache_lock; 82160558Sjhbstatic eventhandler_tag svr4_sockcache_exit_tag, svr4_sockcache_exec_tag; 83160558Sjhb 84160558Sjhbstatic void svr4_purge_sockcache(void *arg, struct proc *p); 85160558Sjhb 86160558Sjhbint 87160558Sjhbsvr4_find_socket(td, fp, dev, ino, saun) 8883366Sjulian struct thread *td; 8943412Snewton struct file *fp; 90130640Sphk dev_t dev; 9143412Snewton ino_t ino; 92160558Sjhb struct sockaddr_un *saun; 9343412Snewton{ 9443412Snewton struct svr4_sockcache_entry *e; 95109153Sdillon void *cookie = ((struct socket *)fp->f_data)->so_emuldata; 9643412Snewton 9783366Sjulian DPRINTF(("svr4_find_socket: [%p,%d,%d]: ", td, dev, ino)); 98160558Sjhb mtx_lock(&svr4_sockcache_lock); 9971449Sjhb TAILQ_FOREACH(e, &svr4_head, entries) 10083366Sjulian if (e->p == td->td_proc && e->dev == dev && e->ino == ino) { 10143412Snewton#ifdef DIAGNOSTIC 10243412Snewton if (e->cookie != NULL && e->cookie != cookie) 10343412Snewton panic("svr4 socket cookie mismatch"); 10443412Snewton#endif 10543412Snewton e->cookie = cookie; 10643412Snewton DPRINTF(("%s\n", e->sock.sun_path)); 107160558Sjhb *saun = e->sock; 108160558Sjhb mtx_unlock(&svr4_sockcache_lock); 109160558Sjhb return (0); 11043412Snewton } 11143412Snewton 112160558Sjhb mtx_unlock(&svr4_sockcache_lock); 11343412Snewton DPRINTF(("not found\n")); 114160558Sjhb return (ENOENT); 11543412Snewton} 11643412Snewton 11743412Snewtonint 11883366Sjuliansvr4_add_socket(td, path, st) 11983366Sjulian struct thread *td; 12043412Snewton const char *path; 12143412Snewton struct stat *st; 12243412Snewton{ 12343412Snewton struct svr4_sockcache_entry *e; 124193015Sdelphij size_t len; 125193015Sdelphij int error; 12643412Snewton 127111119Simp e = malloc(sizeof(*e), M_TEMP, M_WAITOK); 12843412Snewton e->cookie = NULL; 12943412Snewton e->dev = st->st_dev; 13043412Snewton e->ino = st->st_ino; 13183366Sjulian e->p = td->td_proc; 13243412Snewton 13343412Snewton if ((error = copyinstr(path, e->sock.sun_path, 13443412Snewton sizeof(e->sock.sun_path), &len)) != 0) { 13543412Snewton DPRINTF(("svr4_add_socket: copyinstr failed %d\n", error)); 13643412Snewton free(e, M_TEMP); 13743412Snewton return error; 13843412Snewton } 13943412Snewton 14043412Snewton e->sock.sun_family = AF_LOCAL; 14143412Snewton e->sock.sun_len = len; 14243412Snewton 143160558Sjhb mtx_lock(&svr4_sockcache_lock); 14443412Snewton TAILQ_INSERT_HEAD(&svr4_head, e, entries); 145160558Sjhb mtx_unlock(&svr4_sockcache_lock); 14643412Snewton DPRINTF(("svr4_add_socket: %s [%p,%d,%d]\n", e->sock.sun_path, 14783366Sjulian td->td_proc, e->dev, e->ino)); 14843412Snewton return 0; 14943412Snewton} 15043412Snewton 151160558Sjhbvoid 152160558Sjhbsvr4_delete_socket(p, fp) 153160558Sjhb struct proc *p; 154160558Sjhb struct file *fp; 155160558Sjhb{ 156160558Sjhb struct svr4_sockcache_entry *e; 157160558Sjhb void *cookie = ((struct socket *)fp->f_data)->so_emuldata; 15843412Snewton 159160558Sjhb mtx_lock(&svr4_sockcache_lock); 160160558Sjhb TAILQ_FOREACH(e, &svr4_head, entries) 161160558Sjhb if (e->p == p && e->cookie == cookie) { 162160558Sjhb TAILQ_REMOVE(&svr4_head, e, entries); 163160558Sjhb mtx_unlock(&svr4_sockcache_lock); 164160558Sjhb DPRINTF(("svr4_delete_socket: %s [%p,%d,%d]\n", 165160558Sjhb e->sock.sun_path, p, (int)e->dev, e->ino)); 166160558Sjhb free(e, M_TEMP); 167160558Sjhb return; 168160558Sjhb } 169160558Sjhb mtx_unlock(&svr4_sockcache_lock); 170160558Sjhb} 171160558Sjhb 172299215Sdchaginstruct svr4_strm * 173299215Sdchaginsvr4_stream_get(fp) 174299215Sdchagin struct file *fp; 175299215Sdchagin{ 176299215Sdchagin struct socket *so; 177299215Sdchagin 178299215Sdchagin if (fp == NULL || fp->f_type != DTYPE_SOCKET) 179299215Sdchagin return NULL; 180299215Sdchagin 181299215Sdchagin so = fp->f_data; 182299215Sdchagin return so->so_emuldata; 183299215Sdchagin} 184299215Sdchagin 185160558Sjhbvoid 186160558Sjhbsvr4_purge_sockcache(arg, p) 187160558Sjhb void *arg; 188160558Sjhb struct proc *p; 189160558Sjhb{ 190160558Sjhb struct svr4_sockcache_entry *e, *ne; 191160558Sjhb 192160558Sjhb mtx_lock(&svr4_sockcache_lock); 193160558Sjhb TAILQ_FOREACH_SAFE(e, &svr4_head, entries, ne) { 194160558Sjhb if (e->p == p) { 195160558Sjhb TAILQ_REMOVE(&svr4_head, e, entries); 196160558Sjhb DPRINTF(("svr4_purge_sockcache: %s [%p,%d,%d]\n", 197160558Sjhb e->sock.sun_path, p, (int)e->dev, e->ino)); 198160558Sjhb free(e, M_TEMP); 199160558Sjhb } 200160558Sjhb } 201160558Sjhb mtx_unlock(&svr4_sockcache_lock); 202160558Sjhb} 203160558Sjhb 204160558Sjhbvoid 205160558Sjhbsvr4_sockcache_init(void) 206160558Sjhb{ 207160558Sjhb 208160558Sjhb TAILQ_INIT(&svr4_head); 209160558Sjhb mtx_init(&svr4_sockcache_lock, "svr4 socket cache", NULL, MTX_DEF); 210160558Sjhb svr4_sockcache_exit_tag = EVENTHANDLER_REGISTER(process_exit, 211160558Sjhb svr4_purge_sockcache, NULL, EVENTHANDLER_PRI_ANY); 212160558Sjhb svr4_sockcache_exec_tag = EVENTHANDLER_REGISTER(process_exec, 213160558Sjhb svr4_purge_sockcache, NULL, EVENTHANDLER_PRI_ANY); 214160558Sjhb} 215160558Sjhb 216160558Sjhbvoid 217160558Sjhbsvr4_sockcache_destroy(void) 218160558Sjhb{ 219160558Sjhb 220160558Sjhb KASSERT(TAILQ_EMPTY(&svr4_head), 221160558Sjhb ("%s: sockcache entries still around", __func__)); 222160558Sjhb EVENTHANDLER_DEREGISTER(process_exec, svr4_sockcache_exec_tag); 223160558Sjhb EVENTHANDLER_DEREGISTER(process_exit, svr4_sockcache_exit_tag); 224160558Sjhb mtx_destroy(&svr4_sockcache_lock); 225160558Sjhb} 226160558Sjhb 22743412Snewtonint 22883366Sjuliansvr4_sys_socket(td, uap) 22983366Sjulian struct thread *td; 23043412Snewton struct svr4_sys_socket_args *uap; 23143412Snewton{ 232107849Salfred switch (uap->type) { 23343412Snewton case SVR4_SOCK_DGRAM: 234107849Salfred uap->type = SOCK_DGRAM; 23543412Snewton break; 23643412Snewton 23743412Snewton case SVR4_SOCK_STREAM: 238107849Salfred uap->type = SOCK_STREAM; 23943412Snewton break; 24043412Snewton 24143412Snewton case SVR4_SOCK_RAW: 242107849Salfred uap->type = SOCK_RAW; 24343412Snewton break; 24443412Snewton 24543412Snewton case SVR4_SOCK_RDM: 246107849Salfred uap->type = SOCK_RDM; 24743412Snewton break; 24843412Snewton 24943412Snewton case SVR4_SOCK_SEQPACKET: 250107849Salfred uap->type = SOCK_SEQPACKET; 25143412Snewton break; 25243412Snewton default: 25343412Snewton return EINVAL; 25443412Snewton } 255225617Skmacy return sys_socket(td, (struct socket_args *)uap); 25643412Snewton} 257