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>
6943412Snewton
70160558Sjhbstruct svr4_sockcache_entry {
71160558Sjhb	struct proc *p;		/* Process for the socket		*/
72160558Sjhb	void *cookie;		/* Internal cookie used for matching	*/
73160558Sjhb	struct sockaddr_un sock;/* Pathname for the socket		*/
74160558Sjhb	dev_t dev;		/* Device where the socket lives on	*/
75160558Sjhb	ino_t ino;		/* Inode where the socket lives on	*/
76160558Sjhb	TAILQ_ENTRY(svr4_sockcache_entry) entries;
77160558Sjhb};
78160558Sjhb
79160558Sjhbstatic TAILQ_HEAD(, svr4_sockcache_entry) svr4_head;
80160558Sjhbstatic struct mtx svr4_sockcache_lock;
81160558Sjhbstatic eventhandler_tag svr4_sockcache_exit_tag, svr4_sockcache_exec_tag;
82160558Sjhb
83160558Sjhbstatic void	svr4_purge_sockcache(void *arg, struct proc *p);
84160558Sjhb
85160558Sjhbint
86160558Sjhbsvr4_find_socket(td, fp, dev, ino, saun)
8783366Sjulian	struct thread *td;
8843412Snewton	struct file *fp;
89130640Sphk	dev_t dev;
9043412Snewton	ino_t ino;
91160558Sjhb	struct sockaddr_un *saun;
9243412Snewton{
9343412Snewton	struct svr4_sockcache_entry *e;
94109153Sdillon	void *cookie = ((struct socket *)fp->f_data)->so_emuldata;
9543412Snewton
9683366Sjulian	DPRINTF(("svr4_find_socket: [%p,%d,%d]: ", td, dev, ino));
97160558Sjhb	mtx_lock(&svr4_sockcache_lock);
9871449Sjhb	TAILQ_FOREACH(e, &svr4_head, entries)
9983366Sjulian		if (e->p == td->td_proc && e->dev == dev && e->ino == ino) {
10043412Snewton#ifdef DIAGNOSTIC
10143412Snewton			if (e->cookie != NULL && e->cookie != cookie)
10243412Snewton				panic("svr4 socket cookie mismatch");
10343412Snewton#endif
10443412Snewton			e->cookie = cookie;
10543412Snewton			DPRINTF(("%s\n", e->sock.sun_path));
106160558Sjhb			*saun = e->sock;
107160558Sjhb			mtx_unlock(&svr4_sockcache_lock);
108160558Sjhb			return (0);
10943412Snewton		}
11043412Snewton
111160558Sjhb	mtx_unlock(&svr4_sockcache_lock);
11243412Snewton	DPRINTF(("not found\n"));
113160558Sjhb	return (ENOENT);
11443412Snewton}
11543412Snewton
11643412Snewtonint
11783366Sjuliansvr4_add_socket(td, path, st)
11883366Sjulian	struct thread *td;
11943412Snewton	const char *path;
12043412Snewton	struct stat *st;
12143412Snewton{
12243412Snewton	struct svr4_sockcache_entry *e;
123193015Sdelphij	size_t len;
124193015Sdelphij	int error;
12543412Snewton
126111119Simp	e = malloc(sizeof(*e), M_TEMP, M_WAITOK);
12743412Snewton	e->cookie = NULL;
12843412Snewton	e->dev = st->st_dev;
12943412Snewton	e->ino = st->st_ino;
13083366Sjulian	e->p = td->td_proc;
13143412Snewton
13243412Snewton	if ((error = copyinstr(path, e->sock.sun_path,
13343412Snewton	    sizeof(e->sock.sun_path), &len)) != 0) {
13443412Snewton		DPRINTF(("svr4_add_socket: copyinstr failed %d\n", error));
13543412Snewton		free(e, M_TEMP);
13643412Snewton		return error;
13743412Snewton	}
13843412Snewton
13943412Snewton	e->sock.sun_family = AF_LOCAL;
14043412Snewton	e->sock.sun_len = len;
14143412Snewton
142160558Sjhb	mtx_lock(&svr4_sockcache_lock);
14343412Snewton	TAILQ_INSERT_HEAD(&svr4_head, e, entries);
144160558Sjhb	mtx_unlock(&svr4_sockcache_lock);
14543412Snewton	DPRINTF(("svr4_add_socket: %s [%p,%d,%d]\n", e->sock.sun_path,
14683366Sjulian		 td->td_proc, e->dev, e->ino));
14743412Snewton	return 0;
14843412Snewton}
14943412Snewton
150160558Sjhbvoid
151160558Sjhbsvr4_delete_socket(p, fp)
152160558Sjhb	struct proc *p;
153160558Sjhb	struct file *fp;
154160558Sjhb{
155160558Sjhb	struct svr4_sockcache_entry *e;
156160558Sjhb	void *cookie = ((struct socket *)fp->f_data)->so_emuldata;
15743412Snewton
158160558Sjhb	mtx_lock(&svr4_sockcache_lock);
159160558Sjhb	TAILQ_FOREACH(e, &svr4_head, entries)
160160558Sjhb		if (e->p == p && e->cookie == cookie) {
161160558Sjhb			TAILQ_REMOVE(&svr4_head, e, entries);
162160558Sjhb			mtx_unlock(&svr4_sockcache_lock);
163160558Sjhb			DPRINTF(("svr4_delete_socket: %s [%p,%d,%d]\n",
164160558Sjhb				 e->sock.sun_path, p, (int)e->dev, e->ino));
165160558Sjhb			free(e, M_TEMP);
166160558Sjhb			return;
167160558Sjhb		}
168160558Sjhb	mtx_unlock(&svr4_sockcache_lock);
169160558Sjhb}
170160558Sjhb
171160558Sjhbvoid
172160558Sjhbsvr4_purge_sockcache(arg, p)
173160558Sjhb	void *arg;
174160558Sjhb	struct proc *p;
175160558Sjhb{
176160558Sjhb	struct svr4_sockcache_entry *e, *ne;
177160558Sjhb
178160558Sjhb	mtx_lock(&svr4_sockcache_lock);
179160558Sjhb	TAILQ_FOREACH_SAFE(e, &svr4_head, entries, ne) {
180160558Sjhb		if (e->p == p) {
181160558Sjhb			TAILQ_REMOVE(&svr4_head, e, entries);
182160558Sjhb			DPRINTF(("svr4_purge_sockcache: %s [%p,%d,%d]\n",
183160558Sjhb				 e->sock.sun_path, p, (int)e->dev, e->ino));
184160558Sjhb			free(e, M_TEMP);
185160558Sjhb		}
186160558Sjhb	}
187160558Sjhb	mtx_unlock(&svr4_sockcache_lock);
188160558Sjhb}
189160558Sjhb
190160558Sjhbvoid
191160558Sjhbsvr4_sockcache_init(void)
192160558Sjhb{
193160558Sjhb
194160558Sjhb	TAILQ_INIT(&svr4_head);
195160558Sjhb	mtx_init(&svr4_sockcache_lock, "svr4 socket cache", NULL, MTX_DEF);
196160558Sjhb	svr4_sockcache_exit_tag = EVENTHANDLER_REGISTER(process_exit,
197160558Sjhb	    svr4_purge_sockcache, NULL, EVENTHANDLER_PRI_ANY);
198160558Sjhb	svr4_sockcache_exec_tag = EVENTHANDLER_REGISTER(process_exec,
199160558Sjhb	    svr4_purge_sockcache, NULL, EVENTHANDLER_PRI_ANY);
200160558Sjhb}
201160558Sjhb
202160558Sjhbvoid
203160558Sjhbsvr4_sockcache_destroy(void)
204160558Sjhb{
205160558Sjhb
206160558Sjhb	KASSERT(TAILQ_EMPTY(&svr4_head),
207160558Sjhb	    ("%s: sockcache entries still around", __func__));
208160558Sjhb	EVENTHANDLER_DEREGISTER(process_exec, svr4_sockcache_exec_tag);
209160558Sjhb	EVENTHANDLER_DEREGISTER(process_exit, svr4_sockcache_exit_tag);
210160558Sjhb	mtx_destroy(&svr4_sockcache_lock);
211160558Sjhb}
212160558Sjhb
21343412Snewtonint
21483366Sjuliansvr4_sys_socket(td, uap)
21583366Sjulian	struct thread *td;
21643412Snewton	struct svr4_sys_socket_args *uap;
21743412Snewton{
218107849Salfred	switch (uap->type) {
21943412Snewton	case SVR4_SOCK_DGRAM:
220107849Salfred		uap->type = SOCK_DGRAM;
22143412Snewton		break;
22243412Snewton
22343412Snewton	case SVR4_SOCK_STREAM:
224107849Salfred		uap->type = SOCK_STREAM;
22543412Snewton		break;
22643412Snewton
22743412Snewton	case SVR4_SOCK_RAW:
228107849Salfred		uap->type = SOCK_RAW;
22943412Snewton		break;
23043412Snewton
23143412Snewton	case SVR4_SOCK_RDM:
232107849Salfred		uap->type = SOCK_RDM;
23343412Snewton		break;
23443412Snewton
23543412Snewton	case SVR4_SOCK_SEQPACKET:
236107849Salfred		uap->type = SOCK_SEQPACKET;
23743412Snewton		break;
23843412Snewton	default:
23943412Snewton		return EINVAL;
24043412Snewton	}
241225617Skmacy	return sys_socket(td, (struct socket_args *)uap);
24243412Snewton}
243