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>
69298519Sdchagin#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
97275856Sgleb	DPRINTF(("svr4_find_socket: [%p,%ju,%ju]: ", td, (uintmax_t)dev,
98275856Sgleb	    (uintmax_t)ino));
99160558Sjhb	mtx_lock(&svr4_sockcache_lock);
10071449Sjhb	TAILQ_FOREACH(e, &svr4_head, entries)
10183366Sjulian		if (e->p == td->td_proc && e->dev == dev && e->ino == ino) {
10243412Snewton#ifdef DIAGNOSTIC
10343412Snewton			if (e->cookie != NULL && e->cookie != cookie)
10443412Snewton				panic("svr4 socket cookie mismatch");
10543412Snewton#endif
10643412Snewton			e->cookie = cookie;
10743412Snewton			DPRINTF(("%s\n", e->sock.sun_path));
108160558Sjhb			*saun = e->sock;
109160558Sjhb			mtx_unlock(&svr4_sockcache_lock);
110160558Sjhb			return (0);
11143412Snewton		}
11243412Snewton
113160558Sjhb	mtx_unlock(&svr4_sockcache_lock);
11443412Snewton	DPRINTF(("not found\n"));
115160558Sjhb	return (ENOENT);
11643412Snewton}
11743412Snewton
11843412Snewtonint
11983366Sjuliansvr4_add_socket(td, path, st)
12083366Sjulian	struct thread *td;
12143412Snewton	const char *path;
12243412Snewton	struct stat *st;
12343412Snewton{
12443412Snewton	struct svr4_sockcache_entry *e;
125193015Sdelphij	size_t len;
126193015Sdelphij	int error;
12743412Snewton
128111119Simp	e = malloc(sizeof(*e), M_TEMP, M_WAITOK);
12943412Snewton	e->cookie = NULL;
13043412Snewton	e->dev = st->st_dev;
13143412Snewton	e->ino = st->st_ino;
13283366Sjulian	e->p = td->td_proc;
13343412Snewton
13443412Snewton	if ((error = copyinstr(path, e->sock.sun_path,
13543412Snewton	    sizeof(e->sock.sun_path), &len)) != 0) {
13643412Snewton		DPRINTF(("svr4_add_socket: copyinstr failed %d\n", error));
13743412Snewton		free(e, M_TEMP);
13843412Snewton		return error;
13943412Snewton	}
14043412Snewton
14143412Snewton	e->sock.sun_family = AF_LOCAL;
14243412Snewton	e->sock.sun_len = len;
14343412Snewton
144160558Sjhb	mtx_lock(&svr4_sockcache_lock);
14543412Snewton	TAILQ_INSERT_HEAD(&svr4_head, e, entries);
146160558Sjhb	mtx_unlock(&svr4_sockcache_lock);
147275856Sgleb	DPRINTF(("svr4_add_socket: %s [%p,%ju,%ju]\n", e->sock.sun_path,
148275856Sgleb		 td->td_proc, (uintmax_t)e->dev, (uintmax_t)e->ino));
14943412Snewton	return 0;
15043412Snewton}
15143412Snewton
152160558Sjhbvoid
153160558Sjhbsvr4_delete_socket(p, fp)
154160558Sjhb	struct proc *p;
155160558Sjhb	struct file *fp;
156160558Sjhb{
157160558Sjhb	struct svr4_sockcache_entry *e;
158160558Sjhb	void *cookie = ((struct socket *)fp->f_data)->so_emuldata;
15943412Snewton
160160558Sjhb	mtx_lock(&svr4_sockcache_lock);
161160558Sjhb	TAILQ_FOREACH(e, &svr4_head, entries)
162160558Sjhb		if (e->p == p && e->cookie == cookie) {
163160558Sjhb			TAILQ_REMOVE(&svr4_head, e, entries);
164160558Sjhb			mtx_unlock(&svr4_sockcache_lock);
165275856Sgleb			DPRINTF(("svr4_delete_socket: %s [%p,%ju,%ju]\n",
166275856Sgleb				 e->sock.sun_path, p, (uintmax_t)e->dev,
167275856Sgleb				 (uintmax_t)e->ino));
168160558Sjhb			free(e, M_TEMP);
169160558Sjhb			return;
170160558Sjhb		}
171160558Sjhb	mtx_unlock(&svr4_sockcache_lock);
172160558Sjhb}
173160558Sjhb
174298519Sdchaginstruct svr4_strm *
175298519Sdchaginsvr4_stream_get(fp)
176298519Sdchagin	struct file *fp;
177298519Sdchagin{
178298519Sdchagin	struct socket *so;
179298519Sdchagin
180298519Sdchagin	if (fp == NULL || fp->f_type != DTYPE_SOCKET)
181298519Sdchagin		return NULL;
182298519Sdchagin
183298519Sdchagin	so = fp->f_data;
184298519Sdchagin	return so->so_emuldata;
185298519Sdchagin}
186298519Sdchagin
187160558Sjhbvoid
188160558Sjhbsvr4_purge_sockcache(arg, p)
189160558Sjhb	void *arg;
190160558Sjhb	struct proc *p;
191160558Sjhb{
192160558Sjhb	struct svr4_sockcache_entry *e, *ne;
193160558Sjhb
194160558Sjhb	mtx_lock(&svr4_sockcache_lock);
195160558Sjhb	TAILQ_FOREACH_SAFE(e, &svr4_head, entries, ne) {
196160558Sjhb		if (e->p == p) {
197160558Sjhb			TAILQ_REMOVE(&svr4_head, e, entries);
198275856Sgleb			DPRINTF(("svr4_purge_sockcache: %s [%p,%ju,%ju]\n",
199275856Sgleb				 e->sock.sun_path, p, (uintmax_t)e->dev,
200275856Sgleb				 (uintmax_t)e->ino));
201160558Sjhb			free(e, M_TEMP);
202160558Sjhb		}
203160558Sjhb	}
204160558Sjhb	mtx_unlock(&svr4_sockcache_lock);
205160558Sjhb}
206160558Sjhb
207160558Sjhbvoid
208160558Sjhbsvr4_sockcache_init(void)
209160558Sjhb{
210160558Sjhb
211160558Sjhb	TAILQ_INIT(&svr4_head);
212160558Sjhb	mtx_init(&svr4_sockcache_lock, "svr4 socket cache", NULL, MTX_DEF);
213160558Sjhb	svr4_sockcache_exit_tag = EVENTHANDLER_REGISTER(process_exit,
214160558Sjhb	    svr4_purge_sockcache, NULL, EVENTHANDLER_PRI_ANY);
215160558Sjhb	svr4_sockcache_exec_tag = EVENTHANDLER_REGISTER(process_exec,
216160558Sjhb	    svr4_purge_sockcache, NULL, EVENTHANDLER_PRI_ANY);
217160558Sjhb}
218160558Sjhb
219160558Sjhbvoid
220160558Sjhbsvr4_sockcache_destroy(void)
221160558Sjhb{
222160558Sjhb
223160558Sjhb	KASSERT(TAILQ_EMPTY(&svr4_head),
224160558Sjhb	    ("%s: sockcache entries still around", __func__));
225160558Sjhb	EVENTHANDLER_DEREGISTER(process_exec, svr4_sockcache_exec_tag);
226160558Sjhb	EVENTHANDLER_DEREGISTER(process_exit, svr4_sockcache_exit_tag);
227160558Sjhb	mtx_destroy(&svr4_sockcache_lock);
228160558Sjhb}
229160558Sjhb
23043412Snewtonint
23183366Sjuliansvr4_sys_socket(td, uap)
23283366Sjulian	struct thread *td;
23343412Snewton	struct svr4_sys_socket_args *uap;
23443412Snewton{
235107849Salfred	switch (uap->type) {
23643412Snewton	case SVR4_SOCK_DGRAM:
237107849Salfred		uap->type = SOCK_DGRAM;
23843412Snewton		break;
23943412Snewton
24043412Snewton	case SVR4_SOCK_STREAM:
241107849Salfred		uap->type = SOCK_STREAM;
24243412Snewton		break;
24343412Snewton
24443412Snewton	case SVR4_SOCK_RAW:
245107849Salfred		uap->type = SOCK_RAW;
24643412Snewton		break;
24743412Snewton
24843412Snewton	case SVR4_SOCK_RDM:
249107849Salfred		uap->type = SOCK_RDM;
25043412Snewton		break;
25143412Snewton
25243412Snewton	case SVR4_SOCK_SEQPACKET:
253107849Salfred		uap->type = SOCK_SEQPACKET;
25443412Snewton		break;
25543412Snewton	default:
25643412Snewton		return EINVAL;
25743412Snewton	}
258225617Skmacy	return sys_socket(td, (struct socket_args *)uap);
25943412Snewton}
260