sys_socket.c revision 110908
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 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 *	@(#)sys_socket.c	8.1 (Berkeley) 6/10/93
34 * $FreeBSD: head/sys/kern/sys_socket.c 110908 2003-02-15 06:04:55Z alfred $
35 */
36
37#include "opt_mac.h"
38
39#include <sys/param.h>
40#include <sys/systm.h>
41#include <sys/file.h>
42#include <sys/filedesc.h>
43#include <sys/mac.h>
44#include <sys/protosw.h>
45#include <sys/sigio.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/filedesc.h>
53#include <sys/ucred.h>
54
55#include <net/if.h>
56#include <net/route.h>
57
58struct	fileops socketops = {
59	soo_read, soo_write, soo_ioctl, soo_poll, soo_kqfilter,
60	soo_stat, soo_close, DFLAG_PASSABLE
61};
62
63/* ARGSUSED */
64int
65soo_read(fp, uio, active_cred, flags, td)
66	struct file *fp;
67	struct uio *uio;
68	struct ucred *active_cred;
69	struct thread *td;
70	int flags;
71{
72	struct socket *so = fp->f_data;
73	int error;
74
75	mtx_lock(&Giant);
76#ifdef MAC
77	error = mac_check_socket_receive(active_cred, so);
78	if (error) {
79		mtx_unlock(&Giant);
80		return (error);
81	}
82#endif
83	error = so->so_proto->pr_usrreqs->pru_soreceive(so, 0, uio, 0, 0, 0);
84	mtx_unlock(&Giant);
85	return (error);
86}
87
88/* ARGSUSED */
89int
90soo_write(fp, uio, active_cred, flags, td)
91	struct file *fp;
92	struct uio *uio;
93	struct ucred *active_cred;
94	struct thread *td;
95	int flags;
96{
97	struct socket *so = fp->f_data;
98	int error;
99
100	mtx_lock(&Giant);
101#ifdef MAC
102	error = mac_check_socket_send(active_cred, so);
103	if (error) {
104		mtx_unlock(&Giant);
105		return (error);
106	}
107#endif
108	error = so->so_proto->pr_usrreqs->pru_sosend(so, 0, uio, 0, 0, 0,
109						    uio->uio_td);
110	mtx_unlock(&Giant);
111	return (error);
112}
113
114int
115soo_ioctl(fp, cmd, data, active_cred, td)
116	struct file *fp;
117	u_long cmd;
118	void *data;
119	struct ucred *active_cred;
120	struct thread *td;
121{
122	register struct socket *so = fp->f_data;
123
124	switch (cmd) {
125
126	case FIONBIO:
127		if (*(int *)data)
128			so->so_state |= SS_NBIO;
129		else
130			so->so_state &= ~SS_NBIO;
131		return (0);
132
133	case FIOASYNC:
134		if (*(int *)data) {
135			so->so_state |= SS_ASYNC;
136			so->so_rcv.sb_flags |= SB_ASYNC;
137			so->so_snd.sb_flags |= SB_ASYNC;
138		} else {
139			so->so_state &= ~SS_ASYNC;
140			so->so_rcv.sb_flags &= ~SB_ASYNC;
141			so->so_snd.sb_flags &= ~SB_ASYNC;
142		}
143		return (0);
144
145	case FIONREAD:
146		*(int *)data = so->so_rcv.sb_cc;
147		return (0);
148
149	case FIOSETOWN:
150		return (fsetown(*(int *)data, &so->so_sigio));
151
152	case FIOGETOWN:
153		*(int *)data = fgetown(&so->so_sigio);
154		return (0);
155
156	case SIOCSPGRP:
157		return (fsetown(-(*(int *)data), &so->so_sigio));
158
159	case SIOCGPGRP:
160		*(int *)data = -fgetown(&so->so_sigio);
161		return (0);
162
163	case SIOCATMARK:
164		*(int *)data = (so->so_state&SS_RCVATMARK) != 0;
165		return (0);
166	}
167	/*
168	 * Interface/routing/protocol specific ioctls:
169	 * interface and routing ioctls should have a
170	 * different entry since a socket's unnecessary
171	 */
172	if (IOCGROUP(cmd) == 'i')
173		return (ifioctl(so, cmd, data, td));
174	if (IOCGROUP(cmd) == 'r')
175		return (rtioctl(cmd, data));
176	return ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd, data, 0, td));
177}
178
179int
180soo_poll(fp, events, active_cred, td)
181	struct file *fp;
182	int events;
183	struct ucred *active_cred;
184	struct thread *td;
185{
186	struct socket *so = fp->f_data;
187	return so->so_proto->pr_usrreqs->pru_sopoll(so, events,
188	    fp->f_cred, td);
189}
190
191int
192soo_stat(fp, ub, active_cred, td)
193	struct file *fp;
194	struct stat *ub;
195	struct ucred *active_cred;
196	struct thread *td;
197{
198	struct socket *so = fp->f_data;
199
200	bzero((caddr_t)ub, sizeof (*ub));
201	ub->st_mode = S_IFSOCK;
202	/*
203	 * If SS_CANTRCVMORE is set, but there's still data left in the
204	 * receive buffer, the socket is still readable.
205	 */
206	if ((so->so_state & SS_CANTRCVMORE) == 0 ||
207	    so->so_rcv.sb_cc != 0)
208		ub->st_mode |= S_IRUSR | S_IRGRP | S_IROTH;
209	if ((so->so_state & SS_CANTSENDMORE) == 0)
210		ub->st_mode |= S_IWUSR | S_IWGRP | S_IWOTH;
211	ub->st_size = so->so_rcv.sb_cc - so->so_rcv.sb_ctl;
212	ub->st_uid = so->so_cred->cr_uid;
213	ub->st_gid = so->so_cred->cr_gid;
214	return ((*so->so_proto->pr_usrreqs->pru_sense)(so, ub));
215}
216
217/*
218 * API socket close on file pointer.  We call soclose() to close the
219 * socket (including initiating closing protocols).  soclose() will
220 * sorele() the file reference but the actual socket will not go away
221 * until the socket's ref count hits 0.
222 */
223/* ARGSUSED */
224int
225soo_close(fp, td)
226	struct file *fp;
227	struct thread *td;
228{
229	int error = 0;
230	struct socket *so;
231
232	so = fp->f_data;
233	fp->f_ops = &badfileops;
234	fp->f_data = NULL;
235
236	if (so)
237		error = soclose(so);
238	return (error);
239}
240