]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/sys_socket.c
xnu-201.tar.gz
[apple/xnu.git] / bsd / kern / sys_socket.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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.
11 *
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
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
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,
73 struct ucred *cred));
74 int soo_write __P((struct file *fp, struct uio *uio,
75 struct ucred *cred));
76 int soo_close __P((struct file *fp, struct proc *p));
77
78 int soo_select __P((struct file *fp, int which, void * wql, struct proc *p));
79
80 struct fileops socketops =
81 { soo_read, soo_write, soo_ioctl, soo_select, soo_close };
82
83 /* ARGSUSED */
84 int
85 soo_read(fp, uio, cred)
86 struct file *fp;
87 struct uio *uio;
88 struct ucred *cred;
89 {
90 struct socket *so = (struct socket *)fp->f_data;
91 struct kextcb *kp;
92 int stat;
93 int (*fsoreceive) __P((struct socket *so,
94 struct sockaddr **paddr,
95 struct uio *uio, struct mbuf **mp0,
96 struct mbuf **controlp, int *flagsp));
97
98
99 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
100
101 fsoreceive = so->so_proto->pr_usrreqs->pru_soreceive;
102 if (fsoreceive != soreceive)
103 { kp = sotokextcb(so);
104 while (kp)
105 { if (kp->e_soif && kp->e_soif->sf_soreceive)
106 (*kp->e_soif->sf_soreceive)(so, 0, &uio,
107 0, 0, 0, kp);
108 kp = kp->e_next;
109 }
110
111 }
112
113 stat = (*fsoreceive)(so, 0, uio, 0, 0, 0);
114 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
115 return stat;
116 }
117
118 /* ARGSUSED */
119 int
120 soo_write(fp, uio, cred)
121 struct file *fp;
122 struct uio *uio;
123 struct ucred *cred;
124 {
125 struct socket *so = (struct socket *)fp->f_data;
126 int (*fsosend) __P((struct socket *so, struct sockaddr *addr,
127 struct uio *uio, struct mbuf *top,
128 struct mbuf *control, int flags));
129 struct kextcb *kp;
130 int stat;
131
132 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
133 fsosend = so->so_proto->pr_usrreqs->pru_sosend;
134 if (fsosend != sosend)
135 { kp = sotokextcb(so);
136 while (kp)
137 { if (kp->e_soif && kp->e_soif->sf_sosend)
138 (*kp->e_soif->sf_sosend)(so, 0, &uio,
139 0, 0, 0, kp);
140 kp = kp->e_next;
141 }
142 }
143
144 stat = (*fsosend)(so, 0, uio, 0, 0, 0);
145 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
146 return stat;
147 }
148
149 int
150 soo_ioctl(fp, cmd, data, p)
151 struct file *fp;
152 u_long cmd;
153 register caddr_t data;
154 struct proc *p;
155 {
156 register struct socket *so = (struct socket *)fp->f_data;
157
158 struct sockopt sopt;
159 struct kextcb *kp;
160 int error = 0;
161 kp = sotokextcb(so);
162 sopt.sopt_level = cmd;
163 sopt.sopt_name = (int)data;
164 sopt.sopt_p = p;
165
166
167 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
168
169 while (kp)
170 { if (kp->e_soif && kp->e_soif->sf_socontrol)
171 (*kp->e_soif->sf_socontrol)(so, &sopt, kp);
172 kp = kp->e_next;
173 }
174
175 switch (cmd) {
176
177 case FIONBIO:
178 if (*(int *)data)
179 so->so_state |= SS_NBIO;
180 else
181 so->so_state &= ~SS_NBIO;
182
183 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
184 return (0);
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 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
197 return (0);
198
199 case FIONREAD:
200 *(int *)data = so->so_rcv.sb_cc;
201 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
202 return (0);
203
204 case SIOCSPGRP:
205 so->so_pgid = *(int *)data;
206 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
207 return (0);
208
209 case SIOCGPGRP:
210 *(int *)data = so->so_pgid;
211 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
212 return (0);
213
214 case SIOCATMARK:
215 *(int *)data = (so->so_state&SS_RCVATMARK) != 0;
216 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
217 return (0);
218
219 case SIOCSETOT: {
220 /*
221 * Set socket level options here and then call protocol
222 * specific routine.
223 */
224 struct socket *cloned_so = NULL;
225 int cloned_fd = *(int *)data;
226
227 /* let's make sure it's either -1 or a valid file descriptor */
228 if (cloned_fd != -1) {
229 struct file *cloned_fp;
230 error = getsock(p->p_fd, cloned_fd, &cloned_fp);
231 if (error) {
232 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
233 return (error);
234 }
235
236 cloned_so = (struct socket *)cloned_fp->f_data;
237 }
238
239 /* Always set socket non-blocking for OT */
240 fp->f_flag |= FNONBLOCK;
241 so->so_state |= SS_NBIO;
242 so->so_options |= SO_DONTTRUNC | SO_WANTMORE;
243
244 if (cloned_so && so != cloned_so) {
245 /* Flags options */
246 so->so_options |= cloned_so->so_options & ~SO_ACCEPTCONN;
247
248 /* SO_LINGER */
249 if (so->so_options & SO_LINGER)
250 so->so_linger = cloned_so->so_linger;
251
252 /* SO_SNDBUF, SO_RCVBUF */
253 if (cloned_so->so_snd.sb_hiwat > 0) {
254 if (sbreserve(&so->so_snd, cloned_so->so_snd.sb_hiwat) == 0) {
255 error = ENOBUFS;
256 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
257 return (error);
258 }
259 }
260 if (cloned_so->so_rcv.sb_hiwat > 0) {
261 if (sbreserve(&so->so_rcv, cloned_so->so_rcv.sb_hiwat) == 0) {
262 error = ENOBUFS;
263 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
264 return (error);
265 }
266 }
267
268 /* SO_SNDLOWAT, SO_RCVLOWAT */
269 so->so_snd.sb_lowat =
270 (cloned_so->so_snd.sb_lowat > so->so_snd.sb_hiwat) ?
271 so->so_snd.sb_hiwat : cloned_so->so_snd.sb_lowat;
272 so->so_rcv.sb_lowat =
273 (cloned_so->so_rcv.sb_lowat > so->so_rcv.sb_hiwat) ?
274 so->so_rcv.sb_hiwat : cloned_so->so_rcv.sb_lowat;
275
276 /* SO_SNDTIMEO, SO_RCVTIMEO */
277 so->so_snd.sb_timeo = cloned_so->so_snd.sb_timeo;
278 so->so_rcv.sb_timeo = cloned_so->so_rcv.sb_timeo;
279 }
280
281 error = (*so->so_proto->pr_usrreqs->pru_control)(so, cmd, data, 0, p);
282 /* Just ignore protocols that do not understand it */
283 if (error == EOPNOTSUPP)
284 error = 0;
285
286 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
287 return (error);
288 }
289 }
290 /*
291 * Interface/routing/protocol specific ioctls:
292 * interface and routing ioctls should have a
293 * different entry since a socket's unnecessary
294 */
295 if (IOCGROUP(cmd) == 'i')
296 error = ifioctl(so, cmd, data, p);
297 else
298 if (IOCGROUP(cmd) == 'r')
299 error = rtioctl(cmd, data, p);
300 else
301 error = (*so->so_proto->pr_usrreqs->pru_control)(so, cmd, data, 0, p);
302
303 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
304 return error;
305 }
306
307 int
308 soo_select(fp, which, wql, p)
309 struct file *fp;
310 int which;
311 void * wql;
312 struct proc *p;
313 {
314 register struct socket *so = (struct socket *)fp->f_data;
315 register int s = splnet();
316 int retnum=0;
317
318
319 switch (which) {
320
321 case FREAD:
322 so->so_rcv.sb_flags |= SB_SEL;
323 if (soreadable(so)) {
324 splx(s);
325 retnum = 1;
326 so->so_rcv.sb_flags &= ~SB_SEL;
327 goto done;
328 }
329 selrecord(p, &so->so_rcv.sb_sel, wql);
330 break;
331
332 case FWRITE:
333 so->so_snd.sb_flags |= SB_SEL;
334 if (sowriteable(so)) {
335 splx(s);
336 retnum = 1;
337 so->so_snd.sb_flags &= ~SB_SEL;
338 goto done;
339 }
340 selrecord(p, &so->so_snd.sb_sel, wql);
341 break;
342
343 case 0:
344 so->so_rcv.sb_flags |= SB_SEL;
345 if (so->so_oobmark || (so->so_state & SS_RCVATMARK)) {
346 splx(s);
347 retnum = 1;
348 so->so_rcv.sb_flags &= ~SB_SEL;
349 goto done;
350 }
351 selrecord(p, &so->so_rcv.sb_sel, wql);
352 break;
353 }
354 splx(s);
355 done:
356 return (retnum);
357 }
358
359
360 int
361 soo_stat(so, ub)
362 register struct socket *so;
363 register struct stat *ub;
364 {
365 int stat;
366
367 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
368 bzero((caddr_t)ub, sizeof (*ub));
369 ub->st_mode = S_IFSOCK;
370 stat = (*so->so_proto->pr_usrreqs->pru_sense)(so, ub);
371 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
372 return stat;
373 }
374
375 /* ARGSUSED */
376 int
377 soo_close(fp, p)
378 struct file *fp;
379 struct proc *p;
380 {
381 int error = 0;
382
383 if (fp->f_data) {
384 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
385 error = soclose((struct socket *)fp->f_data);
386 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
387 }
388
389 fp->f_data = 0;
390 return (error);
391 }