]>
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 | * | |
e5568f75 A |
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. | |
1c79356b | 11 | * |
e5568f75 A |
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 | |
1c79356b A |
14 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
15 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
e5568f75 A |
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. | |
1c79356b A |
19 | * |
20 | * @APPLE_LICENSE_HEADER_END@ | |
21 | */ | |
22 | /* | |
23 | * Copyright (c) 1982, 1986, 1990, 1993 | |
24 | * The Regents of the University of California. All rights reserved. | |
25 | * | |
26 | * Redistribution and use in source and binary forms, with or without | |
27 | * modification, are permitted provided that the following conditions | |
28 | * are met: | |
29 | * 1. Redistributions of source code must retain the above copyright | |
30 | * notice, this list of conditions and the following disclaimer. | |
31 | * 2. Redistributions in binary form must reproduce the above copyright | |
32 | * notice, this list of conditions and the following disclaimer in the | |
33 | * documentation and/or other materials provided with the distribution. | |
34 | * 3. All advertising materials mentioning features or use of this software | |
35 | * must display the following acknowledgement: | |
36 | * This product includes software developed by the University of | |
37 | * California, Berkeley and its contributors. | |
38 | * 4. Neither the name of the University nor the names of its contributors | |
39 | * may be used to endorse or promote products derived from this software | |
40 | * without specific prior written permission. | |
41 | * | |
42 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
43 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
44 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
45 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
46 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
47 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
48 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
49 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
50 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
51 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
52 | * SUCH DAMAGE. | |
53 | * | |
54 | * @(#)sys_socket.c 8.1 (Berkeley) 6/10/93 | |
55 | */ | |
56 | ||
57 | #include <sys/param.h> | |
58 | #include <sys/systm.h> | |
59 | #include <sys/file.h> | |
55e303ae | 60 | #include <sys/event.h> |
1c79356b A |
61 | #include <sys/protosw.h> |
62 | #include <sys/socket.h> | |
63 | #include <sys/socketvar.h> | |
64 | #include <sys/filio.h> /* XXX */ | |
65 | #include <sys/sockio.h> | |
66 | #include <sys/stat.h> | |
67 | #include <sys/uio.h> | |
68 | #include <sys/filedesc.h> | |
69 | ||
70 | #include <net/if.h> | |
71 | #include <net/route.h> | |
72 | ||
73 | int soo_read __P((struct file *fp, struct uio *uio, | |
9bccf70c | 74 | struct ucred *cred, int flags, struct proc *p)); |
1c79356b | 75 | int soo_write __P((struct file *fp, struct uio *uio, |
9bccf70c | 76 | struct ucred *cred, int flags, struct proc *p)); |
1c79356b A |
77 | int soo_close __P((struct file *fp, struct proc *p)); |
78 | ||
0b4e3aa0 | 79 | int soo_select __P((struct file *fp, int which, void * wql, struct proc *p)); |
1c79356b | 80 | |
55e303ae A |
81 | int soo_kqfilter __P((struct file *fp, struct knote *kn, struct proc *p)); |
82 | ||
1c79356b | 83 | struct fileops socketops = |
55e303ae | 84 | { soo_read, soo_write, soo_ioctl, soo_select, soo_close, soo_kqfilter }; |
1c79356b A |
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 | ||
55e303ae | 349 | if (so == NULL || so == (struct socket*)-1) goto done; |
1c79356b A |
350 | |
351 | switch (which) { | |
352 | ||
353 | case FREAD: | |
0b4e3aa0 | 354 | so->so_rcv.sb_flags |= SB_SEL; |
1c79356b A |
355 | if (soreadable(so)) { |
356 | splx(s); | |
357 | retnum = 1; | |
0b4e3aa0 | 358 | so->so_rcv.sb_flags &= ~SB_SEL; |
1c79356b A |
359 | goto done; |
360 | } | |
0b4e3aa0 | 361 | selrecord(p, &so->so_rcv.sb_sel, wql); |
1c79356b A |
362 | break; |
363 | ||
364 | case FWRITE: | |
0b4e3aa0 | 365 | so->so_snd.sb_flags |= SB_SEL; |
1c79356b A |
366 | if (sowriteable(so)) { |
367 | splx(s); | |
368 | retnum = 1; | |
0b4e3aa0 | 369 | so->so_snd.sb_flags &= ~SB_SEL; |
1c79356b A |
370 | goto done; |
371 | } | |
0b4e3aa0 | 372 | selrecord(p, &so->so_snd.sb_sel, wql); |
1c79356b A |
373 | break; |
374 | ||
375 | case 0: | |
0b4e3aa0 | 376 | so->so_rcv.sb_flags |= SB_SEL; |
1c79356b A |
377 | if (so->so_oobmark || (so->so_state & SS_RCVATMARK)) { |
378 | splx(s); | |
379 | retnum = 1; | |
0b4e3aa0 | 380 | so->so_rcv.sb_flags &= ~SB_SEL; |
1c79356b A |
381 | goto done; |
382 | } | |
0b4e3aa0 | 383 | selrecord(p, &so->so_rcv.sb_sel, wql); |
1c79356b A |
384 | break; |
385 | } | |
386 | splx(s); | |
387 | done: | |
1c79356b A |
388 | return (retnum); |
389 | } | |
390 | ||
391 | ||
392 | int | |
393 | soo_stat(so, ub) | |
394 | register struct socket *so; | |
395 | register struct stat *ub; | |
396 | { | |
9bccf70c | 397 | int stat; |
1c79356b | 398 | |
9bccf70c A |
399 | /* |
400 | * DANGER: by the time we get the network funnel the socket | |
401 | * may have been closed | |
402 | */ | |
1c79356b A |
403 | thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL); |
404 | bzero((caddr_t)ub, sizeof (*ub)); | |
405 | ub->st_mode = S_IFSOCK; | |
406 | stat = (*so->so_proto->pr_usrreqs->pru_sense)(so, ub); | |
407 | thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL); | |
408 | return stat; | |
409 | } | |
410 | ||
411 | /* ARGSUSED */ | |
412 | int | |
413 | soo_close(fp, p) | |
414 | struct file *fp; | |
415 | struct proc *p; | |
416 | { | |
417 | int error = 0; | |
55e303ae A |
418 | struct socket *sp; |
419 | ||
420 | sp = (struct socket *)fp->f_data; | |
421 | fp->f_data = NULL; | |
1c79356b | 422 | |
9bccf70c A |
423 | thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL); |
424 | ||
55e303ae A |
425 | if (sp) |
426 | error = soclose(sp); | |
9bccf70c A |
427 | |
428 | thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL); | |
1c79356b | 429 | |
1c79356b A |
430 | return (error); |
431 | } |