xref: /freebsd/sys/kern/sys_socket.c (revision cf4c5a533126ca1ddb1f070af73f8f53b9e77fd4)
1 /*-
2  * Copyright (c) 1982, 1986, 1990, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
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.
16  *
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.
28  *
29  *	@(#)sys_socket.c	8.1 (Berkeley) 6/10/93
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include "opt_mac.h"
36 
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>
53 #include <sys/vimage.h>
54 
55 #include <net/if.h>
56 #include <net/route.h>
57 
58 #include <security/mac/mac_framework.h>
59 
60 struct fileops	socketops = {
61 	.fo_read = soo_read,
62 	.fo_write = soo_write,
63 	.fo_truncate = soo_truncate,
64 	.fo_ioctl = soo_ioctl,
65 	.fo_poll = soo_poll,
66 	.fo_kqfilter = soo_kqfilter,
67 	.fo_stat = soo_stat,
68 	.fo_close = soo_close,
69 	.fo_flags = DFLAG_PASSABLE
70 };
71 
72 /* ARGSUSED */
73 int
74 soo_read(struct file *fp, struct uio *uio, struct ucred *active_cred,
75     int flags, struct thread *td)
76 {
77 	struct socket *so = fp->f_data;
78 	int error;
79 
80 #ifdef MAC
81 	SOCK_LOCK(so);
82 	error = mac_socket_check_receive(active_cred, so);
83 	SOCK_UNLOCK(so);
84 	if (error)
85 		return (error);
86 #endif
87 	CURVNET_SET(so->so_vnet);
88 	error = soreceive(so, 0, uio, 0, 0, 0);
89 	CURVNET_RESTORE();
90 	return (error);
91 }
92 
93 /* ARGSUSED */
94 int
95 soo_write(struct file *fp, struct uio *uio, struct ucred *active_cred,
96     int flags, struct thread *td)
97 {
98 	struct socket *so = fp->f_data;
99 	int error;
100 
101 #ifdef MAC
102 	SOCK_LOCK(so);
103 	error = mac_socket_check_send(active_cred, so);
104 	SOCK_UNLOCK(so);
105 	if (error)
106 		return (error);
107 #endif
108 	error = sosend(so, 0, uio, 0, 0, 0, uio->uio_td);
109 	if (error == EPIPE && (so->so_options & SO_NOSIGPIPE) == 0) {
110 		PROC_LOCK(uio->uio_td->td_proc);
111 		psignal(uio->uio_td->td_proc, SIGPIPE);
112 		PROC_UNLOCK(uio->uio_td->td_proc);
113 	}
114 	return (error);
115 }
116 
117 int
118 soo_truncate(struct file *fp, off_t length, struct ucred *active_cred,
119     struct thread *td)
120 {
121 
122 	return (EINVAL);
123 }
124 
125 int
126 soo_ioctl(struct file *fp, u_long cmd, void *data, struct ucred *active_cred,
127     struct thread *td)
128 {
129 	struct socket *so = fp->f_data;
130 	int error = 0;
131 
132 	CURVNET_SET(so->so_vnet);
133 	switch (cmd) {
134 	case FIONBIO:
135 		SOCK_LOCK(so);
136 		if (*(int *)data)
137 			so->so_state |= SS_NBIO;
138 		else
139 			so->so_state &= ~SS_NBIO;
140 		SOCK_UNLOCK(so);
141 		break;
142 
143 	case FIOASYNC:
144 		/*
145 		 * XXXRW: This code separately acquires SOCK_LOCK(so) and
146 		 * SOCKBUF_LOCK(&so->so_rcv) even though they are the same
147 		 * mutex to avoid introducing the assumption that they are
148 		 * the same.
149 		 */
150 		if (*(int *)data) {
151 			SOCK_LOCK(so);
152 			so->so_state |= SS_ASYNC;
153 			SOCK_UNLOCK(so);
154 			SOCKBUF_LOCK(&so->so_rcv);
155 			so->so_rcv.sb_flags |= SB_ASYNC;
156 			SOCKBUF_UNLOCK(&so->so_rcv);
157 			SOCKBUF_LOCK(&so->so_snd);
158 			so->so_snd.sb_flags |= SB_ASYNC;
159 			SOCKBUF_UNLOCK(&so->so_snd);
160 		} else {
161 			SOCK_LOCK(so);
162 			so->so_state &= ~SS_ASYNC;
163 			SOCK_UNLOCK(so);
164 			SOCKBUF_LOCK(&so->so_rcv);
165 			so->so_rcv.sb_flags &= ~SB_ASYNC;
166 			SOCKBUF_UNLOCK(&so->so_rcv);
167 			SOCKBUF_LOCK(&so->so_snd);
168 			so->so_snd.sb_flags &= ~SB_ASYNC;
169 			SOCKBUF_UNLOCK(&so->so_snd);
170 		}
171 		break;
172 
173 	case FIONREAD:
174 		/* Unlocked read. */
175 		*(int *)data = so->so_rcv.sb_cc;
176 		break;
177 
178 	case FIOSETOWN:
179 		error = fsetown(*(int *)data, &so->so_sigio);
180 		break;
181 
182 	case FIOGETOWN:
183 		*(int *)data = fgetown(&so->so_sigio);
184 		break;
185 
186 	case SIOCSPGRP:
187 		error = fsetown(-(*(int *)data), &so->so_sigio);
188 		break;
189 
190 	case SIOCGPGRP:
191 		*(int *)data = -fgetown(&so->so_sigio);
192 		break;
193 
194 	case SIOCATMARK:
195 		/* Unlocked read. */
196 		*(int *)data = (so->so_rcv.sb_state & SBS_RCVATMARK) != 0;
197 		break;
198 	default:
199 		/*
200 		 * Interface/routing/protocol specific ioctls: interface and
201 		 * routing ioctls should have a different entry since a
202 		 * socket is unnecessary.
203 		 */
204 		if (IOCGROUP(cmd) == 'i')
205 			error = ifioctl(so, cmd, data, td);
206 		else if (IOCGROUP(cmd) == 'r')
207 			error = rtioctl_fib(cmd, data, so->so_fibnum);
208 		else
209 			error = ((*so->so_proto->pr_usrreqs->pru_control)
210 			    (so, cmd, data, 0, td));
211 		break;
212 	}
213 	CURVNET_RESTORE();
214 	return (error);
215 }
216 
217 int
218 soo_poll(struct file *fp, int events, struct ucred *active_cred,
219     struct thread *td)
220 {
221 	struct socket *so = fp->f_data;
222 #ifdef MAC
223 	int error;
224 
225 	SOCK_LOCK(so);
226 	error = mac_socket_check_poll(active_cred, so);
227 	SOCK_UNLOCK(so);
228 	if (error)
229 		return (error);
230 #endif
231 	return (sopoll(so, events, fp->f_cred, td));
232 }
233 
234 int
235 soo_stat(struct file *fp, struct stat *ub, struct ucred *active_cred,
236     struct thread *td)
237 {
238 	struct socket *so = fp->f_data;
239 #ifdef MAC
240 	int error;
241 #endif
242 
243 	bzero((caddr_t)ub, sizeof (*ub));
244 	ub->st_mode = S_IFSOCK;
245 #ifdef MAC
246 	SOCK_LOCK(so);
247 	error = mac_socket_check_stat(active_cred, so);
248 	SOCK_UNLOCK(so);
249 	if (error)
250 		return (error);
251 #endif
252 	/*
253 	 * If SBS_CANTRCVMORE is set, but there's still data left in the
254 	 * receive buffer, the socket is still readable.
255 	 */
256 	SOCKBUF_LOCK(&so->so_rcv);
257 	if ((so->so_rcv.sb_state & SBS_CANTRCVMORE) == 0 ||
258 	    so->so_rcv.sb_cc != 0)
259 		ub->st_mode |= S_IRUSR | S_IRGRP | S_IROTH;
260 	ub->st_size = so->so_rcv.sb_cc - so->so_rcv.sb_ctl;
261 	SOCKBUF_UNLOCK(&so->so_rcv);
262 	/* Unlocked read. */
263 	if ((so->so_snd.sb_state & SBS_CANTSENDMORE) == 0)
264 		ub->st_mode |= S_IWUSR | S_IWGRP | S_IWOTH;
265 	ub->st_uid = so->so_cred->cr_uid;
266 	ub->st_gid = so->so_cred->cr_gid;
267 	return (*so->so_proto->pr_usrreqs->pru_sense)(so, ub);
268 }
269 
270 /*
271  * API socket close on file pointer.  We call soclose() to close the socket
272  * (including initiating closing protocols).  soclose() will sorele() the
273  * file reference but the actual socket will not go away until the socket's
274  * ref count hits 0.
275  */
276 /* ARGSUSED */
277 int
278 soo_close(struct file *fp, struct thread *td)
279 {
280 	int error = 0;
281 	struct socket *so;
282 
283 	so = fp->f_data;
284 	fp->f_ops = &badfileops;
285 	fp->f_data = NULL;
286 
287 	if (so)
288 		error = soclose(so);
289 	return (error);
290 }
291