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