]>
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 | * | |
de355530 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 | * |
de355530 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, | |
de355530 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> | |
60 | #include <sys/protosw.h> | |
61 | #include <sys/socket.h> | |
62 | #include <sys/socketvar.h> | |
63 | #include <sys/filio.h> /* XXX */ | |
64 | #include <sys/sockio.h> | |
65 | #include <sys/stat.h> | |
66 | #include <sys/uio.h> | |
67 | #include <sys/filedesc.h> | |
68 | ||
69 | #include <net/if.h> | |
70 | #include <net/route.h> | |
71 | ||
72 | int soo_read __P((struct file *fp, struct uio *uio, | |
9bccf70c | 73 | struct ucred *cred, int flags, struct proc *p)); |
1c79356b | 74 | int soo_write __P((struct file *fp, struct uio *uio, |
9bccf70c | 75 | struct ucred *cred, int flags, struct proc *p)); |
1c79356b A |
76 | int soo_close __P((struct file *fp, struct proc *p)); |
77 | ||
0b4e3aa0 | 78 | int soo_select __P((struct file *fp, int which, void * wql, struct proc *p)); |
1c79356b A |
79 | |
80 | struct fileops socketops = | |
81 | { soo_read, soo_write, soo_ioctl, soo_select, soo_close }; | |
82 | ||
83 | /* ARGSUSED */ | |
84 | int | |
9bccf70c | 85 | soo_read(fp, uio, cred, flags, p) |
1c79356b A |
86 | struct file *fp; |
87 | struct uio *uio; | |
88 | struct ucred *cred; | |
9bccf70c A |
89 | int flags; |
90 | struct proc *p; | |
1c79356b | 91 | { |
9bccf70c | 92 | struct socket *so; |
1c79356b A |
93 | struct kextcb *kp; |
94 | int stat; | |
95 | int (*fsoreceive) __P((struct socket *so, | |
96 | struct sockaddr **paddr, | |
97 | struct uio *uio, struct mbuf **mp0, | |
98 | struct mbuf **controlp, int *flagsp)); | |
99 | ||
0b4e3aa0 | 100 | |
1c79356b | 101 | thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL); |
0b4e3aa0 | 102 | |
9bccf70c A |
103 | if ((so = (struct socket *)fp->f_data) == NULL) { |
104 | /* This is not a valid open file descriptor */ | |
105 | thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL); | |
106 | return (EBADF); | |
107 | } | |
108 | ||
1c79356b A |
109 | fsoreceive = so->so_proto->pr_usrreqs->pru_soreceive; |
110 | if (fsoreceive != soreceive) | |
111 | { kp = sotokextcb(so); | |
112 | while (kp) | |
113 | { if (kp->e_soif && kp->e_soif->sf_soreceive) | |
114 | (*kp->e_soif->sf_soreceive)(so, 0, &uio, | |
115 | 0, 0, 0, kp); | |
116 | kp = kp->e_next; | |
117 | } | |
118 | ||
119 | } | |
120 | ||
121 | stat = (*fsoreceive)(so, 0, uio, 0, 0, 0); | |
122 | thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL); | |
123 | return stat; | |
124 | } | |
125 | ||
126 | /* ARGSUSED */ | |
127 | int | |
9bccf70c | 128 | soo_write(fp, uio, cred, flags, p) |
1c79356b A |
129 | struct file *fp; |
130 | struct uio *uio; | |
131 | struct ucred *cred; | |
9bccf70c A |
132 | int flags; |
133 | struct proc *p; | |
1c79356b | 134 | { |
9bccf70c | 135 | struct socket *so; |
1c79356b A |
136 | int (*fsosend) __P((struct socket *so, struct sockaddr *addr, |
137 | struct uio *uio, struct mbuf *top, | |
138 | struct mbuf *control, int flags)); | |
139 | struct kextcb *kp; | |
140 | int stat; | |
141 | ||
142 | thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL); | |
9bccf70c A |
143 | |
144 | if ((so = (struct socket *)fp->f_data) == NULL) { | |
145 | /* This is not a valid open file descriptor */ | |
146 | thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL); | |
147 | return (EBADF); | |
148 | } | |
149 | ||
1c79356b A |
150 | fsosend = so->so_proto->pr_usrreqs->pru_sosend; |
151 | if (fsosend != sosend) | |
152 | { kp = sotokextcb(so); | |
153 | while (kp) | |
154 | { if (kp->e_soif && kp->e_soif->sf_sosend) | |
155 | (*kp->e_soif->sf_sosend)(so, 0, &uio, | |
156 | 0, 0, 0, kp); | |
157 | kp = kp->e_next; | |
158 | } | |
159 | } | |
160 | ||
161 | stat = (*fsosend)(so, 0, uio, 0, 0, 0); | |
162 | thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL); | |
9bccf70c A |
163 | |
164 | /* Generation of SIGPIPE can be controlled per socket */ | |
165 | if (stat == EPIPE && uio->uio_procp && !(so->so_flags & SOF_NOSIGPIPE)) | |
166 | psignal(uio->uio_procp, SIGPIPE); | |
167 | ||
168 | return stat; | |
1c79356b A |
169 | } |
170 | ||
171 | int | |
172 | soo_ioctl(fp, cmd, data, p) | |
173 | struct file *fp; | |
174 | u_long cmd; | |
175 | register caddr_t data; | |
176 | struct proc *p; | |
177 | { | |
9bccf70c | 178 | register struct socket *so; |
1c79356b A |
179 | struct sockopt sopt; |
180 | struct kextcb *kp; | |
181 | int error = 0; | |
1c79356b A |
182 | |
183 | thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL); | |
184 | ||
9bccf70c A |
185 | if ((so = (struct socket *)fp->f_data) == NULL) { |
186 | /* This is not a valid open file descriptor */ | |
187 | thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL); | |
188 | return (EBADF); | |
189 | } | |
190 | ||
191 | kp = sotokextcb(so); | |
192 | sopt.sopt_level = cmd; | |
193 | sopt.sopt_name = (int)data; | |
194 | sopt.sopt_p = p; | |
195 | ||
1c79356b A |
196 | while (kp) |
197 | { if (kp->e_soif && kp->e_soif->sf_socontrol) | |
198 | (*kp->e_soif->sf_socontrol)(so, &sopt, kp); | |
199 | kp = kp->e_next; | |
200 | } | |
201 | ||
202 | switch (cmd) { | |
203 | ||
204 | case FIONBIO: | |
205 | if (*(int *)data) | |
206 | so->so_state |= SS_NBIO; | |
207 | else | |
208 | so->so_state &= ~SS_NBIO; | |
209 | ||
210 | thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL); | |
211 | return (0); | |
212 | ||
213 | case FIOASYNC: | |
214 | if (*(int *)data) { | |
215 | so->so_state |= SS_ASYNC; | |
216 | so->so_rcv.sb_flags |= SB_ASYNC; | |
217 | so->so_snd.sb_flags |= SB_ASYNC; | |
218 | } else { | |
219 | so->so_state &= ~SS_ASYNC; | |
220 | so->so_rcv.sb_flags &= ~SB_ASYNC; | |
221 | so->so_snd.sb_flags &= ~SB_ASYNC; | |
222 | } | |
223 | thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL); | |
224 | return (0); | |
225 | ||
226 | case FIONREAD: | |
227 | *(int *)data = so->so_rcv.sb_cc; | |
228 | thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL); | |
229 | return (0); | |
230 | ||
231 | case SIOCSPGRP: | |
232 | so->so_pgid = *(int *)data; | |
233 | thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL); | |
234 | return (0); | |
235 | ||
236 | case SIOCGPGRP: | |
237 | *(int *)data = so->so_pgid; | |
238 | thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL); | |
239 | return (0); | |
240 | ||
241 | case SIOCATMARK: | |
242 | *(int *)data = (so->so_state&SS_RCVATMARK) != 0; | |
243 | thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL); | |
244 | return (0); | |
245 | ||
246 | case SIOCSETOT: { | |
247 | /* | |
248 | * Set socket level options here and then call protocol | |
249 | * specific routine. | |
250 | */ | |
251 | struct socket *cloned_so = NULL; | |
252 | int cloned_fd = *(int *)data; | |
253 | ||
254 | /* let's make sure it's either -1 or a valid file descriptor */ | |
255 | if (cloned_fd != -1) { | |
256 | struct file *cloned_fp; | |
257 | error = getsock(p->p_fd, cloned_fd, &cloned_fp); | |
258 | if (error) { | |
259 | thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL); | |
260 | return (error); | |
261 | } | |
262 | ||
263 | cloned_so = (struct socket *)cloned_fp->f_data; | |
264 | } | |
265 | ||
266 | /* Always set socket non-blocking for OT */ | |
267 | fp->f_flag |= FNONBLOCK; | |
268 | so->so_state |= SS_NBIO; | |
269 | so->so_options |= SO_DONTTRUNC | SO_WANTMORE; | |
9bccf70c | 270 | so->so_flags |= SOF_NOSIGPIPE; |
1c79356b A |
271 | |
272 | if (cloned_so && so != cloned_so) { | |
273 | /* Flags options */ | |
274 | so->so_options |= cloned_so->so_options & ~SO_ACCEPTCONN; | |
275 | ||
276 | /* SO_LINGER */ | |
277 | if (so->so_options & SO_LINGER) | |
278 | so->so_linger = cloned_so->so_linger; | |
279 | ||
280 | /* SO_SNDBUF, SO_RCVBUF */ | |
281 | if (cloned_so->so_snd.sb_hiwat > 0) { | |
282 | if (sbreserve(&so->so_snd, cloned_so->so_snd.sb_hiwat) == 0) { | |
283 | error = ENOBUFS; | |
284 | thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL); | |
285 | return (error); | |
286 | } | |
287 | } | |
288 | if (cloned_so->so_rcv.sb_hiwat > 0) { | |
289 | if (sbreserve(&so->so_rcv, cloned_so->so_rcv.sb_hiwat) == 0) { | |
290 | error = ENOBUFS; | |
291 | thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL); | |
292 | return (error); | |
293 | } | |
294 | } | |
295 | ||
296 | /* SO_SNDLOWAT, SO_RCVLOWAT */ | |
297 | so->so_snd.sb_lowat = | |
298 | (cloned_so->so_snd.sb_lowat > so->so_snd.sb_hiwat) ? | |
299 | so->so_snd.sb_hiwat : cloned_so->so_snd.sb_lowat; | |
300 | so->so_rcv.sb_lowat = | |
301 | (cloned_so->so_rcv.sb_lowat > so->so_rcv.sb_hiwat) ? | |
302 | so->so_rcv.sb_hiwat : cloned_so->so_rcv.sb_lowat; | |
303 | ||
304 | /* SO_SNDTIMEO, SO_RCVTIMEO */ | |
305 | so->so_snd.sb_timeo = cloned_so->so_snd.sb_timeo; | |
306 | so->so_rcv.sb_timeo = cloned_so->so_rcv.sb_timeo; | |
307 | } | |
308 | ||
309 | error = (*so->so_proto->pr_usrreqs->pru_control)(so, cmd, data, 0, p); | |
310 | /* Just ignore protocols that do not understand it */ | |
311 | if (error == EOPNOTSUPP) | |
312 | error = 0; | |
313 | ||
314 | thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL); | |
315 | return (error); | |
316 | } | |
317 | } | |
318 | /* | |
319 | * Interface/routing/protocol specific ioctls: | |
320 | * interface and routing ioctls should have a | |
321 | * different entry since a socket's unnecessary | |
322 | */ | |
323 | if (IOCGROUP(cmd) == 'i') | |
324 | error = ifioctl(so, cmd, data, p); | |
325 | else | |
326 | if (IOCGROUP(cmd) == 'r') | |
327 | error = rtioctl(cmd, data, p); | |
328 | else | |
329 | error = (*so->so_proto->pr_usrreqs->pru_control)(so, cmd, data, 0, p); | |
330 | ||
331 | thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL); | |
332 | return error; | |
333 | } | |
334 | ||
335 | int | |
0b4e3aa0 | 336 | soo_select(fp, which, wql, p) |
1c79356b A |
337 | struct file *fp; |
338 | int which; | |
0b4e3aa0 | 339 | void * wql; |
1c79356b A |
340 | struct proc *p; |
341 | { | |
342 | register struct socket *so = (struct socket *)fp->f_data; | |
343 | register int s = splnet(); | |
344 | int retnum=0; | |
345 | ||
1c79356b A |
346 | |
347 | switch (which) { | |
348 | ||
349 | case FREAD: | |
0b4e3aa0 | 350 | so->so_rcv.sb_flags |= SB_SEL; |
1c79356b A |
351 | if (soreadable(so)) { |
352 | splx(s); | |
353 | retnum = 1; | |
0b4e3aa0 | 354 | so->so_rcv.sb_flags &= ~SB_SEL; |
1c79356b A |
355 | goto done; |
356 | } | |
0b4e3aa0 | 357 | selrecord(p, &so->so_rcv.sb_sel, wql); |
1c79356b A |
358 | break; |
359 | ||
360 | case FWRITE: | |
0b4e3aa0 | 361 | so->so_snd.sb_flags |= SB_SEL; |
1c79356b A |
362 | if (sowriteable(so)) { |
363 | splx(s); | |
364 | retnum = 1; | |
0b4e3aa0 | 365 | so->so_snd.sb_flags &= ~SB_SEL; |
1c79356b A |
366 | goto done; |
367 | } | |
0b4e3aa0 | 368 | selrecord(p, &so->so_snd.sb_sel, wql); |
1c79356b A |
369 | break; |
370 | ||
371 | case 0: | |
0b4e3aa0 | 372 | so->so_rcv.sb_flags |= SB_SEL; |
1c79356b A |
373 | if (so->so_oobmark || (so->so_state & SS_RCVATMARK)) { |
374 | splx(s); | |
375 | retnum = 1; | |
0b4e3aa0 | 376 | so->so_rcv.sb_flags &= ~SB_SEL; |
1c79356b A |
377 | goto done; |
378 | } | |
0b4e3aa0 | 379 | selrecord(p, &so->so_rcv.sb_sel, wql); |
1c79356b A |
380 | break; |
381 | } | |
382 | splx(s); | |
383 | done: | |
1c79356b A |
384 | return (retnum); |
385 | } | |
386 | ||
387 | ||
388 | int | |
389 | soo_stat(so, ub) | |
390 | register struct socket *so; | |
391 | register struct stat *ub; | |
392 | { | |
9bccf70c | 393 | int stat; |
1c79356b | 394 | |
9bccf70c A |
395 | /* |
396 | * DANGER: by the time we get the network funnel the socket | |
397 | * may have been closed | |
398 | */ | |
1c79356b A |
399 | thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL); |
400 | bzero((caddr_t)ub, sizeof (*ub)); | |
401 | ub->st_mode = S_IFSOCK; | |
402 | stat = (*so->so_proto->pr_usrreqs->pru_sense)(so, ub); | |
403 | thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL); | |
404 | return stat; | |
405 | } | |
406 | ||
407 | /* ARGSUSED */ | |
408 | int | |
409 | soo_close(fp, p) | |
410 | struct file *fp; | |
411 | struct proc *p; | |
412 | { | |
413 | int error = 0; | |
414 | ||
9bccf70c A |
415 | thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL); |
416 | ||
417 | if (fp->f_data) | |
1c79356b | 418 | error = soclose((struct socket *)fp->f_data); |
9bccf70c A |
419 | |
420 | thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL); | |
1c79356b A |
421 | |
422 | fp->f_data = 0; | |
423 | return (error); | |
424 | } |