]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
9bccf70c | 2 | * Copyright (c) 2000-2002 Apple Computer, Inc. All rights reserved. |
1c79356b A |
3 | * |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
d7e50217 | 6 | * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved. |
1c79356b | 7 | * |
d7e50217 A |
8 | * This file contains Original Code and/or Modifications of Original Code |
9 | * as defined in and that are subject to the Apple Public Source License | |
10 | * Version 2.0 (the 'License'). You may not use this file except in | |
11 | * compliance with the License. Please obtain a copy of the License at | |
12 | * http://www.opensource.apple.com/apsl/ and read it before using this | |
13 | * file. | |
14 | * | |
15 | * The Original Code and all software distributed under the License are | |
16 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
1c79356b A |
17 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
18 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
d7e50217 A |
19 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
20 | * Please see the License for the specific language governing rights and | |
21 | * limitations under the License. | |
1c79356b A |
22 | * |
23 | * @APPLE_LICENSE_HEADER_END@ | |
24 | */ | |
25 | /* | |
26 | * Copyright (c) 1982, 1986, 1990, 1993 | |
27 | * The Regents of the University of California. All rights reserved. | |
28 | * | |
29 | * Redistribution and use in source and binary forms, with or without | |
30 | * modification, are permitted provided that the following conditions | |
31 | * are met: | |
32 | * 1. Redistributions of source code must retain the above copyright | |
33 | * notice, this list of conditions and the following disclaimer. | |
34 | * 2. Redistributions in binary form must reproduce the above copyright | |
35 | * notice, this list of conditions and the following disclaimer in the | |
36 | * documentation and/or other materials provided with the distribution. | |
37 | * 3. All advertising materials mentioning features or use of this software | |
38 | * must display the following acknowledgement: | |
39 | * This product includes software developed by the University of | |
40 | * California, Berkeley and its contributors. | |
41 | * 4. Neither the name of the University nor the names of its contributors | |
42 | * may be used to endorse or promote products derived from this software | |
43 | * without specific prior written permission. | |
44 | * | |
45 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
46 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
47 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
48 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
49 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
50 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
51 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
52 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
53 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
54 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
55 | * SUCH DAMAGE. | |
56 | * | |
57 | * @(#)sys_socket.c 8.1 (Berkeley) 6/10/93 | |
58 | */ | |
59 | ||
60 | #include <sys/param.h> | |
61 | #include <sys/systm.h> | |
62 | #include <sys/file.h> | |
63 | #include <sys/protosw.h> | |
64 | #include <sys/socket.h> | |
65 | #include <sys/socketvar.h> | |
66 | #include <sys/filio.h> /* XXX */ | |
67 | #include <sys/sockio.h> | |
68 | #include <sys/stat.h> | |
69 | #include <sys/uio.h> | |
70 | #include <sys/filedesc.h> | |
71 | ||
72 | #include <net/if.h> | |
73 | #include <net/route.h> | |
74 | ||
75 | int soo_read __P((struct file *fp, struct uio *uio, | |
9bccf70c | 76 | struct ucred *cred, int flags, struct proc *p)); |
1c79356b | 77 | int soo_write __P((struct file *fp, struct uio *uio, |
9bccf70c | 78 | struct ucred *cred, int flags, struct proc *p)); |
1c79356b A |
79 | int soo_close __P((struct file *fp, struct proc *p)); |
80 | ||
0b4e3aa0 | 81 | int soo_select __P((struct file *fp, int which, void * wql, struct proc *p)); |
1c79356b A |
82 | |
83 | struct fileops socketops = | |
84 | { soo_read, soo_write, soo_ioctl, soo_select, soo_close }; | |
85 | ||
86 | /* ARGSUSED */ | |
87 | int | |
9bccf70c | 88 | soo_read(fp, uio, cred, flags, p) |
1c79356b A |
89 | struct file *fp; |
90 | struct uio *uio; | |
91 | struct ucred *cred; | |
9bccf70c A |
92 | int flags; |
93 | struct proc *p; | |
1c79356b | 94 | { |
9bccf70c | 95 | struct socket *so; |
1c79356b A |
96 | struct kextcb *kp; |
97 | int stat; | |
98 | int (*fsoreceive) __P((struct socket *so, | |
99 | struct sockaddr **paddr, | |
100 | struct uio *uio, struct mbuf **mp0, | |
101 | struct mbuf **controlp, int *flagsp)); | |
102 | ||
0b4e3aa0 | 103 | |
1c79356b | 104 | thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL); |
0b4e3aa0 | 105 | |
9bccf70c A |
106 | if ((so = (struct socket *)fp->f_data) == NULL) { |
107 | /* This is not a valid open file descriptor */ | |
108 | thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL); | |
109 | return (EBADF); | |
110 | } | |
111 | ||
1c79356b A |
112 | fsoreceive = so->so_proto->pr_usrreqs->pru_soreceive; |
113 | if (fsoreceive != soreceive) | |
114 | { kp = sotokextcb(so); | |
115 | while (kp) | |
116 | { if (kp->e_soif && kp->e_soif->sf_soreceive) | |
117 | (*kp->e_soif->sf_soreceive)(so, 0, &uio, | |
118 | 0, 0, 0, kp); | |
119 | kp = kp->e_next; | |
120 | } | |
121 | ||
122 | } | |
123 | ||
124 | stat = (*fsoreceive)(so, 0, uio, 0, 0, 0); | |
125 | thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL); | |
126 | return stat; | |
127 | } | |
128 | ||
129 | /* ARGSUSED */ | |
130 | int | |
9bccf70c | 131 | soo_write(fp, uio, cred, flags, p) |
1c79356b A |
132 | struct file *fp; |
133 | struct uio *uio; | |
134 | struct ucred *cred; | |
9bccf70c A |
135 | int flags; |
136 | struct proc *p; | |
1c79356b | 137 | { |
9bccf70c | 138 | struct socket *so; |
1c79356b A |
139 | int (*fsosend) __P((struct socket *so, struct sockaddr *addr, |
140 | struct uio *uio, struct mbuf *top, | |
141 | struct mbuf *control, int flags)); | |
142 | struct kextcb *kp; | |
143 | int stat; | |
144 | ||
145 | thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL); | |
9bccf70c A |
146 | |
147 | if ((so = (struct socket *)fp->f_data) == NULL) { | |
148 | /* This is not a valid open file descriptor */ | |
149 | thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL); | |
150 | return (EBADF); | |
151 | } | |
152 | ||
1c79356b A |
153 | fsosend = so->so_proto->pr_usrreqs->pru_sosend; |
154 | if (fsosend != sosend) | |
155 | { kp = sotokextcb(so); | |
156 | while (kp) | |
157 | { if (kp->e_soif && kp->e_soif->sf_sosend) | |
158 | (*kp->e_soif->sf_sosend)(so, 0, &uio, | |
159 | 0, 0, 0, kp); | |
160 | kp = kp->e_next; | |
161 | } | |
162 | } | |
163 | ||
164 | stat = (*fsosend)(so, 0, uio, 0, 0, 0); | |
165 | thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL); | |
9bccf70c A |
166 | |
167 | /* Generation of SIGPIPE can be controlled per socket */ | |
168 | if (stat == EPIPE && uio->uio_procp && !(so->so_flags & SOF_NOSIGPIPE)) | |
169 | psignal(uio->uio_procp, SIGPIPE); | |
170 | ||
171 | return stat; | |
1c79356b A |
172 | } |
173 | ||
174 | int | |
175 | soo_ioctl(fp, cmd, data, p) | |
176 | struct file *fp; | |
177 | u_long cmd; | |
178 | register caddr_t data; | |
179 | struct proc *p; | |
180 | { | |
9bccf70c | 181 | register struct socket *so; |
1c79356b A |
182 | struct sockopt sopt; |
183 | struct kextcb *kp; | |
184 | int error = 0; | |
1c79356b A |
185 | |
186 | thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL); | |
187 | ||
9bccf70c A |
188 | if ((so = (struct socket *)fp->f_data) == NULL) { |
189 | /* This is not a valid open file descriptor */ | |
190 | thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL); | |
191 | return (EBADF); | |
192 | } | |
193 | ||
194 | kp = sotokextcb(so); | |
195 | sopt.sopt_level = cmd; | |
196 | sopt.sopt_name = (int)data; | |
197 | sopt.sopt_p = p; | |
198 | ||
1c79356b A |
199 | while (kp) |
200 | { if (kp->e_soif && kp->e_soif->sf_socontrol) | |
201 | (*kp->e_soif->sf_socontrol)(so, &sopt, kp); | |
202 | kp = kp->e_next; | |
203 | } | |
204 | ||
205 | switch (cmd) { | |
206 | ||
207 | case FIONBIO: | |
208 | if (*(int *)data) | |
209 | so->so_state |= SS_NBIO; | |
210 | else | |
211 | so->so_state &= ~SS_NBIO; | |
212 | ||
213 | thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL); | |
214 | return (0); | |
215 | ||
216 | case FIOASYNC: | |
217 | if (*(int *)data) { | |
218 | so->so_state |= SS_ASYNC; | |
219 | so->so_rcv.sb_flags |= SB_ASYNC; | |
220 | so->so_snd.sb_flags |= SB_ASYNC; | |
221 | } else { | |
222 | so->so_state &= ~SS_ASYNC; | |
223 | so->so_rcv.sb_flags &= ~SB_ASYNC; | |
224 | so->so_snd.sb_flags &= ~SB_ASYNC; | |
225 | } | |
226 | thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL); | |
227 | return (0); | |
228 | ||
229 | case FIONREAD: | |
230 | *(int *)data = so->so_rcv.sb_cc; | |
231 | thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL); | |
232 | return (0); | |
233 | ||
234 | case SIOCSPGRP: | |
235 | so->so_pgid = *(int *)data; | |
236 | thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL); | |
237 | return (0); | |
238 | ||
239 | case SIOCGPGRP: | |
240 | *(int *)data = so->so_pgid; | |
241 | thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL); | |
242 | return (0); | |
243 | ||
244 | case SIOCATMARK: | |
245 | *(int *)data = (so->so_state&SS_RCVATMARK) != 0; | |
246 | thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL); | |
247 | return (0); | |
248 | ||
249 | case SIOCSETOT: { | |
250 | /* | |
251 | * Set socket level options here and then call protocol | |
252 | * specific routine. | |
253 | */ | |
254 | struct socket *cloned_so = NULL; | |
255 | int cloned_fd = *(int *)data; | |
256 | ||
257 | /* let's make sure it's either -1 or a valid file descriptor */ | |
258 | if (cloned_fd != -1) { | |
259 | struct file *cloned_fp; | |
260 | error = getsock(p->p_fd, cloned_fd, &cloned_fp); | |
261 | if (error) { | |
262 | thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL); | |
263 | return (error); | |
264 | } | |
265 | ||
266 | cloned_so = (struct socket *)cloned_fp->f_data; | |
267 | } | |
268 | ||
269 | /* Always set socket non-blocking for OT */ | |
270 | fp->f_flag |= FNONBLOCK; | |
271 | so->so_state |= SS_NBIO; | |
272 | so->so_options |= SO_DONTTRUNC | SO_WANTMORE; | |
9bccf70c | 273 | so->so_flags |= SOF_NOSIGPIPE; |
1c79356b A |
274 | |
275 | if (cloned_so && so != cloned_so) { | |
276 | /* Flags options */ | |
277 | so->so_options |= cloned_so->so_options & ~SO_ACCEPTCONN; | |
278 | ||
279 | /* SO_LINGER */ | |
280 | if (so->so_options & SO_LINGER) | |
281 | so->so_linger = cloned_so->so_linger; | |
282 | ||
283 | /* SO_SNDBUF, SO_RCVBUF */ | |
284 | if (cloned_so->so_snd.sb_hiwat > 0) { | |
285 | if (sbreserve(&so->so_snd, cloned_so->so_snd.sb_hiwat) == 0) { | |
286 | error = ENOBUFS; | |
287 | thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL); | |
288 | return (error); | |
289 | } | |
290 | } | |
291 | if (cloned_so->so_rcv.sb_hiwat > 0) { | |
292 | if (sbreserve(&so->so_rcv, cloned_so->so_rcv.sb_hiwat) == 0) { | |
293 | error = ENOBUFS; | |
294 | thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL); | |
295 | return (error); | |
296 | } | |
297 | } | |
298 | ||
299 | /* SO_SNDLOWAT, SO_RCVLOWAT */ | |
300 | so->so_snd.sb_lowat = | |
301 | (cloned_so->so_snd.sb_lowat > so->so_snd.sb_hiwat) ? | |
302 | so->so_snd.sb_hiwat : cloned_so->so_snd.sb_lowat; | |
303 | so->so_rcv.sb_lowat = | |
304 | (cloned_so->so_rcv.sb_lowat > so->so_rcv.sb_hiwat) ? | |
305 | so->so_rcv.sb_hiwat : cloned_so->so_rcv.sb_lowat; | |
306 | ||
307 | /* SO_SNDTIMEO, SO_RCVTIMEO */ | |
308 | so->so_snd.sb_timeo = cloned_so->so_snd.sb_timeo; | |
309 | so->so_rcv.sb_timeo = cloned_so->so_rcv.sb_timeo; | |
310 | } | |
311 | ||
312 | error = (*so->so_proto->pr_usrreqs->pru_control)(so, cmd, data, 0, p); | |
313 | /* Just ignore protocols that do not understand it */ | |
314 | if (error == EOPNOTSUPP) | |
315 | error = 0; | |
316 | ||
317 | thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL); | |
318 | return (error); | |
319 | } | |
320 | } | |
321 | /* | |
322 | * Interface/routing/protocol specific ioctls: | |
323 | * interface and routing ioctls should have a | |
324 | * different entry since a socket's unnecessary | |
325 | */ | |
326 | if (IOCGROUP(cmd) == 'i') | |
327 | error = ifioctl(so, cmd, data, p); | |
328 | else | |
329 | if (IOCGROUP(cmd) == 'r') | |
330 | error = rtioctl(cmd, data, p); | |
331 | else | |
332 | error = (*so->so_proto->pr_usrreqs->pru_control)(so, cmd, data, 0, p); | |
333 | ||
334 | thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL); | |
335 | return error; | |
336 | } | |
337 | ||
338 | int | |
0b4e3aa0 | 339 | soo_select(fp, which, wql, p) |
1c79356b A |
340 | struct file *fp; |
341 | int which; | |
0b4e3aa0 | 342 | void * wql; |
1c79356b A |
343 | struct proc *p; |
344 | { | |
345 | register struct socket *so = (struct socket *)fp->f_data; | |
346 | register int s = splnet(); | |
347 | int retnum=0; | |
348 | ||
1c79356b A |
349 | |
350 | switch (which) { | |
351 | ||
352 | case FREAD: | |
0b4e3aa0 | 353 | so->so_rcv.sb_flags |= SB_SEL; |
1c79356b A |
354 | if (soreadable(so)) { |
355 | splx(s); | |
356 | retnum = 1; | |
0b4e3aa0 | 357 | so->so_rcv.sb_flags &= ~SB_SEL; |
1c79356b A |
358 | goto done; |
359 | } | |
0b4e3aa0 | 360 | selrecord(p, &so->so_rcv.sb_sel, wql); |
1c79356b A |
361 | break; |
362 | ||
363 | case FWRITE: | |
0b4e3aa0 | 364 | so->so_snd.sb_flags |= SB_SEL; |
1c79356b A |
365 | if (sowriteable(so)) { |
366 | splx(s); | |
367 | retnum = 1; | |
0b4e3aa0 | 368 | so->so_snd.sb_flags &= ~SB_SEL; |
1c79356b A |
369 | goto done; |
370 | } | |
0b4e3aa0 | 371 | selrecord(p, &so->so_snd.sb_sel, wql); |
1c79356b A |
372 | break; |
373 | ||
374 | case 0: | |
0b4e3aa0 | 375 | so->so_rcv.sb_flags |= SB_SEL; |
1c79356b A |
376 | if (so->so_oobmark || (so->so_state & SS_RCVATMARK)) { |
377 | splx(s); | |
378 | retnum = 1; | |
0b4e3aa0 | 379 | so->so_rcv.sb_flags &= ~SB_SEL; |
1c79356b A |
380 | goto done; |
381 | } | |
0b4e3aa0 | 382 | selrecord(p, &so->so_rcv.sb_sel, wql); |
1c79356b A |
383 | break; |
384 | } | |
385 | splx(s); | |
386 | done: | |
1c79356b A |
387 | return (retnum); |
388 | } | |
389 | ||
390 | ||
391 | int | |
392 | soo_stat(so, ub) | |
393 | register struct socket *so; | |
394 | register struct stat *ub; | |
395 | { | |
9bccf70c | 396 | int stat; |
1c79356b | 397 | |
9bccf70c A |
398 | /* |
399 | * DANGER: by the time we get the network funnel the socket | |
400 | * may have been closed | |
401 | */ | |
1c79356b A |
402 | thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL); |
403 | bzero((caddr_t)ub, sizeof (*ub)); | |
404 | ub->st_mode = S_IFSOCK; | |
405 | stat = (*so->so_proto->pr_usrreqs->pru_sense)(so, ub); | |
406 | thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL); | |
407 | return stat; | |
408 | } | |
409 | ||
410 | /* ARGSUSED */ | |
411 | int | |
412 | soo_close(fp, p) | |
413 | struct file *fp; | |
414 | struct proc *p; | |
415 | { | |
416 | int error = 0; | |
417 | ||
9bccf70c A |
418 | thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL); |
419 | ||
420 | if (fp->f_data) | |
1c79356b | 421 | error = soclose((struct socket *)fp->f_data); |
9bccf70c A |
422 | |
423 | thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL); | |
1c79356b A |
424 | |
425 | fp->f_data = 0; | |
426 | return (error); | |
427 | } |