]> git.saurik.com Git - apple/xnu.git/blame - bsd/kern/sys_socket.c
xnu-7195.101.1.tar.gz
[apple/xnu.git] / bsd / kern / sys_socket.c
CommitLineData
1c79356b 1/*
39236c6e 2 * Copyright (c) 2000-2013 Apple Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
39236c6e 5 *
2d21ac55
A
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.
39236c6e 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
39236c6e 17 *
2d21ac55
A
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
39236c6e 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
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 */
2d21ac55
A
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 */
1c79356b
A
68
69#include <sys/param.h>
70#include <sys/systm.h>
91447636 71#include <sys/file_internal.h>
55e303ae 72#include <sys/event.h>
1c79356b
A
73#include <sys/protosw.h>
74#include <sys/socket.h>
75#include <sys/socketvar.h>
0a7de745 76#include <sys/filio.h> /* XXX */
1c79356b
A
77#include <sys/sockio.h>
78#include <sys/stat.h>
79#include <sys/uio.h>
80#include <sys/filedesc.h>
91447636
A
81#include <sys/kauth.h>
82#include <sys/signalvar.h>
2d21ac55 83#include <sys/vnode.h>
1c79356b
A
84
85#include <net/if.h>
86#include <net/route.h>
87
2d21ac55
A
88#if CONFIG_MACF
89#include <security/mac_framework.h>
90#endif
91
91447636
A
92/*
93 * File operations on sockets.
94 */
2d21ac55
A
95static int soo_read(struct fileproc *, struct uio *, int, vfs_context_t ctx);
96static int soo_write(struct fileproc *, struct uio *, int, vfs_context_t ctx);
97static int soo_close(struct fileglob *, vfs_context_t ctx);
98static int soo_drain(struct fileproc *, vfs_context_t ctx);
99
39236c6e 100const struct fileops socketops = {
cb323159
A
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,
39037602 108 .fo_kqfilter = soo_kqfilter,
2d21ac55 109};
1c79356b
A
110
111/* ARGSUSED */
2d21ac55
A
112static int
113soo_read(struct fileproc *fp, struct uio *uio, __unused int flags,
f427ee49 114 __unused vfs_context_t ctx)
1c79356b 115{
9bccf70c 116 struct socket *so;
1c79356b 117 int stat;
1c79356b 118
2d21ac55
A
119 int (*fsoreceive)(struct socket *so2, struct sockaddr **paddr,
120 struct uio *uio2, struct mbuf **mp0, struct mbuf **controlp,
121 int *flagsp);
0b4e3aa0 122
f427ee49 123 if ((so = (struct socket *)fp->fp_glob->fg_data) == NULL) {
2d21ac55 124 /* This is not a valid open file descriptor */
0a7de745 125 return EBADF;
2d21ac55
A
126 }
127
1c79356b 128 fsoreceive = so->so_proto->pr_usrreqs->pru_soreceive;
2d21ac55 129
1c79356b 130 stat = (*fsoreceive)(so, 0, uio, 0, 0, 0);
0a7de745 131 return stat;
1c79356b
A
132}
133
134/* ARGSUSED */
2d21ac55
A
135static int
136soo_write(struct fileproc *fp, struct uio *uio, __unused int flags,
0a7de745 137 vfs_context_t ctx)
1c79356b 138{
9bccf70c 139 struct socket *so;
2d21ac55
A
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
f427ee49 146 if ((so = (struct socket *)fp->fp_glob->fg_data) == NULL) {
91447636 147 /* This is not a valid open file descriptor */
0a7de745 148 return EBADF;
91447636 149 }
9bccf70c 150
1c79356b 151 fsosend = so->so_proto->pr_usrreqs->pru_sosend;
1c79356b
A
152
153 stat = (*fsosend)(so, 0, uio, 0, 0, 0);
9bccf70c 154
91447636 155 /* Generation of SIGPIPE can be controlled per socket */
2d21ac55 156 procp = vfs_context_proc(ctx);
0a7de745 157 if (stat == EPIPE && !(so->so_flags & SOF_NOSIGPIPE)) {
91447636 158 psignal(procp, SIGPIPE);
0a7de745 159 }
9bccf70c 160
0a7de745 161 return stat;
1c79356b
A
162}
163
91447636 164__private_extern__ int
2d21ac55 165soioctl(struct socket *so, u_long cmd, caddr_t data, struct proc *p)
1c79356b 166{
2d21ac55 167 int error = 0;
316670eb 168 int int_arg;
9bccf70c 169
5ba3f43e
A
170#if CONFIG_MACF_SOCKET_SUBSET
171 error = mac_socket_check_ioctl(kauth_cred_get(), so, cmd);
0a7de745
A
172 if (error) {
173 return error;
174 }
5ba3f43e
A
175#endif
176
b0d623f7 177 socket_lock(so, 1);
1c79356b 178
39236c6e 179 /* call the socket filter's ioctl handler anything but ours */
2d21ac55 180 if (IOCGROUP(cmd) != 'i' && IOCGROUP(cmd) != 'r') {
39236c6e
A
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);
0a7de745 195 if (error != 0) {
39236c6e 196 goto out;
0a7de745 197 }
39236c6e
A
198 break;
199 }
2d21ac55
A
200 }
201
1c79356b 202 switch (cmd) {
0a7de745
A
203 case FIONBIO: /* int */
204 bcopy(data, &int_arg, sizeof(int_arg));
205 if (int_arg) {
1c79356b 206 so->so_state |= SS_NBIO;
0a7de745 207 } else {
1c79356b 208 so->so_state &= ~SS_NBIO;
0a7de745 209 }
1c79356b 210
91447636 211 goto out;
1c79356b 212
0a7de745
A
213 case FIOASYNC: /* int */
214 bcopy(data, &int_arg, sizeof(int_arg));
316670eb 215 if (int_arg) {
1c79356b
A
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 }
91447636 224 goto out;
1c79356b 225
0a7de745
A
226 case FIONREAD: /* int */
227 bcopy(&so->so_rcv.sb_cc, data, sizeof(u_int32_t));
91447636 228 goto out;
1c79356b 229
0a7de745
A
230 case SIOCSPGRP: /* int */
231 bcopy(data, &so->so_pgid, sizeof(pid_t));
91447636 232 goto out;
1c79356b 233
0a7de745
A
234 case SIOCGPGRP: /* int */
235 bcopy(&so->so_pgid, data, sizeof(pid_t));
91447636 236 goto out;
1c79356b 237
0a7de745 238 case SIOCATMARK: /* int */
316670eb 239 int_arg = (so->so_state & SS_RCVATMARK) != 0;
0a7de745 240 bcopy(&int_arg, data, sizeof(int_arg));
91447636 241 goto out;
1c79356b 242
0a7de745 243 case SIOCSETOT: /* int; deprecated */
39236c6e
A
244 error = EOPNOTSUPP;
245 goto out;
1c79356b 246
0a7de745
A
247 case SIOCGASSOCIDS32: /* so_aidreq32 */
248 case SIOCGASSOCIDS64: /* so_aidreq64 */
249 case SIOCGCONNIDS32: /* so_cidreq32 */
250 case SIOCGCONNIDS64: /* so_cidreq64 */
251 case SIOCGCONNINFO32: /* so_cinforeq32 */
252 case SIOCGCONNINFO64: /* so_cinforeq64 */
253 case SIOCSCONNORDER: /* so_cordreq */
254 case SIOCGCONNORDER: /* so_cordreq */
39236c6e
A
255 error = (*so->so_proto->pr_usrreqs->pru_control)(so,
256 cmd, data, NULL, p);
91447636 257 goto out;
2d21ac55 258 }
39236c6e 259
1c79356b
A
260 /*
261 * Interface/routing/protocol specific ioctls:
262 * interface and routing ioctls should have a
263 * different entry since a socket's unnecessary
264 */
2d21ac55
A
265 if (IOCGROUP(cmd) == 'i') {
266 error = ifioctllocked(so, cmd, data, p);
267 } else {
0a7de745 268 if (IOCGROUP(cmd) == 'r') {
2d21ac55 269 error = rtioctl(cmd, data, p);
0a7de745 270 } else {
2d21ac55 271 error = (*so->so_proto->pr_usrreqs->pru_control)(so,
39236c6e 272 cmd, data, NULL, p);
0a7de745 273 }
2d21ac55 274 }
1c79356b 275
91447636 276out:
91447636
A
277 socket_unlock(so, 1);
278
0a7de745 279 if (error == EJUSTRETURN) {
2d21ac55 280 error = 0;
0a7de745 281 }
2d21ac55 282
0a7de745 283 return error;
91447636
A
284}
285
286int
2d21ac55 287soo_ioctl(struct fileproc *fp, u_long cmd, caddr_t data, vfs_context_t ctx)
91447636 288{
2d21ac55 289 struct socket *so;
2d21ac55 290 proc_t procp = vfs_context_proc(ctx);
91447636 291
f427ee49 292 if ((so = (struct socket *)fp->fp_glob->fg_data) == NULL) {
91447636 293 /* This is not a valid open file descriptor */
0a7de745 294 return EBADF;
91447636 295 }
2d21ac55 296
0a7de745 297 return soioctl(so, cmd, data, procp);
1c79356b
A
298}
299
300int
2d21ac55 301soo_select(struct fileproc *fp, int which, void *wql, vfs_context_t ctx)
1c79356b 302{
f427ee49 303 struct socket *so = (struct socket *)fp->fp_glob->fg_data;
2d21ac55
A
304 int retnum = 0;
305 proc_t procp;
306
0a7de745
A
307 if (so == NULL || so == (struct socket *)-1) {
308 return 0;
309 }
39236c6e 310
2d21ac55 311 procp = vfs_context_proc(ctx);
1c79356b 312
91447636 313 socket_lock(so, 1);
1c79356b 314 switch (which) {
1c79356b 315 case FREAD:
0b4e3aa0 316 so->so_rcv.sb_flags |= SB_SEL;
1c79356b 317 if (soreadable(so)) {
1c79356b 318 retnum = 1;
0b4e3aa0 319 so->so_rcv.sb_flags &= ~SB_SEL;
1c79356b
A
320 goto done;
321 }
2d21ac55 322 selrecord(procp, &so->so_rcv.sb_sel, wql);
1c79356b
A
323 break;
324
325 case FWRITE:
0b4e3aa0 326 so->so_snd.sb_flags |= SB_SEL;
1c79356b 327 if (sowriteable(so)) {
1c79356b 328 retnum = 1;
0b4e3aa0 329 so->so_snd.sb_flags &= ~SB_SEL;
1c79356b
A
330 goto done;
331 }
2d21ac55 332 selrecord(procp, &so->so_snd.sb_sel, wql);
1c79356b
A
333 break;
334
335 case 0:
0b4e3aa0 336 so->so_rcv.sb_flags |= SB_SEL;
1c79356b 337 if (so->so_oobmark || (so->so_state & SS_RCVATMARK)) {
1c79356b 338 retnum = 1;
0b4e3aa0 339 so->so_rcv.sb_flags &= ~SB_SEL;
1c79356b
A
340 goto done;
341 }
2d21ac55 342 selrecord(procp, &so->so_rcv.sb_sel, wql);
1c79356b
A
343 break;
344 }
2d21ac55 345
1c79356b 346done:
91447636 347 socket_unlock(so, 1);
0a7de745 348 return retnum;
1c79356b
A
349}
350
1c79356b 351int
2d21ac55 352soo_stat(struct socket *so, void *ub, int isstat64)
1c79356b 353{
2d21ac55
A
354 int ret;
355 /* warning avoidance ; protected by isstat64 */
356 struct stat *sb = (struct stat *)0;
357 /* warning avoidance ; protected by isstat64 */
358 struct stat64 *sb64 = (struct stat64 *)0;
359
5ba3f43e 360#if CONFIG_MACF_SOCKET_SUBSET
2d21ac55 361 ret = mac_socket_check_stat(kauth_cred_get(), so);
0a7de745
A
362 if (ret) {
363 return ret;
364 }
2d21ac55
A
365#endif
366
367 if (isstat64 != 0) {
368 sb64 = (struct stat64 *)ub;
0a7de745 369 bzero((caddr_t)sb64, sizeof(*sb64));
2d21ac55
A
370 } else {
371 sb = (struct stat *)ub;
0a7de745 372 bzero((caddr_t)sb, sizeof(*sb));
2d21ac55 373 }
1c79356b 374
91447636 375 socket_lock(so, 1);
2d21ac55
A
376 if (isstat64 != 0) {
377 sb64->st_mode = S_IFSOCK;
378 if ((so->so_state & SS_CANTRCVMORE) == 0 ||
0a7de745 379 so->so_rcv.sb_cc != 0) {
2d21ac55 380 sb64->st_mode |= S_IRUSR | S_IRGRP | S_IROTH;
0a7de745
A
381 }
382 if ((so->so_state & SS_CANTSENDMORE) == 0) {
2d21ac55 383 sb64->st_mode |= S_IWUSR | S_IWGRP | S_IWOTH;
0a7de745 384 }
2d21ac55 385 sb64->st_size = so->so_rcv.sb_cc - so->so_rcv.sb_ctl;
316670eb
A
386 sb64->st_uid = kauth_cred_getuid(so->so_cred);
387 sb64->st_gid = kauth_cred_getgid(so->so_cred);
2d21ac55
A
388 } else {
389 sb->st_mode = S_IFSOCK;
390 if ((so->so_state & SS_CANTRCVMORE) == 0 ||
0a7de745 391 so->so_rcv.sb_cc != 0) {
2d21ac55 392 sb->st_mode |= S_IRUSR | S_IRGRP | S_IROTH;
0a7de745
A
393 }
394 if ((so->so_state & SS_CANTSENDMORE) == 0) {
2d21ac55 395 sb->st_mode |= S_IWUSR | S_IWGRP | S_IWOTH;
0a7de745 396 }
2d21ac55 397 sb->st_size = so->so_rcv.sb_cc - so->so_rcv.sb_ctl;
316670eb
A
398 sb->st_uid = kauth_cred_getuid(so->so_cred);
399 sb->st_gid = kauth_cred_getgid(so->so_cred);
2d21ac55
A
400 }
401
402 ret = (*so->so_proto->pr_usrreqs->pru_sense)(so, ub, isstat64);
91447636 403 socket_unlock(so, 1);
0a7de745 404 return ret;
1c79356b
A
405}
406
407/* ARGSUSED */
2d21ac55
A
408static int
409soo_close(struct fileglob *fg, __unused vfs_context_t ctx)
1c79356b
A
410{
411 int error = 0;
55e303ae
A
412 struct socket *sp;
413
91447636
A
414 sp = (struct socket *)fg->fg_data;
415 fg->fg_data = NULL;
1c79356b 416
0a7de745 417 if (sp) {
2d21ac55 418 error = soclose(sp);
0a7de745 419 }
1c79356b 420
0a7de745 421 return error;
1c79356b 422}
91447636 423
2d21ac55
A
424static int
425soo_drain(struct fileproc *fp, __unused vfs_context_t ctx)
91447636
A
426{
427 int error = 0;
f427ee49 428 struct socket *so = (struct socket *)fp->fp_glob->fg_data;
91447636
A
429
430 if (so) {
431 socket_lock(so, 1);
432 so->so_state |= SS_DRAINING;
433
434 wakeup((caddr_t)&so->so_timeo);
435 sorwakeup(so);
436 sowwakeup(so);
316670eb 437 soevent(so, SO_FILT_HINT_LOCKED);
2d21ac55 438
91447636
A
439 socket_unlock(so, 1);
440 }
441
0a7de745 442 return error;
91447636 443}
39236c6e
A
444
445/*
446 * 's' group ioctls.
447 *
448 * The switch statement below does nothing at runtime, as it serves as a
449 * compile time check to ensure that all of the socket 's' ioctls (those
450 * in the 's' group going thru soo_ioctl) that are made available by the
451 * networking stack is unique. This works as long as this routine gets
452 * updated each time a new interface ioctl gets added.
453 *
454 * Any failures at compile time indicates duplicated ioctl values.
455 */
456static __attribute__((unused)) void
457soioctl_cassert(void)
458{
459 /*
460 * This is equivalent to _CASSERT() and the compiler wouldn't
461 * generate any instructions, thus for compile time only.
462 */
463 switch ((u_long)0) {
464 case 0:
465
466 /* bsd/sys/sockio.h */
467 case SIOCSHIWAT:
468 case SIOCGHIWAT:
469 case SIOCSLOWAT:
470 case SIOCGLOWAT:
471 case SIOCATMARK:
472 case SIOCSPGRP:
473 case SIOCGPGRP:
474 case SIOCSETOT:
475 case SIOCGASSOCIDS32:
476 case SIOCGASSOCIDS64:
477 case SIOCGCONNIDS32:
478 case SIOCGCONNIDS64:
479 case SIOCGCONNINFO32:
480 case SIOCGCONNINFO64:
481 case SIOCSCONNORDER:
482 case SIOCGCONNORDER:
483 ;
484 }
485}