1 /*
2 * Copyright (c) 2000-2013 Apple 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 * 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,
66 * Version 2.0.
67 */
68
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>
78 #include <sys/stat.h>
79 #include <sys/uio.h>
80 #include <sys/filedesc.h>
81 #include <sys/kauth.h>
82 #include <sys/signalvar.h>
83 #include <sys/vnode.h>
84
85 #include <net/if.h>
86 #include <net/route.h>
87
88 #if CONFIG_MACF
89 #include <security/mac_framework.h>
90 #endif
91
92 /*
93 * File operations on sockets.
94 */
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);
99
100 const struct fileops socketops = {
101 .fo_type = DTYPE_SOCKET,
102 .fo_read = soo_read,
103 .fo_write = soo_write,
104 .fo_ioctl = soo_ioctl,
105 .fo_select = soo_select,
106 .fo_close = soo_close,
107 .fo_drain = soo_drain,
108 .fo_kqfilter = soo_kqfilter,
109 };
110
111 /* ARGSUSED */
112 static int
soo_read(struct fileproc * fp,struct uio * uio,__unused int flags,__unused vfs_context_t ctx)113 soo_read(struct fileproc *fp, struct uio *uio, __unused int flags,
114 __unused vfs_context_t ctx)
115 {
116 struct socket *__single so;
117 int stat;
118
119 int (*fsoreceive)(struct socket *so2, struct sockaddr **paddr,
120 struct uio *uio2, struct mbuf **mp0, struct mbuf **controlp,
121 int *flagsp);
122
123 if ((so = (struct socket *)fp_get_data(fp)) == NULL) {
124 /* This is not a valid open file descriptor */
125 return EBADF;
126 }
127
128 fsoreceive = so->so_proto->pr_usrreqs->pru_soreceive;
129
130 stat = (*fsoreceive)(so, 0, uio, 0, 0, 0);
131 return stat;
132 }
133
134 /* ARGSUSED */
135 static int
soo_write(struct fileproc * fp,struct uio * uio,__unused int flags,vfs_context_t ctx)136 soo_write(struct fileproc *fp, struct uio *uio, __unused int flags,
137 vfs_context_t ctx)
138 {
139 struct socket *__single so;
140 int stat;
141 int (*fsosend)(struct socket *so2, struct sockaddr *addr,
142 struct uio *uio2, struct mbuf *top, struct mbuf *control,
143 int flags2);
144 proc_t procp;
145
146 if ((so = (struct socket *)fp_get_data(fp)) == NULL) {
147 /* This is not a valid open file descriptor */
148 return EBADF;
149 }
150
151 fsosend = so->so_proto->pr_usrreqs->pru_sosend;
152
153 stat = (*fsosend)(so, 0, uio, 0, 0, 0);
154
155 /* Generation of SIGPIPE can be controlled per socket */
156 procp = vfs_context_proc(ctx);
157 if (stat == EPIPE && !(so->so_flags & SOF_NOSIGPIPE)) {
158 psignal(procp, SIGPIPE);
159 }
160
161 return stat;
162 }
163
164 __private_extern__ int
soioctl(struct socket * so,u_long cmd,caddr_t __sized_by (IOCPARM_LEN (cmd))data,struct proc * p)165 soioctl(struct socket *so, u_long cmd, caddr_t __sized_by(IOCPARM_LEN(cmd)) data, struct proc *p)
166 {
167 int error = 0;
168 int int_arg;
169
170 #if CONFIG_MACF_SOCKET_SUBSET
171 error = mac_socket_check_ioctl(kauth_cred_get(), so, cmd);
172 if (error) {
173 return error;
174 }
175 #endif
176
177 socket_lock(so, 1);
178
179 /* call the socket filter's ioctl handler anything but ours */
180 if (IOCGROUP(cmd) != 'i' && IOCGROUP(cmd) != 'r') {
181 switch (cmd) {
182 case SIOCGASSOCIDS32:
183 case SIOCGASSOCIDS64:
184 case SIOCGCONNIDS32:
185 case SIOCGCONNIDS64:
186 case SIOCGCONNINFO32:
187 case SIOCGCONNINFO64:
188 case SIOCSCONNORDER:
189 case SIOCGCONNORDER:
190 /* don't pass to filter */
191 break;
192
193 default:
194 error = sflt_ioctl(so, cmd, data);
195 if (error != 0) {
196 goto out;
197 }
198 break;
199 }
200 }
201
202 switch (cmd) {
203 case FIONBIO: /* int */
204 bcopy(data, &int_arg, sizeof(int_arg));
205 if (int_arg) {
206 so->so_state |= SS_NBIO;
207 } else {
208 so->so_state &= ~SS_NBIO;
209 }
210
211 goto out;
212
213 case FIOASYNC: /* int */
214 bcopy(data, &int_arg, sizeof(int_arg));
215 if (int_arg) {
216 so->so_state |= SS_ASYNC;
217 so->so_rcv.sb_flags |= SB_ASYNC;
218 so->so_snd.sb_flags |= SB_ASYNC;
219 } else {
220 so->so_state &= ~SS_ASYNC;
221 so->so_rcv.sb_flags &= ~SB_ASYNC;
222 so->so_snd.sb_flags &= ~SB_ASYNC;
223 }
224 goto out;
225
226 case FIONREAD: /* int */
227 bcopy(&so->so_rcv.sb_cc, data, sizeof(u_int32_t));
228 goto out;
229
230 case SIOCSPGRP: /* int */
231 bcopy(data, &so->so_pgid, sizeof(pid_t));
232 goto out;
233
234 case SIOCGPGRP: /* int */
235 bcopy(&so->so_pgid, data, sizeof(pid_t));
236 goto out;
237
238 case SIOCATMARK: /* int */
239 int_arg = (so->so_state & SS_RCVATMARK) != 0;
240 bcopy(&int_arg, data, sizeof(int_arg));
241 goto out;
242
243 case SIOCGASSOCIDS32: /* so_aidreq32 */
244 case SIOCGASSOCIDS64: /* so_aidreq64 */
245 case SIOCGCONNIDS32: /* so_cidreq32 */
246 case SIOCGCONNIDS64: /* so_cidreq64 */
247 case SIOCGCONNINFO32: /* so_cinforeq32 */
248 case SIOCGCONNINFO64: /* so_cinforeq64 */
249 case SIOCSCONNORDER: /* so_cordreq */
250 case SIOCGCONNORDER: /* so_cordreq */
251 error = (*so->so_proto->pr_usrreqs->pru_control)(so,
252 cmd, data, NULL, p);
253 goto out;
254 }
255
256 /*
257 * Interface/routing/protocol specific ioctls:
258 * interface and routing ioctls should have a
259 * different entry since a socket's unnecessary
260 */
261 if (IOCGROUP(cmd) == 'i') {
262 error = ifioctllocked(so, cmd, data, p);
263 } else {
264 if (IOCGROUP(cmd) == 'r') {
265 error = rtioctl(cmd, data, p);
266 } else {
267 error = (*so->so_proto->pr_usrreqs->pru_control)(so,
268 cmd, data, NULL, p);
269 }
270 }
271
272 out:
273 socket_unlock(so, 1);
274
275 if (error == EJUSTRETURN) {
276 error = 0;
277 }
278
279 return error;
280 }
281
282 int
soo_ioctl(struct fileproc * fp,u_long cmd,caddr_t __sized_by (IOCPARM_LEN (cmd))data,vfs_context_t ctx)283 soo_ioctl(struct fileproc *fp, u_long cmd, caddr_t __sized_by(IOCPARM_LEN(cmd)) data, vfs_context_t ctx)
284 {
285 struct socket *__single so;
286 proc_t procp = vfs_context_proc(ctx);
287
288 if ((so = (struct socket *)fp_get_data(fp)) == NULL) {
289 /* This is not a valid open file descriptor */
290 return EBADF;
291 }
292
293 return soioctl(so, cmd, data, procp);
294 }
295
296 int
soo_select(struct fileproc * fp,int which,void * wql,vfs_context_t ctx)297 soo_select(struct fileproc *fp, int which, void *wql, vfs_context_t ctx)
298 {
299 struct socket *__single so = (struct socket *)fp_get_data(fp);
300 int retnum = 0;
301 proc_t procp;
302
303 if (so == NULL || so == (struct socket *)-1) {
304 return 0;
305 }
306
307 procp = vfs_context_proc(ctx);
308
309 socket_lock(so, 1);
310 switch (which) {
311 case FREAD:
312 so->so_rcv.sb_flags |= SB_SEL;
313 if (soreadable(so)) {
314 retnum = 1;
315 so->so_rcv.sb_flags &= ~SB_SEL;
316 goto done;
317 }
318 selrecord(procp, &so->so_rcv.sb_sel, wql);
319 break;
320
321 case FWRITE:
322 so->so_snd.sb_flags |= SB_SEL;
323 if (sowriteable(so)) {
324 retnum = 1;
325 so->so_snd.sb_flags &= ~SB_SEL;
326 goto done;
327 }
328 selrecord(procp, &so->so_snd.sb_sel, wql);
329 break;
330
331 case 0:
332 so->so_rcv.sb_flags |= SB_SEL;
333 if (so->so_oobmark || (so->so_state & SS_RCVATMARK)) {
334 retnum = 1;
335 so->so_rcv.sb_flags &= ~SB_SEL;
336 goto done;
337 }
338 selrecord(procp, &so->so_rcv.sb_sel, wql);
339 break;
340 }
341
342 done:
343 socket_unlock(so, 1);
344 return retnum;
345 }
346
347 int
soo_stat(struct socket * so,void * ub,int isstat64)348 soo_stat(struct socket *so, void *ub, int isstat64)
349 {
350 int ret;
351 /* warning avoidance ; protected by isstat64 */
352 struct stat *sb = (struct stat *)0;
353 /* warning avoidance ; protected by isstat64 */
354 struct stat64 *sb64 = (struct stat64 *)0;
355
356 #if CONFIG_MACF_SOCKET_SUBSET
357 ret = mac_socket_check_stat(kauth_cred_get(), so);
358 if (ret) {
359 return ret;
360 }
361 #endif
362
363 if (isstat64 != 0) {
364 sb64 = (struct stat64 *)ub;
365 bzero((caddr_t)sb64, sizeof(*sb64));
366 } else {
367 sb = (struct stat *)ub;
368 bzero((caddr_t)sb, sizeof(*sb));
369 }
370
371 socket_lock(so, 1);
372 if (isstat64 != 0) {
373 sb64->st_mode = S_IFSOCK;
374 if ((so->so_state & SS_CANTRCVMORE) == 0 ||
375 so->so_rcv.sb_cc != 0) {
376 sb64->st_mode |= S_IRUSR | S_IRGRP | S_IROTH;
377 }
378 if ((so->so_state & SS_CANTSENDMORE) == 0) {
379 sb64->st_mode |= S_IWUSR | S_IWGRP | S_IWOTH;
380 }
381 sb64->st_size = so->so_rcv.sb_cc - so->so_rcv.sb_ctl;
382 sb64->st_uid = kauth_cred_getuid(so->so_cred);
383 sb64->st_gid = kauth_cred_getgid(so->so_cred);
384 } else {
385 sb->st_mode = S_IFSOCK;
386 if ((so->so_state & SS_CANTRCVMORE) == 0 ||
387 so->so_rcv.sb_cc != 0) {
388 sb->st_mode |= S_IRUSR | S_IRGRP | S_IROTH;
389 }
390 if ((so->so_state & SS_CANTSENDMORE) == 0) {
391 sb->st_mode |= S_IWUSR | S_IWGRP | S_IWOTH;
392 }
393 sb->st_size = so->so_rcv.sb_cc - so->so_rcv.sb_ctl;
394 sb->st_uid = kauth_cred_getuid(so->so_cred);
395 sb->st_gid = kauth_cred_getgid(so->so_cred);
396 }
397
398 ret = (*so->so_proto->pr_usrreqs->pru_sense)(so, ub, isstat64);
399 socket_unlock(so, 1);
400 return ret;
401 }
402
403 /* ARGSUSED */
404 static int
soo_close(struct fileglob * fg,__unused vfs_context_t ctx)405 soo_close(struct fileglob *fg, __unused vfs_context_t ctx)
406 {
407 int error = 0;
408 struct socket *__single sp;
409
410 sp = (struct socket *)fg_get_data(fg);
411 fg_set_data(fg, NULL);
412
413 if (sp) {
414 error = soclose(sp);
415 }
416
417 return error;
418 }
419
420 static int
soo_drain(struct fileproc * fp,__unused vfs_context_t ctx)421 soo_drain(struct fileproc *fp, __unused vfs_context_t ctx)
422 {
423 int error = 0;
424 struct socket *__single so = (struct socket *)fp_get_data(fp);
425
426 if (so) {
427 socket_lock(so, 1);
428 so->so_state |= SS_DRAINING;
429
430 wakeup((caddr_t)&so->so_timeo);
431 sorwakeup(so);
432 sowwakeup(so);
433 soevent(so, SO_FILT_HINT_LOCKED);
434
435 socket_unlock(so, 1);
436 }
437
438 return error;
439 }
440
441 /*
442 * 's' group ioctls.
443 *
444 * The switch statement below does nothing at runtime, as it serves as a
445 * compile time check to ensure that all of the socket 's' ioctls (those
446 * in the 's' group going thru soo_ioctl) that are made available by the
447 * networking stack is unique. This works as long as this routine gets
448 * updated each time a new interface ioctl gets added.
449 *
450 * Any failures at compile time indicates duplicated ioctl values.
451 */
452 static __attribute__((unused)) void
soioctl_cassert(void)453 soioctl_cassert(void)
454 {
455 /*
456 * This is equivalent to _CASSERT() and the compiler wouldn't
457 * generate any instructions, thus for compile time only.
458 */
459 switch ((u_long)0) {
460 case 0:
461
462 /* bsd/sys/sockio.h */
463 case SIOCSHIWAT:
464 case SIOCGHIWAT:
465 case SIOCSLOWAT:
466 case SIOCGLOWAT:
467 case SIOCATMARK:
468 case SIOCSPGRP:
469 case SIOCGPGRP:
470 case SIOCGASSOCIDS32:
471 case SIOCGASSOCIDS64:
472 case SIOCGCONNIDS32:
473 case SIOCGCONNIDS64:
474 case SIOCGCONNINFO32:
475 case SIOCGCONNINFO64:
476 case SIOCSCONNORDER:
477 case SIOCGCONNORDER:
478 ;
479 }
480 }
481