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