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