2 * Copyright (c) 2000-2002 Apple Computer, 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 #include <sys/param.h>
64 #include <sys/systm.h>
65 #include <sys/file_internal.h>
66 #include <sys/event.h>
67 #include <sys/protosw.h>
68 #include <sys/socket.h>
69 #include <sys/socketvar.h>
70 #include <sys/filio.h> /* XXX */
71 #include <sys/sockio.h>
74 #include <sys/filedesc.h>
75 #include <sys/kauth.h>
76 #include <sys/signalvar.h>
79 #include <net/route.h>
82 * File operations on sockets.
84 int soo_read(struct fileproc
*fp
, struct uio
*uio
, kauth_cred_t cred
,
85 int flags
, struct proc
*p
);
86 int soo_write(struct fileproc
*fp
, struct uio
*uio
, kauth_cred_t cred
,
87 int flags
, struct proc
*p
);
88 int soo_close(struct fileglob
*fp
, struct proc
*p
);
89 int soo_ioctl(struct fileproc
*fp
, u_long cmd
, caddr_t data
, struct proc
*p
);
90 int soo_stat(struct socket
*so
, struct stat
*ub
);
91 int soo_select(struct fileproc
*fp
, int which
, void * wql
, struct proc
*p
);
92 int soo_kqfilter(struct fileproc
*fp
, struct knote
*kn
, struct proc
*p
);
93 int soo_drain(struct fileproc
*fp
, struct proc
*p
);
95 struct fileops socketops
=
96 { soo_read
, soo_write
, soo_ioctl
, soo_select
, soo_close
, soo_kqfilter
, soo_drain
};
103 __unused kauth_cred_t cred
,
105 __unused
struct proc
*p
)
109 int (*fsoreceive
)(struct socket
*so2
,
110 struct sockaddr
**paddr
,
111 struct uio
*uio2
, struct mbuf
**mp0
,
112 struct mbuf
**controlp
, int *flagsp
);
116 if ((so
= (struct socket
*)fp
->f_fglob
->fg_data
) == NULL
) {
117 /* This is not a valid open file descriptor */
120 //###LD will have to change
121 fsoreceive
= so
->so_proto
->pr_usrreqs
->pru_soreceive
;
123 stat
= (*fsoreceive
)(so
, 0, uio
, 0, 0, 0);
132 __unused kauth_cred_t cred
,
137 int (*fsosend
)(struct socket
*so2
, struct sockaddr
*addr
,
138 struct uio
*uio2
, struct mbuf
*top
,
139 struct mbuf
*control
, int flags2
);
142 if ((so
= (struct socket
*)fp
->f_fglob
->fg_data
) == NULL
) {
143 /* This is not a valid open file descriptor */
147 fsosend
= so
->so_proto
->pr_usrreqs
->pru_sosend
;
149 stat
= (*fsosend
)(so
, 0, uio
, 0, 0, 0);
151 /* Generation of SIGPIPE can be controlled per socket */
152 if (stat
== EPIPE
&& procp
&& !(so
->so_flags
& SOF_NOSIGPIPE
))
153 psignal(procp
, SIGPIPE
);
158 __private_extern__
int
167 int dropsockref
= -1;
172 sopt
.sopt_level
= cmd
;
173 sopt
.sopt_name
= (int)data
;
180 so
->so_state
|= SS_NBIO
;
182 so
->so_state
&= ~SS_NBIO
;
188 so
->so_state
|= SS_ASYNC
;
189 so
->so_rcv
.sb_flags
|= SB_ASYNC
;
190 so
->so_snd
.sb_flags
|= SB_ASYNC
;
192 so
->so_state
&= ~SS_ASYNC
;
193 so
->so_rcv
.sb_flags
&= ~SB_ASYNC
;
194 so
->so_snd
.sb_flags
&= ~SB_ASYNC
;
199 *(int *)data
= so
->so_rcv
.sb_cc
;
203 so
->so_pgid
= *(int *)data
;
207 *(int *)data
= so
->so_pgid
;
211 *(int *)data
= (so
->so_state
&SS_RCVATMARK
) != 0;
216 * Set socket level options here and then call protocol
219 struct socket
*cloned_so
= NULL
;
220 int cloned_fd
= *(int *)data
;
222 /* let's make sure it's either -1 or a valid file descriptor */
223 if (cloned_fd
!= -1) {
224 error
= file_socket(cloned_fd
, &cloned_so
);
228 dropsockref
= cloned_fd
;
231 /* Always set socket non-blocking for OT */
232 so
->so_state
|= SS_NBIO
;
233 so
->so_options
|= SO_DONTTRUNC
| SO_WANTMORE
;
234 so
->so_flags
|= SOF_NOSIGPIPE
;
236 if (cloned_so
&& so
!= cloned_so
) {
238 so
->so_options
|= cloned_so
->so_options
& ~SO_ACCEPTCONN
;
241 if (so
->so_options
& SO_LINGER
)
242 so
->so_linger
= cloned_so
->so_linger
;
244 /* SO_SNDBUF, SO_RCVBUF */
245 if (cloned_so
->so_snd
.sb_hiwat
> 0) {
246 if (sbreserve(&so
->so_snd
, cloned_so
->so_snd
.sb_hiwat
) == 0) {
251 if (cloned_so
->so_rcv
.sb_hiwat
> 0) {
252 if (sbreserve(&so
->so_rcv
, cloned_so
->so_rcv
.sb_hiwat
) == 0) {
258 /* SO_SNDLOWAT, SO_RCVLOWAT */
259 so
->so_snd
.sb_lowat
=
260 (cloned_so
->so_snd
.sb_lowat
> so
->so_snd
.sb_hiwat
) ?
261 so
->so_snd
.sb_hiwat
: cloned_so
->so_snd
.sb_lowat
;
262 so
->so_rcv
.sb_lowat
=
263 (cloned_so
->so_rcv
.sb_lowat
> so
->so_rcv
.sb_hiwat
) ?
264 so
->so_rcv
.sb_hiwat
: cloned_so
->so_rcv
.sb_lowat
;
266 /* SO_SNDTIMEO, SO_RCVTIMEO */
267 so
->so_snd
.sb_timeo
= cloned_so
->so_snd
.sb_timeo
;
268 so
->so_rcv
.sb_timeo
= cloned_so
->so_rcv
.sb_timeo
;
271 error
= (*so
->so_proto
->pr_usrreqs
->pru_control
)(so
, cmd
, data
, 0, p
);
272 /* Just ignore protocols that do not understand it */
273 if (error
== EOPNOTSUPP
)
280 * Interface/routing/protocol specific ioctls:
281 * interface and routing ioctls should have a
282 * different entry since a socket's unnecessary
284 if (IOCGROUP(cmd
) == 'i')
285 error
= ifioctllocked(so
, cmd
, data
, p
);
287 if (IOCGROUP(cmd
) == 'r')
288 error
= rtioctl(cmd
, data
, p
);
290 error
= (*so
->so_proto
->pr_usrreqs
->pru_control
)(so
, cmd
, data
, 0, p
);
293 if (dropsockref
!= -1)
294 file_drop(dropsockref
);
295 socket_unlock(so
, 1);
301 soo_ioctl(fp
, cmd
, data
, p
)
304 register caddr_t data
;
307 register struct socket
*so
;
311 if ((so
= (struct socket
*)fp
->f_fglob
->fg_data
) == NULL
) {
312 /* This is not a valid open file descriptor */
316 error
= soioctl(so
, cmd
, data
, p
);
318 if (error
== 0 && cmd
== SIOCSETOT
)
319 fp
->f_fglob
->fg_flag
|= FNONBLOCK
;
325 soo_select(fp
, which
, wql
, p
)
331 register struct socket
*so
= (struct socket
*)fp
->f_fglob
->fg_data
;
334 if (so
== NULL
|| so
== (struct socket
*)-1)
341 so
->so_rcv
.sb_flags
|= SB_SEL
;
342 if (soreadable(so
)) {
344 so
->so_rcv
.sb_flags
&= ~SB_SEL
;
347 selrecord(p
, &so
->so_rcv
.sb_sel
, wql
);
351 so
->so_snd
.sb_flags
|= SB_SEL
;
352 if (sowriteable(so
)) {
354 so
->so_snd
.sb_flags
&= ~SB_SEL
;
357 selrecord(p
, &so
->so_snd
.sb_sel
, wql
);
361 so
->so_rcv
.sb_flags
|= SB_SEL
;
362 if (so
->so_oobmark
|| (so
->so_state
& SS_RCVATMARK
)) {
364 so
->so_rcv
.sb_flags
&= ~SB_SEL
;
367 selrecord(p
, &so
->so_rcv
.sb_sel
, wql
);
372 socket_unlock(so
, 1);
379 register struct socket
*so
;
380 register struct stat
*ub
;
384 bzero((caddr_t
)ub
, sizeof (*ub
));
386 ub
->st_mode
= S_IFSOCK
;
387 stat
= (*so
->so_proto
->pr_usrreqs
->pru_sense
)(so
, ub
);
388 socket_unlock(so
, 1);
394 soo_close(struct fileglob
*fg
, __unused proc_t p
)
399 sp
= (struct socket
*)fg
->fg_data
;
411 soo_drain(struct fileproc
*fp
, __unused
struct proc
*p
)
414 struct socket
*so
= (struct socket
*)fp
->f_fglob
->fg_data
;
418 so
->so_state
|= SS_DRAINING
;
420 wakeup((caddr_t
)&so
->so_timeo
);
424 socket_unlock(so
, 1);