1/*
2 * Copyright (c) 1995-2007 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28/*
29 *  Change Log:
30 *    Created February 20, 1995 by Tuyen Nguyen
31 *    Modified for MP, 1996 by Tuyen Nguyen
32 *    Modified, March 17, 1997 by Tuyen Nguyen for MacOSX.
33 */
34#include <sys/errno.h>
35#include <sys/types.h>
36#include <sys/param.h>
37#include <machine/spl.h>
38#include <sys/systm.h>
39#include <sys/kernel.h>
40#include <sys/proc_internal.h>	/* for p_fd in fdflags */
41#include <sys/filedesc.h>
42#include <sys/fcntl.h>
43#include <sys/mbuf.h>
44#include <sys/malloc.h>
45#include <sys/file_internal.h>
46#include <sys/socket.h>
47#include <sys/socketvar.h>
48#include <sys/sysproto.h>
49#include <sys/kdebug.h>
50#include <net/if_var.h>
51
52#include <netat/sysglue.h>
53#include <netat/appletalk.h>
54#include <netat/at_pcb.h>
55#include <netat/at_var.h>
56#include <netat/debug.h>
57
58int falloc_locked(proc_t, struct fileproc **, int *, vfs_context_t, int);
59
60extern at_ifaddr_t *ifID_home;	/* default interface */
61extern lck_mtx_t * atalk_mutex;
62
63#define f_flag f_fglob->fg_flag
64#define f_type f_fglob->fg_type
65#define f_msgcount f_fglob->fg_msgcount
66#define f_cred f_fglob->fg_cred
67#define f_ops f_fglob->fg_ops
68#define f_offset f_fglob->fg_offset
69#define f_data f_fglob->fg_data
70
71
72int _ATkqfilter(struct fileproc *, struct knote *, vfs_context_t);
73int _ATselect(struct fileproc *, int, void *, vfs_context_t);
74int _ATioctl(struct fileproc *, u_long, caddr_t, vfs_context_t);
75int _ATwrite(struct fileproc *, struct uio *, int, vfs_context_t);
76int _ATread(struct fileproc *, struct uio *, int, vfs_context_t);
77int _ATclose(struct fileglob *, vfs_context_t);
78
79int ATsocket(proc, uap, retval)
80	struct proc *proc;
81	struct ATsocket_args *uap;
82	int *retval;
83{
84	int err;
85	atalk_lock();
86	if (1 /* _ATsocket*/) {
87		/* required check for all AppleTalk system calls */
88		if (!(at_state.flags & AT_ST_STARTED) || !ifID_home) {
89			*retval = -1;
90			err = ENOTREADY;
91		} else {
92			*retval = _ATsocket((int)uap->proto, (int *)&err, (void *)proc);
93		}
94	} else {
95		*retval = -1;
96		err = ENXIO;
97	}
98	atalk_unlock();
99	return err;
100}
101
102int ATgetmsg(proc, uap, retval)
103	struct proc *proc;
104	struct ATgetmsg_args *uap;
105	int *retval;
106{
107	int err;
108
109	atalk_lock();
110	if (1 /* _ATgetmsg */) {
111		/* required check for all AppleTalk system calls */
112		if (!(at_state.flags & AT_ST_STARTED) || !ifID_home) {
113			*retval = -1;
114			err = ENOTREADY;
115		} else {
116			*retval =
117			  (*_ATgetmsg)(uap->fd, uap->ctlptr, uap->datptr,
118					  uap->flags, &err, proc);
119		}
120	} else {
121		*retval = -1;
122		err = ENXIO;
123	}
124	atalk_unlock();
125	return err;
126}
127
128int ATputmsg(proc, uap, retval)
129	struct proc *proc;
130	struct ATputmsg_args *uap;
131	int *retval;
132{
133	int err;
134
135	atalk_lock();
136	if (1 /* _ATputmsg */) {
137		/* required check for all AppleTalk system calls */
138		if (!(at_state.flags & AT_ST_STARTED) || !ifID_home) {
139			*retval = -1;
140			err = ENOTREADY;
141		} else {
142			*retval =
143			  _ATputmsg(uap->fd, uap->ctlptr, uap->datptr,
144					  uap->flags, &err, proc);
145		}
146	} else {
147		*retval = -1;
148		err = ENXIO;
149	}
150	atalk_unlock();
151	return err;
152}
153
154int ATPsndreq(proc, uap, retval)
155	struct proc *proc;
156	struct ATPsndreq_args *uap;
157	int *retval;
158{
159	int err;
160
161	atalk_lock();
162	if (1 /* _ATPsndreq */) {
163		/* required check for all AppleTalk system calls */
164		if (!(at_state.flags & AT_ST_STARTED) || !ifID_home) {
165			*retval = -1;
166			err = ENOTREADY;
167		} else {
168			*retval =
169			  _ATPsndreq(uap->fd, uap->buf, uap->len,
170					   uap->nowait, &err, proc);
171		}
172	} else {
173		*retval = -1;
174		err= ENXIO;
175	}
176	atalk_unlock();
177	return err;
178}
179
180int ATPsndrsp(proc, uap, retval)
181	struct proc *proc;
182	struct ATPsndrsp_args *uap;
183	int *retval;
184{
185	int err;
186
187	atalk_lock();
188	if (1 /*_ATPsndrsp*/) {
189		/* required check for all AppleTalk system calls */
190		if (!(at_state.flags & AT_ST_STARTED) || !ifID_home) {
191			*retval = -1;
192			err = ENOTREADY;
193		} else {
194			*retval =
195			  _ATPsndrsp(uap->fd, uap->respbuff,
196					   uap->resplen, uap->datalen, &err, proc);
197		}
198	} else {
199		*retval = -1;
200		err = ENXIO;
201	}
202	atalk_unlock();
203	return err;
204}
205
206int ATPgetreq(proc, uap, retval)
207	struct proc *proc;
208	struct ATPgetreq_args *uap;
209	int *retval;
210{
211	int err;
212
213	atalk_lock();
214	if (1 /* _ATPgetreq */) {
215		/* required check for all AppleTalk system calls */
216		if (!(at_state.flags & AT_ST_STARTED) || !ifID_home) {
217			*retval = -1;
218			err = ENOTREADY;
219		} else {
220			*retval =
221			  _ATPgetreq(uap->fd, uap->buf, uap->buflen,
222					   &err, proc);
223		}
224	} else {
225		*retval = -1;
226		err = ENXIO;
227	}
228	atalk_unlock();
229	return err;
230}
231
232int ATPgetrsp(proc, uap, retval)
233	struct proc *proc;
234	struct ATPgetrsp_args *uap;
235	int *retval;
236{
237	int err = 0;
238
239	atalk_lock();
240	if (1 /*_ATPgetrsp*/) {
241		/* required check for all AppleTalk system calls */
242		if (!(at_state.flags & AT_ST_STARTED) || !ifID_home) {
243			*retval = -1;
244			err = ENOTREADY;
245		} else {
246			*retval =
247			  _ATPgetrsp(uap->fd, (struct atpBDS *)uap->bdsp, &err, proc);
248		}
249	} else {
250		*retval = -1;
251		err = ENXIO;
252	}
253	atalk_unlock();
254	return err;
255}
256
257int atalk_closeref(fg, grefp)
258	struct fileglob *fg;
259	gref_t **grefp;
260{
261	if ((*grefp = (gref_t *)fg->fg_data)) {
262		fg->fg_data = 0;
263		return(0);
264	}
265	return(EBADF);
266}
267
268int atalk_openref(gref, retfd, proc)
269	gref_t *gref;
270	int *retfd;
271	struct proc *proc;
272{
273	static struct fileops fileops =
274		{_ATread, _ATwrite, _ATioctl, _ATselect, _ATclose, _ATkqfilter, 0};
275	int err, fd;
276	struct fileproc *fp;
277
278	lck_mtx_assert(atalk_mutex, LCK_MTX_ASSERT_OWNED);
279
280	proc_fdlock(proc);
281	if ((err = falloc_locked(proc, &fp, &fd, vfs_context_current(), 1)) != 0) {
282		proc_fdunlock(proc);
283		return err;
284	}
285
286	fp->f_flag = FREAD|FWRITE;
287	/*##### LD 5/7/96 Warning: we don't have a "DTYPE_OTHER" for
288	 * MacOSX, so defines DTYPE_ATALK as DTYPE_SOCKET...
289	 */
290	fp->f_type = DTYPE_ATALK+1;
291	fp->f_ops = &fileops;
292	fp->f_data = (void *)gref;
293
294	procfdtbl_releasefd(proc, fd, NULL);
295	*retfd = fd;
296	fp_drop(proc, fd, fp, 1);
297	proc_fdunlock(proc);
298/*
299	kprintf("atalk_openref: fp = 0x%x, gref = 0x%x\n", (u_int)fp, (u_int)gref);
300*/
301	return 0;
302}
303
304/*
305 * go from file descriptor to gref, which has been saved in fp->f_data
306 *
307 * This routine returns with an iocount on the fileproc when the fp is null
308 * as it converts fd to fileproc. Callers of this api who pass fp as null
309 * need to drop the iocount when they are done with the fp
310 */
311int atalk_getref(fp, fd, grefp, proc, droponerr)
312struct fileproc *fp;
313int fd;
314gref_t **grefp;
315struct proc *proc;
316int droponerr;
317{
318	int error;
319
320	proc_fdlock(proc);
321	error = atalk_getref_locked(fp, fd, grefp, proc, droponerr);
322	proc_fdunlock(proc);
323	return error;
324}
325
326int atalk_getref_locked(fp, fd, grefp, proc, droponerr)
327struct fileproc *fp;
328int fd;
329gref_t **grefp;
330struct proc *proc;
331int droponerr;
332{
333	lck_mtx_assert(atalk_mutex, LCK_MTX_ASSERT_OWNED);
334	if (fp == 0) {
335		int error = fp_lookup(proc, fd, &fp, 1);
336
337		if (error) {
338
339			*grefp = (gref_t *) 0;
340		   return EBADF;
341		}
342	}
343	*grefp = (gref_t *)fp->f_data;
344	if (fp->f_type != (DTYPE_ATALK+1) || *grefp == 0 || *grefp == (gref_t *)(-1)) {
345		if (droponerr)
346			fp_drop(proc, fd, fp, 1);
347		printf("atalk_getref_locked EBADF f_data: %p\n", fp->f_data);
348		return EBADF;
349	}
350
351	if ((*grefp)->errno) {
352		if (droponerr)
353			fp_drop(proc, fd, fp, 1);
354		return (int)(*grefp)->errno;
355	}
356	return 0;
357}
358