2 * Copyright (c) 2000-2011 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (c) 1982, 1986, 1990, 1993
30 * The Regents of the University of California. All rights reserved.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
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.
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
60 * @(#)sys_socket.c 8.1 (Berkeley) 6/10/93
63 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
64 * support for mandatory and extensible security protections. This notice
65 * is included in support of clause 2.2 (b) of the Apple Public License,
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/file_internal.h>
72 #include <sys/event.h>
73 #include <sys/protosw.h>
74 #include <sys/socket.h>
75 #include <sys/socketvar.h>
76 #include <sys/filio.h> /* XXX */
77 #include <sys/sockio.h>
80 #include <sys/filedesc.h>
81 #include <sys/kauth.h>
82 #include <sys/signalvar.h>
83 #include <sys/vnode.h>
86 #include <net/route.h>
89 #include <security/mac_framework.h>
93 * File operations on sockets.
95 static int soo_read(struct fileproc
*, struct uio
*, int, vfs_context_t ctx
);
96 static int soo_write(struct fileproc
*, struct uio
*, int, vfs_context_t ctx
);
97 static int soo_close(struct fileglob
*, vfs_context_t ctx
);
98 static int soo_drain(struct fileproc
*, vfs_context_t ctx
);
100 /* TODO: these should be in header file */
101 extern int soo_ioctl(struct fileproc
*, u_long
, caddr_t
, vfs_context_t ctx
);
102 extern int soo_stat(struct socket
*, void *, int);
103 extern int soo_select(struct fileproc
*, int, void *, vfs_context_t ctx
);
104 extern int soo_kqfilter(struct fileproc
*, struct knote
*, vfs_context_t ctx
);
106 struct fileops socketops
= {
107 soo_read
, soo_write
, soo_ioctl
, soo_select
, soo_close
,
108 soo_kqfilter
, soo_drain
113 soo_read(struct fileproc
*fp
, struct uio
*uio
, __unused
int flags
,
114 #if !CONFIG_MACF_SOCKET
121 #if CONFIG_MACF_SOCKET
125 int (*fsoreceive
)(struct socket
*so2
, struct sockaddr
**paddr
,
126 struct uio
*uio2
, struct mbuf
**mp0
, struct mbuf
**controlp
,
129 if ((so
= (struct socket
*)fp
->f_fglob
->fg_data
) == NULL
) {
130 /* This is not a valid open file descriptor */
134 #if CONFIG_MACF_SOCKET
135 error
= mac_socket_check_receive(vfs_context_ucred(ctx
), so
);
138 #endif /* CONFIG_MACF_SOCKET */
140 //###LD will have to change
141 fsoreceive
= so
->so_proto
->pr_usrreqs
->pru_soreceive
;
143 stat
= (*fsoreceive
)(so
, 0, uio
, 0, 0, 0);
149 soo_write(struct fileproc
*fp
, struct uio
*uio
, __unused
int flags
,
154 int (*fsosend
)(struct socket
*so2
, struct sockaddr
*addr
,
155 struct uio
*uio2
, struct mbuf
*top
, struct mbuf
*control
,
159 #if CONFIG_MACF_SOCKET
163 if ((so
= (struct socket
*)fp
->f_fglob
->fg_data
) == NULL
) {
164 /* This is not a valid open file descriptor */
168 #if CONFIG_MACF_SOCKET
169 /* JMM - have to fetch the socket's remote addr */
170 error
= mac_socket_check_send(vfs_context_ucred(ctx
), so
, NULL
);
173 #endif /* CONFIG_MACF_SOCKET */
175 fsosend
= so
->so_proto
->pr_usrreqs
->pru_sosend
;
177 stat
= (*fsosend
)(so
, 0, uio
, 0, 0, 0);
179 /* Generation of SIGPIPE can be controlled per socket */
180 procp
= vfs_context_proc(ctx
);
181 if (stat
== EPIPE
&& !(so
->so_flags
& SOF_NOSIGPIPE
))
182 psignal(procp
, SIGPIPE
);
187 __private_extern__
int
188 soioctl(struct socket
*so
, u_long cmd
, caddr_t data
, struct proc
*p
)
191 int dropsockref
= -1;
195 /* Call the socket filter's ioctl handler for most ioctls */
196 if (IOCGROUP(cmd
) != 'i' && IOCGROUP(cmd
) != 'r') {
197 error
= sflt_ioctl(so
, cmd
, data
);
206 so
->so_state
|= SS_NBIO
;
208 so
->so_state
&= ~SS_NBIO
;
214 so
->so_state
|= SS_ASYNC
;
215 so
->so_rcv
.sb_flags
|= SB_ASYNC
;
216 so
->so_snd
.sb_flags
|= SB_ASYNC
;
218 so
->so_state
&= ~SS_ASYNC
;
219 so
->so_rcv
.sb_flags
&= ~SB_ASYNC
;
220 so
->so_snd
.sb_flags
&= ~SB_ASYNC
;
225 *(int *)data
= so
->so_rcv
.sb_cc
;
229 so
->so_pgid
= *(int *)data
;
233 *(int *)data
= so
->so_pgid
;
237 *(int *)data
= (so
->so_state
&SS_RCVATMARK
) != 0;
242 * Set socket level options here and then call protocol
245 struct socket
*cloned_so
= NULL
;
246 int cloned_fd
= *(int *)data
;
248 /* let's make sure it's either -1 or a valid file descriptor */
249 if (cloned_fd
!= -1) {
250 error
= file_socket(cloned_fd
, &cloned_so
);
254 dropsockref
= cloned_fd
;
257 /* Always set socket non-blocking for OT */
258 so
->so_state
|= SS_NBIO
;
259 so
->so_options
|= SO_DONTTRUNC
| SO_WANTMORE
;
260 so
->so_flags
|= SOF_NOSIGPIPE
| SOF_NPX_SETOPTSHUT
;
262 if (cloned_so
&& so
!= cloned_so
) {
265 cloned_so
->so_options
& ~SO_ACCEPTCONN
;
268 if (so
->so_options
& SO_LINGER
)
269 so
->so_linger
= cloned_so
->so_linger
;
271 /* SO_SNDBUF, SO_RCVBUF */
272 if (cloned_so
->so_snd
.sb_hiwat
> 0) {
273 if (sbreserve(&so
->so_snd
,
274 cloned_so
->so_snd
.sb_hiwat
) == 0) {
279 if (cloned_so
->so_rcv
.sb_hiwat
> 0) {
280 if (sbreserve(&so
->so_rcv
,
281 cloned_so
->so_rcv
.sb_hiwat
) == 0) {
287 /* SO_SNDLOWAT, SO_RCVLOWAT */
288 so
->so_snd
.sb_lowat
=
289 (cloned_so
->so_snd
.sb_lowat
> so
->so_snd
.sb_hiwat
) ?
290 so
->so_snd
.sb_hiwat
: cloned_so
->so_snd
.sb_lowat
;
291 so
->so_rcv
.sb_lowat
=
292 (cloned_so
->so_rcv
.sb_lowat
> so
->so_rcv
.sb_hiwat
) ?
293 so
->so_rcv
.sb_hiwat
: cloned_so
->so_rcv
.sb_lowat
;
295 /* SO_SNDTIMEO, SO_RCVTIMEO */
296 so
->so_snd
.sb_timeo
= cloned_so
->so_snd
.sb_timeo
;
297 so
->so_rcv
.sb_timeo
= cloned_so
->so_rcv
.sb_timeo
;
300 error
= (*so
->so_proto
->pr_usrreqs
->pru_control
)(so
, cmd
,
302 /* Just ignore protocols that do not understand it */
303 if (error
== EOPNOTSUPP
)
310 * Interface/routing/protocol specific ioctls:
311 * interface and routing ioctls should have a
312 * different entry since a socket's unnecessary
314 if (IOCGROUP(cmd
) == 'i') {
315 error
= ifioctllocked(so
, cmd
, data
, p
);
317 if (IOCGROUP(cmd
) == 'r')
318 error
= rtioctl(cmd
, data
, p
);
320 error
= (*so
->so_proto
->pr_usrreqs
->pru_control
)(so
,
325 if (dropsockref
!= -1)
326 file_drop(dropsockref
);
327 socket_unlock(so
, 1);
329 if (error
== EJUSTRETURN
)
336 soo_ioctl(struct fileproc
*fp
, u_long cmd
, caddr_t data
, vfs_context_t ctx
)
340 proc_t procp
= vfs_context_proc(ctx
);
342 if ((so
= (struct socket
*)fp
->f_fglob
->fg_data
) == NULL
) {
343 /* This is not a valid open file descriptor */
347 error
= soioctl(so
, cmd
, data
, procp
);
349 if (error
== 0 && cmd
== SIOCSETOT
)
350 fp
->f_fglob
->fg_flag
|= FNONBLOCK
;
356 soo_select(struct fileproc
*fp
, int which
, void *wql
, vfs_context_t ctx
)
358 struct socket
*so
= (struct socket
*)fp
->f_fglob
->fg_data
;
362 if (so
== NULL
|| so
== (struct socket
*)-1)
365 procp
= vfs_context_proc(ctx
);
367 #if CONFIG_MACF_SOCKET
368 if (mac_socket_check_select(vfs_context_ucred(ctx
), so
, which
) != 0);
370 #endif /* CONFIG_MACF_SOCKET */
377 so
->so_rcv
.sb_flags
|= SB_SEL
;
378 if (soreadable(so
)) {
380 so
->so_rcv
.sb_flags
&= ~SB_SEL
;
383 selrecord(procp
, &so
->so_rcv
.sb_sel
, wql
);
387 so
->so_snd
.sb_flags
|= SB_SEL
;
388 if (sowriteable(so
)) {
390 so
->so_snd
.sb_flags
&= ~SB_SEL
;
393 selrecord(procp
, &so
->so_snd
.sb_sel
, wql
);
397 so
->so_rcv
.sb_flags
|= SB_SEL
;
398 if (so
->so_oobmark
|| (so
->so_state
& SS_RCVATMARK
)) {
400 so
->so_rcv
.sb_flags
&= ~SB_SEL
;
403 selrecord(procp
, &so
->so_rcv
.sb_sel
, wql
);
408 socket_unlock(so
, 1);
413 soo_stat(struct socket
*so
, void *ub
, int isstat64
)
416 /* warning avoidance ; protected by isstat64 */
417 struct stat
*sb
= (struct stat
*)0;
418 /* warning avoidance ; protected by isstat64 */
419 struct stat64
*sb64
= (struct stat64
*)0;
421 #if CONFIG_MACF_SOCKET
422 ret
= mac_socket_check_stat(kauth_cred_get(), so
);
428 sb64
= (struct stat64
*)ub
;
429 bzero((caddr_t
)sb64
, sizeof (*sb64
));
431 sb
= (struct stat
*)ub
;
432 bzero((caddr_t
)sb
, sizeof (*sb
));
437 sb64
->st_mode
= S_IFSOCK
;
438 if ((so
->so_state
& SS_CANTRCVMORE
) == 0 ||
439 so
->so_rcv
.sb_cc
!= 0)
440 sb64
->st_mode
|= S_IRUSR
| S_IRGRP
| S_IROTH
;
441 if ((so
->so_state
& SS_CANTSENDMORE
) == 0)
442 sb64
->st_mode
|= S_IWUSR
| S_IWGRP
| S_IWOTH
;
443 sb64
->st_size
= so
->so_rcv
.sb_cc
- so
->so_rcv
.sb_ctl
;
444 sb64
->st_uid
= so
->so_uid
;
445 sb64
->st_gid
= so
->so_gid
;
447 sb
->st_mode
= S_IFSOCK
;
448 if ((so
->so_state
& SS_CANTRCVMORE
) == 0 ||
449 so
->so_rcv
.sb_cc
!= 0)
450 sb
->st_mode
|= S_IRUSR
| S_IRGRP
| S_IROTH
;
451 if ((so
->so_state
& SS_CANTSENDMORE
) == 0)
452 sb
->st_mode
|= S_IWUSR
| S_IWGRP
| S_IWOTH
;
453 sb
->st_size
= so
->so_rcv
.sb_cc
- so
->so_rcv
.sb_ctl
;
454 sb
->st_uid
= so
->so_uid
;
455 sb
->st_gid
= so
->so_gid
;
458 ret
= (*so
->so_proto
->pr_usrreqs
->pru_sense
)(so
, ub
, isstat64
);
459 socket_unlock(so
, 1);
465 soo_close(struct fileglob
*fg
, __unused vfs_context_t ctx
)
470 sp
= (struct socket
*)fg
->fg_data
;
480 soo_drain(struct fileproc
*fp
, __unused vfs_context_t ctx
)
483 struct socket
*so
= (struct socket
*)fp
->f_fglob
->fg_data
;
487 so
->so_state
|= SS_DRAINING
;
489 wakeup((caddr_t
)&so
->so_timeo
);
493 socket_unlock(so
, 1);