sys_socket.c revision 193511
1255670Sdes/*-
2124208Sdes * Copyright (c) 1982, 1986, 1990, 1993
3124208Sdes *	The Regents of the University of California.  All rights reserved.
4124208Sdes *
5124208Sdes * Redistribution and use in source and binary forms, with or without
6126274Sdes * modification, are permitted provided that the following conditions
7124208Sdes * are met:
8124208Sdes * 1. Redistributions of source code must retain the above copyright
9124208Sdes *    notice, this list of conditions and the following disclaimer.
10124208Sdes * 2. Redistributions in binary form must reproduce the above copyright
11124208Sdes *    notice, this list of conditions and the following disclaimer in the
12124208Sdes *    documentation and/or other materials provided with the distribution.
13124208Sdes * 4. Neither the name of the University nor the names of its contributors
14204861Sdes *    may be used to endorse or promote products derived from this software
15124208Sdes *    without specific prior written permission.
16124208Sdes *
17124208Sdes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18124208Sdes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19124208Sdes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20204861Sdes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21124208Sdes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22124208Sdes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23124208Sdes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24124208Sdes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25124208Sdes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26124208Sdes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27124208Sdes * SUCH DAMAGE.
28204861Sdes *
29124208Sdes *	@(#)sys_socket.c	8.1 (Berkeley) 6/10/93
30124208Sdes */
31124208Sdes
32124208Sdes#include <sys/cdefs.h>
33124208Sdes__FBSDID("$FreeBSD: head/sys/kern/sys_socket.c 193511 2009-06-05 14:55:22Z rwatson $");
34204861Sdes
35124208Sdes#include <sys/param.h>
36124208Sdes#include <sys/systm.h>
37124208Sdes#include <sys/file.h>
38124208Sdes#include <sys/filedesc.h>
39124208Sdes#include <sys/proc.h>
40124208Sdes#include <sys/protosw.h>
41124208Sdes#include <sys/sigio.h>
42204861Sdes#include <sys/signal.h>
43124208Sdes#include <sys/signalvar.h>
44124208Sdes#include <sys/socket.h>
45124208Sdes#include <sys/socketvar.h>
46124208Sdes#include <sys/filio.h>			/* XXX */
47124208Sdes#include <sys/sockio.h>
48124208Sdes#include <sys/stat.h>
49124208Sdes#include <sys/uio.h>
50204861Sdes#include <sys/ucred.h>
51124208Sdes#include <sys/vimage.h>
52124208Sdes
53124208Sdes#include <net/if.h>
54124208Sdes#include <net/route.h>
55124208Sdes
56124208Sdes#include <security/mac/mac_framework.h>
57124208Sdes
58124208Sdesstruct fileops	socketops = {
59124208Sdes	.fo_read = soo_read,
60204861Sdes	.fo_write = soo_write,
61124208Sdes	.fo_truncate = soo_truncate,
62124208Sdes	.fo_ioctl = soo_ioctl,
63124208Sdes	.fo_poll = soo_poll,
64124208Sdes	.fo_kqfilter = soo_kqfilter,
65124208Sdes	.fo_stat = soo_stat,
66	.fo_close = soo_close,
67	.fo_flags = DFLAG_PASSABLE
68};
69
70/* ARGSUSED */
71int
72soo_read(struct file *fp, struct uio *uio, struct ucred *active_cred,
73    int flags, struct thread *td)
74{
75	struct socket *so = fp->f_data;
76	int error;
77
78#ifdef MAC
79	error = mac_socket_check_receive(active_cred, so);
80	if (error)
81		return (error);
82#endif
83	CURVNET_SET(so->so_vnet);
84	error = soreceive(so, 0, uio, 0, 0, 0);
85	CURVNET_RESTORE();
86	return (error);
87}
88
89/* ARGSUSED */
90int
91soo_write(struct file *fp, struct uio *uio, struct ucred *active_cred,
92    int flags, struct thread *td)
93{
94	struct socket *so = fp->f_data;
95	int error;
96
97#ifdef MAC
98	error = mac_socket_check_send(active_cred, so);
99	if (error)
100		return (error);
101#endif
102	error = sosend(so, 0, uio, 0, 0, 0, uio->uio_td);
103	if (error == EPIPE && (so->so_options & SO_NOSIGPIPE) == 0) {
104		PROC_LOCK(uio->uio_td->td_proc);
105		psignal(uio->uio_td->td_proc, SIGPIPE);
106		PROC_UNLOCK(uio->uio_td->td_proc);
107	}
108	return (error);
109}
110
111int
112soo_truncate(struct file *fp, off_t length, struct ucred *active_cred,
113    struct thread *td)
114{
115
116	return (EINVAL);
117}
118
119int
120soo_ioctl(struct file *fp, u_long cmd, void *data, struct ucred *active_cred,
121    struct thread *td)
122{
123	struct socket *so = fp->f_data;
124	int error = 0;
125
126	CURVNET_SET(so->so_vnet);
127	switch (cmd) {
128	case FIONBIO:
129		SOCK_LOCK(so);
130		if (*(int *)data)
131			so->so_state |= SS_NBIO;
132		else
133			so->so_state &= ~SS_NBIO;
134		SOCK_UNLOCK(so);
135		break;
136
137	case FIOASYNC:
138		/*
139		 * XXXRW: This code separately acquires SOCK_LOCK(so) and
140		 * SOCKBUF_LOCK(&so->so_rcv) even though they are the same
141		 * mutex to avoid introducing the assumption that they are
142		 * the same.
143		 */
144		if (*(int *)data) {
145			SOCK_LOCK(so);
146			so->so_state |= SS_ASYNC;
147			SOCK_UNLOCK(so);
148			SOCKBUF_LOCK(&so->so_rcv);
149			so->so_rcv.sb_flags |= SB_ASYNC;
150			SOCKBUF_UNLOCK(&so->so_rcv);
151			SOCKBUF_LOCK(&so->so_snd);
152			so->so_snd.sb_flags |= SB_ASYNC;
153			SOCKBUF_UNLOCK(&so->so_snd);
154		} else {
155			SOCK_LOCK(so);
156			so->so_state &= ~SS_ASYNC;
157			SOCK_UNLOCK(so);
158			SOCKBUF_LOCK(&so->so_rcv);
159			so->so_rcv.sb_flags &= ~SB_ASYNC;
160			SOCKBUF_UNLOCK(&so->so_rcv);
161			SOCKBUF_LOCK(&so->so_snd);
162			so->so_snd.sb_flags &= ~SB_ASYNC;
163			SOCKBUF_UNLOCK(&so->so_snd);
164		}
165		break;
166
167	case FIONREAD:
168		/* Unlocked read. */
169		*(int *)data = so->so_rcv.sb_cc;
170		break;
171
172	case FIOSETOWN:
173		error = fsetown(*(int *)data, &so->so_sigio);
174		break;
175
176	case FIOGETOWN:
177		*(int *)data = fgetown(&so->so_sigio);
178		break;
179
180	case SIOCSPGRP:
181		error = fsetown(-(*(int *)data), &so->so_sigio);
182		break;
183
184	case SIOCGPGRP:
185		*(int *)data = -fgetown(&so->so_sigio);
186		break;
187
188	case SIOCATMARK:
189		/* Unlocked read. */
190		*(int *)data = (so->so_rcv.sb_state & SBS_RCVATMARK) != 0;
191		break;
192	default:
193		/*
194		 * Interface/routing/protocol specific ioctls: interface and
195		 * routing ioctls should have a different entry since a
196		 * socket is unnecessary.
197		 */
198		if (IOCGROUP(cmd) == 'i')
199			error = ifioctl(so, cmd, data, td);
200		else if (IOCGROUP(cmd) == 'r')
201			error = rtioctl_fib(cmd, data, so->so_fibnum);
202		else
203			error = ((*so->so_proto->pr_usrreqs->pru_control)
204			    (so, cmd, data, 0, td));
205		break;
206	}
207	CURVNET_RESTORE();
208	return (error);
209}
210
211int
212soo_poll(struct file *fp, int events, struct ucred *active_cred,
213    struct thread *td)
214{
215	struct socket *so = fp->f_data;
216#ifdef MAC
217	int error;
218
219	error = mac_socket_check_poll(active_cred, so);
220	if (error)
221		return (error);
222#endif
223	return (sopoll(so, events, fp->f_cred, td));
224}
225
226int
227soo_stat(struct file *fp, struct stat *ub, struct ucred *active_cred,
228    struct thread *td)
229{
230	struct socket *so = fp->f_data;
231#ifdef MAC
232	int error;
233#endif
234
235	bzero((caddr_t)ub, sizeof (*ub));
236	ub->st_mode = S_IFSOCK;
237#ifdef MAC
238	error = mac_socket_check_stat(active_cred, so);
239	if (error)
240		return (error);
241#endif
242	/*
243	 * If SBS_CANTRCVMORE is set, but there's still data left in the
244	 * receive buffer, the socket is still readable.
245	 */
246	SOCKBUF_LOCK(&so->so_rcv);
247	if ((so->so_rcv.sb_state & SBS_CANTRCVMORE) == 0 ||
248	    so->so_rcv.sb_cc != 0)
249		ub->st_mode |= S_IRUSR | S_IRGRP | S_IROTH;
250	ub->st_size = so->so_rcv.sb_cc - so->so_rcv.sb_ctl;
251	SOCKBUF_UNLOCK(&so->so_rcv);
252	/* Unlocked read. */
253	if ((so->so_snd.sb_state & SBS_CANTSENDMORE) == 0)
254		ub->st_mode |= S_IWUSR | S_IWGRP | S_IWOTH;
255	ub->st_uid = so->so_cred->cr_uid;
256	ub->st_gid = so->so_cred->cr_gid;
257	return (*so->so_proto->pr_usrreqs->pru_sense)(so, ub);
258}
259
260/*
261 * API socket close on file pointer.  We call soclose() to close the socket
262 * (including initiating closing protocols).  soclose() will sorele() the
263 * file reference but the actual socket will not go away until the socket's
264 * ref count hits 0.
265 */
266/* ARGSUSED */
267int
268soo_close(struct file *fp, struct thread *td)
269{
270	int error = 0;
271	struct socket *so;
272
273	so = fp->f_data;
274	fp->f_ops = &badfileops;
275	fp->f_data = NULL;
276
277	if (so)
278		error = soclose(so);
279	return (error);
280}
281