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