1/*-
2 * Copyright (c) 1998 Mark Newton
3 * Copyright (c) 1996 Christos Zoulas.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 *    must display the following acknowledgement:
16 *	This product includes software developed by Christos Zoulas.
17 * 4. The name of the author may not be used to endorse or promote products
18 *    derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32/*
33 * In SVR4 unix domain sockets are referenced sometimes
34 * (in putmsg(2) for example) as a [device, inode] pair instead of a pathname.
35 * Since there is no iname() routine in the kernel, and we need access to
36 * a mapping from inode to pathname, we keep our own table. This is a simple
37 * linked list that contains the pathname, the [device, inode] pair, the
38 * file corresponding to that socket and the process. When the
39 * socket gets closed we remove the item from the list. The list gets loaded
40 * every time a stat(2) call finds a socket.
41 */
42
43#include <sys/cdefs.h>
44__FBSDID("$FreeBSD$");
45
46#include <sys/param.h>
47#include <sys/systm.h>
48#include <sys/queue.h>
49#include <sys/eventhandler.h>
50#include <sys/file.h>
51#include <sys/kernel.h>
52#include <sys/lock.h>
53#include <sys/mutex.h>
54#include <sys/socket.h>
55#include <sys/socketvar.h>
56#include <sys/sysproto.h>
57#include <sys/un.h>
58#include <sys/stat.h>
59#include <sys/proc.h>
60#include <sys/malloc.h>
61
62#include <compat/svr4/svr4.h>
63#include <compat/svr4/svr4_types.h>
64#include <compat/svr4/svr4_util.h>
65#include <compat/svr4/svr4_socket.h>
66#include <compat/svr4/svr4_signal.h>
67#include <compat/svr4/svr4_sockmod.h>
68#include <compat/svr4/svr4_proto.h>
69#include <compat/svr4/svr4_stropts.h>
70
71struct svr4_sockcache_entry {
72	struct proc *p;		/* Process for the socket		*/
73	void *cookie;		/* Internal cookie used for matching	*/
74	struct sockaddr_un sock;/* Pathname for the socket		*/
75	dev_t dev;		/* Device where the socket lives on	*/
76	ino_t ino;		/* Inode where the socket lives on	*/
77	TAILQ_ENTRY(svr4_sockcache_entry) entries;
78};
79
80static TAILQ_HEAD(, svr4_sockcache_entry) svr4_head;
81static struct mtx svr4_sockcache_lock;
82static eventhandler_tag svr4_sockcache_exit_tag, svr4_sockcache_exec_tag;
83
84static void	svr4_purge_sockcache(void *arg, struct proc *p);
85
86int
87svr4_find_socket(td, fp, dev, ino, saun)
88	struct thread *td;
89	struct file *fp;
90	dev_t dev;
91	ino_t ino;
92	struct sockaddr_un *saun;
93{
94	struct svr4_sockcache_entry *e;
95	void *cookie = ((struct socket *)fp->f_data)->so_emuldata;
96
97	DPRINTF(("svr4_find_socket: [%p,%ju,%ju]: ", td, (uintmax_t)dev,
98	    (uintmax_t)ino));
99	mtx_lock(&svr4_sockcache_lock);
100	TAILQ_FOREACH(e, &svr4_head, entries)
101		if (e->p == td->td_proc && e->dev == dev && e->ino == ino) {
102#ifdef DIAGNOSTIC
103			if (e->cookie != NULL && e->cookie != cookie)
104				panic("svr4 socket cookie mismatch");
105#endif
106			e->cookie = cookie;
107			DPRINTF(("%s\n", e->sock.sun_path));
108			*saun = e->sock;
109			mtx_unlock(&svr4_sockcache_lock);
110			return (0);
111		}
112
113	mtx_unlock(&svr4_sockcache_lock);
114	DPRINTF(("not found\n"));
115	return (ENOENT);
116}
117
118int
119svr4_add_socket(td, path, st)
120	struct thread *td;
121	const char *path;
122	struct stat *st;
123{
124	struct svr4_sockcache_entry *e;
125	size_t len;
126	int error;
127
128	e = malloc(sizeof(*e), M_TEMP, M_WAITOK);
129	e->cookie = NULL;
130	e->dev = st->st_dev;
131	e->ino = st->st_ino;
132	e->p = td->td_proc;
133
134	if ((error = copyinstr(path, e->sock.sun_path,
135	    sizeof(e->sock.sun_path), &len)) != 0) {
136		DPRINTF(("svr4_add_socket: copyinstr failed %d\n", error));
137		free(e, M_TEMP);
138		return error;
139	}
140
141	e->sock.sun_family = AF_LOCAL;
142	e->sock.sun_len = len;
143
144	mtx_lock(&svr4_sockcache_lock);
145	TAILQ_INSERT_HEAD(&svr4_head, e, entries);
146	mtx_unlock(&svr4_sockcache_lock);
147	DPRINTF(("svr4_add_socket: %s [%p,%ju,%ju]\n", e->sock.sun_path,
148		 td->td_proc, (uintmax_t)e->dev, (uintmax_t)e->ino));
149	return 0;
150}
151
152void
153svr4_delete_socket(p, fp)
154	struct proc *p;
155	struct file *fp;
156{
157	struct svr4_sockcache_entry *e;
158	void *cookie = ((struct socket *)fp->f_data)->so_emuldata;
159
160	mtx_lock(&svr4_sockcache_lock);
161	TAILQ_FOREACH(e, &svr4_head, entries)
162		if (e->p == p && e->cookie == cookie) {
163			TAILQ_REMOVE(&svr4_head, e, entries);
164			mtx_unlock(&svr4_sockcache_lock);
165			DPRINTF(("svr4_delete_socket: %s [%p,%ju,%ju]\n",
166				 e->sock.sun_path, p, (uintmax_t)e->dev,
167				 (uintmax_t)e->ino));
168			free(e, M_TEMP);
169			return;
170		}
171	mtx_unlock(&svr4_sockcache_lock);
172}
173
174struct svr4_strm *
175svr4_stream_get(fp)
176	struct file *fp;
177{
178	struct socket *so;
179
180	if (fp == NULL || fp->f_type != DTYPE_SOCKET)
181		return NULL;
182
183	so = fp->f_data;
184	return so->so_emuldata;
185}
186
187void
188svr4_purge_sockcache(arg, p)
189	void *arg;
190	struct proc *p;
191{
192	struct svr4_sockcache_entry *e, *ne;
193
194	mtx_lock(&svr4_sockcache_lock);
195	TAILQ_FOREACH_SAFE(e, &svr4_head, entries, ne) {
196		if (e->p == p) {
197			TAILQ_REMOVE(&svr4_head, e, entries);
198			DPRINTF(("svr4_purge_sockcache: %s [%p,%ju,%ju]\n",
199				 e->sock.sun_path, p, (uintmax_t)e->dev,
200				 (uintmax_t)e->ino));
201			free(e, M_TEMP);
202		}
203	}
204	mtx_unlock(&svr4_sockcache_lock);
205}
206
207void
208svr4_sockcache_init(void)
209{
210
211	TAILQ_INIT(&svr4_head);
212	mtx_init(&svr4_sockcache_lock, "svr4 socket cache", NULL, MTX_DEF);
213	svr4_sockcache_exit_tag = EVENTHANDLER_REGISTER(process_exit,
214	    svr4_purge_sockcache, NULL, EVENTHANDLER_PRI_ANY);
215	svr4_sockcache_exec_tag = EVENTHANDLER_REGISTER(process_exec,
216	    svr4_purge_sockcache, NULL, EVENTHANDLER_PRI_ANY);
217}
218
219void
220svr4_sockcache_destroy(void)
221{
222
223	KASSERT(TAILQ_EMPTY(&svr4_head),
224	    ("%s: sockcache entries still around", __func__));
225	EVENTHANDLER_DEREGISTER(process_exec, svr4_sockcache_exec_tag);
226	EVENTHANDLER_DEREGISTER(process_exit, svr4_sockcache_exit_tag);
227	mtx_destroy(&svr4_sockcache_lock);
228}
229
230int
231svr4_sys_socket(td, uap)
232	struct thread *td;
233	struct svr4_sys_socket_args *uap;
234{
235	switch (uap->type) {
236	case SVR4_SOCK_DGRAM:
237		uap->type = SOCK_DGRAM;
238		break;
239
240	case SVR4_SOCK_STREAM:
241		uap->type = SOCK_STREAM;
242		break;
243
244	case SVR4_SOCK_RAW:
245		uap->type = SOCK_RAW;
246		break;
247
248	case SVR4_SOCK_RDM:
249		uap->type = SOCK_RDM;
250		break;
251
252	case SVR4_SOCK_SEQPACKET:
253		uap->type = SOCK_SEQPACKET;
254		break;
255	default:
256		return EINVAL;
257	}
258	return sys_socket(td, (struct socket_args *)uap);
259}
260