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