Ignore machine-check MSRs
[freebsd-src/fkvm-freebsd.git] / sys / kern / sys_socket.c
blobaeeaf33f46c90d9693b27430581da8b24051433f
1 /*-
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
7 * are met:
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
27 * SUCH DAMAGE.
29 * @(#)sys_socket.c 8.1 (Berkeley) 6/10/93
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include "opt_mac.h"
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/file.h>
40 #include <sys/filedesc.h>
41 #include <sys/proc.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>
50 #include <sys/stat.h>
51 #include <sys/uio.h>
52 #include <sys/ucred.h>
54 #include <net/if.h>
55 #include <net/route.h>
57 #include <security/mac/mac_framework.h>
59 struct fileops socketops = {
60 .fo_read = soo_read,
61 .fo_write = soo_write,
62 .fo_truncate = soo_truncate,
63 .fo_ioctl = soo_ioctl,
64 .fo_poll = soo_poll,
65 .fo_kqfilter = soo_kqfilter,
66 .fo_stat = soo_stat,
67 .fo_close = soo_close,
68 .fo_flags = DFLAG_PASSABLE
71 /* ARGSUSED */
72 int
73 soo_read(struct file *fp, struct uio *uio, struct ucred *active_cred,
74 int flags, struct thread *td)
76 struct socket *so = fp->f_data;
77 #ifdef MAC
78 int error;
80 SOCK_LOCK(so);
81 error = mac_socket_check_receive(active_cred, so);
82 SOCK_UNLOCK(so);
83 if (error)
84 return (error);
85 #endif
86 return (soreceive(so, 0, uio, 0, 0, 0));
89 /* ARGSUSED */
90 int
91 soo_write(struct file *fp, struct uio *uio, struct ucred *active_cred,
92 int flags, struct thread *td)
94 struct socket *so = fp->f_data;
95 int error;
97 #ifdef MAC
98 SOCK_LOCK(so);
99 error = mac_socket_check_send(active_cred, so);
100 SOCK_UNLOCK(so);
101 if (error)
102 return (error);
103 #endif
104 error = sosend(so, 0, uio, 0, 0, 0, uio->uio_td);
105 if (error == EPIPE && (so->so_options & SO_NOSIGPIPE) == 0) {
106 PROC_LOCK(uio->uio_td->td_proc);
107 psignal(uio->uio_td->td_proc, SIGPIPE);
108 PROC_UNLOCK(uio->uio_td->td_proc);
110 return (error);
114 soo_truncate(struct file *fp, off_t length, struct ucred *active_cred,
115 struct thread *td)
118 return (EINVAL);
122 soo_ioctl(struct file *fp, u_long cmd, void *data, struct ucred *active_cred,
123 struct thread *td)
125 struct socket *so = fp->f_data;
126 int error = 0;
128 switch (cmd) {
129 case FIONBIO:
130 SOCK_LOCK(so);
131 if (*(int *)data)
132 so->so_state |= SS_NBIO;
133 else
134 so->so_state &= ~SS_NBIO;
135 SOCK_UNLOCK(so);
136 break;
138 case FIOASYNC:
140 * XXXRW: This code separately acquires SOCK_LOCK(so) and
141 * SOCKBUF_LOCK(&so->so_rcv) even though they are the same
142 * mutex to avoid introducing the assumption that they are
143 * the same.
145 if (*(int *)data) {
146 SOCK_LOCK(so);
147 so->so_state |= SS_ASYNC;
148 SOCK_UNLOCK(so);
149 SOCKBUF_LOCK(&so->so_rcv);
150 so->so_rcv.sb_flags |= SB_ASYNC;
151 SOCKBUF_UNLOCK(&so->so_rcv);
152 SOCKBUF_LOCK(&so->so_snd);
153 so->so_snd.sb_flags |= SB_ASYNC;
154 SOCKBUF_UNLOCK(&so->so_snd);
155 } else {
156 SOCK_LOCK(so);
157 so->so_state &= ~SS_ASYNC;
158 SOCK_UNLOCK(so);
159 SOCKBUF_LOCK(&so->so_rcv);
160 so->so_rcv.sb_flags &= ~SB_ASYNC;
161 SOCKBUF_UNLOCK(&so->so_rcv);
162 SOCKBUF_LOCK(&so->so_snd);
163 so->so_snd.sb_flags &= ~SB_ASYNC;
164 SOCKBUF_UNLOCK(&so->so_snd);
166 break;
168 case FIONREAD:
169 /* Unlocked read. */
170 *(int *)data = so->so_rcv.sb_cc;
171 break;
173 case FIOSETOWN:
174 error = fsetown(*(int *)data, &so->so_sigio);
175 break;
177 case FIOGETOWN:
178 *(int *)data = fgetown(&so->so_sigio);
179 break;
181 case SIOCSPGRP:
182 error = fsetown(-(*(int *)data), &so->so_sigio);
183 break;
185 case SIOCGPGRP:
186 *(int *)data = -fgetown(&so->so_sigio);
187 break;
189 case SIOCATMARK:
190 /* Unlocked read. */
191 *(int *)data = (so->so_rcv.sb_state & SBS_RCVATMARK) != 0;
192 break;
193 default:
195 * Interface/routing/protocol specific ioctls: interface and
196 * routing ioctls should have a different entry since a
197 * socket is unnecessary.
199 if (IOCGROUP(cmd) == 'i')
200 error = ifioctl(so, cmd, data, td);
201 else if (IOCGROUP(cmd) == 'r')
202 error = rtioctl_fib(cmd, data, so->so_fibnum);
203 else
204 error = ((*so->so_proto->pr_usrreqs->pru_control)
205 (so, cmd, data, 0, td));
206 break;
208 return (error);
212 soo_poll(struct file *fp, int events, struct ucred *active_cred,
213 struct thread *td)
215 struct socket *so = fp->f_data;
216 #ifdef MAC
217 int error;
219 SOCK_LOCK(so);
220 error = mac_socket_check_poll(active_cred, so);
221 SOCK_UNLOCK(so);
222 if (error)
223 return (error);
224 #endif
225 return (sopoll(so, events, fp->f_cred, td));
229 soo_stat(struct file *fp, struct stat *ub, struct ucred *active_cred,
230 struct thread *td)
232 struct socket *so = fp->f_data;
233 #ifdef MAC
234 int error;
235 #endif
237 bzero((caddr_t)ub, sizeof (*ub));
238 ub->st_mode = S_IFSOCK;
239 #ifdef MAC
240 SOCK_LOCK(so);
241 error = mac_socket_check_stat(active_cred, so);
242 SOCK_UNLOCK(so);
243 if (error)
244 return (error);
245 #endif
247 * If SBS_CANTRCVMORE is set, but there's still data left in the
248 * receive buffer, the socket is still readable.
250 * XXXRW: perhaps should lock socket buffer so st_size result is
251 * consistent.
253 /* Unlocked read. */
254 if ((so->so_rcv.sb_state & SBS_CANTRCVMORE) == 0 ||
255 so->so_rcv.sb_cc != 0)
256 ub->st_mode |= S_IRUSR | S_IRGRP | S_IROTH;
257 if ((so->so_snd.sb_state & SBS_CANTSENDMORE) == 0)
258 ub->st_mode |= S_IWUSR | S_IWGRP | S_IWOTH;
259 ub->st_size = so->so_rcv.sb_cc - so->so_rcv.sb_ctl;
260 ub->st_uid = so->so_cred->cr_uid;
261 ub->st_gid = so->so_cred->cr_gid;
262 return (*so->so_proto->pr_usrreqs->pru_sense)(so, ub);
266 * API socket close on file pointer. We call soclose() to close the socket
267 * (including initiating closing protocols). soclose() will sorele() the
268 * file reference but the actual socket will not go away until the socket's
269 * ref count hits 0.
271 /* ARGSUSED */
273 soo_close(struct file *fp, struct thread *td)
275 int error = 0;
276 struct socket *so;
278 so = fp->f_data;
279 fp->f_ops = &badfileops;
280 fp->f_data = NULL;
282 if (so)
283 error = soclose(so);
284 return (error);