sys_socket.c revision 224914
1208538Sraj/*-
2208538Sraj * Copyright (c) 1982, 1986, 1990, 1993
3208538Sraj *	The Regents of the University of California.  All rights reserved.
4208538Sraj *
5208538Sraj * Redistribution and use in source and binary forms, with or without
6208538Sraj * modification, are permitted provided that the following conditions
7208538Sraj * are met:
8208538Sraj * 1. Redistributions of source code must retain the above copyright
9208538Sraj *    notice, this list of conditions and the following disclaimer.
10208538Sraj * 2. Redistributions in binary form must reproduce the above copyright
11208538Sraj *    notice, this list of conditions and the following disclaimer in the
12208538Sraj *    documentation and/or other materials provided with the distribution.
13208538Sraj * 4. Neither the name of the University nor the names of its contributors
14208538Sraj *    may be used to endorse or promote products derived from this software
15208538Sraj *    without specific prior written permission.
16208538Sraj *
17208538Sraj * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18208538Sraj * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19208538Sraj * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20208538Sraj * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21208538Sraj * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22208538Sraj * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23208538Sraj * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24208538Sraj * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25208538Sraj * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26208538Sraj * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27208538Sraj * SUCH DAMAGE.
28208538Sraj *
29208538Sraj *	@(#)sys_socket.c	8.1 (Berkeley) 6/10/93
30208538Sraj */
31208538Sraj
32208538Sraj#include <sys/cdefs.h>
33208538Sraj__FBSDID("$FreeBSD: head/sys/kern/sys_socket.c 224914 2011-08-16 20:07:47Z kib $");
34208538Sraj
35208538Sraj#include <sys/param.h>
36233230Sraj#include <sys/systm.h>
37233230Sraj#include <sys/file.h>
38233230Sraj#include <sys/filedesc.h>
39208538Sraj#include <sys/proc.h>
40208538Sraj#include <sys/protosw.h>
41208538Sraj#include <sys/sigio.h>
42208538Sraj#include <sys/signal.h>
43208538Sraj#include <sys/signalvar.h>
44208538Sraj#include <sys/socket.h>
45208538Sraj#include <sys/socketvar.h>
46208538Sraj#include <sys/filio.h>			/* XXX */
47208538Sraj#include <sys/sockio.h>
48208538Sraj#include <sys/stat.h>
49208538Sraj#include <sys/uio.h>
50208538Sraj#include <sys/ucred.h>
51208538Sraj
52208538Sraj#include <net/if.h>
53208538Sraj#include <net/route.h>
54208538Sraj#include <net/vnet.h>
55208538Sraj
56208538Sraj#include <security/mac/mac_framework.h>
57208538Sraj
58235529Skientzlestruct fileops	socketops = {
59235529Skientzle	.fo_read = soo_read,
60233230Sraj	.fo_write = soo_write,
61233230Sraj	.fo_truncate = soo_truncate,
62233230Sraj	.fo_ioctl = soo_ioctl,
63243693Sgonzo	.fo_poll = soo_poll,
64243693Sgonzo	.fo_kqfilter = soo_kqfilter,
65247201Skientzle	.fo_stat = soo_stat,
66247250Skientzle	.fo_close = soo_close,
67247201Skientzle	.fo_chmod = invfo_chmod,
68247250Skientzle	.fo_chown = invfo_chown,
69247250Skientzle	.fo_flags = DFLAG_PASSABLE
70208538Sraj};
71235529Skientzle
72235529Skientzle/* ARGSUSED */
73247250Skientzleint
74247250Skientzlesoo_read(struct file *fp, struct uio *uio, struct ucred *active_cred,
75247250Skientzle    int flags, struct thread *td)
76235529Skientzle{
77208538Sraj	struct socket *so = fp->f_data;
78243693Sgonzo	int error;
79243693Sgonzo
80208538Sraj#ifdef MAC
81208538Sraj	error = mac_socket_check_receive(active_cred, so);
82243693Sgonzo	if (error)
83208538Sraj		return (error);
84208538Sraj#endif
85208538Sraj	error = soreceive(so, 0, uio, 0, 0, 0);
86208538Sraj	return (error);
87208538Sraj}
88208538Sraj
89208538Sraj/* ARGSUSED */
90208538Srajint
91243693Sgonzosoo_write(struct file *fp, struct uio *uio, struct ucred *active_cred,
92208538Sraj    int flags, struct thread *td)
93208538Sraj{
94208538Sraj	struct socket *so = fp->f_data;
95208538Sraj	int error;
96208538Sraj
97208538Sraj#ifdef MAC
98243693Sgonzo	error = mac_socket_check_send(active_cred, so);
99208538Sraj	if (error)
100208538Sraj		return (error);
101208538Sraj#endif
102243693Sgonzo	error = sosend(so, 0, uio, 0, 0, 0, uio->uio_td);
103243693Sgonzo	if (error == EPIPE && (so->so_options & SO_NOSIGPIPE) == 0) {
104243693Sgonzo		PROC_LOCK(uio->uio_td->td_proc);
105243693Sgonzo		tdsignal(uio->uio_td, SIGPIPE);
106243693Sgonzo		PROC_UNLOCK(uio->uio_td->td_proc);
107243693Sgonzo	}
108243693Sgonzo	return (error);
109243693Sgonzo}
110243693Sgonzo
111243693Sgonzoint
112243693Sgonzosoo_truncate(struct file *fp, off_t length, struct ucred *active_cred,
113208538Sraj    struct thread *td)
114208538Sraj{
115208538Sraj
116208538Sraj	return (EINVAL);
117208538Sraj}
118233230Sraj
119235529Skientzleint
120233230Srajsoo_ioctl(struct file *fp, u_long cmd, void *data, struct ucred *active_cred,
121248121Sian    struct thread *td)
122248121Sian{
123233230Sraj	struct socket *so = fp->f_data;
124248121Sian	int error = 0;
125233230Sraj
126233230Sraj	switch (cmd) {
127233230Sraj	case FIONBIO:
128235529Skientzle		SOCK_LOCK(so);
129248121Sian		if (*(int *)data)
130233230Sraj			so->so_state |= SS_NBIO;
131248934Skientzle		else
132235529Skientzle			so->so_state &= ~SS_NBIO;
133233230Sraj		SOCK_UNLOCK(so);
134233230Sraj		break;
135233230Sraj
136233230Sraj	case FIOASYNC:
137233230Sraj		/*
138233230Sraj		 * XXXRW: This code separately acquires SOCK_LOCK(so) and
139233230Sraj		 * SOCKBUF_LOCK(&so->so_rcv) even though they are the same
140248121Sian		 * mutex to avoid introducing the assumption that they are
141248121Sian		 * the same.
142233230Sraj		 */
143233230Sraj		if (*(int *)data) {
144248121Sian			SOCK_LOCK(so);
145233230Sraj			so->so_state |= SS_ASYNC;
146248121Sian			SOCK_UNLOCK(so);
147233230Sraj			SOCKBUF_LOCK(&so->so_rcv);
148233230Sraj			so->so_rcv.sb_flags |= SB_ASYNC;
149248121Sian			SOCKBUF_UNLOCK(&so->so_rcv);
150233230Sraj			SOCKBUF_LOCK(&so->so_snd);
151248121Sian			so->so_snd.sb_flags |= SB_ASYNC;
152248121Sian			SOCKBUF_UNLOCK(&so->so_snd);
153248121Sian		} else {
154248121Sian			SOCK_LOCK(so);
155248121Sian			so->so_state &= ~SS_ASYNC;
156248121Sian			SOCK_UNLOCK(so);
157233230Sraj			SOCKBUF_LOCK(&so->so_rcv);
158233230Sraj			so->so_rcv.sb_flags &= ~SB_ASYNC;
159233230Sraj			SOCKBUF_UNLOCK(&so->so_rcv);
160233230Sraj			SOCKBUF_LOCK(&so->so_snd);
161233230Sraj			so->so_snd.sb_flags &= ~SB_ASYNC;
162233230Sraj			SOCKBUF_UNLOCK(&so->so_snd);
163233230Sraj		}
164233230Sraj		break;
165233230Sraj
166233230Sraj	case FIONREAD:
167233230Sraj		/* Unlocked read. */
168233230Sraj		*(int *)data = so->so_rcv.sb_cc;
169233230Sraj		break;
170235529Skientzle
171235529Skientzle	case FIONWRITE:
172235529Skientzle		/* Unlocked read. */
173235529Skientzle		*(int *)data = so->so_snd.sb_cc;
174235529Skientzle		break;
175233230Sraj
176233230Sraj	case FIONSPACE:
177233230Sraj		if ((so->so_snd.sb_hiwat < so->so_snd.sb_cc) ||
178235529Skientzle		    (so->so_snd.sb_mbmax < so->so_snd.sb_mbcnt))
179235529Skientzle			*(int *)data = 0;
180235529Skientzle		else
181233230Sraj			*(int *)data = sbspace(&so->so_snd);
182233230Sraj		break;
183235529Skientzle
184233230Sraj	case FIOSETOWN:
185233230Sraj		error = fsetown(*(int *)data, &so->so_sigio);
186208538Sraj		break;
187243693Sgonzo
188208538Sraj	case FIOGETOWN:
189235529Skientzle		*(int *)data = fgetown(&so->so_sigio);
190208538Sraj		break;
191208538Sraj
192243693Sgonzo	case SIOCSPGRP:
193243693Sgonzo		error = fsetown(-(*(int *)data), &so->so_sigio);
194243693Sgonzo		break;
195243693Sgonzo
196243693Sgonzo	case SIOCGPGRP:
197243693Sgonzo		*(int *)data = -fgetown(&so->so_sigio);
198243693Sgonzo		break;
199243693Sgonzo
200243693Sgonzo	case SIOCATMARK:
201243693Sgonzo		/* Unlocked read. */
202243693Sgonzo		*(int *)data = (so->so_rcv.sb_state & SBS_RCVATMARK) != 0;
203243693Sgonzo		break;
204243693Sgonzo	default:
205243693Sgonzo		/*
206208538Sraj		 * Interface/routing/protocol specific ioctls: interface and
207243693Sgonzo		 * routing ioctls should have a different entry since a
208208538Sraj		 * socket is unnecessary.
209243693Sgonzo		 */
210243693Sgonzo		if (IOCGROUP(cmd) == 'i')
211208538Sraj			error = ifioctl(so, cmd, data, td);
212235529Skientzle		else if (IOCGROUP(cmd) == 'r') {
213235529Skientzle			CURVNET_SET(so->so_vnet);
214243693Sgonzo			error = rtioctl_fib(cmd, data, so->so_fibnum);
215235529Skientzle			CURVNET_RESTORE();
216235529Skientzle		} else {
217243693Sgonzo			CURVNET_SET(so->so_vnet);
218235529Skientzle			error = ((*so->so_proto->pr_usrreqs->pru_control)
219235529Skientzle			    (so, cmd, data, 0, td));
220243693Sgonzo			CURVNET_RESTORE();
221243693Sgonzo		}
222243693Sgonzo		break;
223208538Sraj	}
224243693Sgonzo	return (error);
225243693Sgonzo}
226243693Sgonzo
227243693Sgonzoint
228247045Skientzlesoo_poll(struct file *fp, int events, struct ucred *active_cred,
229243693Sgonzo    struct thread *td)
230243693Sgonzo{
231247250Skientzle	struct socket *so = fp->f_data;
232247250Skientzle#ifdef MAC
233247250Skientzle	int error;
234247250Skientzle
235247250Skientzle	error = mac_socket_check_poll(active_cred, so);
236247250Skientzle	if (error)
237247045Skientzle		return (error);
238208538Sraj#endif
239247250Skientzle	return (sopoll(so, events, fp->f_cred, td));
240247250Skientzle}
241247250Skientzle
242247250Skientzleint
243247045Skientzlesoo_stat(struct file *fp, struct stat *ub, struct ucred *active_cred,
244243693Sgonzo    struct thread *td)
245247045Skientzle{
246247045Skientzle	struct socket *so = fp->f_data;
247247045Skientzle#ifdef MAC
248247045Skientzle	int error;
249247045Skientzle#endif
250247201Skientzle
251247201Skientzle	bzero((caddr_t)ub, sizeof (*ub));
252247045Skientzle	ub->st_mode = S_IFSOCK;
253247045Skientzle#ifdef MAC
254247045Skientzle	error = mac_socket_check_stat(active_cred, so);
255247045Skientzle	if (error)
256247045Skientzle		return (error);
257247201Skientzle#endif
258247045Skientzle	/*
259247201Skientzle	 * If SBS_CANTRCVMORE is set, but there's still data left in the
260247201Skientzle	 * receive buffer, the socket is still readable.
261247201Skientzle	 */
262247201Skientzle	SOCKBUF_LOCK(&so->so_rcv);
263247201Skientzle	if ((so->so_rcv.sb_state & SBS_CANTRCVMORE) == 0 ||
264247201Skientzle	    so->so_rcv.sb_cc != 0)
265247045Skientzle		ub->st_mode |= S_IRUSR | S_IRGRP | S_IROTH;
266247045Skientzle	ub->st_size = so->so_rcv.sb_cc - so->so_rcv.sb_ctl;
267247045Skientzle	SOCKBUF_UNLOCK(&so->so_rcv);
268247045Skientzle	/* Unlocked read. */
269247045Skientzle	if ((so->so_snd.sb_state & SBS_CANTSENDMORE) == 0)
270247045Skientzle		ub->st_mode |= S_IWUSR | S_IWGRP | S_IWOTH;
271247045Skientzle	ub->st_uid = so->so_cred->cr_uid;
272247045Skientzle	ub->st_gid = so->so_cred->cr_gid;
273247045Skientzle	return (*so->so_proto->pr_usrreqs->pru_sense)(so, ub);
274247045Skientzle}
275247045Skientzle
276247045Skientzle/*
277247045Skientzle * API socket close on file pointer.  We call soclose() to close the socket
278247045Skientzle * (including initiating closing protocols).  soclose() will sorele() the
279247045Skientzle * file reference but the actual socket will not go away until the socket's
280247045Skientzle * ref count hits 0.
281208538Sraj */
282208538Sraj/* ARGSUSED */
283208538Srajint
284208538Srajsoo_close(struct file *fp, struct thread *td)
285208538Sraj{
286208538Sraj	int error = 0;
287208538Sraj	struct socket *so;
288208538Sraj
289208538Sraj	so = fp->f_data;
290208538Sraj	fp->f_ops = &badfileops;
291208538Sraj	fp->f_data = NULL;
292208538Sraj
293208538Sraj	if (so)
294208538Sraj		error = soclose(so);
295208538Sraj	return (error);
296208538Sraj}
297208538Sraj