2 * Copyright (c) 1982, 1986, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)sys_socket.c 8.1 (Berkeley) 6/10/93
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
37 #include <sys/param.h>
38 #include <sys/systm.h>
40 #include <sys/filedesc.h>
42 #include <sys/protosw.h>
43 #include <sys/sigio.h>
44 #include <sys/signal.h>
45 #include <sys/signalvar.h>
46 #include <sys/socket.h>
47 #include <sys/socketvar.h>
48 #include <sys/filio.h> /* XXX */
49 #include <sys/sockio.h>
52 #include <sys/ucred.h>
55 #include <net/route.h>
57 #include <security/mac/mac_framework.h>
59 struct fileops socketops = {
61 .fo_write = soo_write,
62 .fo_ioctl = soo_ioctl,
64 .fo_kqfilter = soo_kqfilter,
66 .fo_close = soo_close,
67 .fo_flags = DFLAG_PASSABLE
72 soo_read(fp, uio, active_cred, flags, td)
75 struct ucred *active_cred;
79 struct socket *so = fp->f_data;
85 error = mac_check_socket_receive(active_cred, so);
92 error = soreceive(so, 0, uio, 0, 0, 0);
99 soo_write(fp, uio, active_cred, flags, td)
102 struct ucred *active_cred;
106 struct socket *so = fp->f_data;
112 error = mac_check_socket_send(active_cred, so);
119 error = sosend(so, 0, uio, 0, 0, 0, uio->uio_td);
120 if (error == EPIPE && (so->so_options & SO_NOSIGPIPE) == 0) {
121 PROC_LOCK(uio->uio_td->td_proc);
122 psignal(uio->uio_td->td_proc, SIGPIPE);
123 PROC_UNLOCK(uio->uio_td->td_proc);
130 soo_ioctl(fp, cmd, data, active_cred, td)
134 struct ucred *active_cred;
137 struct socket *so = fp->f_data;
146 so->so_state |= SS_NBIO;
148 so->so_state &= ~SS_NBIO;
154 * XXXRW: This code separately acquires SOCK_LOCK(so)
155 * and SOCKBUF_LOCK(&so->so_rcv) even though they are
156 * the same mutex to avoid introducing the assumption
157 * that they are the same.
161 so->so_state |= SS_ASYNC;
163 SOCKBUF_LOCK(&so->so_rcv);
164 so->so_rcv.sb_flags |= SB_ASYNC;
165 SOCKBUF_UNLOCK(&so->so_rcv);
166 SOCKBUF_LOCK(&so->so_snd);
167 so->so_snd.sb_flags |= SB_ASYNC;
168 SOCKBUF_UNLOCK(&so->so_snd);
171 so->so_state &= ~SS_ASYNC;
173 SOCKBUF_LOCK(&so->so_rcv);
174 so->so_rcv.sb_flags &= ~SB_ASYNC;
175 SOCKBUF_UNLOCK(&so->so_rcv);
176 SOCKBUF_LOCK(&so->so_snd);
177 so->so_snd.sb_flags &= ~SB_ASYNC;
178 SOCKBUF_UNLOCK(&so->so_snd);
184 *(int *)data = so->so_rcv.sb_cc;
188 error = fsetown(*(int *)data, &so->so_sigio);
192 *(int *)data = fgetown(&so->so_sigio);
196 error = fsetown(-(*(int *)data), &so->so_sigio);
200 *(int *)data = -fgetown(&so->so_sigio);
205 *(int *)data = (so->so_rcv.sb_state & SBS_RCVATMARK) != 0;
209 * Interface/routing/protocol specific ioctls:
210 * interface and routing ioctls should have a
211 * different entry since a socket's unnecessary
213 if (IOCGROUP(cmd) == 'i')
214 error = ifioctl(so, cmd, data, td);
215 else if (IOCGROUP(cmd) == 'r')
216 error = rtioctl(cmd, data);
218 error = ((*so->so_proto->pr_usrreqs->pru_control)
219 (so, cmd, data, 0, td));
227 soo_poll(fp, events, active_cred, td)
230 struct ucred *active_cred;
233 struct socket *so = fp->f_data;
239 error = mac_check_socket_poll(active_cred, so);
246 error = sopoll(so, events, fp->f_cred, td);
253 soo_stat(fp, ub, active_cred, td)
256 struct ucred *active_cred;
259 struct socket *so = fp->f_data;
262 bzero((caddr_t)ub, sizeof (*ub));
263 ub->st_mode = S_IFSOCK;
267 error = mac_check_socket_stat(active_cred, so);
275 * If SBS_CANTRCVMORE is set, but there's still data left in the
276 * receive buffer, the socket is still readable.
278 * XXXRW: perhaps should lock socket buffer so st_size result
282 if ((so->so_rcv.sb_state & SBS_CANTRCVMORE) == 0 ||
283 so->so_rcv.sb_cc != 0)
284 ub->st_mode |= S_IRUSR | S_IRGRP | S_IROTH;
285 if ((so->so_snd.sb_state & SBS_CANTSENDMORE) == 0)
286 ub->st_mode |= S_IWUSR | S_IWGRP | S_IWOTH;
287 ub->st_size = so->so_rcv.sb_cc - so->so_rcv.sb_ctl;
288 ub->st_uid = so->so_cred->cr_uid;
289 ub->st_gid = so->so_cred->cr_gid;
290 error = (*so->so_proto->pr_usrreqs->pru_sense)(so, ub);
296 * API socket close on file pointer. We call soclose() to close the
297 * socket (including initiating closing protocols). soclose() will
298 * sorele() the file reference but the actual socket will not go away
299 * until the socket's ref count hits 0.
312 fp->f_ops = &badfileops;