xref: /xnu-8796.141.3/bsd/kern/sys_socket.c (revision 1b191cb58250d0705d8a51287127505aa4bc0789)
1*1b191cb5SApple OSS Distributions /*
2*1b191cb5SApple OSS Distributions  * Copyright (c) 2000-2013 Apple Inc. All rights reserved.
3*1b191cb5SApple OSS Distributions  *
4*1b191cb5SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*1b191cb5SApple OSS Distributions  *
6*1b191cb5SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*1b191cb5SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*1b191cb5SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*1b191cb5SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*1b191cb5SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*1b191cb5SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*1b191cb5SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*1b191cb5SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*1b191cb5SApple OSS Distributions  *
15*1b191cb5SApple OSS Distributions  * Please obtain a copy of the License at
16*1b191cb5SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*1b191cb5SApple OSS Distributions  *
18*1b191cb5SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*1b191cb5SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*1b191cb5SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*1b191cb5SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*1b191cb5SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*1b191cb5SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*1b191cb5SApple OSS Distributions  * limitations under the License.
25*1b191cb5SApple OSS Distributions  *
26*1b191cb5SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*1b191cb5SApple OSS Distributions  */
28*1b191cb5SApple OSS Distributions /*
29*1b191cb5SApple OSS Distributions  * Copyright (c) 1982, 1986, 1990, 1993
30*1b191cb5SApple OSS Distributions  *	The Regents of the University of California.  All rights reserved.
31*1b191cb5SApple OSS Distributions  *
32*1b191cb5SApple OSS Distributions  * Redistribution and use in source and binary forms, with or without
33*1b191cb5SApple OSS Distributions  * modification, are permitted provided that the following conditions
34*1b191cb5SApple OSS Distributions  * are met:
35*1b191cb5SApple OSS Distributions  * 1. Redistributions of source code must retain the above copyright
36*1b191cb5SApple OSS Distributions  *    notice, this list of conditions and the following disclaimer.
37*1b191cb5SApple OSS Distributions  * 2. Redistributions in binary form must reproduce the above copyright
38*1b191cb5SApple OSS Distributions  *    notice, this list of conditions and the following disclaimer in the
39*1b191cb5SApple OSS Distributions  *    documentation and/or other materials provided with the distribution.
40*1b191cb5SApple OSS Distributions  * 3. All advertising materials mentioning features or use of this software
41*1b191cb5SApple OSS Distributions  *    must display the following acknowledgement:
42*1b191cb5SApple OSS Distributions  *	This product includes software developed by the University of
43*1b191cb5SApple OSS Distributions  *	California, Berkeley and its contributors.
44*1b191cb5SApple OSS Distributions  * 4. Neither the name of the University nor the names of its contributors
45*1b191cb5SApple OSS Distributions  *    may be used to endorse or promote products derived from this software
46*1b191cb5SApple OSS Distributions  *    without specific prior written permission.
47*1b191cb5SApple OSS Distributions  *
48*1b191cb5SApple OSS Distributions  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49*1b191cb5SApple OSS Distributions  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50*1b191cb5SApple OSS Distributions  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51*1b191cb5SApple OSS Distributions  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52*1b191cb5SApple OSS Distributions  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53*1b191cb5SApple OSS Distributions  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54*1b191cb5SApple OSS Distributions  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55*1b191cb5SApple OSS Distributions  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56*1b191cb5SApple OSS Distributions  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57*1b191cb5SApple OSS Distributions  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58*1b191cb5SApple OSS Distributions  * SUCH DAMAGE.
59*1b191cb5SApple OSS Distributions  *
60*1b191cb5SApple OSS Distributions  *	@(#)sys_socket.c	8.1 (Berkeley) 6/10/93
61*1b191cb5SApple OSS Distributions  */
62*1b191cb5SApple OSS Distributions /*
63*1b191cb5SApple OSS Distributions  * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
64*1b191cb5SApple OSS Distributions  * support for mandatory and extensible security protections.  This notice
65*1b191cb5SApple OSS Distributions  * is included in support of clause 2.2 (b) of the Apple Public License,
66*1b191cb5SApple OSS Distributions  * Version 2.0.
67*1b191cb5SApple OSS Distributions  */
68*1b191cb5SApple OSS Distributions 
69*1b191cb5SApple OSS Distributions #include <sys/param.h>
70*1b191cb5SApple OSS Distributions #include <sys/systm.h>
71*1b191cb5SApple OSS Distributions #include <sys/file_internal.h>
72*1b191cb5SApple OSS Distributions #include <sys/event.h>
73*1b191cb5SApple OSS Distributions #include <sys/protosw.h>
74*1b191cb5SApple OSS Distributions #include <sys/socket.h>
75*1b191cb5SApple OSS Distributions #include <sys/socketvar.h>
76*1b191cb5SApple OSS Distributions #include <sys/filio.h>                  /* XXX */
77*1b191cb5SApple OSS Distributions #include <sys/sockio.h>
78*1b191cb5SApple OSS Distributions #include <sys/stat.h>
79*1b191cb5SApple OSS Distributions #include <sys/uio.h>
80*1b191cb5SApple OSS Distributions #include <sys/filedesc.h>
81*1b191cb5SApple OSS Distributions #include <sys/kauth.h>
82*1b191cb5SApple OSS Distributions #include <sys/signalvar.h>
83*1b191cb5SApple OSS Distributions #include <sys/vnode.h>
84*1b191cb5SApple OSS Distributions 
85*1b191cb5SApple OSS Distributions #include <net/if.h>
86*1b191cb5SApple OSS Distributions #include <net/route.h>
87*1b191cb5SApple OSS Distributions 
88*1b191cb5SApple OSS Distributions #if CONFIG_MACF
89*1b191cb5SApple OSS Distributions #include <security/mac_framework.h>
90*1b191cb5SApple OSS Distributions #endif
91*1b191cb5SApple OSS Distributions 
92*1b191cb5SApple OSS Distributions /*
93*1b191cb5SApple OSS Distributions  * File operations on sockets.
94*1b191cb5SApple OSS Distributions  */
95*1b191cb5SApple OSS Distributions static int soo_read(struct fileproc *, struct uio *, int, vfs_context_t ctx);
96*1b191cb5SApple OSS Distributions static int soo_write(struct fileproc *, struct uio *, int, vfs_context_t ctx);
97*1b191cb5SApple OSS Distributions static int soo_close(struct fileglob *, vfs_context_t ctx);
98*1b191cb5SApple OSS Distributions static int soo_drain(struct fileproc *, vfs_context_t ctx);
99*1b191cb5SApple OSS Distributions 
100*1b191cb5SApple OSS Distributions const struct fileops socketops = {
101*1b191cb5SApple OSS Distributions 	.fo_type     = DTYPE_SOCKET,
102*1b191cb5SApple OSS Distributions 	.fo_read     = soo_read,
103*1b191cb5SApple OSS Distributions 	.fo_write    = soo_write,
104*1b191cb5SApple OSS Distributions 	.fo_ioctl    = soo_ioctl,
105*1b191cb5SApple OSS Distributions 	.fo_select   = soo_select,
106*1b191cb5SApple OSS Distributions 	.fo_close    = soo_close,
107*1b191cb5SApple OSS Distributions 	.fo_drain    = soo_drain,
108*1b191cb5SApple OSS Distributions 	.fo_kqfilter = soo_kqfilter,
109*1b191cb5SApple OSS Distributions };
110*1b191cb5SApple OSS Distributions 
111*1b191cb5SApple OSS Distributions /* ARGSUSED */
112*1b191cb5SApple OSS Distributions static int
soo_read(struct fileproc * fp,struct uio * uio,__unused int flags,__unused vfs_context_t ctx)113*1b191cb5SApple OSS Distributions soo_read(struct fileproc *fp, struct uio *uio, __unused int flags,
114*1b191cb5SApple OSS Distributions     __unused vfs_context_t ctx)
115*1b191cb5SApple OSS Distributions {
116*1b191cb5SApple OSS Distributions 	struct socket *so;
117*1b191cb5SApple OSS Distributions 	int stat;
118*1b191cb5SApple OSS Distributions 
119*1b191cb5SApple OSS Distributions 	int (*fsoreceive)(struct socket *so2, struct sockaddr **paddr,
120*1b191cb5SApple OSS Distributions 	    struct uio *uio2, struct mbuf **mp0, struct mbuf **controlp,
121*1b191cb5SApple OSS Distributions 	    int *flagsp);
122*1b191cb5SApple OSS Distributions 
123*1b191cb5SApple OSS Distributions 	if ((so = (struct socket *)fp_get_data(fp)) == NULL) {
124*1b191cb5SApple OSS Distributions 		/* This is not a valid open file descriptor */
125*1b191cb5SApple OSS Distributions 		return EBADF;
126*1b191cb5SApple OSS Distributions 	}
127*1b191cb5SApple OSS Distributions 
128*1b191cb5SApple OSS Distributions 	fsoreceive = so->so_proto->pr_usrreqs->pru_soreceive;
129*1b191cb5SApple OSS Distributions 
130*1b191cb5SApple OSS Distributions 	stat = (*fsoreceive)(so, 0, uio, 0, 0, 0);
131*1b191cb5SApple OSS Distributions 	return stat;
132*1b191cb5SApple OSS Distributions }
133*1b191cb5SApple OSS Distributions 
134*1b191cb5SApple OSS Distributions /* ARGSUSED */
135*1b191cb5SApple OSS Distributions static int
soo_write(struct fileproc * fp,struct uio * uio,__unused int flags,vfs_context_t ctx)136*1b191cb5SApple OSS Distributions soo_write(struct fileproc *fp, struct uio *uio, __unused int flags,
137*1b191cb5SApple OSS Distributions     vfs_context_t ctx)
138*1b191cb5SApple OSS Distributions {
139*1b191cb5SApple OSS Distributions 	struct socket *so;
140*1b191cb5SApple OSS Distributions 	int stat;
141*1b191cb5SApple OSS Distributions 	int (*fsosend)(struct socket *so2, struct sockaddr *addr,
142*1b191cb5SApple OSS Distributions 	    struct uio *uio2, struct mbuf *top, struct mbuf *control,
143*1b191cb5SApple OSS Distributions 	    int flags2);
144*1b191cb5SApple OSS Distributions 	proc_t procp;
145*1b191cb5SApple OSS Distributions 
146*1b191cb5SApple OSS Distributions 	if ((so = (struct socket *)fp_get_data(fp)) == NULL) {
147*1b191cb5SApple OSS Distributions 		/* This is not a valid open file descriptor */
148*1b191cb5SApple OSS Distributions 		return EBADF;
149*1b191cb5SApple OSS Distributions 	}
150*1b191cb5SApple OSS Distributions 
151*1b191cb5SApple OSS Distributions 	fsosend = so->so_proto->pr_usrreqs->pru_sosend;
152*1b191cb5SApple OSS Distributions 
153*1b191cb5SApple OSS Distributions 	stat = (*fsosend)(so, 0, uio, 0, 0, 0);
154*1b191cb5SApple OSS Distributions 
155*1b191cb5SApple OSS Distributions 	/* Generation of SIGPIPE can be controlled per socket */
156*1b191cb5SApple OSS Distributions 	procp = vfs_context_proc(ctx);
157*1b191cb5SApple OSS Distributions 	if (stat == EPIPE && !(so->so_flags & SOF_NOSIGPIPE)) {
158*1b191cb5SApple OSS Distributions 		psignal(procp, SIGPIPE);
159*1b191cb5SApple OSS Distributions 	}
160*1b191cb5SApple OSS Distributions 
161*1b191cb5SApple OSS Distributions 	return stat;
162*1b191cb5SApple OSS Distributions }
163*1b191cb5SApple OSS Distributions 
164*1b191cb5SApple OSS Distributions __private_extern__ int
soioctl(struct socket * so,u_long cmd,caddr_t data,struct proc * p)165*1b191cb5SApple OSS Distributions soioctl(struct socket *so, u_long cmd, caddr_t data, struct proc *p)
166*1b191cb5SApple OSS Distributions {
167*1b191cb5SApple OSS Distributions 	int error = 0;
168*1b191cb5SApple OSS Distributions 	int int_arg;
169*1b191cb5SApple OSS Distributions 
170*1b191cb5SApple OSS Distributions #if CONFIG_MACF_SOCKET_SUBSET
171*1b191cb5SApple OSS Distributions 	error = mac_socket_check_ioctl(kauth_cred_get(), so, cmd);
172*1b191cb5SApple OSS Distributions 	if (error) {
173*1b191cb5SApple OSS Distributions 		return error;
174*1b191cb5SApple OSS Distributions 	}
175*1b191cb5SApple OSS Distributions #endif
176*1b191cb5SApple OSS Distributions 
177*1b191cb5SApple OSS Distributions 	socket_lock(so, 1);
178*1b191cb5SApple OSS Distributions 
179*1b191cb5SApple OSS Distributions 	/* call the socket filter's ioctl handler anything but ours */
180*1b191cb5SApple OSS Distributions 	if (IOCGROUP(cmd) != 'i' && IOCGROUP(cmd) != 'r') {
181*1b191cb5SApple OSS Distributions 		switch (cmd) {
182*1b191cb5SApple OSS Distributions 		case SIOCGASSOCIDS32:
183*1b191cb5SApple OSS Distributions 		case SIOCGASSOCIDS64:
184*1b191cb5SApple OSS Distributions 		case SIOCGCONNIDS32:
185*1b191cb5SApple OSS Distributions 		case SIOCGCONNIDS64:
186*1b191cb5SApple OSS Distributions 		case SIOCGCONNINFO32:
187*1b191cb5SApple OSS Distributions 		case SIOCGCONNINFO64:
188*1b191cb5SApple OSS Distributions 		case SIOCSCONNORDER:
189*1b191cb5SApple OSS Distributions 		case SIOCGCONNORDER:
190*1b191cb5SApple OSS Distributions 			/* don't pass to filter */
191*1b191cb5SApple OSS Distributions 			break;
192*1b191cb5SApple OSS Distributions 
193*1b191cb5SApple OSS Distributions 		default:
194*1b191cb5SApple OSS Distributions 			error = sflt_ioctl(so, cmd, data);
195*1b191cb5SApple OSS Distributions 			if (error != 0) {
196*1b191cb5SApple OSS Distributions 				goto out;
197*1b191cb5SApple OSS Distributions 			}
198*1b191cb5SApple OSS Distributions 			break;
199*1b191cb5SApple OSS Distributions 		}
200*1b191cb5SApple OSS Distributions 	}
201*1b191cb5SApple OSS Distributions 
202*1b191cb5SApple OSS Distributions 	switch (cmd) {
203*1b191cb5SApple OSS Distributions 	case FIONBIO:                   /* int */
204*1b191cb5SApple OSS Distributions 		bcopy(data, &int_arg, sizeof(int_arg));
205*1b191cb5SApple OSS Distributions 		if (int_arg) {
206*1b191cb5SApple OSS Distributions 			so->so_state |= SS_NBIO;
207*1b191cb5SApple OSS Distributions 		} else {
208*1b191cb5SApple OSS Distributions 			so->so_state &= ~SS_NBIO;
209*1b191cb5SApple OSS Distributions 		}
210*1b191cb5SApple OSS Distributions 
211*1b191cb5SApple OSS Distributions 		goto out;
212*1b191cb5SApple OSS Distributions 
213*1b191cb5SApple OSS Distributions 	case FIOASYNC:                  /* int */
214*1b191cb5SApple OSS Distributions 		bcopy(data, &int_arg, sizeof(int_arg));
215*1b191cb5SApple OSS Distributions 		if (int_arg) {
216*1b191cb5SApple OSS Distributions 			so->so_state |= SS_ASYNC;
217*1b191cb5SApple OSS Distributions 			so->so_rcv.sb_flags |= SB_ASYNC;
218*1b191cb5SApple OSS Distributions 			so->so_snd.sb_flags |= SB_ASYNC;
219*1b191cb5SApple OSS Distributions 		} else {
220*1b191cb5SApple OSS Distributions 			so->so_state &= ~SS_ASYNC;
221*1b191cb5SApple OSS Distributions 			so->so_rcv.sb_flags &= ~SB_ASYNC;
222*1b191cb5SApple OSS Distributions 			so->so_snd.sb_flags &= ~SB_ASYNC;
223*1b191cb5SApple OSS Distributions 		}
224*1b191cb5SApple OSS Distributions 		goto out;
225*1b191cb5SApple OSS Distributions 
226*1b191cb5SApple OSS Distributions 	case FIONREAD:                  /* int */
227*1b191cb5SApple OSS Distributions 		bcopy(&so->so_rcv.sb_cc, data, sizeof(u_int32_t));
228*1b191cb5SApple OSS Distributions 		goto out;
229*1b191cb5SApple OSS Distributions 
230*1b191cb5SApple OSS Distributions 	case SIOCSPGRP:                 /* int */
231*1b191cb5SApple OSS Distributions 		bcopy(data, &so->so_pgid, sizeof(pid_t));
232*1b191cb5SApple OSS Distributions 		goto out;
233*1b191cb5SApple OSS Distributions 
234*1b191cb5SApple OSS Distributions 	case SIOCGPGRP:                 /* int */
235*1b191cb5SApple OSS Distributions 		bcopy(&so->so_pgid, data, sizeof(pid_t));
236*1b191cb5SApple OSS Distributions 		goto out;
237*1b191cb5SApple OSS Distributions 
238*1b191cb5SApple OSS Distributions 	case SIOCATMARK:                /* int */
239*1b191cb5SApple OSS Distributions 		int_arg = (so->so_state & SS_RCVATMARK) != 0;
240*1b191cb5SApple OSS Distributions 		bcopy(&int_arg, data, sizeof(int_arg));
241*1b191cb5SApple OSS Distributions 		goto out;
242*1b191cb5SApple OSS Distributions 
243*1b191cb5SApple OSS Distributions 	case SIOCSETOT:                 /* int; deprecated */
244*1b191cb5SApple OSS Distributions 		error = EOPNOTSUPP;
245*1b191cb5SApple OSS Distributions 		goto out;
246*1b191cb5SApple OSS Distributions 
247*1b191cb5SApple OSS Distributions 	case SIOCGASSOCIDS32:           /* so_aidreq32 */
248*1b191cb5SApple OSS Distributions 	case SIOCGASSOCIDS64:           /* so_aidreq64 */
249*1b191cb5SApple OSS Distributions 	case SIOCGCONNIDS32:            /* so_cidreq32 */
250*1b191cb5SApple OSS Distributions 	case SIOCGCONNIDS64:            /* so_cidreq64 */
251*1b191cb5SApple OSS Distributions 	case SIOCGCONNINFO32:           /* so_cinforeq32 */
252*1b191cb5SApple OSS Distributions 	case SIOCGCONNINFO64:           /* so_cinforeq64 */
253*1b191cb5SApple OSS Distributions 	case SIOCSCONNORDER:            /* so_cordreq */
254*1b191cb5SApple OSS Distributions 	case SIOCGCONNORDER:            /* so_cordreq */
255*1b191cb5SApple OSS Distributions 		error = (*so->so_proto->pr_usrreqs->pru_control)(so,
256*1b191cb5SApple OSS Distributions 		    cmd, data, NULL, p);
257*1b191cb5SApple OSS Distributions 		goto out;
258*1b191cb5SApple OSS Distributions 	}
259*1b191cb5SApple OSS Distributions 
260*1b191cb5SApple OSS Distributions 	/*
261*1b191cb5SApple OSS Distributions 	 * Interface/routing/protocol specific ioctls:
262*1b191cb5SApple OSS Distributions 	 * interface and routing ioctls should have a
263*1b191cb5SApple OSS Distributions 	 * different entry since a socket's unnecessary
264*1b191cb5SApple OSS Distributions 	 */
265*1b191cb5SApple OSS Distributions 	if (IOCGROUP(cmd) == 'i') {
266*1b191cb5SApple OSS Distributions 		error = ifioctllocked(so, cmd, data, p);
267*1b191cb5SApple OSS Distributions 	} else {
268*1b191cb5SApple OSS Distributions 		if (IOCGROUP(cmd) == 'r') {
269*1b191cb5SApple OSS Distributions 			error = rtioctl(cmd, data, p);
270*1b191cb5SApple OSS Distributions 		} else {
271*1b191cb5SApple OSS Distributions 			error = (*so->so_proto->pr_usrreqs->pru_control)(so,
272*1b191cb5SApple OSS Distributions 			    cmd, data, NULL, p);
273*1b191cb5SApple OSS Distributions 		}
274*1b191cb5SApple OSS Distributions 	}
275*1b191cb5SApple OSS Distributions 
276*1b191cb5SApple OSS Distributions out:
277*1b191cb5SApple OSS Distributions 	socket_unlock(so, 1);
278*1b191cb5SApple OSS Distributions 
279*1b191cb5SApple OSS Distributions 	if (error == EJUSTRETURN) {
280*1b191cb5SApple OSS Distributions 		error = 0;
281*1b191cb5SApple OSS Distributions 	}
282*1b191cb5SApple OSS Distributions 
283*1b191cb5SApple OSS Distributions 	return error;
284*1b191cb5SApple OSS Distributions }
285*1b191cb5SApple OSS Distributions 
286*1b191cb5SApple OSS Distributions int
soo_ioctl(struct fileproc * fp,u_long cmd,caddr_t data,vfs_context_t ctx)287*1b191cb5SApple OSS Distributions soo_ioctl(struct fileproc *fp, u_long cmd, caddr_t data, vfs_context_t ctx)
288*1b191cb5SApple OSS Distributions {
289*1b191cb5SApple OSS Distributions 	struct socket *so;
290*1b191cb5SApple OSS Distributions 	proc_t procp = vfs_context_proc(ctx);
291*1b191cb5SApple OSS Distributions 
292*1b191cb5SApple OSS Distributions 	if ((so = (struct socket *)fp_get_data(fp)) == NULL) {
293*1b191cb5SApple OSS Distributions 		/* This is not a valid open file descriptor */
294*1b191cb5SApple OSS Distributions 		return EBADF;
295*1b191cb5SApple OSS Distributions 	}
296*1b191cb5SApple OSS Distributions 
297*1b191cb5SApple OSS Distributions 	return soioctl(so, cmd, data, procp);
298*1b191cb5SApple OSS Distributions }
299*1b191cb5SApple OSS Distributions 
300*1b191cb5SApple OSS Distributions int
soo_select(struct fileproc * fp,int which,void * wql,vfs_context_t ctx)301*1b191cb5SApple OSS Distributions soo_select(struct fileproc *fp, int which, void *wql, vfs_context_t ctx)
302*1b191cb5SApple OSS Distributions {
303*1b191cb5SApple OSS Distributions 	struct socket *so = (struct socket *)fp_get_data(fp);
304*1b191cb5SApple OSS Distributions 	int retnum = 0;
305*1b191cb5SApple OSS Distributions 	proc_t procp;
306*1b191cb5SApple OSS Distributions 
307*1b191cb5SApple OSS Distributions 	if (so == NULL || so == (struct socket *)-1) {
308*1b191cb5SApple OSS Distributions 		return 0;
309*1b191cb5SApple OSS Distributions 	}
310*1b191cb5SApple OSS Distributions 
311*1b191cb5SApple OSS Distributions 	procp = vfs_context_proc(ctx);
312*1b191cb5SApple OSS Distributions 
313*1b191cb5SApple OSS Distributions 	socket_lock(so, 1);
314*1b191cb5SApple OSS Distributions 	switch (which) {
315*1b191cb5SApple OSS Distributions 	case FREAD:
316*1b191cb5SApple OSS Distributions 		so->so_rcv.sb_flags |= SB_SEL;
317*1b191cb5SApple OSS Distributions 		if (soreadable(so)) {
318*1b191cb5SApple OSS Distributions 			retnum = 1;
319*1b191cb5SApple OSS Distributions 			so->so_rcv.sb_flags &= ~SB_SEL;
320*1b191cb5SApple OSS Distributions 			goto done;
321*1b191cb5SApple OSS Distributions 		}
322*1b191cb5SApple OSS Distributions 		selrecord(procp, &so->so_rcv.sb_sel, wql);
323*1b191cb5SApple OSS Distributions 		break;
324*1b191cb5SApple OSS Distributions 
325*1b191cb5SApple OSS Distributions 	case FWRITE:
326*1b191cb5SApple OSS Distributions 		so->so_snd.sb_flags |= SB_SEL;
327*1b191cb5SApple OSS Distributions 		if (sowriteable(so)) {
328*1b191cb5SApple OSS Distributions 			retnum = 1;
329*1b191cb5SApple OSS Distributions 			so->so_snd.sb_flags &= ~SB_SEL;
330*1b191cb5SApple OSS Distributions 			goto done;
331*1b191cb5SApple OSS Distributions 		}
332*1b191cb5SApple OSS Distributions 		selrecord(procp, &so->so_snd.sb_sel, wql);
333*1b191cb5SApple OSS Distributions 		break;
334*1b191cb5SApple OSS Distributions 
335*1b191cb5SApple OSS Distributions 	case 0:
336*1b191cb5SApple OSS Distributions 		so->so_rcv.sb_flags |= SB_SEL;
337*1b191cb5SApple OSS Distributions 		if (so->so_oobmark || (so->so_state & SS_RCVATMARK)) {
338*1b191cb5SApple OSS Distributions 			retnum = 1;
339*1b191cb5SApple OSS Distributions 			so->so_rcv.sb_flags &= ~SB_SEL;
340*1b191cb5SApple OSS Distributions 			goto done;
341*1b191cb5SApple OSS Distributions 		}
342*1b191cb5SApple OSS Distributions 		selrecord(procp, &so->so_rcv.sb_sel, wql);
343*1b191cb5SApple OSS Distributions 		break;
344*1b191cb5SApple OSS Distributions 	}
345*1b191cb5SApple OSS Distributions 
346*1b191cb5SApple OSS Distributions done:
347*1b191cb5SApple OSS Distributions 	socket_unlock(so, 1);
348*1b191cb5SApple OSS Distributions 	return retnum;
349*1b191cb5SApple OSS Distributions }
350*1b191cb5SApple OSS Distributions 
351*1b191cb5SApple OSS Distributions int
soo_stat(struct socket * so,void * ub,int isstat64)352*1b191cb5SApple OSS Distributions soo_stat(struct socket *so, void *ub, int isstat64)
353*1b191cb5SApple OSS Distributions {
354*1b191cb5SApple OSS Distributions 	int ret;
355*1b191cb5SApple OSS Distributions 	/* warning avoidance ; protected by isstat64 */
356*1b191cb5SApple OSS Distributions 	struct stat *sb = (struct stat *)0;
357*1b191cb5SApple OSS Distributions 	/* warning avoidance ; protected by isstat64 */
358*1b191cb5SApple OSS Distributions 	struct stat64 *sb64 = (struct stat64 *)0;
359*1b191cb5SApple OSS Distributions 
360*1b191cb5SApple OSS Distributions #if CONFIG_MACF_SOCKET_SUBSET
361*1b191cb5SApple OSS Distributions 	ret = mac_socket_check_stat(kauth_cred_get(), so);
362*1b191cb5SApple OSS Distributions 	if (ret) {
363*1b191cb5SApple OSS Distributions 		return ret;
364*1b191cb5SApple OSS Distributions 	}
365*1b191cb5SApple OSS Distributions #endif
366*1b191cb5SApple OSS Distributions 
367*1b191cb5SApple OSS Distributions 	if (isstat64 != 0) {
368*1b191cb5SApple OSS Distributions 		sb64 = (struct stat64 *)ub;
369*1b191cb5SApple OSS Distributions 		bzero((caddr_t)sb64, sizeof(*sb64));
370*1b191cb5SApple OSS Distributions 	} else {
371*1b191cb5SApple OSS Distributions 		sb = (struct stat *)ub;
372*1b191cb5SApple OSS Distributions 		bzero((caddr_t)sb, sizeof(*sb));
373*1b191cb5SApple OSS Distributions 	}
374*1b191cb5SApple OSS Distributions 
375*1b191cb5SApple OSS Distributions 	socket_lock(so, 1);
376*1b191cb5SApple OSS Distributions 	if (isstat64 != 0) {
377*1b191cb5SApple OSS Distributions 		sb64->st_mode = S_IFSOCK;
378*1b191cb5SApple OSS Distributions 		if ((so->so_state & SS_CANTRCVMORE) == 0 ||
379*1b191cb5SApple OSS Distributions 		    so->so_rcv.sb_cc != 0) {
380*1b191cb5SApple OSS Distributions 			sb64->st_mode |= S_IRUSR | S_IRGRP | S_IROTH;
381*1b191cb5SApple OSS Distributions 		}
382*1b191cb5SApple OSS Distributions 		if ((so->so_state & SS_CANTSENDMORE) == 0) {
383*1b191cb5SApple OSS Distributions 			sb64->st_mode |= S_IWUSR | S_IWGRP | S_IWOTH;
384*1b191cb5SApple OSS Distributions 		}
385*1b191cb5SApple OSS Distributions 		sb64->st_size = so->so_rcv.sb_cc - so->so_rcv.sb_ctl;
386*1b191cb5SApple OSS Distributions 		sb64->st_uid = kauth_cred_getuid(so->so_cred);
387*1b191cb5SApple OSS Distributions 		sb64->st_gid = kauth_cred_getgid(so->so_cred);
388*1b191cb5SApple OSS Distributions 	} else {
389*1b191cb5SApple OSS Distributions 		sb->st_mode = S_IFSOCK;
390*1b191cb5SApple OSS Distributions 		if ((so->so_state & SS_CANTRCVMORE) == 0 ||
391*1b191cb5SApple OSS Distributions 		    so->so_rcv.sb_cc != 0) {
392*1b191cb5SApple OSS Distributions 			sb->st_mode |= S_IRUSR | S_IRGRP | S_IROTH;
393*1b191cb5SApple OSS Distributions 		}
394*1b191cb5SApple OSS Distributions 		if ((so->so_state & SS_CANTSENDMORE) == 0) {
395*1b191cb5SApple OSS Distributions 			sb->st_mode |= S_IWUSR | S_IWGRP | S_IWOTH;
396*1b191cb5SApple OSS Distributions 		}
397*1b191cb5SApple OSS Distributions 		sb->st_size = so->so_rcv.sb_cc - so->so_rcv.sb_ctl;
398*1b191cb5SApple OSS Distributions 		sb->st_uid = kauth_cred_getuid(so->so_cred);
399*1b191cb5SApple OSS Distributions 		sb->st_gid = kauth_cred_getgid(so->so_cred);
400*1b191cb5SApple OSS Distributions 	}
401*1b191cb5SApple OSS Distributions 
402*1b191cb5SApple OSS Distributions 	ret = (*so->so_proto->pr_usrreqs->pru_sense)(so, ub, isstat64);
403*1b191cb5SApple OSS Distributions 	socket_unlock(so, 1);
404*1b191cb5SApple OSS Distributions 	return ret;
405*1b191cb5SApple OSS Distributions }
406*1b191cb5SApple OSS Distributions 
407*1b191cb5SApple OSS Distributions /* ARGSUSED */
408*1b191cb5SApple OSS Distributions static int
soo_close(struct fileglob * fg,__unused vfs_context_t ctx)409*1b191cb5SApple OSS Distributions soo_close(struct fileglob *fg, __unused vfs_context_t ctx)
410*1b191cb5SApple OSS Distributions {
411*1b191cb5SApple OSS Distributions 	int error = 0;
412*1b191cb5SApple OSS Distributions 	struct socket *sp;
413*1b191cb5SApple OSS Distributions 
414*1b191cb5SApple OSS Distributions 	sp = (struct socket *)fg_get_data(fg);
415*1b191cb5SApple OSS Distributions 	fg_set_data(fg, NULL);
416*1b191cb5SApple OSS Distributions 
417*1b191cb5SApple OSS Distributions 	if (sp) {
418*1b191cb5SApple OSS Distributions 		error = soclose(sp);
419*1b191cb5SApple OSS Distributions 	}
420*1b191cb5SApple OSS Distributions 
421*1b191cb5SApple OSS Distributions 	return error;
422*1b191cb5SApple OSS Distributions }
423*1b191cb5SApple OSS Distributions 
424*1b191cb5SApple OSS Distributions static int
soo_drain(struct fileproc * fp,__unused vfs_context_t ctx)425*1b191cb5SApple OSS Distributions soo_drain(struct fileproc *fp, __unused vfs_context_t ctx)
426*1b191cb5SApple OSS Distributions {
427*1b191cb5SApple OSS Distributions 	int error = 0;
428*1b191cb5SApple OSS Distributions 	struct socket *so = (struct socket *)fp_get_data(fp);
429*1b191cb5SApple OSS Distributions 
430*1b191cb5SApple OSS Distributions 	if (so) {
431*1b191cb5SApple OSS Distributions 		socket_lock(so, 1);
432*1b191cb5SApple OSS Distributions 		so->so_state |= SS_DRAINING;
433*1b191cb5SApple OSS Distributions 
434*1b191cb5SApple OSS Distributions 		wakeup((caddr_t)&so->so_timeo);
435*1b191cb5SApple OSS Distributions 		sorwakeup(so);
436*1b191cb5SApple OSS Distributions 		sowwakeup(so);
437*1b191cb5SApple OSS Distributions 		soevent(so, SO_FILT_HINT_LOCKED);
438*1b191cb5SApple OSS Distributions 
439*1b191cb5SApple OSS Distributions 		socket_unlock(so, 1);
440*1b191cb5SApple OSS Distributions 	}
441*1b191cb5SApple OSS Distributions 
442*1b191cb5SApple OSS Distributions 	return error;
443*1b191cb5SApple OSS Distributions }
444*1b191cb5SApple OSS Distributions 
445*1b191cb5SApple OSS Distributions /*
446*1b191cb5SApple OSS Distributions  * 's' group ioctls.
447*1b191cb5SApple OSS Distributions  *
448*1b191cb5SApple OSS Distributions  * The switch statement below does nothing at runtime, as it serves as a
449*1b191cb5SApple OSS Distributions  * compile time check to ensure that all of the socket 's' ioctls (those
450*1b191cb5SApple OSS Distributions  * in the 's' group going thru soo_ioctl) that are made available by the
451*1b191cb5SApple OSS Distributions  * networking stack is unique.  This works as long as this routine gets
452*1b191cb5SApple OSS Distributions  * updated each time a new interface ioctl gets added.
453*1b191cb5SApple OSS Distributions  *
454*1b191cb5SApple OSS Distributions  * Any failures at compile time indicates duplicated ioctl values.
455*1b191cb5SApple OSS Distributions  */
456*1b191cb5SApple OSS Distributions static __attribute__((unused)) void
soioctl_cassert(void)457*1b191cb5SApple OSS Distributions soioctl_cassert(void)
458*1b191cb5SApple OSS Distributions {
459*1b191cb5SApple OSS Distributions 	/*
460*1b191cb5SApple OSS Distributions 	 * This is equivalent to _CASSERT() and the compiler wouldn't
461*1b191cb5SApple OSS Distributions 	 * generate any instructions, thus for compile time only.
462*1b191cb5SApple OSS Distributions 	 */
463*1b191cb5SApple OSS Distributions 	switch ((u_long)0) {
464*1b191cb5SApple OSS Distributions 	case 0:
465*1b191cb5SApple OSS Distributions 
466*1b191cb5SApple OSS Distributions 	/* bsd/sys/sockio.h */
467*1b191cb5SApple OSS Distributions 	case SIOCSHIWAT:
468*1b191cb5SApple OSS Distributions 	case SIOCGHIWAT:
469*1b191cb5SApple OSS Distributions 	case SIOCSLOWAT:
470*1b191cb5SApple OSS Distributions 	case SIOCGLOWAT:
471*1b191cb5SApple OSS Distributions 	case SIOCATMARK:
472*1b191cb5SApple OSS Distributions 	case SIOCSPGRP:
473*1b191cb5SApple OSS Distributions 	case SIOCGPGRP:
474*1b191cb5SApple OSS Distributions 	case SIOCSETOT:
475*1b191cb5SApple OSS Distributions 	case SIOCGASSOCIDS32:
476*1b191cb5SApple OSS Distributions 	case SIOCGASSOCIDS64:
477*1b191cb5SApple OSS Distributions 	case SIOCGCONNIDS32:
478*1b191cb5SApple OSS Distributions 	case SIOCGCONNIDS64:
479*1b191cb5SApple OSS Distributions 	case SIOCGCONNINFO32:
480*1b191cb5SApple OSS Distributions 	case SIOCGCONNINFO64:
481*1b191cb5SApple OSS Distributions 	case SIOCSCONNORDER:
482*1b191cb5SApple OSS Distributions 	case SIOCGCONNORDER:
483*1b191cb5SApple OSS Distributions 		;
484*1b191cb5SApple OSS Distributions 	}
485*1b191cb5SApple OSS Distributions }
486