xref: /freebsd/sys/kern/sys_socket.c (revision f0adf7f5cdd241db2f2c817683191a6ef64a4e95)
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/mac.h>
42 #include <sys/protosw.h>
43 #include <sys/sigio.h>
44 #include <sys/socket.h>
45 #include <sys/socketvar.h>
46 #include <sys/filio.h>			/* XXX */
47 #include <sys/sockio.h>
48 #include <sys/stat.h>
49 #include <sys/uio.h>
50 #include <sys/ucred.h>
51 
52 #include <net/if.h>
53 #include <net/route.h>
54 
55 struct	fileops socketops = {
56 	.fo_read = soo_read,
57 	.fo_write = soo_write,
58 	.fo_ioctl = soo_ioctl,
59 	.fo_poll = soo_poll,
60 	.fo_kqfilter = soo_kqfilter,
61 	.fo_stat = soo_stat,
62 	.fo_close = soo_close,
63 	.fo_flags = DFLAG_PASSABLE
64 };
65 
66 /* ARGSUSED */
67 int
68 soo_read(fp, uio, active_cred, flags, td)
69 	struct file *fp;
70 	struct uio *uio;
71 	struct ucred *active_cred;
72 	struct thread *td;
73 	int flags;
74 {
75 	struct socket *so = fp->f_data;
76 	int error;
77 
78 	NET_LOCK_GIANT();
79 #ifdef MAC
80 	SOCK_LOCK(so);
81 	error = mac_check_socket_receive(active_cred, so);
82 	SOCK_UNLOCK(so);
83 	if (error) {
84 		NET_UNLOCK_GIANT();
85 		return (error);
86 	}
87 #endif
88 	error = so->so_proto->pr_usrreqs->pru_soreceive(so, 0, uio, 0, 0, 0);
89 	NET_UNLOCK_GIANT();
90 	return (error);
91 }
92 
93 /* ARGSUSED */
94 int
95 soo_write(fp, uio, active_cred, flags, td)
96 	struct file *fp;
97 	struct uio *uio;
98 	struct ucred *active_cred;
99 	struct thread *td;
100 	int flags;
101 {
102 	struct socket *so = fp->f_data;
103 	int error;
104 
105 	NET_LOCK_GIANT();
106 #ifdef MAC
107 	SOCK_LOCK(so);
108 	error = mac_check_socket_send(active_cred, so);
109 	SOCK_UNLOCK(so);
110 	if (error) {
111 		NET_UNLOCK_GIANT();
112 		return (error);
113 	}
114 #endif
115 	error = so->so_proto->pr_usrreqs->pru_sosend(so, 0, uio, 0, 0, 0,
116 						    uio->uio_td);
117 	NET_UNLOCK_GIANT();
118 	return (error);
119 }
120 
121 int
122 soo_ioctl(fp, cmd, data, active_cred, td)
123 	struct file *fp;
124 	u_long cmd;
125 	void *data;
126 	struct ucred *active_cred;
127 	struct thread *td;
128 {
129 	register struct socket *so = fp->f_data;
130 
131 	switch (cmd) {
132 
133 	case FIONBIO:
134 		SOCK_LOCK(so);
135 		if (*(int *)data)
136 			so->so_state |= SS_NBIO;
137 		else
138 			so->so_state &= ~SS_NBIO;
139 		SOCK_UNLOCK(so);
140 		return (0);
141 
142 	case FIOASYNC:
143 		/*
144 		 * XXXRW: This code separately acquires SOCK_LOCK(so)
145 		 * and SOCKBUF_LOCK(&so->so_rcv) even though they are
146 		 * the same mutex to avoid introducing the assumption
147 		 * that they are the same.
148 		 */
149 		if (*(int *)data) {
150 			SOCK_LOCK(so);
151 			so->so_state |= SS_ASYNC;
152 			SOCK_UNLOCK(so);
153 			SOCKBUF_LOCK(&so->so_rcv);
154 			so->so_rcv.sb_flags |= SB_ASYNC;
155 			SOCKBUF_UNLOCK(&so->so_rcv);
156 			SOCKBUF_LOCK(&so->so_snd);
157 			so->so_snd.sb_flags |= SB_ASYNC;
158 			SOCKBUF_UNLOCK(&so->so_snd);
159 		} else {
160 			SOCK_LOCK(so);
161 			so->so_state &= ~SS_ASYNC;
162 			SOCK_UNLOCK(so);
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);
169 		}
170 		return (0);
171 
172 	case FIONREAD:
173 		/* Unlocked read. */
174 		*(int *)data = so->so_rcv.sb_cc;
175 		return (0);
176 
177 	case FIOSETOWN:
178 		return (fsetown(*(int *)data, &so->so_sigio));
179 
180 	case FIOGETOWN:
181 		*(int *)data = fgetown(&so->so_sigio);
182 		return (0);
183 
184 	case SIOCSPGRP:
185 		return (fsetown(-(*(int *)data), &so->so_sigio));
186 
187 	case SIOCGPGRP:
188 		*(int *)data = -fgetown(&so->so_sigio);
189 		return (0);
190 
191 	case SIOCATMARK:
192 		/* Unlocked read. */
193 		*(int *)data = (so->so_rcv.sb_state & SBS_RCVATMARK) != 0;
194 		return (0);
195 	}
196 	/*
197 	 * Interface/routing/protocol specific ioctls:
198 	 * interface and routing ioctls should have a
199 	 * different entry since a socket's unnecessary
200 	 */
201 	if (IOCGROUP(cmd) == 'i')
202 		return (ifioctl(so, cmd, data, td));
203 	if (IOCGROUP(cmd) == 'r')
204 		return (rtioctl(cmd, data));
205 	return ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd, data, 0, td));
206 }
207 
208 int
209 soo_poll(fp, events, active_cred, td)
210 	struct file *fp;
211 	int events;
212 	struct ucred *active_cred;
213 	struct thread *td;
214 {
215 	struct socket *so = fp->f_data;
216 	return so->so_proto->pr_usrreqs->pru_sopoll(so, events,
217 	    fp->f_cred, td);
218 }
219 
220 int
221 soo_stat(fp, ub, active_cred, td)
222 	struct file *fp;
223 	struct stat *ub;
224 	struct ucred *active_cred;
225 	struct thread *td;
226 {
227 	struct socket *so = fp->f_data;
228 
229 	bzero((caddr_t)ub, sizeof (*ub));
230 	ub->st_mode = S_IFSOCK;
231 	/*
232 	 * If SBS_CANTRCVMORE is set, but there's still data left in the
233 	 * receive buffer, the socket is still readable.
234 	 *
235 	 * XXXRW: perhaps should lock socket buffer so st_size result
236 	 * is consistent.
237 	 */
238 	/* Unlocked read. */
239 	if ((so->so_rcv.sb_state & SBS_CANTRCVMORE) == 0 ||
240 	    so->so_rcv.sb_cc != 0)
241 		ub->st_mode |= S_IRUSR | S_IRGRP | S_IROTH;
242 	if ((so->so_snd.sb_state & SBS_CANTSENDMORE) == 0)
243 		ub->st_mode |= S_IWUSR | S_IWGRP | S_IWOTH;
244 	ub->st_size = so->so_rcv.sb_cc - so->so_rcv.sb_ctl;
245 	ub->st_uid = so->so_cred->cr_uid;
246 	ub->st_gid = so->so_cred->cr_gid;
247 	return ((*so->so_proto->pr_usrreqs->pru_sense)(so, ub));
248 }
249 
250 /*
251  * API socket close on file pointer.  We call soclose() to close the
252  * socket (including initiating closing protocols).  soclose() will
253  * sorele() the file reference but the actual socket will not go away
254  * until the socket's ref count hits 0.
255  */
256 /* ARGSUSED */
257 int
258 soo_close(fp, td)
259 	struct file *fp;
260 	struct thread *td;
261 {
262 	int error = 0;
263 	struct socket *so;
264 
265 	so = fp->f_data;
266 	fp->f_ops = &badfileops;
267 	fp->f_data = NULL;
268 
269 	if (so)
270 		error = soclose(so);
271 	return (error);
272 }
273