]> git.saurik.com Git - apple/xnu.git/blob - bsd/netat/sys_dep.c
4f63e73516c2383d4fbc97fdb88058749afca060
[apple/xnu.git] / bsd / netat / sys_dep.c
1 /*
2 * Copyright (c) 2000 Apple Computer, 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 * Copyright (c) 1995-1998 Apple Computer, Inc.
30 *
31 * Change Log:
32 * Created February 20, 1995 by Tuyen Nguyen
33 * Modified for MP, 1996 by Tuyen Nguyen
34 * Modified, March 17, 1997 by Tuyen Nguyen for MacOSX.
35 */
36 #include <sys/errno.h>
37 #include <sys/types.h>
38 #include <sys/param.h>
39 #include <machine/spl.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/proc_internal.h> /* for p_fd in fdflags */
43 #include <sys/filedesc.h>
44 #include <sys/fcntl.h>
45 #include <sys/mbuf.h>
46 #include <sys/malloc.h>
47 #include <sys/file_internal.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
50 #include <sys/sysproto.h>
51 #include <sys/kdebug.h>
52 #include <net/if_var.h>
53
54 #include <netat/sysglue.h>
55 #include <netat/appletalk.h>
56 #include <netat/at_var.h>
57 #include <netat/at_pcb.h>
58 #include <netat/debug.h>
59
60 int (*sys_ATsocket)() = 0;
61 int (*sys_ATgetmsg)() = 0;
62 int (*sys_ATputmsg)() = 0;
63 int (*sys_ATPsndreq)() = 0;
64 int (*sys_ATPsndrsp)() = 0;
65 int (*sys_ATPgetreq)() = 0;
66 int (*sys_ATPgetrsp)() = 0;
67
68 extern at_state_t at_state; /* global state of AT network */
69 extern at_ifaddr_t *ifID_home; /* default interface */
70 extern lck_mtx_t * atalk_mutex;
71
72 #define f_flag f_fglob->fg_flag
73 #define f_type f_fglob->fg_type
74 #define f_msgcount f_fglob->fg_msgcount
75 #define f_cred f_fglob->fg_cred
76 #define f_ops f_fglob->fg_ops
77 #define f_offset f_fglob->fg_offset
78 #define f_data f_fglob->fg_data
79
80 int ATsocket(proc, uap, retval)
81 struct proc *proc;
82 struct ATsocket_args *uap;
83 int *retval;
84 {
85 int err;
86 atalk_lock();
87 if (sys_ATsocket) {
88 /* required check for all AppleTalk system calls */
89 if (!(at_state.flags & AT_ST_STARTED) || !ifID_home) {
90 *retval = -1;
91 err = ENOTREADY;
92 } else {
93 *retval = (*sys_ATsocket)(uap->proto, &err, proc);
94 }
95 } else {
96 *retval = -1;
97 err = ENXIO;
98 }
99 atalk_unlock();
100 return err;
101 }
102
103 int ATgetmsg(proc, uap, retval)
104 struct proc *proc;
105 struct ATgetmsg_args *uap;
106 int *retval;
107 {
108 int err;
109
110 atalk_lock();
111 if (sys_ATgetmsg) {
112 /* required check for all AppleTalk system calls */
113 if (!(at_state.flags & AT_ST_STARTED) || !ifID_home) {
114 *retval = -1;
115 err = ENOTREADY;
116 } else {
117 *retval =
118 (*sys_ATgetmsg)(uap->fd, uap->ctlptr, uap->datptr,
119 uap->flags, &err, proc);
120 }
121 } else {
122 *retval = -1;
123 err = ENXIO;
124 }
125 atalk_unlock();
126 return err;
127 }
128
129 int ATputmsg(proc, uap, retval)
130 struct proc *proc;
131 struct ATputmsg_args *uap;
132 int *retval;
133 {
134 int err;
135
136 atalk_lock();
137 if (sys_ATputmsg) {
138 /* required check for all AppleTalk system calls */
139 if (!(at_state.flags & AT_ST_STARTED) || !ifID_home) {
140 *retval = -1;
141 err = ENOTREADY;
142 } else {
143 *retval =
144 (*sys_ATputmsg)(uap->fd, uap->ctlptr, uap->datptr,
145 uap->flags, &err, proc);
146 }
147 } else {
148 *retval = -1;
149 err = ENXIO;
150 }
151 atalk_unlock();
152 return err;
153 }
154
155 int ATPsndreq(proc, uap, retval)
156 struct proc *proc;
157 struct ATPsndreq_args *uap;
158 int *retval;
159 {
160 int err;
161
162 atalk_lock();
163 if (sys_ATPsndreq) {
164 /* required check for all AppleTalk system calls */
165 if (!(at_state.flags & AT_ST_STARTED) || !ifID_home) {
166 *retval = -1;
167 err = ENOTREADY;
168 } else {
169 *retval =
170 (*sys_ATPsndreq)(uap->fd, uap->buf, uap->len,
171 uap->nowait, &err, proc);
172 }
173 } else {
174 *retval = -1;
175 err= ENXIO;
176 }
177 atalk_unlock();
178 return err;
179 }
180
181 int ATPsndrsp(proc, uap, retval)
182 struct proc *proc;
183 struct ATPsndrsp_args *uap;
184 int *retval;
185 {
186 int err;
187
188 atalk_lock();
189 if (sys_ATPsndrsp) {
190 /* required check for all AppleTalk system calls */
191 if (!(at_state.flags & AT_ST_STARTED) || !ifID_home) {
192 *retval = -1;
193 err = ENOTREADY;
194 } else {
195 *retval =
196 (*sys_ATPsndrsp)(uap->fd, uap->respbuff,
197 uap->resplen, uap->datalen, &err, proc);
198 }
199 } else {
200 *retval = -1;
201 err = ENXIO;
202 }
203 atalk_unlock();
204 return err;
205 }
206
207 int ATPgetreq(proc, uap, retval)
208 struct proc *proc;
209 struct ATPgetreq_args *uap;
210 int *retval;
211 {
212 int err;
213
214 atalk_lock();
215 if (sys_ATPgetreq) {
216 /* required check for all AppleTalk system calls */
217 if (!(at_state.flags & AT_ST_STARTED) || !ifID_home) {
218 *retval = -1;
219 err = ENOTREADY;
220 } else {
221 *retval =
222 (*sys_ATPgetreq)(uap->fd, uap->buf, uap->buflen,
223 &err, proc);
224 }
225 } else {
226 *retval = -1;
227 err = ENXIO;
228 }
229 atalk_unlock();
230 return err;
231 }
232
233 int ATPgetrsp(proc, uap, retval)
234 struct proc *proc;
235 struct ATPgetrsp_args *uap;
236 int *retval;
237 {
238 int err = 0;
239
240 atalk_lock();
241 if (sys_ATPgetrsp) {
242 /* required check for all AppleTalk system calls */
243 if (!(at_state.flags & AT_ST_STARTED) || !ifID_home) {
244 *retval = -1;
245 err = ENOTREADY;
246 } else {
247 *retval =
248 (*sys_ATPgetrsp)(uap->fd, uap->bdsp, &err, proc);
249 }
250 } else {
251 *retval = -1;
252 err = ENXIO;
253 }
254 atalk_unlock();
255 return err;
256 }
257
258 int atalk_closeref(fg, grefp)
259 struct fileglob *fg;
260 gref_t **grefp;
261 {
262 if ((*grefp = (gref_t *)fg->fg_data)) {
263 fg->fg_data = 0;
264 return(0);
265 }
266 return(EBADF);
267 }
268
269 int atalk_openref(gref, retfd, proc)
270 gref_t *gref;
271 int *retfd;
272 struct proc *proc;
273 {
274 extern int _ATread(), _ATwrite(),_ATioctl(), _ATselect(), _ATclose(), _ATkqfilter();
275 static struct fileops fileops =
276 {_ATread, _ATwrite, _ATioctl, _ATselect, _ATclose, _ATkqfilter, 0};
277 int err, fd;
278 struct fileproc *fp;
279
280 lck_mtx_assert(atalk_mutex, LCK_MTX_ASSERT_OWNED);
281
282 proc_fdlock(proc);
283 if ((err = falloc_locked(proc, &fp, &fd, 1)) != 0) {
284 proc_fdunlock(proc);
285 return err;
286 }
287
288 fp->f_flag = FREAD|FWRITE;
289 /*##### LD 5/7/96 Warning: we don't have a "DTYPE_OTHER" for
290 * MacOSX, so defines DTYPE_ATALK as DTYPE_SOCKET...
291 */
292 fp->f_type = DTYPE_ATALK+1;
293 fp->f_ops = &fileops;
294 fp->f_data = (void *)gref;
295
296 *fdflags(proc, fd) &= ~UF_RESERVED;
297 *retfd = fd;
298 fp_drop(proc, fd, fp, 1);
299 proc_fdunlock(proc);
300 /*
301 kprintf("atalk_openref: fp = 0x%x, gref = 0x%x\n", (u_int)fp, (u_int)gref);
302 */
303 return 0;
304 }
305
306 /*
307 * go from file descriptor to gref, which has been saved in fp->f_data
308 *
309 * This routine returns with an iocount on the fileproc when the fp is null
310 * as it converts fd to fileproc. Callers of this api who pass fp as null
311 * need to drop the iocount when they are done with the fp
312 */
313 int atalk_getref(fp, fd, grefp, proc, droponerr)
314 struct fileproc *fp;
315 int fd;
316 gref_t **grefp;
317 struct proc *proc;
318 int droponerr;
319 {
320 int error;
321
322 proc_fdlock(proc);
323 error = atalk_getref_locked(fp, fd, grefp, proc, droponerr);
324 proc_fdunlock(proc);
325 return error;
326 }
327
328 int atalk_getref_locked(fp, fd, grefp, proc, droponerr)
329 struct fileproc *fp;
330 int fd;
331 gref_t **grefp;
332 struct proc *proc;
333 int droponerr;
334 {
335 lck_mtx_assert(atalk_mutex, LCK_MTX_ASSERT_OWNED);
336 if (fp == 0) {
337 int error = fp_lookup(proc, fd, &fp, 1);
338
339 if (error) {
340
341 *grefp = (gref_t *) 0;
342 return EBADF;
343 }
344 }
345 *grefp = (gref_t *)fp->f_data;
346 if (fp->f_type != (DTYPE_ATALK+1) || *grefp == 0 || *grefp == (gref_t *)(-1)) {
347 if (droponerr)
348 fp_drop(proc, fd, fp, 1);
349 printf("atalk_getref_locked EBADF f_data: %x\n", fp->f_data);
350 return EBADF;
351 }
352
353 if ((*grefp)->errno) {
354 if (droponerr)
355 fp_drop(proc, fd, fp, 1);
356 return (int)(*grefp)->errno;
357 }
358 return 0;
359 }